-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
79 lines (68 loc) · 2.52 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
75
76
77
78
79
import * as core from '@actions/core';
import * as github from '@actions/github';
const githubToken = core.getInput('github-token', { required: true });
const titleRegexInput = core.getInput('title-regex');
const onFailRequestChanges = core.getInput('on-fail-request-changes') == 'true';
const onFailAddComment = core.getInput('on-fail-add-comment') == 'true';
const onFailMessage = core.getInput('on-fail-message');
const onFailFailAction = core.getInput('on-fail-fail-action') == 'true';
const ignoredContributors = (core.getInput('ignored-contributors') || '').split(',').map((s) => s.trim());
const octokit = github.getOctokit(githubToken);
async function run(): Promise<void> {
const githubContext = github.context;
const pullRequest = githubContext.issue;
core.debug(`Ignored Contributors: ${ignoredContributors.length}`);
if (ignoredContributors.includes(githubContext.actor)) {
core.debug('Ignoring actor');
return;
}
const titleRegex = new RegExp(titleRegexInput);
const title: string = (githubContext.payload.pull_request?.title as string) ?? '';
const comment = onFailMessage.replace('%regex%', titleRegex.source);
core.debug(`Title Regex: ${titleRegex.source}`);
core.debug(`Title: ${title}`);
const titleMatchesRegex: boolean = titleRegex.test(title);
if (!titleMatchesRegex) {
if (onFailRequestChanges || onFailAddComment) {
createReview(comment, pullRequest);
}
if (onFailFailAction) {
core.setFailed(comment);
}
} else {
if (onFailRequestChanges) {
await dismissReview(pullRequest);
}
}
}
function createReview(comment: string, pullRequest: { owner: string; repo: string; number: number }) {
void octokit.rest.pulls.createReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
body: comment,
event: onFailAddComment ? 'COMMENT' : 'REQUEST_CHANGES',
});
}
async function dismissReview(pullRequest: { owner: string; repo: string; number: number }) {
const reviews = await octokit.rest.pulls.listReviews({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
});
reviews.data.forEach((review) => {
const user = review.user;
if (user && user.login == 'github-actions[bot]') {
void octokit.rest.pulls.dismissReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
review_id: review.id,
message: 'All good!',
});
}
});
}
run().catch((error) => {
core.setFailed(error);
});