/
validatePrTitle.js
104 lines (87 loc) · 3.14 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
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, subjectPattern, subjectPatternError} = {}
) {
if (!types) types = defaultTypes;
const {parserOpts} = await conventionalCommitsConfig();
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);
}
if (!result.type) {
throw new Error(
`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) {
throw new Error(`No subject found in pull request title "${prTitle}".`);
}
if (!types.includes(result.type)) {
throw new Error(
`Unknown release type "${
result.type
}" found in pull request title "${prTitle}". \n\n${printAvailableTypes()}`
);
}
if (requireScope && !result.scope) {
throw new Error(
`No scope found in pull request title "${prTitle}". Use one of the available scopes: ${scopes.join(
', '
)}.`
);
}
const givenScopes = result.scope
? result.scope.split(',').map((scope) => scope.trim())
: undefined;
const unknownScopes = givenScopes ? givenScopes.filter(isUnknownScope) : [];
if (scopes && unknownScopes.length > 0) {
throw new Error(
`Unknown ${
unknownScopes.length > 1 ? 'scopes' : 'scope'
} "${unknownScopes.join(
','
)}" found in pull request title "${prTitle}". Use one of the available scopes: ${scopes.join(
', '
)}.`
);
}
function throwSubjectPatternError(message) {
if (subjectPatternError) {
message = formatMessage(subjectPatternError, {
subject: result.subject,
title: prTitle
});
}
throw new Error(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.`
);
}
}
};