/
transformer.js
59 lines (51 loc) · 1.84 KB
/
transformer.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
export function transform(PD) {
// capture incoming webhook body
let body = PD.inputRequest.body;
// capture incoming webhook headers
const headers = PD.inputRequest.headers;
let emitEvent = true;
// GitHub stuff
let githubEventType = '';
// capture the GitHub event type
for (var i = 0; i < headers.length; i++) {
if('x-github-event' in headers[i]) {
githubEventType = headers[i]['x-github-event'];
}
}
let githubLinkURL = '';
// Incident stuff
let incidentSummary = '';
let dedupKey = '';
let incidentAction = PD.Trigger;
// only process webhook if it's an 'issues' event type
if (githubEventType === 'issues') {
dedupKey = `${body.issue.id}`;
// if the github issue is opened set incident summary with the issue title
if (body.action === 'reopened' || body.action === 'opened') {
incidentSummary = `[${body.repository.full_name}] Issue: ${body.issue.title}`;
githubLinkURL = body.issue.html_url;
// if neither of these actions are occurring, don't send an event
} else {
emitEvent = false;
}
// if the github event type is something other than issues, don't send an event
} else {
emitEvent = false;
}
// preparing the normalized_event to be sent to pagerduty
let normalized_event = {
event_action: incidentAction,
dedup_key: dedupKey,
payload: {
summary: incidentSummary,
source: 'GitHub',
severity: PD.Critical,
custom_details: `${body.issue.body} (${body.issue.user.login})`
},
links: [{
"href": githubLinkURL,
"text": "View In GitHub"
}],
}
if (emitEvent) PD.emitEventsV2([normalized_event])
}