-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add workflow to comment on PRs when released #1772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yugannkt
wants to merge
4
commits into
modelcontextprotocol:main
Choose a base branch
from
yugannkt:feat/pr-release-notification-workflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| name: Comment on PRs in Release | ||
|
|
||
| on: | ||
| release: | ||
| types: [published] | ||
|
|
||
| permissions: | ||
| pull-requests: write | ||
| contents: read | ||
|
|
||
| jobs: | ||
| comment-on-prs: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Get previous release | ||
| id: previous_release | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const currentTag = '${{ github.event.release.tag_name }}'; | ||
|
|
||
| // Get all releases | ||
| const { data: releases } = await github.rest.repos.listReleases({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| per_page: 100 | ||
| }); | ||
|
|
||
| // Find current release index | ||
| const currentIndex = releases.findIndex(r => r.tag_name === currentTag); | ||
|
|
||
| if (currentIndex === -1) { | ||
| console.log('Current release not found in list'); | ||
| return null; | ||
| } | ||
|
|
||
| // Get previous release (next in the list since they're sorted by date desc) | ||
| const previousRelease = releases[currentIndex + 1]; | ||
|
|
||
| if (!previousRelease) { | ||
| console.log('No previous release found, this might be the first release'); | ||
| return null; | ||
| } | ||
|
|
||
| console.log(`Found previous release: ${previousRelease.tag_name}`); | ||
|
|
||
| return previousRelease.tag_name; | ||
|
|
||
| - name: Get merged PRs between releases | ||
| id: get_prs | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const currentTag = '${{ github.event.release.tag_name }}'; | ||
| const previousTag = ${{ steps.previous_release.outputs.result }}; | ||
|
|
||
| if (!previousTag) { | ||
| console.log('No previous release found, skipping'); | ||
| return []; | ||
| } | ||
|
|
||
| console.log(`Finding PRs between ${previousTag} and ${currentTag}`); | ||
|
|
||
| // Get commits between previous and current release | ||
| const comparison = await github.rest.repos.compareCommits({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| base: previousTag, | ||
| head: currentTag | ||
| }); | ||
|
|
||
| const commits = comparison.data.commits; | ||
| console.log(`Found ${commits.length} commits`); | ||
|
|
||
| // Get PRs associated with each commit using GitHub API | ||
| const prNumbers = new Set(); | ||
|
|
||
| for (const commit of commits) { | ||
| try { | ||
| const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| commit_sha: commit.sha | ||
| }); | ||
|
|
||
| for (const pr of prs) { | ||
| if (pr.merged_at) { | ||
| prNumbers.add(pr.number); | ||
| console.log(`Found merged PR: #${pr.number}`); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.log(`Failed to get PRs for commit ${commit.sha}: ${error.message}`); | ||
| } | ||
| } | ||
|
|
||
| console.log(`Found ${prNumbers.size} merged PRs`); | ||
| return Array.from(prNumbers); | ||
|
|
||
| - name: Comment on PRs | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const prNumbers = ${{ steps.get_prs.outputs.result }}; | ||
| const releaseTag = '${{ github.event.release.tag_name }}'; | ||
| const releaseUrl = '${{ github.event.release.html_url }}'; | ||
|
|
||
| const comment = `This pull request is included in [${releaseTag}](${releaseUrl})`; | ||
|
|
||
| let commentedCount = 0; | ||
|
|
||
| for (const prNumber of prNumbers) { | ||
| try { | ||
| // Check if we've already commented on this PR for this release | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| per_page: 100 | ||
| }); | ||
|
|
||
| const alreadyCommented = comments.some(c => | ||
| c.user.type === 'Bot' && c.body.includes(releaseTag) | ||
| ); | ||
|
|
||
| if (alreadyCommented) { | ||
| console.log(`Skipping PR #${prNumber} - already commented for ${releaseTag}`); | ||
| continue; | ||
| } | ||
|
|
||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| body: comment | ||
| }); | ||
| commentedCount++; | ||
| console.log(`Successfully commented on PR #${prNumber}`); | ||
| } catch (error) { | ||
| console.error(`Failed to comment on PR #${prNumber}:`, error.message); | ||
| } | ||
| } | ||
|
|
||
| console.log(`Commented on ${commentedCount} of ${prNumbers.length} PRs`); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.