forked from Entle/action-pagerduty-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (47 loc) · 1.63 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
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) {
try {
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)}`
);
}
} catch (error) {
core.setFailed(error.message);
}
}
// Run the action
try {
const integrationKey = core.getInput('pagerduty-integration-key');
const eventAction = core.getInput('event-type');
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',
runbook: core.getInput('runbook-url'),
},
},
routing_key: integrationKey,
event_action: eventAction,
};
const dedupKey = core.getInput('pagerduty-dedup-key');
if (dedupKey != '') {
alert.dedup_key = dedupKey;
}
sendAlert(alert);
} catch (error) {
core.setFailed(error.message);
}