-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (167 loc) · 5.3 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const core = require("@actions/core");
const github = require("@actions/github");
const axios = require("axios");
const DEPLOYMENTS_URL = "https://api.ilert.com/api/deployment-events";
const GH_ACTION_VERSION = "1.0.1";
async function sendDeploymentEvent(payload) {
try {
const response = await axios.post(DEPLOYMENTS_URL, payload);
console.log(`Deployment response: ${response.status} - ${JSON.stringify(response.data)}`);
if (response.status !== 202) {
core.setFailed(`ilert API returned status code ${response.status}`);
}
} catch (error) {
core.setFailed(error.message);
}
}
function handleCustomEvent(summary, integrationKey) {
const repository = process.env.GITHUB_REPOSITORY;
const deploymentEvent = {
apiKey: integrationKey,
summary,
timestamp: (new Date()).toISOString(),
customDetails: {
ilertDeploymentEventsAction: true,
githubActionVersion: GH_ACTION_VERSION
},
// userEmail: "",
// userName: "",
// version: "",
// environment: "",
// commit: "",
repository,
links: [
{
href: `${process.env.GITHUB_SERVER_URL}/${repository}/actions/runs/${process.env.GITHUB_RUN_ID}`,
text: "View run"
}
]
};
let _ = sendDeploymentEvent(deploymentEvent);
}
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 deploymentEvent = {
apiKey: integrationKey,
summary: `${senderLogin} pushed branch ${branch} from ${repoFullName}`.slice(0, 1024),
timestamp: (new Date()).toISOString(),
customDetails: {
ilertDeploymentEventsAction: true,
githubActionVersion: GH_ACTION_VERSION
},
// userEmail: "",
userName: senderLogin,
// version: "",
// environment: "",
// commit: "",
repository: repoFullName,
links: [
{
href: compareHref,
text: "View on GitHub"
}, {
href: repoHref,
text: "Repo"
}, {
href: senderHref,
text: `Sender - ${senderLogin}`
}
]
};
let _ = sendDeploymentEvent(deploymentEvent);
}
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 deploymentEvent = {
apiKey: integrationKey,
summary: `PR merged - ${repoName} ${title}`.slice(0, 1024),
timestamp: mergedAt,
customDetails: {
ilertDeploymentEventsAction: true,
githubActionVersion: GH_ACTION_VERSION,
body: body,
repo: repoName,
commits: commits,
review_comments: reviewComments,
additions: additions,
deletions: deletions,
changed_files: changedFiles
},
// userEmail: "",
userName: userLogin,
// version: "",
// environment: "",
// commit: "",
repository: repoName,
links: [
{
href: pullRequestUrl,
text: "View on GitHub"
}, {
href: mergedByUrl,
text: `Merged by - ${mergedByLogin}`
}, {
href: userUrl,
text: `Opened by - ${userLogin}`
}
]
};
const deploymentEventString = JSON.stringify(deploymentEvent);
// enforce the 256kb message size limit
if (deploymentEventString.length > 262144) {
deploymentEvent.customDetails.body = body.slice(0, deploymentEventString.length - 262144);
}
let _ = sendDeploymentEvent(deploymentEvent);
}
try {
const integrationKey = core.getInput("integration-key");
const customEvent = core.getInput("custom-event");
const data = github.context.payload;
if (typeof customEvent === "string" && customEvent !== "") {
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. This event or action is not handled by this action.");
}
} catch (error) {
core.setFailed(error.message)
}