Skip to content

Commit

Permalink
Add a CI workflow that validates PR labels
Browse files Browse the repository at this point in the history
  • Loading branch information
akheron committed Nov 5, 2024
1 parent f6e40f2 commit 9f87756
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/validate-labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: 2017-2024 City of Espoo
#
# SPDX-License-Identifier: LGPL-2.1-or-later

name: 'Validate PR labels'
on:
pull_request:
types:
- labeled
- unlabeled
- opened
- reopened
- ready_for_review

jobs:
validate-labels:
if: ${{ github.event.pull_request.state == 'open' && !github.event.pull_request.draft }}
runs-on: ubuntu-latest
steps:
- run: |
echo '${{ toJSON(github.event.pull_request.labels[*].name) }}' | bin/validate-labels.js
54 changes: 54 additions & 0 deletions bin/validate-labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env node

// SPDX-FileCopyrightText: 2017-2024 City of Espoo
//
// SPDX-License-Identifier: LGPL-2.1-or-later

// Usage:
//
// echo '["foo", "bar"]' | 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(', '));
process.exit(1);
}
});
}

const knownLabels = [
'no-changelog',
'breaking',
'enhancement',
'bug',
'unknown',
'tech',
'dependencies'
];

function validateLabels(labels) {
const numKnownLabels = labels.reduce(
(acc, label) => (knownLabels.includes(label)) ? acc + 1 : acc,
0
);
return numKnownLabels === 1;
}

main();

0 comments on commit 9f87756

Please sign in to comment.