From 7959091ce49d030c35c17af831f4b5585a418033 Mon Sep 17 00:00:00 2001 From: Scott Morrison Date: Tue, 29 Aug 2023 14:09:20 +1000 Subject: [PATCH] feat: add labels from comments (#2460) --- .github/PULL_REQUEST_TEMPLATE.md | 3 ++ .github/workflows/labels-from-comments.yml | 43 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 .github/workflows/labels-from-comments.yml diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 59832d5a18..174ef60d41 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -9,3 +9,6 @@ * Please make sure the PR has excellent documentation and tests. If we label it `missing documentation` or `missing tests` then it needs fixing! + +* You can manage the `awaiting-review`, `awaiting-author`, and `WIP` labels + yourself, by writing a comment containing one of these labels on its own line. diff --git a/.github/workflows/labels-from-comments.yml b/.github/workflows/labels-from-comments.yml new file mode 100644 index 0000000000..c39af881c2 --- /dev/null +++ b/.github/workflows/labels-from-comments.yml @@ -0,0 +1,43 @@ +# This workflow allows any user to add one of the `awaiting-review`, `awaiting-author`, or `WIP` labels, +# by commenting on the PR or issue. +# Other labels from this set are removed automatically at the same time. + +name: Label PR based on Comment + +on: + issue_comment: + types: [created] + +jobs: + update-label: + if: github.event.issue.pull_request != null && (contains(github.event.comment.body, 'awaiting-review') || contains(github.event.comment.body, 'awaiting-author') || contains(github.event.comment.body, 'WIP')) + runs-on: ubuntu-latest + + steps: + - name: Add label based on comment + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const { owner, repo, number: issue_number } = context.issue; + const commentLines = context.payload.comment.body.split('\r\n'); + + const awaitingReview = commentLines.includes('awaiting-review'); + const awaitingAuthor = commentLines.includes('awaiting-author'); + const wip = commentLines.includes('WIP'); + + if (awaitingReview || awaitingAuthor || wip) { + await github.rest.issues.removeLabel({ owner, repo, issue_number, name: 'awaiting-review' }).catch(() => {}); + await github.rest.issues.removeLabel({ owner, repo, issue_number, name: 'awaiting-author' }).catch(() => {}); + await github.rest.issues.removeLabel({ owner, repo, issue_number, name: 'WIP' }).catch(() => {}); + } + + if (awaitingReview) { + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['awaiting-review'] }); + } + if (awaitingAuthor) { + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['awaiting-author'] }); + } + if (wip) { + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['WIP'] }); + }