-
-
Notifications
You must be signed in to change notification settings - Fork 396
Add simple PR comment sub-action #636
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
6
commits into
main
Choose a base branch
from
pr-comment
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
6 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,5 @@ | ||
| --- | ||
| "@changesets/action": minor | ||
| --- | ||
|
|
||
| Add a new `@changesets/action/pr-comment` sub-action to comment on PRs |
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,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 | ||
| ``` |
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,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 | ||
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,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
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. 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!"); | ||
| } | ||
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.
There was a problem hiding this comment.
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-idwouldn't be required to get thatUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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-statusaction. 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?
There was a problem hiding this comment.
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/falseto opt out from the default behavior