atlassian-cli 0.9.2

Unified CLI for Atlassian Cloud products
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use anyhow::{Context, Result};
use serde::Serialize;
use url::form_urlencoded;

use super::utils::{Alert, ApiResponse, OpsgenieContext, PagedResponse};

/// List alerts with optional filters.
pub async fn list_alerts(
    ctx: &OpsgenieContext<'_>,
    query: Option<&str>,
    limit: usize,
    status: Option<&str>,
) -> Result<()> {
    let mut serializer = form_urlencoded::Serializer::new(String::new());
    serializer.append_pair("limit", &limit.min(100).to_string());

    // Combine query and status into single query param
    let mut query_parts = Vec::new();
    if let Some(q) = query {
        query_parts.push(q.to_string());
    }
    if let Some(s) = status {
        query_parts.push(format!("status:{}", s));
    }
    if !query_parts.is_empty() {
        serializer.append_pair("query", &query_parts.join(" AND "));
    }

    let path = format!("alerts?{}", serializer.finish());
    tracing::debug!("Listing alerts with limit {}", limit);

    let response: PagedResponse<Alert> = ctx
        .client
        .get(&path)
        .await
        .context("Failed to list alerts")?;

    #[derive(Serialize)]
    struct Row<'a> {
        id: &'a str,
        tiny_id: &'a str,
        message: &'a str,
        status: &'a str,
        priority: &'a str,
        acknowledged: bool,
        created_at: &'a str,
    }

    let rows: Vec<Row<'_>> = response
        .data
        .iter()
        .map(|alert| Row {
            id: &alert.id,
            tiny_id: alert.tiny_id.as_deref().unwrap_or(""),
            message: &alert.message,
            status: alert.status.as_deref().unwrap_or(""),
            priority: alert.priority.as_deref().unwrap_or(""),
            acknowledged: alert.acknowledged.unwrap_or(false),
            created_at: alert.created_at.as_deref().unwrap_or(""),
        })
        .collect();

    if rows.is_empty() {
        tracing::info!("No alerts found");
    } else {
        tracing::info!("Found {} alerts", rows.len());
    }
    ctx.renderer.render(&rows)
}

/// Get alert details by ID or alias.
pub async fn get_alert(ctx: &OpsgenieContext<'_>, identifier: &str) -> Result<()> {
    let path = format!("alerts/{}", identifier);
    tracing::debug!("Fetching alert {}", identifier);

    let response: ApiResponse<Alert> = ctx
        .client
        .get(&path)
        .await
        .with_context(|| format!("Failed to fetch alert {}", identifier))?;

    let alert = response.data;

    #[derive(Serialize)]
    struct View<'a> {
        id: &'a str,
        tiny_id: &'a str,
        alias: &'a str,
        message: &'a str,
        status: &'a str,
        priority: &'a str,
        acknowledged: bool,
        snoozed: bool,
        source: &'a str,
        owner: &'a str,
        tags: String,
        created_at: &'a str,
        updated_at: &'a str,
    }

    let view = View {
        id: &alert.id,
        tiny_id: alert.tiny_id.as_deref().unwrap_or(""),
        alias: alert.alias.as_deref().unwrap_or(""),
        message: &alert.message,
        status: alert.status.as_deref().unwrap_or(""),
        priority: alert.priority.as_deref().unwrap_or(""),
        acknowledged: alert.acknowledged.unwrap_or(false),
        snoozed: alert.snoozed.unwrap_or(false),
        source: alert.source.as_deref().unwrap_or(""),
        owner: alert.owner.as_deref().unwrap_or(""),
        tags: alert
            .tags
            .as_ref()
            .map(|t| t.join(", "))
            .unwrap_or_default(),
        created_at: alert.created_at.as_deref().unwrap_or(""),
        updated_at: alert.updated_at.as_deref().unwrap_or(""),
    };

    tracing::info!("Retrieved alert {}", identifier);
    ctx.renderer.render(&view)
}

/// Create a new alert.
pub async fn create_alert(
    ctx: &OpsgenieContext<'_>,
    message: String,
    alias: Option<String>,
    description: Option<String>,
    priority: Option<String>,
    source: Option<String>,
    tags: Vec<String>,
) -> Result<()> {
    #[derive(Serialize)]
    struct CreateAlertBody {
        message: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        alias: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        priority: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        source: Option<String>,
        #[serde(skip_serializing_if = "Vec::is_empty")]
        tags: Vec<String>,
    }

    let body = CreateAlertBody {
        message: message.clone(),
        alias,
        description,
        priority,
        source,
        tags,
    };

    tracing::debug!("Creating alert: {}", message);

    #[allow(dead_code)]
    #[derive(serde::Deserialize)]
    struct CreateResult {
        #[serde(rename = "alertId")]
        alert_id: Option<String>,
        alias: Option<String>,
    }

    let response: ApiResponse<CreateResult> = ctx
        .client
        .post("alerts", &body)
        .await
        .context("Failed to create alert")?;

    let id = response.data.alert_id.unwrap_or_default();
    tracing::info!("Created alert {}", id);
    println!("Created alert: {} (id: {})", message, id);
    Ok(())
}

