-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
workflow: Force issue number mention in PR description and auto close…
… issue. Closes #3032.
- Loading branch information
1 parent
f6e758b
commit ad9540a
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 # |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |