Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-cameras-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": minor
---

Add a new `@changesets/action/pr-comment` sub-action to comment on PRs
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions pr-comment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# @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).

See the [action metadata](action.yml) for details on the inputs and outputs.

## Example setup

```yaml
name: PR Comment

on:
pull_request:

jobs:
pr-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 update the comment that contains this id,
# or create a comment with this id to be updated later.
update-id: changesets
```
22 changes: 22 additions & 0 deletions pr-comment/action.yml
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we maybe use a default value for this one? It feels like almost all users will like the auto-update mode to be enabled for them so it would be easier if update-id wouldn't be required to get that

Copy link
Copy Markdown
Member Author

@bluwy bluwy May 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that this could be used for other things in the future, like #1, so I didn't want the action to be built specifically for the pr-status action. Or if for some reason they use this action for other workflows that comment on PRs.

Maybe it's not worth planning for that now and we can break later if we want? What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better for us to make this simpler for people. We should support update-id: null/false to opt out from the default behavior

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
5 changes: 4 additions & 1 deletion rolldown.config.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
72 changes: 72 additions & 0 deletions src/pr-comment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as core from "@actions/core";
import * as github from "@actions/github";

type Octokit = ReturnType<typeof github.getOctokit>;
type CreateCommentParams = NonNullable<
Parameters<Octokit["rest"]["issues"]["createComment"]>[0]
>;
type UpdateCommentParams = NonNullable<
Parameters<Octokit["rest"]["issues"]["updateComment"]>[0]
>;
Comment on lines +5 to +10
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

octokit package layout... requires acquired taste 🫠


try {
await main();
} catch (err) {
core.setFailed((err as Error).message);
}

async function main() {
const context = github.context.payload.pull_request;
if (!context) {
throw new Error(
"This action should only be run on `pull_request_target` or `pull_request` events",
);
}

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
? `<!-- changesets-action-pr-comment:${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!");
}