-
Notifications
You must be signed in to change notification settings - Fork 14
/
ack.ts
100 lines (89 loc) · 3.23 KB
/
ack.ts
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
import Command from '../../base'
import {flags} from '@oclif/command'
import chalk from 'chalk'
import cli from 'cli-ux'
import getStream from 'get-stream'
import * as pd from '../../pd'
import * as utils from '../../utils'
export default class IncidentAck extends Command {
static description = 'Acknowledge PagerDuty Incidents'
static flags = {
...Command.flags,
me: flags.boolean({
char: 'm',
description: 'Acknowledge all incidents assigned to me',
exclusive: ['ids', 'pipe'],
}),
ids: flags.string({
char: 'i',
description: 'Incident ID\'s to acknowledge. Specify multiple times for multiple incidents.',
multiple: true,
exclusive: ['me', 'pipe'],
}),
pipe: flags.boolean({
char: 'p',
description: 'Read incident ID\'s from stdin.',
exclusive: ['me', 'ids'],
}),
}
async run() {
const {flags} = this.parse(IncidentAck)
// get a validated token from base class
const token = this.token as string
let incident_ids: string[] = []
if (flags.me) {
let r = await pd.me(token)
this.dieIfFailed(r, {prefixMessage: 'Request to /users/me failed'})
const me = r.getValue()
const params = {user_ids: [me.user.id]}
cli.action.start('Getting incidents from PD')
r = await pd.fetch(token, '/incidents', params)
this.dieIfFailed(r, {prefixMessage: 'Request to list incidents failed'})
const incidents = r.getValue()
if (incidents.length === 0) {
cli.action.stop(chalk.bold.red('none found'))
return
}
cli.action.stop(`got ${incidents.length} before filtering`)
incident_ids = incidents.map((e: { id: any }) => e.id)
} else if (flags.ids) {
incident_ids = utils.splitDedupAndFlatten(flags.ids)
} else if (flags.pipe) {
const str: string = await getStream(process.stdin)
incident_ids = utils.splitDedupAndFlatten([str])
} else {
this.error('You must specify one of: -i, -m, -p', {exit: 1})
}
const invalid_ids = utils.invalidPagerDutyIDs(incident_ids)
if (invalid_ids && invalid_ids.length > 0) {
this.error(`Invalid incident ID's: ${invalid_ids.join(', ')}`, {exit: 1})
}
const requests: any[] = []
cli.action.start(`Acknowledging incident(s) ${chalk.bold.blue(incident_ids.join(', '))}`)
for (const incident_id of incident_ids) {
const body: Record<string, any> = pd.putBodyForSetAttribute('incident', incident_id, 'status', 'acknowledged')
requests.push({
token: token,
endpoint: `/incidents/${incident_id}`,
method: 'PUT',
params: {},
data: body,
})
}
const r = await pd.batchedRequest(requests)
this.dieIfFailed(r, {prefixMessage: 'Acknowledge request failed'})
const returnedIncidents = r.getValue()
const failed = []
for (const r of returnedIncidents) {
if (!(r && r.incident && r.incident.status && r.incident.status === 'acknowledged')) {
failed.push(r.incident.id)
}
}
if (failed.length > 0) {
cli.action.stop(chalk.bold.red('failed!'))
this.error(`Acknowledge request failed for incidents ${chalk.bold.red(failed.join(', '))}`)
} else {
cli.action.stop(chalk.bold.green('done'))
}
}
}