/
validatePrTitle.js
141 lines (120 loc) · 3.97 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
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.includes(s);
}
function isDisallowedScope(s) {
return disallowScopes && disallowScopes.includes(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 += ` Use one of the available scopes: ${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}". Use one of the available scopes: ${scopes.join(
', '
)}.`
);
}
const disallowedScopes = givenScopes
? givenScopes.filter(isDisallowedScope)
: [];
if (disallowScopes && disallowedScopes.length > 0) {
raiseError(
`Disallowed ${
disallowedScopes.length === 1 ? 'scope was' : 'scopes were'
} found: ${disallowScopes.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);
}
};