|
| 1 | +name: Comment on PRs in Release |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + |
| 7 | +permissions: |
| 8 | + pull-requests: write |
| 9 | + contents: read |
| 10 | + |
| 11 | +jobs: |
| 12 | + comment-on-prs: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Get previous release |
| 21 | + id: previous_release |
| 22 | + uses: actions/github-script@v7 |
| 23 | + with: |
| 24 | + script: | |
| 25 | + const currentTag = '${{ github.event.release.tag_name }}'; |
| 26 | +
|
| 27 | + // Get all releases |
| 28 | + const { data: releases } = await github.rest.repos.listReleases({ |
| 29 | + owner: context.repo.owner, |
| 30 | + repo: context.repo.repo, |
| 31 | + per_page: 100 |
| 32 | + }); |
| 33 | +
|
| 34 | + // Find current release index |
| 35 | + const currentIndex = releases.findIndex(r => r.tag_name === currentTag); |
| 36 | +
|
| 37 | + if (currentIndex === -1) { |
| 38 | + console.log('Current release not found in list'); |
| 39 | + return null; |
| 40 | + } |
| 41 | +
|
| 42 | + // Get previous release (next in the list since they're sorted by date desc) |
| 43 | + const previousRelease = releases[currentIndex + 1]; |
| 44 | +
|
| 45 | + if (!previousRelease) { |
| 46 | + console.log('No previous release found, this might be the first release'); |
| 47 | + return null; |
| 48 | + } |
| 49 | +
|
| 50 | + console.log(`Found previous release: ${previousRelease.tag_name}`); |
| 51 | +
|
| 52 | + return previousRelease.tag_name; |
| 53 | +
|
| 54 | + - name: Get merged PRs between releases |
| 55 | + id: get_prs |
| 56 | + uses: actions/github-script@v7 |
| 57 | + with: |
| 58 | + script: | |
| 59 | + const currentTag = '${{ github.event.release.tag_name }}'; |
| 60 | + const previousTag = ${{ steps.previous_release.outputs.result }}; |
| 61 | +
|
| 62 | + if (!previousTag) { |
| 63 | + console.log('No previous release found, skipping'); |
| 64 | + return []; |
| 65 | + } |
| 66 | +
|
| 67 | + console.log(`Finding PRs between ${previousTag} and ${currentTag}`); |
| 68 | +
|
| 69 | + // Get commits between previous and current release |
| 70 | + const comparison = await github.rest.repos.compareCommits({ |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + base: previousTag, |
| 74 | + head: currentTag |
| 75 | + }); |
| 76 | +
|
| 77 | + const commits = comparison.data.commits; |
| 78 | + console.log(`Found ${commits.length} commits`); |
| 79 | +
|
| 80 | + // Get PRs associated with each commit using GitHub API |
| 81 | + const prNumbers = new Set(); |
| 82 | +
|
| 83 | + for (const commit of commits) { |
| 84 | + try { |
| 85 | + const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ |
| 86 | + owner: context.repo.owner, |
| 87 | + repo: context.repo.repo, |
| 88 | + commit_sha: commit.sha |
| 89 | + }); |
| 90 | +
|
| 91 | + for (const pr of prs) { |
| 92 | + if (pr.merged_at) { |
| 93 | + prNumbers.add(pr.number); |
| 94 | + console.log(`Found merged PR: #${pr.number}`); |
| 95 | + } |
| 96 | + } |
| 97 | + } catch (error) { |
| 98 | + console.log(`Failed to get PRs for commit ${commit.sha}: ${error.message}`); |
| 99 | + } |
| 100 | + } |
| 101 | +
|
| 102 | + console.log(`Found ${prNumbers.size} merged PRs`); |
| 103 | + return Array.from(prNumbers); |
| 104 | +
|
| 105 | + - name: Comment on PRs |
| 106 | + uses: actions/github-script@v7 |
| 107 | + with: |
| 108 | + script: | |
| 109 | + const prNumbers = ${{ steps.get_prs.outputs.result }}; |
| 110 | + const releaseTag = '${{ github.event.release.tag_name }}'; |
| 111 | + const releaseUrl = '${{ github.event.release.html_url }}'; |
| 112 | +
|
| 113 | + const comment = `This pull request is included in [${releaseTag}](${releaseUrl})`; |
| 114 | +
|
| 115 | + let commentedCount = 0; |
| 116 | +
|
| 117 | + for (const prNumber of prNumbers) { |
| 118 | + try { |
| 119 | + // Check if we've already commented on this PR for this release |
| 120 | + const { data: comments } = await github.rest.issues.listComments({ |
| 121 | + owner: context.repo.owner, |
| 122 | + repo: context.repo.repo, |
| 123 | + issue_number: prNumber, |
| 124 | + per_page: 100 |
| 125 | + }); |
| 126 | +
|
| 127 | + const alreadyCommented = comments.some(c => |
| 128 | + c.user.type === 'Bot' && c.body.includes(releaseTag) |
| 129 | + ); |
| 130 | +
|
| 131 | + if (alreadyCommented) { |
| 132 | + console.log(`Skipping PR #${prNumber} - already commented for ${releaseTag}`); |
| 133 | + continue; |
| 134 | + } |
| 135 | +
|
| 136 | + await github.rest.issues.createComment({ |
| 137 | + owner: context.repo.owner, |
| 138 | + repo: context.repo.repo, |
| 139 | + issue_number: prNumber, |
| 140 | + body: comment |
| 141 | + }); |
| 142 | + commentedCount++; |
| 143 | + console.log(`Successfully commented on PR #${prNumber}`); |
| 144 | + } catch (error) { |
| 145 | + console.error(`Failed to comment on PR #${prNumber}:`, error.message); |
| 146 | + } |
| 147 | + } |
| 148 | +
|
| 149 | + console.log(`Commented on ${commentedCount} of ${prNumbers.length} PRs`); |
0 commit comments