From 95d80ea1a244df737b7a1a9a374195e83105e2ab Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 28 May 2026 19:14:37 +0800 Subject: [PATCH 1/6] Add simple PR comment sub-action --- README.md | 4 +++ pr-comment/README.md | 27 +++++++++++++++++ pr-comment/action.yml | 22 ++++++++++++++ src/pr-comment/index.ts | 65 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 pr-comment/README.md create mode 100644 pr-comment/action.yml create mode 100644 src/pr-comment/index.ts diff --git a/README.md b/README.md index b465bcc0..c6faacca 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ This action for [Changesets](https://github.com/changesets/changesets) creates a pull request with all of the package versions updated and changelogs updated and when there are new changesets on [your configured `baseBranch`](https://github.com/changesets/changesets/blob/main/docs/config-file-options.md#basebranch-git-branch-name), the PR will be updated. When you're ready, you can merge the pull request and you can either publish the packages to npm manually or setup the action to do it for you. +There are also sub-actions hosted in this repository. Check out their respective READMEs for more details: + +- [pr-comment](./pr-comment/README.md): Comment on PRs. + ## Usage ### Inputs diff --git a/pr-comment/README.md b/pr-comment/README.md new file mode 100644 index 00000000..e7dac7c6 --- /dev/null +++ b/pr-comment/README.md @@ -0,0 +1,27 @@ +# @changesets/action/pr-comment + +A simple GitHub Action to comment on PRs aimed to complement [`@changesets/action/pr-status`](../pr-status/README.md). + +This action is intentionally simple without advanced features. Check out other actions if so, such as [mshick/add-pr-comment](https://github.com/marketplace/actions/add-pr-comment) and [peter-evans/create-or-update-comment](https://github.com/marketplace/actions/create-or-update-comment). + +## Example setup + +```yaml +name: PR Comment + +on: + pull_request: + +jobs: + comment: + runs-on: ubuntu-slim + permissions: + pull-requests: write # to create and update comments on PRs + steps: + - uses: changesets/action/pr-comment@v1 + with: + body: Hello world! + # Optional. If provided, the action will look for an existing comment with + # the same update id and update it instead of creating a new one. + update-id: changesets +``` diff --git a/pr-comment/action.yml b/pr-comment/action.yml new file mode 100644 index 00000000..6fd0a3fd --- /dev/null +++ b/pr-comment/action.yml @@ -0,0 +1,22 @@ +name: Changesets - PR Comment +description: A simple GitHub Action to comment on PRs +runs: + using: node24 + main: ../dist/pr-comment.js +inputs: + github-token: + description: "The GitHub token to use for authentication. Defaults to the GitHub-provided token." + required: false + default: ${{ github.token }} + body: + description: "The comment body to post on the PR." + required: true + update-id: + description: "If provided, the action will update the comment that contains this id, or create a comment with this id to be updated later." + required: false +outputs: + comment-id: + description: "The comment id of the comment that was created or updated." +branding: + icon: message-circle + color: blue diff --git a/src/pr-comment/index.ts b/src/pr-comment/index.ts new file mode 100644 index 00000000..8972275c --- /dev/null +++ b/src/pr-comment/index.ts @@ -0,0 +1,65 @@ +import * as core from "@actions/core"; +import * as github from "@actions/github"; + +type Octokit = ReturnType; +type CreateCommentParams = NonNullable< + Parameters[0] +>; +type UpdateCommentParams = NonNullable< + Parameters[0] +>; + +(async () => { + const context = github.context.payload.pull_request; + if (!context) { + core.error( + "This action should only be run on `pull_request_target` or `pull_request` events", + ); + return; + } + + const githubToken = core.getInput("github-token", { required: true }); + const body = core.getInput("body", { required: true }); + const updateId = core.getInput("update-id", { required: false }); + + const commentMarker = updateId ? `` : null; + const commentBody = commentMarker ? `${commentMarker}\n\n${body}` : body; + const commentParam: CreateCommentParams | UpdateCommentParams = { + repo: context.base.repo.name, + owner: context.base.repo.owner.login, + issue_number: context.number, + body: commentBody, + }; + + const octokit = github.getOctokit(githubToken); + + let existingCommentId: number | undefined; + if (commentMarker) { + core.info("Checking for existing comment..."); + 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, + }); + core.setOutput("comment-id", existingCommentId); + } else { + core.info("Creating new comment..."); + const result = await octokit.rest.issues.createComment(commentParam); + core.setOutput("comment-id", result.data.id); + } + + core.info("Done!"); +})(); From 1e25d9ffc5d468853017ae74c3fb7d47a7bd017a Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 28 May 2026 19:15:31 +0800 Subject: [PATCH 2/6] Update rolldown config --- rolldown.config.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rolldown.config.js b/rolldown.config.js index dd0db91c..3eb869b4 100644 --- a/rolldown.config.js +++ b/rolldown.config.js @@ -1,7 +1,10 @@ import { defineConfig } from "rolldown"; export default defineConfig({ - input: "src/index.ts", + input: { + index: "src/index.ts", + ["pr-comment"]: "src/pr-comment/index.ts", + }, output: { dir: "dist", format: "esm", From 18dcce71af634fc3f83c4fd994b4265325b4bce0 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 28 May 2026 19:16:29 +0800 Subject: [PATCH 3/6] Add changeset --- .changeset/curly-cameras-matter.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/curly-cameras-matter.md diff --git a/.changeset/curly-cameras-matter.md b/.changeset/curly-cameras-matter.md new file mode 100644 index 00000000..3a91556e --- /dev/null +++ b/.changeset/curly-cameras-matter.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": minor +--- + +Add a new `@changesets/action/pr-comment` sub-action to comment on PRs From 8b3627c41e88ce191698fe2243d77e1a9f4dd392 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 28 May 2026 19:22:08 +0800 Subject: [PATCH 4/6] Update readme --- pr-comment/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pr-comment/README.md b/pr-comment/README.md index e7dac7c6..32e0176d 100644 --- a/pr-comment/README.md +++ b/pr-comment/README.md @@ -4,6 +4,8 @@ A simple GitHub Action to comment on PRs aimed to complement [`@changesets/actio This action is intentionally simple without advanced features. Check out other actions if so, such as [mshick/add-pr-comment](https://github.com/marketplace/actions/add-pr-comment) and [peter-evans/create-or-update-comment](https://github.com/marketplace/actions/create-or-update-comment). +See the [action metadata](action.yml) for details on the inputs and outputs. + ## Example setup ```yaml From 7848e484f93fcafcb7da73d890a83eec107bf821 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 29 May 2026 15:14:42 +0800 Subject: [PATCH 5/6] Use TLA --- pr-comment/README.md | 6 +++--- src/pr-comment/index.ts | 13 +++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pr-comment/README.md b/pr-comment/README.md index 32e0176d..a98de5c8 100644 --- a/pr-comment/README.md +++ b/pr-comment/README.md @@ -15,7 +15,7 @@ on: pull_request: jobs: - comment: + pr-comment: runs-on: ubuntu-slim permissions: pull-requests: write # to create and update comments on PRs @@ -23,7 +23,7 @@ jobs: - uses: changesets/action/pr-comment@v1 with: body: Hello world! - # Optional. If provided, the action will look for an existing comment with - # the same update id and update it instead of creating a new one. + # Optional. If provided, the action will update the comment that contains this id, + # or create a comment with this id to be updated later. update-id: changesets ``` diff --git a/src/pr-comment/index.ts b/src/pr-comment/index.ts index 8972275c..ffaab174 100644 --- a/src/pr-comment/index.ts +++ b/src/pr-comment/index.ts @@ -9,13 +9,18 @@ type UpdateCommentParams = NonNullable< Parameters[0] >; -(async () => { +try { + await main(); +} catch (err) { + core.setFailed((err as Error).message); +} + +async function main() { const context = github.context.payload.pull_request; if (!context) { - core.error( + throw new Error( "This action should only be run on `pull_request_target` or `pull_request` events", ); - return; } const githubToken = core.getInput("github-token", { required: true }); @@ -62,4 +67,4 @@ type UpdateCommentParams = NonNullable< } core.info("Done!"); -})(); +} From 88b94b9c7b2235e0f8292c39dbc8df555b33a34f Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 29 May 2026 19:38:24 +0800 Subject: [PATCH 6/6] Add prefix --- src/pr-comment/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pr-comment/index.ts b/src/pr-comment/index.ts index ffaab174..584aba5a 100644 --- a/src/pr-comment/index.ts +++ b/src/pr-comment/index.ts @@ -27,7 +27,9 @@ async function main() { const body = core.getInput("body", { required: true }); const updateId = core.getInput("update-id", { required: false }); - const commentMarker = updateId ? `` : null; + const commentMarker = updateId + ? `` + : null; const commentBody = commentMarker ? `${commentMarker}\n\n${body}` : body; const commentParam: CreateCommentParams | UpdateCommentParams = { repo: context.base.repo.name,