From 792f65f176f3c0ab28a02f0a842b8ad25eb88b69 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Tue, 13 Feb 2024 01:44:25 +0200 Subject: [PATCH] Add PR label action --- .github/workflows/require_label.yml | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 .github/workflows/require_label.yml diff --git a/.github/workflows/require_label.yml b/.github/workflows/require_label.yml new file mode 100644 index 0000000000..a4bbccc544 --- /dev/null +++ b/.github/workflows/require_label.yml @@ -0,0 +1,59 @@ +name: Pull Request Labels + +on: + pull_request: + types: [opened, synchronize, reopened, labeled, unlabeled] + +jobs: + check-labels: + runs-on: ubuntu-latest + env: + GH_TOKEN: ${{ github.token }} + steps: + - uses: actions/checkout@v4 + - name: Check for Pull Request Labels + run: | + REQUIRED_LABELS=("documentation" "bugfix" "chore" "enhancement" "ignore-for-release" "security" "sorbet") + LABELS=$(gh pr view --json labels --jq ".labels[] .name" ${{ github.event.pull_request.number }}) + + errorMessage="" + if [ -z "$LABELS" ]; then + echo "No labels found on PR" + errorMessage="No labels found on PR" + else + matched=0 + for label in $LABELS; do + if [[ "${REQUIRED_LABELS[@]}" =~ "${label}" ]]; then + matched=1 + echo "Matched label: `$label`" + break + fi + done + + if [ $matched -eq 0 ]; then + echo "No matching required labels found on PR" + errorMessage="No matching required labels found on PR" + fi + fi + + if [ -n "$errorMessage" ]; then + echo "### Pull Request Labels " > comment.md + echo " " >> comment.md + echo 'Error: `'$errorMessage'`'" " >> comment.md + echo " " >> comment.md + echo "Please add one of the following labels to your pull request:" >> comment.md + echo '`'"${REQUIRED_LABELS[@]}"'`'" " >> comment.md + echo " " >> comment.md + + gh pr comment ${{ github.event.pull_request.number }} --body-file comment.md --edit-last || \ + gh pr comment ${{ github.event.pull_request.number }} --body-file comment.md + exit 1 + else + echo "All required labels found on PR" + commentId=$(gh pr view -c --json comments --jq ".comments | reverse[] | select(.viewerDidAuthor == true) | .id" ${{ github.event.pull_request.number }}) + if [ -n "$commentId" ]; then + echo "Deleting previous comment: /repos/${{ github.repository }}/issues/comments/$commentId" + gh api graphql -f query="mutation {deleteIssueComment(input: { id: \"$commentId\" }) { clientMutationId }}" || true + fi + fi + shell: bash