forked from miparnisari/action-pagerduty-alert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
69 lines (58 loc) · 2.16 KB
/
index.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
const core = require('@actions/core');
const { context } = require('@actions/github');
const axios = require('axios');
// Trigger the PagerDuty webhook with a given alert
async function sendAlert(alert) {
core.info('Sending API call');
const headers = {
'Content-Type': 'application/json',
};
const response = await axios.post('https://events.pagerduty.com/v2/enqueue', alert, {headers: headers});
if (response.status === 202) {
core.info(`Successfully sent PagerDuty alert. Response: ${JSON.stringify(response.data)}`);
} else {
core.error(`PagerDuty API returned status code ${response.status} - ${JSON.stringify(response.data)}`);
core.setFailed(
`PagerDuty API returned status code ${response.status} - ${JSON.stringify(response.data)}`
);
}
}
// Run the action
(async () => {
const integrationKey = core.getInput('pagerduty-integration-key');
core.info('Reading pagerduty-integration-key');
let alert = {
payload: {
summary: `${context.repo.repo}: Error in "${context.workflow}" run by @${context.actor}`,
timestamp: new Date().toISOString(),
source: 'GitHub Actions',
severity: 'critical',
custom_details: {
run_details: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
},
},
// Not sure why we see an extra `$` sign at the front of the key, but
// skipping the first char here to make a correct API call.
routing_key: integrationKey.substring(1),
event_action: 'trigger',
};
core.info('Forming default request body');
const customSummary = core.getInput('incident-summary');
if (customSummary != '') {
alert.payload.summary = customSummary;
}
const region = core.getInput('incident-region');
if (region != '') {
alert.payload.custom_details.region = region;
}
const environment = core.getInput('incident-environment');
if (environment != '') {
alert.payload.custom_details.environment = environment;
}
const dedupKey = core.getInput('pagerduty-dedup-key');
if (dedupKey != '') {
alert.dedup_key = dedupKey;
}
core.info('Customizing request body');
await sendAlert(alert);
})();