/// Close an alert.
pub async fn close_alert(
    ctx: &OpsgenieContext<'_>,
    identifier: &str,
    note: Option<String>,
) -> Result<()> {
    #[derive(Serialize)]
    struct CloseBody {
        #[serde(skip_serializing_if = "Option::is_none")]
        note: Option<String>,
    }

    let path = format!("alerts/{}/close", identifier);
    let body = CloseBody { note };

    tracing::debug!("Closing alert {}", identifier);
    ctx.client
        .post::<serde_json::Value, _>(&path, &body)
        .await
        .with_context(|| format!("Failed to close alert {}", identifier))?;

    tracing::info!("Closed alert {}", identifier);
    println!("Successfully closed alert {}", identifier);
    Ok(())
}

/// Acknowledge an alert.
pub async fn acknowledge_alert(
    ctx: &OpsgenieContext<'_>,
    identifier: &str,
    note: Option<String>,
) -> Result<()> {
    #[derive(Serialize)]
    struct AckBody {
        #[serde(skip_serializing_if = "Option::is_none")]
        note: Option<String>,
    }

    let path = format!("alerts/{}/acknowledge", identifier);
    let body = AckBody { note };

    tracing::debug!("Acknowledging alert {}", identifier);
    ctx.client
        .post::<serde_json::Value, _>(&path, &body)
        .await
        .with_context(|| format!("Failed to acknowledge alert {}", identifier))?;

    tracing::info!("Acknowledged alert {}", identifier);
    println!("Successfully acknowledged alert {}", identifier);
    Ok(())
}

/// Unacknowledge an alert.
pub async fn unacknowledge_alert(
    ctx: &OpsgenieContext<'_>,
    identifier: &str,
    note: Option<String>,
) -> Result<()> {
    #[derive(Serialize)]
    struct UnackBody {
        #[serde(skip_serializing_if = "Option::is_none")]
        note: Option<String>,
    }

    let path = format!("alerts/{}/unacknowledge", identifier);
    let body = UnackBody { note };

    tracing::debug!("Unacknowledging alert {}", identifier);
    ctx.client
        .post::<serde_json::Value, _>(&path, &body)
        .await
        .with_context(|| format!("Failed to unacknowledge alert {}", identifier))?;

    tracing::info!("Unacknowledged alert {}", identifier);
    println!("Successfully unacknowledged alert {}", identifier);
    Ok(())
}

/// Snooze an alert.
pub async fn snooze_alert(
    ctx: &OpsgenieContext<'_>,
    identifier: &str,
    end_time: &str,
    note: Option<String>,
) -> Result<()> {
    #[derive(Serialize)]
    struct SnoozeBody {
        #[serde(rename = "endTime")]
        end_time: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        note: Option<String>,
    }

    let path = format!("alerts/{}/snooze", identifier);
    let body = SnoozeBody {
        end_time: end_time.to_string(),
        note,
    };

    tracing::debug!("Snoozing alert {} until {}", identifier, end_time);
    ctx.client
        .post::<serde_json::Value, _>(&path, &body)
        .await
        .with_context(|| format!("Failed to snooze alert {}", identifier))?;

    tracing::info!("Snoozed alert {} until {}", identifier, end_time);
    println!(
        "Successfully snoozed alert {} until {}",
        identifier, end_time
    );
    Ok(())
}

/// Escalate an alert to next responders.
pub async fn escalate_alert(
    ctx: &OpsgenieContext<'_>,
    identifier: &str,
    escalation_id: &str,
    note: Option<String>,
) -> Result<()> {
    #[derive(Serialize)]
    struct EscalateBody {
        escalation: EscalationRef,
        #[serde(skip_serializing_if = "Option::is_none")]
        note: Option<String>,
    }

    #[derive(Serialize)]
    struct EscalationRef {
        id: String,
    }

    let path = format!("alerts/{}/escalate", identifier);
    let body = EscalateBody {
        escalation: EscalationRef {
            id: escalation_id.to_string(),
        },
        note,
    };

    tracing::debug!(
        "Escalating alert {} with escalation {}",
        identifier,
        escalation_id
    );
    ctx.client
        .post::<serde_json::Value, _>(&path, &body)
        .await
        .with_context(|| format!("Failed to escalate alert {}", identifier))?;

    tracing::info!("Escalated alert {}", identifier);
    println!("Successfully escalated alert {}", identifier);
    Ok(())
}

