|
7 | 7 |
|
8 | 8 | jobs: |
9 | 9 | close-related-issues: |
10 | | - runs-on: ubuntu-latest |
11 | | - if: github.event.pull_request.merged == true && startsWith(github.ref, 'refs/heads/release/') |
| 10 | + runs-on: ubuntu-22.04 |
| 11 | + if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.base.ref, 'release/') |
12 | 12 | permissions: |
13 | 13 | issues: write |
| 14 | + contents: read |
14 | 15 | steps: |
15 | | - - name: Extract issue number |
16 | | - id: extract_issue_number |
| 16 | + - name: Extract and close all linked issues |
17 | 17 | run: | |
18 | | - issue_number=$(echo "${{ github.event.pull_request.body }}" | grep -oE '#[0-9]+' | head -n 1 | tr -d '#') |
19 | | - echo "ISSUE_NUMBER=$issue_number" >> $GITHUB_ENV |
| 18 | + # Debug information |
| 19 | + echo "=== Debug Information ===" |
| 20 | + echo "PR Number: ${{ github.event.pull_request.number }}" |
| 21 | + echo "Source Branch: ${{ github.event.pull_request.head.ref }}" |
| 22 | + echo "Target Branch: ${{ github.event.pull_request.base.ref }}" |
| 23 | + echo "Repository: ${{ github.repository }}" |
| 24 | + echo "Repository Owner: ${{ github.repository_owner }}" |
| 25 | + echo "Repository Name: ${{ github.event.repository.name }}" |
| 26 | + echo "=========================" |
| 27 | + |
| 28 | + # Get linked issues using GitHub's GraphQL API |
| 29 | + # This matches exactly what GitHub shows in "Successfully merging this pull request may close these issues" |
| 30 | + |
| 31 | + echo "Fetching GitHub-linked issues..." |
| 32 | + |
| 33 | + # Check if jq is installed |
| 34 | + if ! command -v jq >/dev/null 2>&1; then |
| 35 | + echo "Error: jq is not installed. Please install jq to run this script." |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + |
| 39 | + # Use GraphQL to get the exact same data GitHub shows in the UI |
| 40 | + graphql_response=$(curl -s -X POST \ |
| 41 | + -H "Accept: application/vnd.github.v4+json" \ |
| 42 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 43 | + -d '{ |
| 44 | + "query": "query($owner: String!, $repo: String!, $number: Int!) { |
| 45 | + repository(owner: $owner, name: $repo) { |
| 46 | + pullRequest(number: $number) { |
| 47 | + closingIssuesReferences(first: 100) { |
| 48 | + nodes { |
| 49 | + number |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + }", |
| 55 | + "variables": { |
| 56 | + "owner": "${{ github.repository_owner }}", |
| 57 | + "repo": "${{ github.event.repository.name }}", |
| 58 | + "number": ${{ github.event.pull_request.number }} |
| 59 | + } |
| 60 | + }' \ |
| 61 | + https://api.github.com/graphql) |
| 62 | + |
| 63 | + # Debug: Show the raw GraphQL response |
| 64 | + echo "GraphQL Response: $graphql_response" |
| 65 | + |
| 66 | + # Check for GraphQL errors or malformed response |
| 67 | + if ! echo "$graphql_response" | jq . >/dev/null 2>&1; then |
| 68 | + echo "Error: Malformed JSON response from GitHub GraphQL API" |
| 69 | + echo "$graphql_response" |
| 70 | + exit 1 |
| 71 | + fi |
20 | 72 |
|
21 | | - - name: Close linked issues |
22 | | - uses: peter-evans/close-issue@v3 |
23 | | - with: |
24 | | - issue-number: ${{ env.ISSUE_NUMBER }} |
25 | | - comment: "This issue is being closed because the related PR has been merged into a release branch." |
| 73 | + if [ "$(echo "$graphql_response" | jq '.errors')" != "null" ]; then |
| 74 | + echo "Error(s) returned from GitHub GraphQL API:" |
| 75 | + echo "$graphql_response" | jq '.errors' |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + |
| 79 | + # Extract issue numbers from GraphQL response, handling null closingIssuesReferences |
| 80 | + issue_numbers=$(echo "$graphql_response" | jq -r ' |
| 81 | + if .data.repository.pullRequest.closingIssuesReferences then |
| 82 | + .data.repository.pullRequest.closingIssuesReferences.nodes[].number |
| 83 | + else |
| 84 | + empty |
| 85 | + end' | sort -u) |
| 86 | + |
| 87 | + if [ -z "$issue_numbers" ]; then |
| 88 | + echo "No linked issues found via GraphQL. Trying fallback method..." |
| 89 | + |
| 90 | + # Fallback: Check PR description for closing keywords |
| 91 | + pr_body="${{ github.event.pull_request.body }}" |
| 92 | + echo "PR Body: $pr_body" |
| 93 | + |
| 94 | + # Extract issue numbers from PR body using common closing keywords and multiple formats |
| 95 | + fallback_issues=$(echo "$pr_body" | grep -ioE '(close[sd]?|fix(e[sd])?|resolve[sd]?)[: ]+((#[0-9]+([, ]+)?)+)' | grep -oE '#[0-9]+' | sed 's/#//' | sort -u) |
| 96 | + |
| 97 | + if [ -n "$fallback_issues" ]; then |
| 98 | + echo "Found issues via fallback method: $fallback_issues" |
| 99 | + issue_numbers="$fallback_issues" |
| 100 | + else |
| 101 | + echo "No linked issues found that would be closed by GitHub's automatic linking or PR body keywords" |
| 102 | + exit 0 |
| 103 | + fi |
| 104 | + fi |
| 105 | + |
| 106 | + echo "Found GitHub-linked issues: $issue_numbers" |
| 107 | + |
| 108 | + # Close each issue individually |
| 109 | + for issue_number in $issue_numbers; do |
| 110 | + if [ -n "$issue_number" ]; then |
| 111 | + echo "Closing issue #$issue_number" |
| 112 | + |
| 113 | + # Close the issue using robust HTTP response handling |
| 114 | + close_tmpfile=$(mktemp) |
| 115 | + close_headers=$(mktemp) |
| 116 | + curl -s -D "$close_headers" -o "$close_tmpfile" -X PATCH \ |
| 117 | + -H "Accept: application/vnd.github.v3+json" \ |
| 118 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 119 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 120 | + "https://api.github.com/repos/${{ github.repository }}/issues/$issue_number" \ |
| 121 | + -d '{"state":"closed"}' |
| 122 | +
|
| 123 | + close_http_code=$(awk 'NR==1 {print $2}' "$close_headers") |
| 124 | +
|
| 125 | + if [ "$close_http_code" = "200" ]; then |
| 126 | + echo "✅ Successfully closed issue #$issue_number" |
| 127 | + |
| 128 | + # Add a comment to explain why the issue was closed using robust HTTP response handling |
| 129 | + comment_tmpfile=$(mktemp) |
| 130 | + comment_headers=$(mktemp) |
| 131 | + curl -s -D "$comment_headers" -o "$comment_tmpfile" -X POST \ |
| 132 | + -H "Accept: application/vnd.github.v3+json" \ |
| 133 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 134 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 135 | + "https://api.github.com/repos/${{ github.repository }}/issues/$issue_number/comments" \ |
| 136 | + -d "{\"body\":\"This issue is being closed because the related PR #${{ github.event.pull_request.number }} has been merged into a release branch (\`${{ github.event.pull_request.base.ref }}\`).\"}" |
| 137 | +
|
| 138 | + comment_http_code=$(awk 'NR==1 {print $2}' "$comment_headers") |
| 139 | +
|
| 140 | + if [ "$comment_http_code" = "201" ]; then |
| 141 | + echo "✅ Added comment to issue #$issue_number" |
| 142 | + else |
| 143 | + echo "⚠️ Failed to add comment to issue #$issue_number (HTTP $comment_http_code)" |
| 144 | + echo "Comment response: $(cat "$comment_tmpfile")" |
| 145 | + fi |
| 146 | + |
| 147 | + # Clean up comment temp files |
| 148 | + rm -f "$comment_tmpfile" "$comment_headers" |
| 149 | + else |
| 150 | + echo "❌ Failed to close issue #$issue_number (HTTP $close_http_code)" |
| 151 | + echo "Close response: $(cat "$close_tmpfile")" |
| 152 | + fi |
| 153 | + |
| 154 | + # Clean up close temp files |
| 155 | + rm -f "$close_tmpfile" "$close_headers" |
| 156 | + fi |
| 157 | + done |
0 commit comments