Skip to content

Commit 7246c6f

Browse files
committed
Adding the teams commands
1 parent e0039e3 commit 7246c6f

4 files changed

Lines changed: 371 additions & 10 deletions

File tree

api.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,94 @@ def list_alert_comments(self, alert_id: str, limit: int = 10,
135135
"offset": offset
136136
}
137137

138+
# TEAMS
139+
# ======
140+
141+
def create_team(
142+
self,
143+
name: str,
144+
notes: Optional[str] = None,
145+
member_account_user_ids: Optional[List[str]] = None,
146+
admin_account_user_ids: Optional[List[str]] = None
147+
) -> Dict[str, Any]:
148+
"""Create a new team in PagerTree."""
149+
payload = {
150+
"name": name,
151+
"notes": notes,
152+
"member_account_user_ids": member_account_user_ids or [],
153+
"admin_account_user_ids": admin_account_user_ids or []
154+
}
155+
payload = {k: v for k, v in payload.items() if v is not None}
156+
response = self.session.post(f"{self.base_url}/teams", json=payload)
157+
response.raise_for_status()
158+
return response.json()
159+
160+
def list_teams(self, limit: int = 10, offset: int = 0, search: Optional[str] = None) -> Dict[str, Any]:
161+
"""List all teams in PagerTree."""
162+
params = {k: v for k, v in {"limit": limit, "offset": offset, "q": search}.items() if v is not None}
163+
response = self.session.get(f"{self.base_url}/teams", params=params)
164+
response.raise_for_status()
165+
data = response.json()
166+
return {
167+
"data": data.get("data", []),
168+
"total": data.get("total_count", 0),
169+
"has_more": data.get("has_more", False),
170+
"limit": limit,
171+
"offset": offset
172+
}
173+
174+
def show_team(self, team_id: str) -> Dict[str, Any]:
175+
"""Fetch a single team by ID from PagerTree."""
176+
response = self.session.get(f"{self.base_url}/teams/{team_id}")
177+
response.raise_for_status()
178+
return response.json()
179+
180+
def update_team(
181+
self,
182+
team_id: str,
183+
name: Optional[str] = None,
184+
notes: Optional[str] = None,
185+
member_account_user_ids: Optional[List[str]] = None,
186+
admin_account_user_ids: Optional[List[str]] = None
187+
) -> Dict[str, Any]:
188+
"""Update a team in PagerTree."""
189+
payload = {
190+
"name": name,
191+
"notes": notes,
192+
"member_account_user_ids": member_account_user_ids,
193+
"admin_account_user_ids": admin_account_user_ids
194+
}
195+
payload = {k: v for k, v in payload.items() if v is not None}
196+
response = self.session.put(f"{self.base_url}/teams/{team_id}", json=payload)
197+
response.raise_for_status()
198+
return response.json()
199+
200+
def delete_team(self, team_id: str) -> Dict[str, Any]:
201+
"""Delete a team in PagerTree."""
202+
response = self.session.delete(f"{self.base_url}/teams/{team_id}")
203+
response.raise_for_status()
204+
return response.json() if response.content else {"message": "Team deleted successfully"}
205+
206+
def get_team_current_oncall(self, team_id: str) -> Dict[str, Any]:
207+
"""Fetch current on-call users for a team in PagerTree."""
208+
response = self.session.get(f"{self.base_url}/teams/{team_id}/current_oncall")
209+
response.raise_for_status()
210+
return response.json()
211+
212+
def get_team_alerts(self, team_id: str, limit: int = 10, offset: int = 0) -> Dict[str, Any]:
213+
"""Fetch alerts for a specific team in PagerTree."""
214+
params = {k: v for k, v in {"limit": limit, "offset": offset}.items() if v is not None}
215+
response = self.session.get(f"{self.base_url}/teams/{team_id}/alerts", params=params)
216+
response.raise_for_status()
217+
data = response.json()
218+
return {
219+
"data": data.get("data", []),
220+
"total": data.get("total_count", 0),
221+
"has_more": data.get("has_more", False),
222+
"limit": limit,
223+
"offset": offset
224+
}
225+
138226
# USERS
139227
# ======
140228

commands/alerts.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def create_alert_cmd(ctx, title, description, team_ids, urgency, tags, alias):
2626
tags=list(tags),
2727
alias=alias
2828
)
29-
click.echo(f"Alert created successfully: {result.get('tiny_id')}")
29+
click.echo(f"Alert created successfully: {result.get('id')}")
3030
except Exception as e:
3131
handle_api_error(e, action="creating alert")
3232

@@ -45,7 +45,7 @@ def list_alerts_cmd(ctx, limit, offset, status, search):
4545
total = result["total"]
4646
# Prepare table data
4747
headers = ["ID", "Title", "Status"]
48-
table_data = [[alert.get("tiny_id"), alert.get("title"), alert.get("status")] for alert in alerts_list]
48+
table_data = [[alert.get("id"), alert.get("title"), alert.get("status")] for alert in alerts_list]
4949
display_paginated_results(alerts_list, total, limit, offset, "alert", headers, table_data)
5050
except Exception as e:
5151
handle_api_error(e, action="listing alerts")
@@ -59,7 +59,7 @@ def show_alert_cmd(ctx, alert_id):
5959
client = ctx.obj # Get PagerTreeClient from context
6060
alert = client.show_alert(alert_id)
6161
fields = {
62-
"tiny_id": "ID",
62+
"id": "ID",
6363
"status": "Status",
6464
"urgency": "Urgency",
6565
"tags": "Tags",
@@ -99,7 +99,7 @@ def acknowledge_alert_cmd(ctx, alert_id):
9999
try:
100100
client = ctx.obj # Get PagerTreeClient from context
101101
result = client.acknowledge_alert(alert_id)
102-
click.echo(f"Alert acknowledged successfully: {result.get('tiny_id')}")
102+
click.echo(f"Alert acknowledged successfully: {result.get('id')}")
103103
except Exception as e:
104104
handle_api_error(e, action="acknowledging alert")
105105

@@ -111,7 +111,7 @@ def reject_alert_cmd(ctx, alert_id):
111111
try:
112112
client = ctx.obj # Get PagerTreeClient from context
113113
result = client.reject_alert(alert_id)
114-
click.echo(f"Alert rejected successfully: {result.get('tiny_id')}")
114+
click.echo(f"Alert rejected successfully: {result.get('id')}")
115115
except Exception as e:
116116
handle_api_error(e, action="rejecting alert")
117117

@@ -123,7 +123,7 @@ def resolve_alert_cmd(ctx, alert_id):
123123
try:
124124
client = ctx.obj # Get PagerTreeClient from context
125125
result = client.resolve_alert(alert_id)
126-
click.echo(f"Alert resolved successfully: {result.get('tiny_id')}")
126+
click.echo(f"Alert resolved successfully: {result.get('id')}")
127127
except Exception as e:
128128
handle_api_error(e, action="resolving alert")
129129

0 commit comments

Comments
 (0)