diff --git a/.github/workflows/label-checker.yml b/.github/workflows/label-checker.yml new file mode 100644 index 00000000000..7e8a4ca6708 --- /dev/null +++ b/.github/workflows/label-checker.yml @@ -0,0 +1,95 @@ +--- +name: Label Checker +on: + pull_request: + types: + - opened + - synchronize + - reopened + - labeled + - unlabeled + + +jobs: + + check_labels: + name: Check labels + runs-on: ubuntu-latest + steps: + - name: Delay for 10 Seconds + run: | + echo "Waiting for 10 seconds in case other labels get changed" + sleep 10 + - 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:
@' + context.repo.owner + ' your PR does not include any **descriptive** label :label:
Make sure to add an appropriate [PR label](https://github.com/sofa-framework/sofa/labels) before merge.
: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:
@'+context.repo.owner+' your PR does not include any **status** label :label:
Make sure to add one (wip, to review or ready).
:warning: :warning: :warning:'; + github.issues.createComment({ + issue_number: prNumber, + owner: context.repo.owner, + repo: context.repo.repo, + body: comment + }); + core.setFailed('Missing status PR label') + } else if (matchingLabelsCount > 1) { + const comment = ':warning: :warning: :warning:
@'+context.repo.owner+' your PR does includes **too many status labels** :label:
Make sure to keep only one (wip, to review or ready).
:warning: :warning: :warning:'; + github.issues.createComment({ + issue_number: prNumber, + owner: context.repo.owner, + repo: context.repo.repo, + body: comment + }); + core.setFailed('Too many status PR labels') + } + + // Add all PR labels in log + console.log('Labels:', labels.join(', '));