forked from aaimio/vercel-preview-url-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (59 loc) · 2.02 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
const core = require("@actions/core");
const github = require("@actions/github");
const { Octokit } = require("@octokit/action");
const cancelAction = async () => {
if (core.getInput("GITHUB_TOKEN")) {
const octokit = new Octokit();
await octokit.actions.cancelWorkflowRun({
...github.context.repo,
run_id: github.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 { payload } = github.context;
const { comment } = payload;
if (!comment) {
console.log("Action triggered on non-comment event.");
await cancelAction();
}
const vercel_bot_name = core.getInput("vercel_bot_name");
if (comment.user.login !== vercel_bot_name) {
console.log("Comment did not originate from Vercel bot.", {
vercel_bot_name,
});
await cancelAction();
}
const cancel_on_strings = core.getInput("cancel_on_strings").split(",");
if (cancel_on_strings.some((word) => comment.body.includes(word))) {
console.log("Comment contained a word that should cancel the action.", {
cancel_on_strings,
comment: comment.body,
});
await cancelAction();
}
const preview_url_regexp = new RegExp(core.getInput("preview_url_regexp"));
const regex_matches = comment.body.match(preview_url_regexp);
if (!regex_matches) {
console.log("Unable to find a preview URL in comment's body.", {
comment: comment.body,
});
await cancelAction();
}
const vercel_preview_url = regex_matches[1];
if (vercel_preview_url) {
console.log("Found preview URL.", { vercel_preview_url });
core.setOutput("vercel_preview_url", vercel_preview_url);
process.exit(0);
} else {
console.log(
"The regular expression is in an invalid format. Please ensure the first capture group caputures the preview URL."
);
process.exit(1);
}
};
runAction();