Skip to content

[GitHub] Action to check labels #19

[GitHub] Action to check labels

[GitHub] Action to check labels #19

Workflow file for this run

---
name: Label Checker
on:
pull_request:
types:
- opened
- synchronize
- reopened
- labeled
- unlabeled
jobs:
check_labels:
name: Check labels
runs-on: ubuntu-latest
steps:
- name: Check Labels and Add Comment
id: check_labels_and_comment
uses: actions/github-script@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Get PR info: id and labels
const prNumber = context.payload.pull_request.number;
const labels = context.payload.pull_request.labels.map(label => label.name);
// Define an array of descriptive labels
const descriptiveLabels = [
'enhancement',
'deprecated',
'refactoring',
'pr: breaking',
'pr: clean',
'pr: fix',
'pr: new feature',
'pr: test'
];
// Check if any descriptive label is included in the 'labels' array
const hasDescriptiveLabel = labels.some(label => descriptiveLabels.includes(label));
// If no descriptive label is set, add a comment in the PR and make the action check fail
if (!hasDescriptiveLabel) {
const comment = ':warning: :warning: :warning:<br>@' + context.repo.owner + ' your PR does not include any **descriptive** label :label:<br> Make sure to add an appropriate [PR label](https://github.com/sofa-framework/sofa/labels) before merge.<br>:warning: :warning: :warning:';
github.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
core.setFailed('Invalid PR label');
}
// Array of possible labels defining the status of the PR
const statusLabels = [
'pr: status wip',
'pr: status to review',
'pr: status ready'
];
// Filter the labels array to get only the status labels
const matchingStatusLabels = labels.filter(label => statusLabels.includes(label));
// Count the number of entries in 'matchingLabels'
const matchingLabelsCount = matchingStatusLabels.length;
// If no descriptive label is set, add a comment in the PR and make the action check fail
if (matchingLabelsCount === 0) {
const comment = ':warning: :warning: :warning:<br>@'+context.repo.owner+' your PR does not include any **status** label :label:<br> Make sure to add one (wip, to review or ready).<br>:warning: :warning: :warning:';
github.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
core.setFailed('Invalid status PR label')
} else if (matchingLabelsCount > 1) {
const comment = ':warning: :warning: :warning:<br>@'+context.repo.owner+' your PR does includes **too many status labels** :label:<br> Make sure to keep only one (wip, to review or ready).<br>:warning: :warning: :warning:';
github.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
core.setFailed('Invalid status PR label')
}
// Add all PR labels in log
console.log('Labels:', labels.join(', '));