diff --git a/.github/workflows/close-iss-on-merge.yml b/.github/workflows/close-iss-on-merge.yml new file mode 100644 index 00000000..425bb0b8 --- /dev/null +++ b/.github/workflows/close-iss-on-merge.yml @@ -0,0 +1,33 @@ +name: Close Issues on PR Merge +on: + pull_request: + types: [closed] + +jobs: + close-issues: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Close linked issues + if: github.event.pull_request.merged == true + run: | + # Extract issue numbers from both direct references and comment links + ISSUES=$(jq -r '.pull_request.body' "$GITHUB_EVENT_PATH" | grep -Eo '(#|issues/)[0-9]+' | grep -Eo '[0-9]+') + + for ISSUE in $ISSUES + do + echo "Closing issue #$ISSUE" + # Post a comment on the issue + curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github.v3+json" \ + https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE/comments \ + -d '{"body":"Closed by PR #${{ github.event.pull_request.number }}"}' + # Close the issue + curl -X PATCH -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github.v3+json" \ + https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE \ + -d '{"state":"closed"}' + done