Skip to content
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

feat: comment tag #160

Merged
merged 2 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ jobs:
message: |
Current branch is `${{ github.head_ref }}`.
_(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_
comment_includes: Current branch
comment_tag: nrt
reactions: eyes, rocket
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: actions/checkout@v3

- name: Comment PR
uses: thollander/actions-comment-pull-request@v1
uses: thollander/actions-comment-pull-request@v2
with:
message: |
Hello world ! :wave:
Expand All @@ -35,7 +35,7 @@ It takes only valid reactions and adds it to the comment you've just created. (S

```yml
- name: PR comment with reactions
uses: thollander/actions-comment-pull-request@v1
uses: thollander/actions-comment-pull-request@v2
with:
message: |
Hello world ! :wave:
Expand All @@ -50,7 +50,7 @@ That is particularly useful for manual workflow for instance (`workflow_run`).
```yml
...
- name: Comment PR
uses: thollander/actions-comment-pull-request@v1
uses: thollander/actions-comment-pull-request@v2
with:
message: |
Hello world ! :wave:
Expand All @@ -60,25 +60,21 @@ That is particularly useful for manual workflow for instance (`workflow_run`).

### Upsert a comment

Editing an existing comment is also possible thanks to the `comment_includes` input.
Editing an existing comment is also possible thanks to the `comment_tag` input.

It will search through all the comments of the PR and get the first one that has the provided text in it.
If the comment body is not found, it will create a new comment.
Thanks to this parameter, it will be possible to identify your comment and then to upsert on it.
If the comment is not found at first, it will create a new comment.

_That is particularly interesting while committing multiple times in a PR and that you just want to have the last execution report printed. It avoids flooding the PR._

```yml
...
- name: Comment PR
uses: thollander/actions-comment-pull-request@v1
with:
message: 'Loading ...'
...
- name: Edit PR comment
uses: thollander/actions-comment-pull-request@v1
- name: Comment PR with execution number
uses: thollander/actions-comment-pull-request@v2
with:
message: 'Content loaded ! (edited)'
comment_includes: 'Loading'
message: |
_(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_
comment_tag: execution
```

## Inputs
Expand All @@ -91,7 +87,7 @@ _That is particularly interesting while committing multiple times in a PR and th
| `message` | The comment body | ✅ | |
| `reactions` | List of reactions for the comment (comma separated). See https://docs.github.com/en/rest/reactions#reaction-types | | |
| `pr_number` | The number of the pull request where to create the comment | | current pull-request/issue number (deduced from context) |
| `comment_includes` | The text that should be used to find comment in case of replacement. | | |
| `comment_tag` | A tag on your comment that will be used to identify a comment in case of replacement | | |

## Contributing

Expand All @@ -103,4 +99,3 @@ It is handled by `vercel/ncc` compiler.
```sh
$ npm run build
```

4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ inputs:
description: 'You can set some reactions on your comments through the `reactions` input.'
pr_number:
description: 'Manual pull request number'
comment_includes:
description: 'The text that should be used to find comment in case of replacement.'
comment_tag:
description: 'A tag on your comment that will be used to identify a comment in case of replacement.'
runs:
using: 'node16'
main: 'lib/index.js'
12 changes: 7 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9545,7 +9545,7 @@ async function run() {
const message = core.getInput('message');
const github_token = core.getInput('GITHUB_TOKEN');
const pr_number = core.getInput('pr_number');
const comment_includes = core.getInput('comment_includes');
const comment_tag = core.getInput('comment_tag');
const reactions = core.getInput('reactions');
const context = github.context;
const issue_number = parseInt(pr_number) || context.payload.pull_request?.number || context.payload.issue?.number;
Expand All @@ -9567,21 +9567,23 @@ async function run() {
});
}));
}
if (comment_includes) {
const comment_tag_pattern = comment_tag ? `<!-- thollander/actions-comment-pull-request "${comment_tag}" -->` : null;
const body = comment_tag_pattern ? `${message}\n${comment_tag_pattern}` : message;
if (comment_tag_pattern) {
let comment;
for await (const { data: comments } of octokit.paginate.iterator(octokit.rest.issues.listComments, {
...context.repo,
issue_number,
})) {
comment = comments.find((comment) => comment?.body?.includes(comment_includes));
comment = comments.find((comment) => comment?.body?.includes(comment_tag_pattern));
if (comment)
break;
}
if (comment) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: comment.id,
body: message,
body,
});
await addReactions(comment.id, reactions);
return;
Expand All @@ -9593,7 +9595,7 @@ async function run() {
const { data: comment } = await octokit.rest.issues.createComment({
...context.repo,
issue_number,
body: message,
body,
});
await addReactions(comment.id, reactions);
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "actions-comment-pull-request",
"version": "1.6.0",
"version": "2.0.0",
"description": "GitHub action for commenting pull-request",
"main": "lib/main.js",
"scripts": {
Expand Down
13 changes: 8 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function run() {
const message: string = core.getInput('message');
const github_token: string = core.getInput('GITHUB_TOKEN');
const pr_number: string = core.getInput('pr_number');
const comment_includes: string = core.getInput('comment_includes');
const comment_tag: string = core.getInput('comment_tag');
const reactions: string = core.getInput('reactions');

const context = github.context;
Expand Down Expand Up @@ -41,7 +41,10 @@ async function run() {
);
}

if (comment_includes) {
const comment_tag_pattern = comment_tag ? `<!-- thollander/actions-comment-pull-request "${comment_tag}" -->` : null;
const body = comment_tag_pattern ? `${message}\n${comment_tag_pattern}` : message;

if (comment_tag_pattern) {
type ListCommentsResponseDataType = GetResponseDataTypeFromEndpointMethod<
typeof octokit.rest.issues.listComments
>;
Expand All @@ -50,15 +53,15 @@ async function run() {
...context.repo,
issue_number,
})) {
comment = comments.find((comment) => comment?.body?.includes(comment_includes));
comment = comments.find((comment) => comment?.body?.includes(comment_tag_pattern));
if (comment) break;
}

if (comment) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: comment.id,
body: message,
body,
});
await addReactions(comment.id, reactions);
return;
Expand All @@ -70,7 +73,7 @@ async function run() {
const { data: comment } = await octokit.rest.issues.createComment({
...context.repo,
issue_number,
body: message,
body,
});

await addReactions(comment.id, reactions);
Expand Down