-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathoncall.ts
148 lines (134 loc) · 4.06 KB
/
oncall.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
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
import Command from '../../base'
import {flags} from '@oclif/command'
import cli from 'cli-ux'
import chalk from 'chalk'
import * as utils from '../../utils'
import * as chrono from 'chrono-node'
import jp from 'jsonpath'
export default class UserOncall extends Command {
static description = 'List a PagerDuty User\'s on call shifts.'
static flags = {
...Command.flags,
me: flags.boolean({
char: 'm',
description: 'Show my oncalls.',
exclusive: ['id', 'email'],
}),
id: flags.string({
char: 'i',
description: 'Show oncalls for the user with this ID.',
exclusive: ['email', 'me'],
}),
email: flags.string({
char: 'e',
description: 'Show oncalls for the user with this login email.',
exclusive: ['id', 'me'],
}),
since: flags.string({
description: 'The start of the date range over which you want to search.',
}),
until: flags.string({
description: 'The end of the date range over which you want to search.',
}),
always: flags.boolean({
char: 'a',
description: 'Include \'Always on call.\'',
default: false,
}),
keys: flags.string({
char: 'k',
description: 'Additional fields to display. Specify multiple times for multiple fields.',
multiple: true,
}),
json: flags.boolean({
char: 'j',
description: 'output full details as JSON',
exclusive: ['columns', 'filter', 'sort', 'csv', 'extended'],
}),
...cli.table.flags(),
}
async run() {
const {flags} = this.parse(UserOncall)
const params: Record<string, any> = {}
let userID
if (flags.id) {
if (utils.invalidPagerDutyIDs([flags.id]).length > 0) {
this.error(`${chalk.bold.blue(flags.id)} is not a valid PagerDuty user ID`)
}
userID = flags.id
} else if (flags.email) {
cli.action.start(`Finding PD user ${chalk.bold.blue(flags.email)}`)
userID = await this.pd.userIDForEmail(flags.email)
if (!userID) {
cli.action.stop(chalk.bold.red('failed!'))
this.error(`No user was found for the email "${flags.email}"`, {exit: 1})
}
} else if (flags.me) {
cli.action.start('Finding your PD user ID')
const me = await this.me(true)
userID = me.user.id
} else {
this.error('You must specify one of: -i, -e, -m', {exit: 1})
}
params['user_ids[]'] = userID
if (flags.since) {
const since = chrono.parseDate(flags.since)
if (since) {
params.since = since.toISOString()
}
}
if (flags.until) {
const until = chrono.parseDate(flags.until)
if (until) {
params.until = until.toISOString()
}
}
let oncalls = await this.pd.fetchWithSpinner('oncalls', {
params: params,
activityDescription: `Getting oncalls for user ${chalk.bold.blue(userID)}`,
})
if (!flags.always) {
oncalls = oncalls.filter((x: any) => x.start && x.end)
}
if (oncalls.length === 0) {
cli.action.stop(chalk.bold.red('none found'))
this.exit(0)
}
cli.action.stop(chalk.bold.green('done'))
if (flags.json) {
await utils.printJsonAndExit(oncalls)
}
const columns: Record<string, object> = {
start: {
get: (row: any) => row.start ? (new Date(row.start)).toLocaleString() : '',
},
end: {
get: (row: any) => row.end ? (new Date(row.end)).toLocaleString() : '',
},
level: {
get: (row: any) => row.escalation_level,
},
escalation_policy_name: {
header: 'Escalation Policy Name',
get: (row: any) => row.escalation_policy.summary,
},
schedule_name: {
header: 'Schedule Name',
get: (row: any) => row.schedule?.summary || '',
},
}
if (flags.keys) {
for (const key of flags.keys) {
columns[key] = {
header: key,
get: (row: any) => utils.formatField(jp.query(row, key), '\n'),
}
}
}
const options = {
printLine: this.log,
...flags, // parsed flags
}
cli.table(oncalls, columns, options)
}
}