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

Force labelling of PRs + connecting to an issue and reopen unlabeled issues #1781

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
55 changes: 55 additions & 0 deletions .github/workflows/enforce-PR-labelling.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: PR Validation

on:
pull_request:
# one limitation here is that there's no trigger to re-run any time we "connect" or "disconnect" an issue
types: [opened, edited, labeled, unlabeled, synchronize]

jobs:
validate-pr:
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v2

- name: Validate PR has labels
id: check_labels
run: |
PR_LABELS=$(jq -r '.pull_request.labels | length' $GITHUB_EVENT_PATH)
if [ "$PR_LABELS" -eq "0" ]; then
echo "No labels found on the pull request."
exit 1
fi

- name: Validate PR is linked to an issue
id: check_linked_issues
run: |
PR_NUMBER=$(jq -r '.pull_request.number' $GITHUB_EVENT_PATH)
REPO_OWNER=$(jq -r '.repository.owner.login' $GITHUB_EVENT_PATH)
REPO_NAME=$(jq -r '.repository.name' $GITHUB_EVENT_PATH)
TIMELINE_JSON=$(curl -s "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/timeline")

# Count the number of times the timeline sees a "connected" event and subract the number of "disconnected" events
# We might also consider using the "cross-referenced" event in the future if actual connecting/disconnecting is too heavy-handed
LINKED_ISSUES=$(echo "$TIMELINE_JSON" | jq '
reduce .[] as $event (
0;
if $event.event == "connected" then
. + 1
elif $event.event == "disconnected" then
. - 1
else
.
end
)')

# If the sum is 0, then no linked issues were found
if [ "$LINKED_ISSUES" -eq "0" ]; then
echo "❌ No linked issues found in the pull request."
exit 1
elif [ "$LINKED_ISSUES" -lt "0" ]; then
echo "Error: More disconnected events than connected events. This shouldn't be possible and likely indicates a big ol' 🪲"
exit 1
else
echo "Linked issues found: $LINKED_ISSUES"
fi
32 changes: 32 additions & 0 deletions .github/workflows/enforce-issue-labelling.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Issue Validation

on:
issues:
types: [closed]

jobs:
validate-issue:
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v2

- name: Validate issue has labels
id: check_labels
run: |
ISSUE_LABELS=$(jq -r '.issue.labels | length' $GITHUB_EVENT_PATH)
if [ "$ISSUE_LABELS" -eq "0" ]; then
echo "No labels found on the issue."
# Re-open the issue
ISSUE_NUMBER=$(jq -r '.issue.number' $GITHUB_EVENT_PATH)
REPO_OWNER=$(jq -r '.repository.owner.login' $GITHUB_EVENT_PATH)
REPO_NAME=$(jq -r '.repository.name' $GITHUB_EVENT_PATH)
curl -L \
-X PATCH \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$ISSUE_NUMBER \
-d '{"state":"open"}'
exit 1
fi
Loading