-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.ts
74 lines (57 loc) · 1.83 KB
/
index.ts
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
import { getInput, setOutput } from "@actions/core";
import { context } from "@actions/github";
import { Octokit } from "@octokit/action";
import { getPreviewUrlFromString } from "./index.utils";
const cancelAction = async () => {
if (getInput("GITHUB_TOKEN")) {
const octokit = new Octokit();
await octokit.actions.cancelWorkflowRun({
...context.repo,
run_id: context.runId,
});
// Wait a maximum of 1 minute for the action to be cancelled.
await new Promise((resolve) => setTimeout(resolve, 60000));
}
// If no GitHub token or timeout has passed, fail action.
process.exit(1);
};
const runAction = async () => {
const { comment } = context.payload;
if (!comment) {
console.log("Action triggered on non-comment event.");
await cancelAction();
return;
}
const vercelBotName = getInput("vercel_bot_name");
const { login } = comment.user;
if (login !== vercelBotName) {
console.log("Comment did not originate from Vercel bot.", {
vercelBotName,
});
await cancelAction();
}
const cancelOnStrings = getInput("cancel_on_strings").split(",");
const { body } = comment;
if (cancelOnStrings.some((word) => body.includes(word))) {
console.log("Comment contained a word that should cancel the action.", {
cancelOnStrings,
comment: body,
});
await cancelAction();
}
const vercelPreviewUrl = getPreviewUrlFromString(
body,
getInput("preview_url_regexp")
);
if (!vercelPreviewUrl) {
console.log(
"The regular expression was not able to capture the preview URL. " +
"Please ensure at least 1 capture group matches the preview URL's pattern."
);
process.exit(1);
}
console.log("Found preview URL.", { vercelPreviewUrl });
setOutput("vercel_preview_url", vercelPreviewUrl);
process.exit(0);
};
runAction();