From 37b41e2549f8cab43ca0865eb8ba56a8b340cce5 Mon Sep 17 00:00:00 2001 From: Justin Hiemstra Date: Wed, 27 Nov 2024 20:21:30 +0000 Subject: [PATCH 1/3] Add GHA that enforces labeling and linking in PRs --- .github/workflows/enforce-PR-labelling.yml | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 .github/workflows/enforce-PR-labelling.yml diff --git a/.github/workflows/enforce-PR-labelling.yml b/.github/workflows/enforce-PR-labelling.yml new file mode 100644 index 000000000..2c935c634 --- /dev/null +++ b/.github/workflows/enforce-PR-labelling.yml @@ -0,0 +1,55 @@ +name: PR and Issue 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 From 9d307aba66b55b426a6fe8f8d16dc5ae7967436f Mon Sep 17 00:00:00 2001 From: Justin Hiemstra Date: Wed, 27 Nov 2024 20:52:33 +0000 Subject: [PATCH 2/3] Add issue labelling enforcement --- .github/workflows/enforce-PR-labelling.yml | 2 +- .github/workflows/enforce-issue-labelling.yml | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/enforce-issue-labelling.yml diff --git a/.github/workflows/enforce-PR-labelling.yml b/.github/workflows/enforce-PR-labelling.yml index 2c935c634..28fa6885f 100644 --- a/.github/workflows/enforce-PR-labelling.yml +++ b/.github/workflows/enforce-PR-labelling.yml @@ -1,4 +1,4 @@ -name: PR and Issue Validation +name: PR Validation on: pull_request: diff --git a/.github/workflows/enforce-issue-labelling.yml b/.github/workflows/enforce-issue-labelling.yml new file mode 100644 index 000000000..113fccd28 --- /dev/null +++ b/.github/workflows/enforce-issue-labelling.yml @@ -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 From 81e5bc64046eeb8a9ac7f47d82d596dc0cfab207 Mon Sep 17 00:00:00 2001 From: Justin Hiemstra Date: Tue, 10 Dec 2024 19:55:57 +0000 Subject: [PATCH 3/3] Add `workflow_dispatch` as trigger for PR validation action Cannon pointed out this lets us re-run the action after linking an issue, since there's no trigger for "connected" and "disconnected" events. Best thing to do is still to link issues before the PR is officially opened, but that shouldn't be the only way to do things! --- .github/workflows/enforce-PR-labelling.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/enforce-PR-labelling.yml b/.github/workflows/enforce-PR-labelling.yml index 28fa6885f..e9d7f9ae7 100644 --- a/.github/workflows/enforce-PR-labelling.yml +++ b/.github/workflows/enforce-PR-labelling.yml @@ -4,6 +4,7 @@ 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] + workflow_dispatch: jobs: validate-pr: