-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
163 lines (148 loc) · 4.05 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios')
async function sendChangeEvent(changeEvent) {
try {
const response = await axios.post('https://events.pagerduty.com/v2/change/enqueue', changeEvent);
console.log(`PagerDuty responded with ${response.status} - ${JSON.stringify(response.data)}`);
if (response.status !== 202) {
core.setFailed(`PagerDuty API returned status code ${response.status}`);
}
} catch (error) {
core.setFailed(error.message);
}
}
function handleCustomEvent(summary, integrationKey) {
const changeEvent = {
routing_key: integrationKey,
payload: {
summary: summary,
source: 'GitHub',
timestamp: (new Date()).toISOString(),
custom_details: {}
},
links: [
{
href: `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`,
text: "View run"
}
]
};
sendChangeEvent(changeEvent);
}
function handlePushEvent(data, integrationKey) {
const {
ref,
compare: compareHref,
repository: {
full_name: repoFullName,
html_url: repoHref
},
sender: {
login: senderLogin,
html_url: senderHref
}
} = data;
const parts = ref.split('/');
const branch = parts[parts.length - 1];
const changeEvent = {
routing_key: integrationKey,
payload: {
summary: `${senderLogin} pushed branch ${branch} from ${repoFullName}`.slice(0, 1024),
source: 'GitHub',
timestamp: (new Date()).toISOString(),
custom_details: {}
},
links: [
{
href: compareHref,
text: 'View on GitHub'
}, {
href: repoHref,
text: 'Repo'
}, {
href: senderHref,
text: `Sender - ${senderLogin}`
}
]
};
sendChangeEvent(changeEvent);
}
function handlePullRequestEvent(data, integrationKey) {
const {
pull_request: {
title,
body,
commits,
additions,
deletions,
changed_files: changedFiles,
review_comments: reviewComments,
merged_at: mergedAt,
html_url: pullRequestUrl,
user: {
login: userLogin,
html_url: userUrl
},
merged_by: {
login: mergedByLogin,
html_url: mergedByUrl
}
},
repository: {
full_name: repoName
}
} = data;
const changeEvent = {
routing_key: integrationKey,
payload: {
summary: `[PR Merged - ${repoName}] ${title}`.slice(0, 1024),
source: 'GitHub',
timestamp: mergedAt,
custom_details: {
body: body,
repo: repoName,
commits: commits,
review_comments: reviewComments,
additions: additions,
deletions: deletions,
changed_files: changedFiles
}
},
links: [
{
href: pullRequestUrl,
text: 'View on GitHub'
}, {
href: mergedByUrl,
text: `Merged by - ${mergedByLogin}`
}, {
href: userUrl,
text: `Opened by - ${userLogin}`
}
]
};
const changeEventString = JSON.stringify(changeEvent);
// enforce the 512kb message size limit
if (changeEventString.length > 524288) {
changeEvent['payload']['custom_details']['body'] = body.slice(0, changeEventString.length - 524288);
}
sendChangeEvent(changeEvent);
}
try {
const integrationKey = core.getInput('integration-key');
const customEvent = core.getInput('custom-event');
const data = github.context.payload;
if (typeof customEvent === 'string' && customEvent !== '') {
// if custom event is described, prefer emitting custom event
handleCustomEvent(customEvent, integrationKey);
} else if (github.context.eventName === 'push') {
handlePushEvent(data, integrationKey);
} else if (github.context.eventName === 'pull_request' && data.action === 'closed' && data.pull_request.merged) {
handlePullRequestEvent(data, integrationKey);
} else {
console.log('No action taken. The event or action are not handled by this Action.');
}
} catch (error) {
core.setFailed(error.message)
}