-
Notifications
You must be signed in to change notification settings - Fork 0
/
tap-pagerduty.js
98 lines (84 loc) · 2.41 KB
/
tap-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
94
95
96
97
98
function PagerDuty() {
var https = require('https');
var config = require('./config.json');
var options = {
host: 'api.pagerduty.com',
port: 443,
path: '/incidents?since=${config.since}',
method: 'GET',
headers: {
"Authorization": `Token token=${config.key}`,
"Accept": "application/vnd.pagerduty+json;version=2"
}
};
var schema = {
"type": "SCHEMA",
"stream": "incidents",
"key_properties": ["incident_number"],
"schema": {
"type": "object",
"properties": {
"incident_number": {"type": "integer"},
"incident_key": {"type": "string"},
"title": {"type": "string"},
"description": {"type": "string"},
"status": {"type": "string"},
"last_status_change_at": {"type": "string", "format": "date-time"},
"urgency": {"type": "string"},
"service_summary": {"type": "string"},
}
}
};
function getRecord(inc) {
return {
type: "RECORD",
stream: "incidents",
record: inc
};
}
function convertIncident(inc) {
var incident = {
"incident_number": inc.incident_number,
"incident_key": inc.incident_key == null ? "" : inc.incident_key,
"title": inc.title,
"description": inc.description,
"status": inc.status,
"last_status_change_at": inc.last_status_change_at,
"urgency": inc.urgency,
"service_summary": inc.last_status_change_by.summary
};
return incident;
}
function requestData(since, limit) {
options.path = `/incidents?limit=${limit}&since=${since}&time_zone=UTC&sort_by=created_at`;
var req = https.request(options, function(res) {
var response = "";
res.on('data', function(d) {
response += d;
});
res.on('end', function(d) {
var incidents = JSON.parse(response).incidents;
var more = JSON.parse(response).more;
incidents.forEach( function(inc) {
console.log(JSON.stringify(getRecord(convertIncident(inc))));
});
since = incidents[incidents.length-1].last_status_change_at;
if (more) {
requestData(since, limit);
}
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
}
var pd = {};
pd.getIncidents = function() {
console.log(JSON.stringify(schema));
requestData(config.since, 100);
}
return pd;
}
var pd = new PagerDuty();
pd.getIncidents();