-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
241 lines (210 loc) · 7.28 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const core = require("@actions/core");
const { context } = require("@actions/github");
const { GitHub } = require("@actions/github/lib/utils");
const { retry } = require("@octokit/plugin-retry");
const { throttling } = require("@octokit/plugin-throttling");
const { validateAnnotationsArray } = require("./validateAnnotationsArray");
const { validateImagesArray } = require("./validateImagesArray");
// Pro-Tip: create a grouping so its easily to manage the output
core.startGroup("setup variables and client");
const successStates = ["neutral", "success"];
const owner = process.env.GITHUB_REPOSITORY.split("/")[0];
const repo = process.env.GITHUB_REPOSITORY.split("/")[1];
// When we use getInput, if there is no value, it comes back as an empty string. We must assume that empty strings are null and check/test appropriately
const status = core.getInput("status");
const title = core.getInput("title");
const details = core.getInput("details");
const summary = core.getInput("summary");
const conclusion = core.getInput("conclusion");
const existingCheckRunId = core.getInput("check-run-id");
const images = core.getInput("images");
const annotations = core.getInput("annotations");
const token = core.getInput("github-token");
// Create a custom Octokit constructor with the retry and throttling plugins installed
const OctokitWithPlugins = GitHub.plugin(retry, throttling);
console.log("created kit");
// initiate the client with the token and plugins
const octokit = new OctokitWithPlugins({
auth: token,
// Enable retries and customize strategy
retry: {
do: true, // enable retries
retryAfter: 30, // time to wait between retries in seconds
maxRetries: 5, // max number of retries
},
// Enable throttling/rate-limiting
throttle: {
onRateLimit: (retryAfter, options) => {
octokit.log.warn(
`Request quota exhausted for your request ${options.method} ${options.url}`
);
if (options.request.retryCount === 0) {
// only retries once
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onSecondaryRateLimit: (retryAfter, options) => {
octokit.log.warn(
`Request quota exhausted for your secondary request ${options.method} ${options.url}`
);
if (options.request.retryCount === 0) {
// only retries once
console.log(`Secondary retrying after ${retryAfter} seconds!`);
return true;
}
},
onAbuseLimit: (retryAfter, options) => {
// does not retry, only logs a warning
octokit.log.warn(
`Abuse detected for your request ${options.method} ${options.url}`
);
},
},
});
// Test inputs and if they fall back to defaults, inform the user that we've made an assumption here
let name = core.getInput("name");
if (name == "") {
// we're creating a warning for the property and advising to the default
core.warning("no name set, using repo name");
name = context.repo.name;
}
const pull_request = context.payload.pull_request;
let commitSha = "";
if (pull_request !== undefined) {
commitSha = pull_request.head.sha;
}
if (commitSha == "" || commitSha === undefined) {
// we're creating a warning for the property and advising to the default
core.warning("no pull request detected, using head sha");
commitSha = context.sha;
}
// get the value for the neutral
let shouldFailForNeutral = core.getInput("fail-on-neutral");
// does a value exist
if (shouldFailForNeutral !== "") {
// is it true
if (shouldFailForNeutral === "true") {
shouldFailForNeutral = true;
// is it false
} else if (shouldFailForNeutral === "false") {
shouldFailForNeutral = false;
} else {
// raise warning if nothing set
core.warning(
"unknown value set for fail-on-neutral property, defaulting to false"
);
shouldFailForNeutral = false;
}
} else {
core.warning("nothing set for fail-on-neutral property, defaulting to false");
shouldFailForNeutral = false;
}
let shouldFailForNonSuccess = core.getInput("fail-on-error");
if (shouldFailForNonSuccess !== "") {
if (shouldFailForNonSuccess === "true") {
shouldFailForNonSuccess = true;
} else if (shouldFailForNonSuccess === "false") {
shouldFailForNonSuccess = false;
} else {
core.warning(
"unknown value set for fail-on-error property, defaulting to false"
);
shouldFailForNonSuccess = false;
}
} else {
core.warning("nothing set for fail-on-error property, defaulting to false");
shouldFailForNonSuccess = false;
}
core.endGroup();
// run async
async function run() {
core.startGroup("validate failure options");
if (conclusion !== "") {
if (shouldFailForNonSuccess && !successStates.includes(conclusion)) {
core.setFailed("check failed for non successive state");
}
if (shouldFailForNeutral && conclusion == "neutral") {
core.setFailed("check failed for non successive state");
}
}
core.endGroup();
try {
core.startGroup("construct payload");
let checkRunId = 0;
let body = {
owner,
repo,
name,
head_sha: commitSha,
status,
output: {
title,
summary,
text: details,
},
};
if (conclusion !== "") {
core.info("conclusion detected");
core.debug(conclusion);
body.conclusion = conclusion;
}
core.endGroup();
core.startGroup("validate annotations and images");
if (core.isDebug()) {
core.debug(annotations);
}
if (annotations) {
// Parse to JSON to handle safely
const annotationsAsJson = JSON.parse(annotations);
const annotationValidationErrors =
validateAnnotationsArray(annotationsAsJson);
if (annotationValidationErrors.length <= 0) {
core.info("successfully validated annotations");
body.output.annotations = annotationsAsJson;
} else {
core.error(annotationValidationErrors.join(" \n "));
core.debug(annotationsAsJson);
core.warning("Annotations parsing error, did not add");
}
}
if (core.isDebug()) {
core.debug(images);
}
if (images) {
// Parse to JSON to handle safely
const imageAsJson = JSON.parse(images);
const imageValidationErrors = validateImagesArray(imageAsJson);
if (imageValidationErrors.length <= 0) {
core.info("successfully validated images");
body.output.images = imageAsJson;
} else {
core.warning("Images parsing error, did not add");
}
}
core.endGroup();
core.startGroup("run command");
if (existingCheckRunId === "") {
core.info("creating a check run");
// Create the check
const createCheck = await octokit.rest.checks.create(body);
checkRunId = createCheck.data.id;
core.info(`created a check run with the id of ${checkRunId}`);
} else {
core.info("updating a check run");
// add the existing check id
body.check_run_id = existingCheckRunId;
// update the check
const updateCheck = await octokit.rest.checks.update(body);
checkRunId = updateCheck.data.id;
core.info(`updated a check run with the id of ${checkRunId}`);
}
core.setOutput("check-run-id", checkRunId);
core.info("action was successful");
core.endGroup();
} catch (error) {
core.error(`Error ${error}, action did not succeed`);
core.endGroup();
}
}
run();