Skip to content

Commit

Permalink
workflow: Force issue number mention in PR description and auto close…
Browse files Browse the repository at this point in the history
… issue.

Closes #3032.
  • Loading branch information
apoorvapendse committed Dec 2, 2024
1 parent ae45ef2 commit aaf7265
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Description

<!-- Enter the PR Description -->


<!-- IMPORTANT: You must add a Fixes #<issue_number> in order for this PR to be reviewed and pass the CI tests
Eg: Fixes #1234.
closes #4444.
Resolves #2323.
-->

Fixes #
42 changes: 42 additions & 0 deletions .github/workflows/close-issue-on-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Close Issues from PR Description

on:
pull_request:
types:
- closed

jobs:
close_issues:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Parse PR description and close issues
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [[ "${{ github.event.pull_request.merged }}" == "true" && "${{ github.event.pull_request.base.ref }}" == "main" ]]; then
echo "PR merged into main. Processing issues..."
# Extract all "fixes/resolves/closes" followed by issue numbers
PR_BODY="${{ github.event.pull_request.body }}"
REGEX="([fF]ixes|[Rr]esolves|[Cc]loses) #([0-9]+)"
while [[ $PR_BODY =~ $REGEX ]]; do
KEYWORD=${BASH_REMATCH[1]}
ISSUE_NUMBER=${BASH_REMATCH[2]}
echo "Closing issue #$ISSUE_NUMBER with keyword $KEYWORD"
curl -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/comments \
-d '{"body":"Automatically closed by PR #'"${{ github.event.pull_request.number }}"'."}'
curl -X PATCH \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER \
-d '{"state":"closed"}'
PR_BODY=${PR_BODY#*"#$ISSUE_NUMBER"} # Move past this issue
done
else
echo "PR not merged into main. Skipping issue closure."
fi
29 changes: 29 additions & 0 deletions .github/workflows/enforce-issue-number-in-description.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Enforce Issue Mention in PR Description

on:
pull_request:
types:
- opened
- edited
- reopened
- synchronize

jobs:
validate_pr_description:
runs-on: ubuntu-latest
steps:
- name: Check PR description for issue mention
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_BODY="${{ github.event.pull_request.body }}"
REGEX="([fF]ixes|[rR]esolves|[Cc]loses) #([0-9]+)"
if [[ -z "$PR_BODY" ]]; then
echo "Error: PR description is empty. Please include a reference to an issue using 'fixes #<issue-number>', 'resolves #<issue-number>', or 'closes #<issue-number>'."
exit 1
fi
if ! [[ $PR_BODY =~ $REGEX ]]; then
echo "Error: PR description does not mention an issue. Please include a reference using 'fixes #<issue-number>', 'resolves #<issue-number>', or 'closes #<issue-number>'."
exit 1
fi
echo "PR description is valid."

0 comments on commit aaf7265

Please sign in to comment.