|
| 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 tag |
| 21 | + id: previous_tag |
| 22 | + run: | |
| 23 | + # Get all tags sorted by version |
| 24 | + CURRENT_TAG="${{ github.event.release.tag_name }}" |
| 25 | + PREVIOUS_TAG=$(git tag --sort=-version:refname | grep -A 1 "^${CURRENT_TAG}$" | tail -n 1) |
| 26 | + |
| 27 | + # If there's no previous tag, use the first commit |
| 28 | + if [ -z "$PREVIOUS_TAG" ] || [ "$PREVIOUS_TAG" = "$CURRENT_TAG" ]; then |
| 29 | + PREVIOUS_TAG=$(git rev-list --max-parents=0 HEAD) |
| 30 | + fi |
| 31 | + |
| 32 | + echo "previous_tag=${PREVIOUS_TAG}" >> $GITHUB_OUTPUT |
| 33 | + echo "Found previous tag/commit: ${PREVIOUS_TAG}" |
| 34 | +
|
| 35 | + - name: Get merged PRs between releases |
| 36 | + id: get_prs |
| 37 | + uses: actions/github-script@v7 |
| 38 | + with: |
| 39 | + script: | |
| 40 | + const currentTag = '${{ github.event.release.tag_name }}'; |
| 41 | + const previousTag = '${{ steps.previous_tag.outputs.previous_tag }}'; |
| 42 | + |
| 43 | + console.log(`Finding PRs between ${previousTag} and ${currentTag}`); |
| 44 | + |
| 45 | + // Get commits between previous and current release |
| 46 | + const comparison = await github.rest.repos.compareCommits({ |
| 47 | + owner: context.repo.owner, |
| 48 | + repo: context.repo.repo, |
| 49 | + base: previousTag, |
| 50 | + head: currentTag |
| 51 | + }); |
| 52 | + |
| 53 | + const commits = comparison.data.commits; |
| 54 | + console.log(`Found ${commits.length} commits`); |
| 55 | + |
| 56 | + // Extract PR numbers from commit messages |
| 57 | + const prNumbers = new Set(); |
| 58 | + const prPattern = /#(\d+)/g; |
| 59 | + |
| 60 | + for (const commit of commits) { |
| 61 | + const message = commit.commit.message; |
| 62 | + const matches = message.matchAll(prPattern); |
| 63 | + |
| 64 | + for (const match of matches) { |
| 65 | + const prNumber = parseInt(match[1]); |
| 66 | + prNumbers.add(prNumber); |
| 67 | + } |
| 68 | + |
| 69 | + // Also check if the commit is associated with a PR |
| 70 | + if (commit.commit.message.includes('Merge pull request #')) { |
| 71 | + const match = commit.commit.message.match(/Merge pull request #(\d+)/); |
| 72 | + if (match) { |
| 73 | + prNumbers.add(parseInt(match[1])); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Verify these are actually PRs (not issues) |
| 79 | + const validPRs = []; |
| 80 | + |
| 81 | + for (const prNumber of prNumbers) { |
| 82 | + try { |
| 83 | + const { data: pr } = await github.rest.pulls.get({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + pull_number: prNumber |
| 87 | + }); |
| 88 | + |
| 89 | + // Only include merged PRs |
| 90 | + if (pr.merged_at) { |
| 91 | + validPRs.push(prNumber); |
| 92 | + console.log(`Found valid merged PR: #${prNumber}`); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + console.log(`#${prNumber} is not a valid PR or was not found`); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + console.log(`Found ${validPRs.length} valid merged PRs`); |
| 100 | + return validPRs; |
| 101 | +
|
| 102 | + - name: Comment on PRs |
| 103 | + uses: actions/github-script@v7 |
| 104 | + with: |
| 105 | + script: | |
| 106 | + const prNumbers = ${{ steps.get_prs.outputs.result }}; |
| 107 | + const releaseTag = '${{ github.event.release.tag_name }}'; |
| 108 | + const releaseUrl = '${{ github.event.release.html_url }}'; |
| 109 | + |
| 110 | + const comment = `🎉 This pull request is included in [${releaseTag}](${releaseUrl})!`; |
| 111 | + |
| 112 | + for (const prNumber of prNumbers) { |
| 113 | + try { |
| 114 | + await github.rest.issues.createComment({ |
| 115 | + owner: context.repo.owner, |
| 116 | + repo: context.repo.repo, |
| 117 | + issue_number: prNumber, |
| 118 | + body: comment |
| 119 | + }); |
| 120 | + console.log(`Successfully commented on PR #${prNumber}`); |
| 121 | + } catch (error) { |
| 122 | + console.error(`Failed to comment on PR #${prNumber}:`, error.message); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + console.log(`Commented on ${prNumbers.length} PRs`); |
0 commit comments