Skip to content

Commit

Permalink
Add workflow to close issue linked to PR that target a release branches
Browse files Browse the repository at this point in the history
This is a workaround the limitation that github only close linked issues
when the PR target the main branch.
  • Loading branch information
FirelightFlagboy committed Feb 13, 2025
1 parent d93eee7 commit 834beda
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/when-pr-is-closed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: When a PR is closed

on:
pull_request:
types:
- closed
branches:
- releases/*

jobs:
close-linked-issue-when-merged:
name: Close linked issue when merged
# Only run the job if the PR is merged.
if: github.event.pull_request.merged
permissions:
# Need write permissions to close issues.
issues: write
pull-requests: read
runs-on: ubuntu-24.04
env:
PR_INFO: ${{ runner.temp }}/pr-info.json
steps:
# Github REST Api does not provide access to linked issues for a PR.
# But we can access that data by using the graphql object `PullRequest.closingIssuesReferences`.
#
# Links:
#
# - [`PullRequest`](https://docs.github.com/en/graphql/reference/objects#pullrequest)
# - [`IssueConnection`](https://docs.github.com/en/graphql/reference/objects#issueconnection)
# - [`Issue`](https://docs.github.com/en/graphql/reference/objects#issue)
- name: Get information about linked issues
shell: bash -e -o pipefail {0}
run: |
gh api graphql \
-f id='${{ github.event.pull_request.node_id }}' \
-f query='
query($id: ID!) {
node(id: $id) {
... on PullRequest {
title
number
closingIssuesReferences(first: 100) {
nodes {
number
}
}
}
}
}
' \
| jq .data.node \
| tee "${{ env.PR_INFO }}"
env:
GH_TOKEN: ${{ github.token }}

- name: Close linked issues
run: |
PR_TITLE="$(jq .title "${{ env.PR_INFO }}")"
PR_NUMBER="$(jq .number "${{ env.PR_INFO }}")"
echo "Closing linked issue to PR-${PR_NUMBER} called '${PR_TITLE}'"
for issue in $(jq '.closingIssuesReferences.nodes[].number' "${{ env.PR_INFO }}"); do
echo "Closing issue #${issue}"
gh issue close "${issue}" --reason complete --comment "Fixed by #${PR_NUMBER}"
done
env:
GH_TOKEN: ${{ github.token }}


0 comments on commit 834beda

Please sign in to comment.