-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (51 loc) · 2.05 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
import * as core from '@actions/core'
import * as github from '@actions/github'
import { readFile } from 'fs/promises'
const validEvent = ['pull_request']
async function content(path) {
return await readFile(path, 'utf8')
}
try {
const { eventName, payload: {repository: repo, pull_request: pr} } = github.context
const allowlist_path = core.getInput('allowlist-path');
if (validEvent.indexOf(eventName) < 0) {
core.error(`Invalid event: ${eventName}`)
} else {
const token = core.getInput('token')
const filterOutPattern = core.getInput('filter_out_pattern')
const filterOutFlags = core.getInput('filter_out_flags')
const octokit = github.getOctokit(token)
core.debug(`owner: ${repo.owner.login}`);
core.debug(`repo: ${repo.name}`);
core.debug(`pull_number: ${pr.number}`);
let { data: commits } = await octokit.rest.pulls.listCommits({
owner: repo.owner.login,
repo: repo.name,
pull_number: pr.number,
});
if (filterOutPattern) {
const regex = new RegExp(filterOutPattern, filterOutFlags)
commits = commits.filter(({ author }) => {
core.debug(`commits.author`, author);
return !regex.test(author.login)
})
}
core.debug(`commits: ${JSON.stringify(commits, null, 2)}`);
let commit_authors = commits.map((commit) => commit.author.login).reduce(
(unique, item) => (unique.includes(item) ? unique : [...unique, item]),
[],
);
console.log(`The commits authors: ${JSON.stringify(commit_authors, null, 2)}`);
const allowlist = await content(`./${allowlist_path}`);
let unknown_authors = commit_authors.filter((author) => {
const regex = new RegExp(`@${author}`, "i");
return !regex.test(allowlist);
})
console.log('Unknown authors:', unknown_authors);
if (unknown_authors.length > 0) {
core.setFailed(`Commits were authored by individuals not added to the allowlist at ${allowlist_path}. Please add them and commit the change to this PR.`)
}
}
} catch (error) {
core.setFailed(error.message);
}