Share #151
Workflow file for this run
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
name: Add Labels to PR | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
permissions: | |
contents: read | |
pull-requests: write | |
on: | |
pull_request: | |
types: | |
- opened | |
- synchronize | |
workflow_dispatch: | |
jobs: | |
labeler: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- run: |- | |
echo "ALL_LABELS=\"$(grep -Po '(?<=name: ).*' ./.github/labels.yml | tr '\n' ' ')\"" >> $GITHUB_OUTPUT | |
id: labeler-labels | |
- run: |- | |
git fetch origin ${{ github.event.pull_request.head.ref }} | |
echo "COMMIT_MESSAGES=\"$(git log --format=%s ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} | tr '\n' ' ')\"" >> $GITHUB_OUTPUT | |
id: labeler-commits | |
- uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: |- | |
const commit_messages = ${{ steps.labeler-commits.outputs.COMMIT_MESSAGES }}.trim().split(" "); | |
const all_labels = ${{ steps.labeler-labels.outputs.ALL_LABELS }}.trim().split(" "); | |
const old_labels_response = await github.rest.issues.listLabelsOnIssue({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.payload.pull_request.number, | |
}); | |
const old_labels = old_labels_response.data.map(label => label.name); | |
for (const old_label of old_labels) { | |
await github.rest.issues.removeLabel({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.payload.pull_request.number, | |
name: old_label, | |
}); | |
} | |
const new_labels = new Set(); | |
for (const commit_message of commit_messages) { | |
for (const label of all_labels) { | |
if (commit_message.includes("[" + label + "]")) new_labels.add(label); | |
} | |
} | |
if (new_labels.size > 0) { | |
await github.rest.issues.addLabels({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.payload.pull_request.number, | |
labels: Array.from(new_labels), | |
}); | |
} |