-
Notifications
You must be signed in to change notification settings - Fork 11
/
validate-labels.js
executable file
·55 lines (47 loc) · 1.25 KB
/
validate-labels.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
#!/usr/bin/env node
// SPDX-FileCopyrightText: 2017-2024 City of Espoo
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Usage in a GitHub Actions workflow:
//
// echo '${{ toJSON(github.event.pull_request.labels[*].name) }}' | bin/check-labels.js
//
function main() {
let inputData = ''
const stdin = process.stdin;
stdin.setEncoding('utf-8')
stdin.on('data', (data) => {
inputData += data
});
stdin.on('end', () => {
let json
try {
json = JSON.parse(inputData);
} catch (error) {
console.error('An error occurred while parsing JSON:', error.message);
process.exit(1);
}
if (!validateLabels(json)) {
console.error('Each pull request must have exactly one of the following labels:', knownLabels.join(', '));
console.error('Other labels are ignored by the validation.')
console.error('This pull request has the following labels:', json.join(', '));
process.exit(1);
}
});
}
const knownLabels = [
'no-changelog',
'breaking',
'enhancement',
'bug',
'tech',
'dependencies'
];
function validateLabels(labels) {
const numKnownLabels = labels.reduce(
(acc, label) => (knownLabels.includes(label)) ? acc + 1 : acc,
0
);
return numKnownLabels === 1;
}
main();