Skip to content

Commit a154885

Browse files
committed
Adding alias functionality to the alerts command
1 parent 755e0ab commit a154885

1 file changed

Lines changed: 111 additions & 11 deletions

File tree

commands/alerts.py

Lines changed: 111 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,50 +92,130 @@ def delete_alert_cmd(ctx, alert_id, force):
9292
handle_api_error(e, action="deleting alert")
9393

9494
@alerts.command(name="acknowledge")
95-
@click.argument("alert_id", required=True)
95+
@click.argument("alert_id", required=False) # Make alert_id optional
96+
@click.option("--alias", help="Alias for the alert")
9697
@click.pass_context
97-
def acknowledge_alert_cmd(ctx, alert_id):
98+
def acknowledge_alert_cmd(ctx, alert_id, alias):
9899
"""Acknowledge an alert in PagerTree."""
99100
try:
100101
client = ctx.obj # Get PagerTreeClient from context
102+
103+
# Ensure at least one of alert_id or alias is provided
104+
if not alert_id and not alias:
105+
click.echo("Error: Either alert_id or alias must be provided.")
106+
return
107+
108+
# If alias is provided, resolve it to alert_id
109+
if alias:
110+
alias_result = client.list_alerts(search=alias, limit=1, offset=0)
111+
if alias_result["total"] == 0:
112+
click.echo(f"No alert found with alias: {alias}")
113+
return
114+
alert_id = alias_result["data"][0]["id"]
115+
116+
# If alert_id is still not set, raise an error
117+
if not alert_id:
118+
click.echo("Error: Could not resolve alert_id.")
119+
return
120+
101121
result = client.acknowledge_alert(alert_id)
102122
click.echo(f"Alert acknowledged successfully: {result.get('id')}")
103123
except Exception as e:
104124
handle_api_error(e, action="acknowledging alert")
105125

106126
@alerts.command(name="reject")
107-
@click.argument("alert_id", required=True)
127+
@click.argument("alert_id", required=False) # Make alert_id optional
128+
@click.option("--alias", help="Alias for the alert")
108129
@click.pass_context
109-
def reject_alert_cmd(ctx, alert_id):
130+
def reject_alert_cmd(ctx, alert_id, alias):
110131
"""Reject an alert in PagerTree."""
111132
try:
112133
client = ctx.obj # Get PagerTreeClient from context
134+
135+
# Ensure at least one of alert_id or alias is provided
136+
if not alert_id and not alias:
137+
click.echo("Error: Either alert_id or alias must be provided.")
138+
return
139+
140+
# If alias is provided, resolve it to alert_id
141+
if alias:
142+
alias_result = client.list_alerts(search=alias, limit=1, offset=0, status="open")
143+
if alias_result["total"] == 0:
144+
click.echo(f"No alert found with alias: {alias}")
145+
return
146+
alert_id = alias_result["data"][0]["id"]
147+
148+
# If alert_id is still not set, raise an error
149+
if not alert_id:
150+
click.echo("Error: Could not resolve alert_id.")
151+
return
152+
113153
result = client.reject_alert(alert_id)
114154
click.echo(f"Alert rejected successfully: {result.get('id')}")
115155
except Exception as e:
116156
handle_api_error(e, action="rejecting alert")
117157

118158
@alerts.command(name="resolve")
119-
@click.argument("alert_id", required=True)
159+
@click.argument("alert_id", required=False) # Make alert_id optional
160+
@click.option("--alias", help="Alias for the alert")
120161
@click.pass_context
121-
def resolve_alert_cmd(ctx, alert_id):
162+
def resolve_alert_cmd(ctx, alert_id, alias):
122163
"""Resolve an alert in PagerTree."""
123164
try:
124165
client = ctx.obj # Get PagerTreeClient from context
166+
167+
# Ensure at least one of alert_id or alias is provided
168+
if not alert_id and not alias:
169+
click.echo("Error: Either alert_id or alias must be provided.")
170+
return
171+
172+
# If alias is provided, resolve it to alert_id
173+
if alias:
174+
alias_result = client.list_alerts(search=alias, limit=1, offset=0)
175+
if alias_result["total"] == 0:
176+
click.echo(f"No alert found with alias: {alias}")
177+
return
178+
alert_id = alias_result["data"][0]["id"]
179+
180+
# If alert_id is still not set, raise an error
181+
if not alert_id:
182+
click.echo("Error: Could not resolve alert_id.")
183+
return
184+
125185
result = client.resolve_alert(alert_id)
126186
click.echo(f"Alert resolved successfully: {result.get('id')}")
127187
except Exception as e:
128188
handle_api_error(e, action="resolving alert")
129189

