This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
pagerduty.js
93 lines (78 loc) · 2.84 KB
/
pagerduty.js
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
'use strict';
var rp = require('request-promise');
function PagerDuty(token) {
this.getEscalationPolicies = function () {
var options = {
url: 'https://api.pagerduty.com/escalation_policies',
qs: {
sort_by: 'name:asc',
},
headers: {
Authorization: `Token token=${token}`,
Accept: 'application/vnd.pagerduty+json;version=2',
},
json: true,
};
var allResults = [];
function handleResponse(json) {
var pageResults = json.escalation_policies.map(policy => {
return {
policyId: policy.id,
policyName: policy.name,
policyDescription: policy.description,
policyUrl: policy.html_url,
};
});
allResults = allResults.concat(pageResults);
if (json.more) {
options.qs.offset = json.offset + json.limit;
return rp(options).then(handleResponse);
}
return allResults;
}
return rp(options).then(handleResponse);
};
this.getOnCalls = function (sinceISO8601) {
var options = {
url: 'https://api.pagerduty.com/oncalls',
qs: {
time_zone: 'UTC',
earliest: true,
sort_by: 'escalation_level:asc',
},
headers: {
Authorization: `Token token=${token}`,
Accept: 'application/vnd.pagerduty+json;version=2',
},
json: true,
};
if (sinceISO8601) {
options.qs.since = sinceISO8601;
options.qs.until = sinceISO8601;
}
var allResults = [];
function handleResponse(json) {
var pageResults = json.oncalls.map(oncall => {
return {
end: oncall.end,
escalationLevel: oncall.escalation_level,
policyId: oncall.escalation_policy.id,
policyName: oncall.escalation_policy.summary,
policyUrl: oncall.escalation_policy.html_url,
scheduleName: !oncall.schedule ? undefined : oncall.schedule.summary,
scheduleUrl: !oncall.schedule ? undefined : oncall.schedule.html_url,
userName: oncall.user.summary,
userUrl: oncall.user.html_url,
};
});
allResults = allResults.concat(pageResults);
if (json.more) {
options.qs.offset = json.offset + json.limit;
return rp(options).then(handleResponse);
}
return allResults;
}
return rp(options).then(handleResponse);
};
}
module.exports = PagerDuty;