From c22fb302208b7b170d252a61a505d2ea27245eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A9rence=20Hollander?= Date: Sun, 4 Dec 2022 19:19:54 +0100 Subject: [PATCH] feat: comment tag (#160) Upserting is now based on a hidden comment generated thanks to comment_tag input. BREAKING CHANGE: `comment_includes` gets replaced with `comment_tag` mechansim Closes #137 --- .github/workflows/ci.yaml | 2 +- README.md | 29 ++++++++++++----------------- action.yml | 4 ++-- lib/index.js | 12 +++++++----- package-lock.json | 4 ++-- package.json | 2 +- src/main.ts | 13 ++++++++----- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7689e699..0ebab36a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index f29e5f02..747f136a 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: @@ -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: @@ -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 @@ -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 @@ -103,4 +99,3 @@ It is handled by `vercel/ncc` compiler. ```sh $ npm run build ``` - diff --git a/action.yml b/action.yml index f9c6686c..06093bc6 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/lib/index.js b/lib/index.js index 6bb716ce..40b41ff8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; @@ -9567,13 +9567,15 @@ async function run() { }); })); } - if (comment_includes) { + const comment_tag_pattern = 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; } @@ -9581,7 +9583,7 @@ async function run() { await octokit.rest.issues.updateComment({ ...context.repo, comment_id: comment.id, - body: message, + body, }); await addReactions(comment.id, reactions); return; @@ -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); } diff --git a/package-lock.json b/package-lock.json index 75341b2e..ef92844b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "actions-comment-pull-request", - "version": "1.6.0", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "actions-comment-pull-request", - "version": "1.6.0", + "version": "2.0.0", "license": "MIT", "dependencies": { "@actions/core": "^1.9.0", diff --git a/package.json b/package.json index 0208d86c..e907712c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/main.ts b/src/main.ts index e19f00fb..6b8320a4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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; @@ -41,7 +41,10 @@ async function run() { ); } - if (comment_includes) { + const comment_tag_pattern = 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 >; @@ -50,7 +53,7 @@ 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; } @@ -58,7 +61,7 @@ async function run() { await octokit.rest.issues.updateComment({ ...context.repo, comment_id: comment.id, - body: message, + body, }); await addReactions(comment.id, reactions); return; @@ -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);