-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a CI workflow that validates PR labels
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |