forked from Entle/action-pagerduty-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (46 loc) · 1.52 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
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');
let alert = {
payload: {
summary: 'GitHub deployment workflow failed',
timestamp: new Date().toISOString(),
source: 'GitHub Actions',
severity: 'critical',
custom_details: {
actor: `${context.actor}`,
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 dedupKey = core.getInput('pagerduty-dedup-key');
if (dedupKey != '') {
alert.dedup_key = dedupKey;
}
sendAlert(alert);
} catch (error) {
core.setFailed(error.message);
}