130190
@alerts.command(name="list-comments")
131-
@click.argument("alert_id", required=True)
191+
@click.argument("alert_id", required=False) # Make alert_id optional
192+
@click.option("--alias", help="Alias for the alert")
132193
@click.option("--limit", default=10, type=click.IntRange(1, 100), help="Number of alerts per page")
133194
@click.option("--offset", default=0, type=click.IntRange(0), help="Starting point for pagination")
134195
@click.pass_context
135-
def list_alert_comment_cmd(ctx, alert_id, limit, offset):
136-
"""List an alerts comments in PagerTree."""
196+
def list_alert_comment_cmd(ctx, alert_id, alias, limit, offset):
197+
"""List an alert's comments in PagerTree."""
137198
try:
138199
client = ctx.obj # Get PagerTreeClient from context
200+
201+
# Ensure at least one of alert_id or alias is provided
202+
if not alert_id and not alias:
203+
click.echo("Error: Either alert_id or alias must be provided.")
204+
return
205+
206+
# If alias is provided, resolve it to alert_id
207+
if alias:
208+
alias_result = client.list_alerts(search=alias, limit=1, offset=0)
209+
if alias_result["total"] == 0:
210+
click.echo(f"No alert found with alias: {alias}")
211+
return
212+
alert_id = alias_result["data"][0]["id"]
213+
214+
# If alert_id is still not set, raise an error
215+
if not alert_id:
216+
click.echo("Error: Could not resolve alert_id.")
217+
return
218+
139219
result = client.list_alert_comments(alert_id, limit=limit, offset=offset)
140220
comments_list = result["data"]
141221
total = result["total"]
@@ -147,13 +227,33 @@ def list_alert_comment_cmd(ctx, alert_id, limit, offset):
147227
handle_api_error(e, action="listing alert comments")
148228

149229
@alerts.command(name="comment")
150-
@click.argument("alert_id", required=True)
230+
@click.argument("alert_id", required=False) # Make alert_id optional
231+
@click.option("--alias", help="Alias for the alert")
151232
@click.option("--comment", required=True, help="Comment to add to the alert")
152233
@click.pass_context
153-
def create_alert_comment_cmd(ctx, alert_id, comment):
234+
def create_alert_comment_cmd(ctx, alert_id, alias, comment):
154235
"""Add a comment to an alert in PagerTree."""
155236
try:
156237
client = ctx.obj # Get PagerTreeClient from context
238+
239+
# Ensure at least one of alert_id or alias is provided
240+
if not alert_id and not alias:
241+
click.echo("Error: Either alert_id or alias must be provided.")
242+
return
243+
244+
# If alias is provided, resolve it to alert_id
245+
if alias:
246+
alias_result = client.list_alerts(search=alias, limit=1, offset=0)
247+
if alias_result["total"] == 0:
248+
click.echo(f"No alert found with alias: {alias}")
249+
return
250+
alert_id = alias_result["data"][0]["id"]
251+
252+
# If alert_id is still not set, raise an error
253+
if not alert_id:
254+
click.echo("Error: Could not resolve alert_id.")
255+
return
256+
157257
result = client.create_alert_comment(alert_id, comment)
158258
click.echo(f"Comment added successfully to alert {alert_id}")
159259
except Exception as e:

0 commit comments

Comments
 (0)