forked from deepakputhraya/action-pr-title
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (62 loc) · 2.3 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
const core = require('@actions/core');
const github = require('@actions/github');
function validateTitlePrefix(title, prefix, caseSensitive) {
if (!caseSensitive) {
prefix = prefix.toLowerCase();
title = prefix.toLowerCase();
}
return title.startsWith(prefix);
}
async function run() {
try {
const eventName = github.context.eventName;
core.info(`Event name: ${eventName}`);
const title = getTitle(github.context.payload);
if (!title) {
core.setFailed(`Invalid event: ${eventName}`);
return;
}
core.info(`Pull Request title: "${title}"`);
// Check if title pass regex
const regex = RegExp(core.getInput('regex'));
core.info(`Regex: ${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 a prefix
const 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
}
} catch (error) {
core.setFailed(error.message);
}
}
function getTitle(payload) {
if (payload && payload.issue && payload.issue.title) {
return payload.issue.title;
}
if (payload &&
payload.pull_request &&
payload.pull_request.title)
{
return payload.pull_request.title;
}
}
run();