-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
196 lines (168 loc) · 5.64 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
const { Toolkit } = require("actions-toolkit");
const { parse, toSeconds } = require("iso8601-duration");
// Run your GitHub Action!
Toolkit.run(async (tools) => {
const isOpenedOrSynchronizedPr =
tools.context.event == "pull_request" &&
["opened", "synchronize"].includes(tools.context.payload.action);
const isPrComment =
tools.context.event == "issue_comment" &&
tools.context.payload.action == "created";
const isScheduled = tools.context.event == "schedule";
if (isOpenedOrSynchronizedPr) {
try {
tools.log.pending("Adding pending status check");
await addPendingStatusCheck(tools);
tools.log.complete("Added pending status check");
} catch (e) {
tools.exit.failure(e.message);
}
} else if (isPrComment) {
await tools.command("skipwait", async (args, match) => {
// Check if they're in the list of approved users
const trusted = tools.inputs.trusted || "";
const allowed = trusted
.split(",")
.map((n) => n.toLowerCase())
.filter((n) => n);
const currentUser = tools.context.actor.toLowerCase();
// If they are, update the checks immediately
if (allowed.includes(currentUser)) {
const pr = (
await tools.github.pulls.get({
...tools.context.repo,
pull_number: tools.context.issue.number,
})
).data;
const ref = {
merge: pr.merge_commit_sha,
head: pr.head.sha,
};
await updateShas(tools, ref, "PT0M"); // We require zero wait when forcing
// Then flag the PR as being skipped
tools.github.issues.addLabels({
...tools.context.repo,
issue_number: tools.context.issue.number,
labels: ["hold-your-horses:skipped"],
});
} else {
// Otherwise let them know that they're not in the list, and how to resolve it
let body = "";
if (allowed.length == 0) {
body =
"Sorry, skipping the required wait time isn't enabled on this repo";
} else {
const nameList = allowed.map((name) => `\n * ${name}`).join("");
body = `Sorry, you're not in the list of approved users. You can ask one of the following people to comment for you if needed: ${nameList}`;
}
await tools.github.issues.createComment({
...tools.context.repo,
issue_number: tools.context.issue.number,
body,
});
}
});
} else if (isScheduled) {
// It's run on schedule, so let's check if any statuses need to be updated
tools.log.info("Schedule code");
const labelDurations = tools.inputs.label_durations
.split(",")
.reduce((labels, current) => {
let [label, duration] = current.split("=");
labels[label] = duration;
return labels;
}, {});
const prs = (
await tools.github.pulls.list({
...tools.context.repo,
state: "open",
})
).data;
const shas = prs.map((pr) => {
// If there are any label durations, check if the PR is labelled
// and use the first match
const labels = pr.labels;
let labelDuration = null;
for (let label in labelDurations) {
let matchingLabel = labels.find((l) => l.name == label);
if (matchingLabel) {
labelDuration = labelDurations[label];
break;
}
}
const requiredDelay = labelDuration || tools.inputs.duration || "PT10M";
return {
merge: pr.merge_commit_sha,
head: pr.head.sha,
delay: requiredDelay,
};
});
// For each sha, check if it's due an update
for (let ref of shas) {
tools.log.info(`Running with duration of ${ref.delay}`);
await updateShas(tools, ref, ref.delay);
}
} else {
tools.exit.failure(`Unknown event: ${tools.context.event}`);
}
tools.exit.success("Action finished");
});
function addPendingStatusCheck(tools) {
return tools.github.repos.createStatus({
...tools.context.repo,
sha: tools.context.sha,
state: "pending",
context: "hold-your-horses",
description: "Giving others the opportunity to review",
});
}
function addSuccessStatusCheck(tools, sha) {
return tools.github.repos.createStatus({
...tools.context.repo,
sha,
state: "success",
context: "hold-your-horses",
description: "Review time elapsed",
});
}
async function updateShas(tools, ref, requiredDelay) {
const statuses = (
await tools.github.repos.listStatusesForRef({
...tools.context.repo,
ref: ref.merge,
})
).data;
tools.log.info(`Found ${statuses.length} statuses`);
const hyhStatuses = statuses.filter((s) => s.context == "hold-your-horses");
if (hyhStatuses.length == 0) {
tools.log.info(`No statuses for ${ref.merge}`);
return;
}
const latestStatus = hyhStatuses[0];
if (latestStatus.state == "success") {
tools.log.info(`Check is already success for ${ref.merge}`);
return;
}
const updatedAt = Date.parse(latestStatus.updated_at);
let duration;
try {
duration = parse(requiredDelay);
} catch (e) {
tools.exit.failure(`Invalid duration provided: ${requiredDelay}`);
return;
}
const elapsed = Math.floor((Date.now() - updatedAt) / 1000);
const markAsSuccess = elapsed > toSeconds(duration);
if (markAsSuccess) {
try {
tools.log.info(`Marking ${ref.merge} as done`);
await addSuccessStatusCheck(tools, ref.merge);
tools.log.info(`Marking ${ref.head} as done`);
await addSuccessStatusCheck(tools, ref.head);
} catch (e) {
tools.log.error(e.message);
}
} else {
tools.log.info(`Skipping ${ref.merge} and ${ref.head}`);
}
}