-
-
Notifications
You must be signed in to change notification settings - Fork 396
Comment changeset status in PRs #625
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
bluwy
wants to merge
10
commits into
main
Choose a base branch
from
comment-pr-changeset
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0f2b7f6
Initial implementation
bluwy 0c33f17
Fix
bluwy 513d3b1
fix
bluwy bdfd7f0
fix
bluwy 3f05264
Make it look nicer
bluwy b3bbf47
Update docs
bluwy b4b967a
Rename to pr-status-comment
bluwy 7457ddb
Remove outputs
bluwy 6057580
Update path
bluwy 8188bb1
update name
bluwy 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,47 @@ | ||
| # changesets/action/pr-status-comment | ||
|
|
||
| This action comments on PRs of its changeset status, e.g. whether it has changeset files and which packages will be released if the PR is merged. | ||
|
|
||
| The action requires the base ref (of the repo) and head ref (of the PR) to be checked out in order to infer the changed files and packages. It also requires the [`pull_request_target`](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request_target) event to be triggered in order to have permissions to comment on the PR and to work in PRs from forks. | ||
|
|
||
| See the [action metadata](action.yml) for details on the inputs and outputs. | ||
|
|
||
| > [!WARNING] | ||
| > **Do not run untrusted code** when using the `pull_request_target` event. The example below only checks out code and does not run any code from the PR. Read more about the `pull_request_target` event in the [GitHub documentation](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request_target). | ||
|
|
||
| ## Example setup | ||
|
|
||
| ```yaml | ||
| # .github/workflows/pr-status-comment.yml | ||
| name: Comment changeset status in PRs | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, synchronize, reopened] | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| pr-status-comment: | ||
| runs-on: ubuntu-slim | ||
| permissions: | ||
| contents: read # to check out files in the repo | ||
| pull-requests: write # to create and update comments on PRs | ||
| steps: | ||
| - name: Check out base ref | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Check out head ref | ||
| run: | | ||
| git fetch "$REPO" "$REF" | ||
| git switch -c pr "$SHA" | ||
| env: | ||
| REPO: ${{ github.event.pull_request.head.repo.clone_url }} | ||
| REF: ${{ github.event.pull_request.head.ref }} | ||
| SHA: ${{ github.event.pull_request.head.sha }} | ||
|
|
||
| - name: Comment changeset status | ||
| uses: changesets/action/pr-status-comment@v1 | ||
| ``` | ||
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,14 @@ | ||
| name: Changesets - PR Status Comment | ||
| description: Comment the changeset status in PRs | ||
| runs: | ||
| using: node24 | ||
| main: ../dist/pr-status-comment.js | ||
| inputs: | ||
| github-token: | ||
| description: The GitHub token to use for authentication. Defaults to the GitHub-provided token. | ||
| required: false | ||
| default: ${{ github.token }} | ||
| outputs: {} | ||
| branding: | ||
| icon: package | ||
| color: blue |
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 |
|---|---|---|
| @@ -1,13 +1,16 @@ | ||
| import { defineConfig } from 'rolldown' | ||
| import { defineConfig } from "rolldown"; | ||
|
|
||
| export default defineConfig({ | ||
| input: 'src/index.ts', | ||
| input: { | ||
| index: "src/index.ts", | ||
| ["pr-status-comment"]: "src/pr-status-comment/index.ts", | ||
| }, | ||
| output: { | ||
| dir: 'dist', | ||
| format: 'esm', | ||
| dir: "dist", | ||
| format: "esm", | ||
| cleanDir: true, | ||
| minify: true, | ||
| comments: false, | ||
| }, | ||
| platform: 'node', | ||
| }) | ||
| platform: "node", | ||
| }); |
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 @@ | ||
| export const commentMarker = "<!-- changesets-action-pr-status-comment -->"; |
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,61 @@ | ||
| import * as core from "@actions/core"; | ||
| import * as github from "@actions/github"; | ||
| import { setupOctokit } from "../octokit.ts"; | ||
| import { commentMarker } from "./constants.ts"; | ||
| import { getCommentMessage } from "./message.ts"; | ||
|
|
||
| type Octokit = ReturnType<typeof setupOctokit>; | ||
| type CreateCommentParams = NonNullable< | ||
| Parameters<Octokit["rest"]["issues"]["createComment"]>[0] | ||
| >; | ||
| type UpdateCommentParams = NonNullable< | ||
| Parameters<Octokit["rest"]["issues"]["updateComment"]>[0] | ||
| >; | ||
|
|
||
| (async () => { | ||
| const context = github.context.payload.pull_request; | ||
| if (!context) { | ||
| core.error("This action should only be run on pull_request_target events"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is scary 😬 |
||
| return; | ||
| } | ||
|
|
||
| core.info("Creating comment message..."); | ||
| const commentBody = await getCommentMessage(context); | ||
| const commentParam: CreateCommentParams | UpdateCommentParams = { | ||
| repo: context.base.repo.name, | ||
| owner: context.base.repo.owner.login, | ||
| issue_number: context.number, | ||
| body: commentBody, | ||
| }; | ||
|
|
||
| const githubToken = core.getInput("github-token", { required: true }); | ||
| const octokit = setupOctokit(githubToken); | ||
|
|
||
| core.info("Checking for existing comment..."); | ||
| const existingCommentId = await octokit.rest.issues | ||
| .listComments({ | ||
| repo: context.base.repo.name, | ||
| owner: context.base.repo.owner.login, | ||
| issue_number: context.number, | ||
| }) | ||
| .then((res) => { | ||
| const comment = res.data.find((c) => c.body?.includes(commentMarker)); | ||
| return comment?.id; | ||
| }); | ||
|
|
||
| if (existingCommentId) { | ||
| core.info(`Updating existing comment (id: ${existingCommentId})...`); | ||
| await octokit.rest.issues.updateComment({ | ||
| ...commentParam, | ||
| comment_id: existingCommentId, | ||
| }); | ||
| } else { | ||
| core.info("Creating new comment..."); | ||
| const result = await octokit.rest.issues.createComment(commentParam); | ||
| } | ||
|
|
||
| core.info("Done!"); | ||
| })().catch((err) => { | ||
| core.error(err); | ||
| core.setFailed(err.message); | ||
| }); | ||
|
bluwy marked this conversation as resolved.
|
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,124 @@ | ||
| import * as github from "@actions/github"; | ||
| import getReleasePlan from "@changesets/get-release-plan"; | ||
| import type { | ||
| ComprehensiveRelease, | ||
| ReleasePlan, | ||
| VersionType, | ||
| } from "@changesets/types"; | ||
| import { markdownTable } from "markdown-table"; | ||
| import { commentMarker } from "./constants.ts"; | ||
| import { | ||
| getNewChangesetTemplateContent, | ||
| getNewChangesetUrl, | ||
| } from "./template.ts"; | ||
|
|
||
| type PullRequestContext = NonNullable< | ||
| typeof github.context.payload.pull_request | ||
| >; | ||
|
|
||
| export async function getCommentMessage(context: PullRequestContext) { | ||
| const cwd = process.cwd(); | ||
| const releasePlan = await getReleasePlan(cwd, context.base.ref); | ||
|
|
||
| const templateContent = await getNewChangesetTemplateContent( | ||
| cwd, | ||
| context.base.ref, | ||
| context.title, | ||
| ); | ||
|
|
||
| const newChangesetUrl = getNewChangesetUrl( | ||
| context.head.repo.html_url, | ||
| context.head.ref, | ||
| templateContent, | ||
| ); | ||
|
|
||
| if (releasePlan.changesets.length > 0) { | ||
| return getApproveMessage(context.head.sha, newChangesetUrl, releasePlan); | ||
| } else { | ||
| return getAbsentMessage(context.head.sha, newChangesetUrl, releasePlan); | ||
| } | ||
| } | ||
|
|
||
| function getApproveMessage( | ||
| commitSha: string, | ||
| newChangesetUrl: string, | ||
| releasePlan: ReleasePlan, | ||
| ) { | ||
| return `\ | ||
| ${commentMarker} | ||
|
|
||
| ### 🦋 Changeset detected | ||
|
|
||
| Latest commit: ${commitSha} | ||
|
|
||
| **The changes in this PR will be included in the next version bump.** | ||
|
|
||
| ${getReleasePlanMessage(releasePlan)} | ||
|
|
||
| Not sure what this means? [Click here to learn what changesets are](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md). | ||
|
|
||
| [Click here if you're a maintainer who wants to add another changeset to this PR](${newChangesetUrl})`; | ||
| } | ||
|
|
||
| function getAbsentMessage( | ||
| commitSha: string, | ||
| newChangesetUrl: string, | ||
| releasePlan: ReleasePlan, | ||
| ) { | ||
| return `\ | ||
| ${commentMarker} | ||
|
|
||
| ### ⚠️ No Changeset found | ||
|
|
||
| Latest commit: ${commitSha} | ||
|
|
||
| Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.** | ||
|
|
||
| ${getReleasePlanMessage(releasePlan)} | ||
|
|
||
| [Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md). | ||
|
|
||
| [Click here if you're a maintainer who wants to add a changeset to this PR](${newChangesetUrl})`; | ||
| } | ||
|
|
||
| function getReleasePlanMessage(releasePlan: ReleasePlan) { | ||
| const publishableReleases = releasePlan.releases.filter( | ||
| (r) => r.type !== "none", | ||
| ) as (ComprehensiveRelease & { type: Exclude<VersionType, "none"> })[]; | ||
|
|
||
| const table = markdownTable([ | ||
| ["Name", "Type"], | ||
| ...publishableReleases.map((release) => { | ||
| return [ | ||
| release.name, | ||
| { | ||
| major: "Major", | ||
| minor: "Minor", | ||
| patch: "Patch", | ||
| }[release.type], | ||
| ]; | ||
| }), | ||
| ]); | ||
|
|
||
| let summary = "This PR includes "; | ||
| if (releasePlan.changesets.length === 0) { | ||
| summary += "no changesets"; | ||
| } else { | ||
| summary += `changesets to release ${publishableReleases.length} package`; | ||
| if (publishableReleases.length !== 1) { | ||
| summary += "s"; | ||
| } | ||
| } | ||
|
|
||
| return `\ | ||
| <details> | ||
| <summary>${summary}</summary> | ||
|
|
||
| ${ | ||
| publishableReleases.length > 0 | ||
| ? table | ||
| : "When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types" | ||
| } | ||
|
|
||
| </details>`; | ||
| } |
Oops, something went wrong.
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.