-
Notifications
You must be signed in to change notification settings - Fork 78
/
index.js
85 lines (71 loc) · 3.18 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
const core = require('@actions/core');
const github = require('@actions/github');
const validEvent = ['pull_request'];
function validateTitlePrefix(title, prefix, caseSensitive) {
if (!caseSensitive) {
prefix = prefix.toLowerCase();
title = title.toLowerCase();
}
return title.startsWith(prefix);
}
async function run() {
try {
const authToken = core.getInput('github_token', {required: true})
const eventName = github.context.eventName;
core.info(`Event name: ${eventName}`);
if (validEvent.indexOf(eventName) < 0) {
core.setFailed(`Invalid event: ${eventName}`);
return;
}
const owner = github.context.payload.pull_request.base.user.login;
const repo = github.context.payload.pull_request.base.repo.name;
const client = new github.getOctokit(authToken);
// The pull request info on the context isn't up to date. When
// the user updates the title and re-runs the workflow, it would
// be outdated. Therefore fetch the pull request via the REST API
// to ensure we use the current title.
const {data: pullRequest} = await client.rest.pulls.get({
owner,
repo,
pull_number: github.context.payload.pull_request.number
});
const title = pullRequest.title;
core.info(`Pull Request title: "${title}"`);
// Check if title pass regex
const regex = RegExp(core.getInput('regex'));
if (!regex.test(title)) {
core.setFailed(`Pull Request title "${title}" failed to pass match regex - ${regex}`);
return
}
// Check min length
const minLen = parseInt(core.getInput('min_length'));
if (title.length < minLen) {
core.setFailed(`Pull Request title "${title}" is smaller than min length specified - ${minLen}`);
return
}
// Check max length
const maxLen = parseInt(core.getInput('max_length'));
if (maxLen > 0 && title.length > maxLen) {
core.setFailed(`Pull Request title "${title}" is greater than max length specified - ${maxLen}`);
return
}
// Check if title starts with an allowed prefix
let prefixes = core.getInput('allowed_prefixes');
const prefixCaseSensitive = (core.getInput('prefix_case_sensitive') === 'true');
core.info(`Allowed Prefixes: ${prefixes}`);
if (prefixes.length > 0 && !prefixes.split(',').some((el) => validateTitlePrefix(title, el, prefixCaseSensitive))) {
core.setFailed(`Pull Request title "${title}" did not match any of the prefixes - ${prefixes}`);
return
}
// Check if title starts with a disallowed prefix
prefixes = core.getInput('disallowed_prefixes');
core.info(`Disallowed Prefixes: ${prefixes}`);
if (prefixes.length > 0 && prefixes.split(',').some((el) => validateTitlePrefix(title, el, prefixCaseSensitive))) {
core.setFailed(`Pull Request title "${title}" matched with a disallowed prefix - ${prefixes}`);
return
}
} catch (error) {
core.setFailed(error.message);
}
}
run();