forked from Entle/action-pagerduty-alert
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
63 lines (55 loc) · 1.96 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
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) {
const response = await axios.post('https://events.pagerduty.com/v2/enqueue', alert);
if (response.status === 202) {
console.log(`Successfully sent PagerDuty alert. Response: ${JSON.stringify(response.data)}`);
} else {
core.setFailed(
`PagerDuty API returned status code ${response.status} - ${JSON.stringify(response.data)}`
);
}
}
// Run the action
(async () => {
try {
const integrationKey = core.getInput('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}`,
related_commits: context.payload.commits
? context.payload.commits.map((commit) => `${commit.message}: ${commit.url}`).join(', ')
: 'No related commits',
},
},
routing_key: integrationKey,
event_action: 'trigger',
};
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;
}
await sendAlert(alert);
} catch (error) {
core.setFailed(error.message);
}
})();