/// Assign an alert to a user.
pub async fn assign_alert(
    ctx: &OpsgenieContext<'_>,
    identifier: &str,
    owner_id: &str,
    note: Option<String>,
) -> Result<()> {
    #[derive(Serialize)]
    struct AssignBody {
        owner: OwnerRef,
        #[serde(skip_serializing_if = "Option::is_none")]
        note: Option<String>,
    }

    #[derive(Serialize)]
    struct OwnerRef {
        id: String,
    }

    let path = format!("alerts/{}/assign", identifier);
    let body = AssignBody {
        owner: OwnerRef {
            id: owner_id.to_string(),
        },
        note,
    };

    tracing::debug!("Assigning alert {} to {}", identifier, owner_id);
    ctx.client
        .post::<serde_json::Value, _>(&path, &body)
        .await
        .with_context(|| format!("Failed to assign alert {}", identifier))?;

    tracing::info!("Assigned alert {} to {}", identifier, owner_id);
    println!("Successfully assigned alert {} to {}", identifier, owner_id);
    Ok(())
}

/// Add note to an alert.
pub async fn add_note(ctx: &OpsgenieContext<'_>, identifier: &str, note: &str) -> Result<()> {
    #[derive(Serialize)]
    struct NoteBody<'a> {
        note: &'a str,
    }

    let path = format!("alerts/{}/notes", identifier);
    let body = NoteBody { note };

    tracing::debug!("Adding note to alert {}", identifier);
    ctx.client
        .post::<serde_json::Value, _>(&path, &body)
        .await
        .with_context(|| format!("Failed to add note to alert {}", identifier))?;

    tracing::info!("Added note to alert {}", identifier);
    println!("Successfully added note to alert {}", identifier);
    Ok(())
}

/// Delete an alert.
pub async fn delete_alert(ctx: &OpsgenieContext<'_>, identifier: &str) -> Result<()> {
    let path = format!("alerts/{}", identifier);

    tracing::debug!("Deleting alert {}", identifier);
    ctx.client
        .delete::<serde_json::Value>(&path)
        .await
        .with_context(|| format!("Failed to delete alert {}", identifier))?;

    tracing::info!("Deleted alert {}", identifier);
    println!("Successfully deleted alert {}", identifier);
    Ok(())
}

/// List alert recipients.
pub async fn list_recipients(ctx: &OpsgenieContext<'_>, identifier: &str) -> Result<()> {
    #[derive(serde::Deserialize)]
    struct RecipientsData {
        users: Vec<RecipientUser>,
    }

    #[derive(serde::Deserialize, Serialize)]
    struct RecipientUser {
        #[serde(default)]
        username: String,
        #[serde(default)]
        state: String,
        #[serde(rename = "stateChangedAt", default)]
        state_changed_at: String,
    }

    let path = format!("alerts/{}/recipients", identifier);
    tracing::debug!("Listing recipients for alert {}", identifier);

    let response: ApiResponse<RecipientsData> = ctx
        .client
        .get(&path)
        .await
        .with_context(|| format!("Failed to list recipients for alert {}", identifier))?;

    if response.data.users.is_empty() {
        tracing::info!("No recipients found for alert {}", identifier);
    } else {
        tracing::info!(
            "Found {} recipients for alert {}",
            response.data.users.len(),
            identifier
        );
    }
    ctx.renderer.render(&response.data.users)
}

/// List alert logs.
pub async fn list_logs(ctx: &OpsgenieContext<'_>, identifier: &str, limit: usize) -> Result<()> {
    #[derive(serde::Deserialize)]
    struct LogsResponse {
        data: Vec<AlertLog>,
    }

    #[derive(serde::Deserialize, Serialize)]
    struct AlertLog {
        log: String,
        #[serde(rename = "type")]
        log_type: String,
        owner: String,
        #[serde(rename = "createdAt")]
        created_at: String,
    }

    let path = format!("alerts/{}/logs?limit={}", identifier, limit.min(100));
    tracing::debug!("Listing logs for alert {}", identifier);

    let response: LogsResponse = ctx
        .client
        .get(&path)
        .await
        .with_context(|| format!("Failed to list logs for alert {}", identifier))?;

    if response.data.is_empty() {
        tracing::info!("No logs found for alert {}", identifier);
    } else {
        tracing::info!(
            "Found {} logs for alert {}",
            response.data.len(),
            identifier
        );
    }
    ctx.renderer.render(&response.data)
}

/// List alert notes.
pub async fn list_notes(ctx: &OpsgenieContext<'_>, identifier: &str, limit: usize) -> Result<()> {
    #[derive(serde::Deserialize)]
    struct NotesResponse {
        data: Vec<AlertNote>,
    }

    #[derive(serde::Deserialize, Serialize)]
    struct AlertNote {
        note: String,
        owner: String,
        #[serde(rename = "createdAt")]
        created_at: String,
    }

    let path = format!("alerts/{}/notes?limit={}", identifier, limit.min(100));
    tracing::debug!("Listing notes for alert {}", identifier);

    let response: NotesResponse = ctx
        .client
        .get(&path)
        .await
        .with_context(|| format!("Failed to list notes for alert {}", identifier))?;

    if response.data.is_empty() {
        tracing::info!("No notes found for alert {}", identifier);
    } else {
        tracing::info!(
            "Found {} notes for alert {}",
            response.data.len(),
            identifier
        );
    }
    ctx.renderer.render(&response.data)
}