From 0313aea7eb21d894ad433512ec0a25e3301ddb12 Mon Sep 17 00:00:00 2001 From: Joshua Sangmeister Date: Wed, 6 Mar 2024 12:58:35 +0100 Subject: [PATCH] Automatically close feature branch issues --- .github/workflows/close-issues.yml | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .github/workflows/close-issues.yml diff --git a/.github/workflows/close-issues.yml b/.github/workflows/close-issues.yml new file mode 100644 index 0000000..d6ea45b --- /dev/null +++ b/.github/workflows/close-issues.yml @@ -0,0 +1,52 @@ +on: + pull_request: + types: + - closed + branches: + - "feature/*" + +jobs: + close-issue: + runs-on: ubuntu-latest + if: github.event.pull_request.merged + steps: + - name: Generate access token + uses: tibdex/github-app-token@v2 + id: generate-token + with: + app_id: ${{ secrets.AUTOMATION_APP_ID }} + private_key: ${{ secrets.AUTOMATION_APP_PRIVATE_KEY }} + + - uses: octokit/graphql-action@v2.x + id: get-issues + env: + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + with: + query: | + query getLinkedIssues($owner: String!, $name: String!, $number: Int!) { + repository(owner: $owner, name: $name) { + pullRequest(number: $number) { + closingIssuesReferences(first: 100) { + nodes { + number + repository { + nameWithOwner + } + } + } + } + } + } + variables: | + owner: ${{ github.event.repository.owner.name }} + repo: ${{ github.event.repository.name }} + number: ${{ github.event.pull_request.number }} + + - name: Close issues + env: + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + run: | + issue_data="$(echo "${{ steps.get-issues.outputs.data }}" | jq -r '.data.repository.pullRequest.closingIssuesReferences.nodes[] | [.number,.repository.nameWithOwner] | @tsv')" + echo "$issue_data" | while read number nameWithOwner; do + gh issue close "$number" -r "$nameWithOwner" + done