-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
validatePrTitle.js
144 lines (123 loc) · 4.05 KB
/
validatePrTitle.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const core = require('@actions/core');
const conventionalCommitsConfig = require('conventional-changelog-conventionalcommits');
const conventionalCommitTypes = require('conventional-commit-types');
const parser = require('conventional-commits-parser').sync;
const formatMessage = require('./formatMessage');
const defaultTypes = Object.keys(conventionalCommitTypes.types);
module.exports = async function validatePrTitle(
prTitle,
{
types,
scopes,
requireScope,
disallowScopes,
subjectPattern,
subjectPatternError,
headerPattern,
headerPatternCorrespondence
} = {}
) {
if (!types) types = defaultTypes;
const {parserOpts} = await conventionalCommitsConfig();
if (headerPattern) {
parserOpts.headerPattern = headerPattern;
}
if (headerPatternCorrespondence) {
parserOpts.headerCorrespondence = headerPatternCorrespondence;
}
const result = parser(prTitle, parserOpts);
function printAvailableTypes() {
return `Available types:\n${types
.map((type) => {
let bullet = ` - ${type}`;
if (types === defaultTypes) {
bullet += `: ${conventionalCommitTypes.types[type].description}`;
}
return bullet;
})
.join('\n')}`;
}
function isUnknownScope(s) {
return scopes && !scopes.some((scope) => new RegExp(`^${scope}$`).test(s));
}
function isDisallowedScope(s) {
return (
disallowScopes &&
disallowScopes.some((scope) => new RegExp(`^${scope}$`).test(s))
);
}
if (!result.type) {
raiseError(
`No release type found in pull request title "${prTitle}". Add a prefix to indicate what kind of release this pull request corresponds to. For reference, see https://www.conventionalcommits.org/\n\n${printAvailableTypes()}`
);
}
if (!result.subject) {
raiseError(`No subject found in pull request title "${prTitle}".`);
}
if (!types.includes(result.type)) {
raiseError(
`Unknown release type "${
result.type
}" found in pull request title "${prTitle}". \n\n${printAvailableTypes()}`
);
}
if (requireScope && !result.scope) {
let message = `No scope found in pull request title "${prTitle}".`;
if (scopes) {
message += ` Scope must match one of: ${scopes.join(', ')}.`;
}
raiseError(message);
}
const givenScopes = result.scope
? result.scope.split(',').map((scope) => scope.trim())
: undefined;
const unknownScopes = givenScopes ? givenScopes.filter(isUnknownScope) : [];
if (scopes && unknownScopes.length > 0) {
raiseError(
`Unknown ${
unknownScopes.length > 1 ? 'scopes' : 'scope'
} "${unknownScopes.join(
','
)}" found in pull request title "${prTitle}". Scope must match one of: ${scopes.join(
', '
)}.`
);
}
const disallowedScopes = givenScopes
? givenScopes.filter(isDisallowedScope)
: [];
if (disallowScopes && disallowedScopes.length > 0) {
raiseError(
`Disallowed ${
disallowedScopes.length === 1 ? 'scope was' : 'scopes were'
} found: ${disallowedScopes.join(', ')}`
);
}
function throwSubjectPatternError(message) {
if (subjectPatternError) {
message = formatMessage(subjectPatternError, {
subject: result.subject,
title: prTitle
});
}
raiseError(message);
}
if (subjectPattern) {
const match = result.subject.match(new RegExp(subjectPattern));
if (!match) {
throwSubjectPatternError(
`The subject "${result.subject}" found in pull request title "${prTitle}" doesn't match the configured pattern "${subjectPattern}".`
);
}
const matchedPart = match[0];
if (matchedPart.length !== result.subject.length) {
throwSubjectPatternError(
`The subject "${result.subject}" found in pull request title "${prTitle}" isn't an exact match for the configured pattern "${subjectPattern}". Please provide a subject that matches the whole pattern exactly.`
);
}
}
function raiseError(message) {
core.setOutput('error_message', message);
throw new Error(message);
}
};