Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PR label action #1796

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/require_label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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
shell: bash
run: |
REQUIRED_LABELS=("documentation" "bugfix" "chore" "enhancement" "ignore-for-release" "security" "sorbet")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the labels will now be required, I wonder if we're missing another category. For example, would a refactor fit into chore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd been using chore for refactors and most other non-user visible changes, yeah. Open to better suggestions, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Honestly, that sounds good, I was just wondering. IMO, ship it and we'll identify if any categories are missing as we open PRs.

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
for label in "${REQUIRED_LABELS[@]}"; do
echo "- \`$label\`" >> comment.md
done
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
Loading