From 99692eb90c07ba36902a067f9add98aba0859752 Mon Sep 17 00:00:00 2001 From: Bojan Rajh Date: Thu, 9 Nov 2023 13:28:07 +0100 Subject: [PATCH 1/4] feat: update existing comment --- README.md | 2 +- src/post.ts | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b5bf554..b70de15 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,6 @@ To use the action, add the following step before the steps you want to track. | `proc_trace_chart_show` | Optional | Enables showing traced processes in trace chart. Defaults to `true`. | `proc_trace_chart_max_count` | Optional | Maximum number of processes to be shown in trace chart (applicable if `proc_trace_chart_show` input is `true`). Must be a number. Defaults to `100`. | `proc_trace_table_show` | Optional | Enables showing traced processes in trace table. Defaults to `true`. -| `comment_on_pr` | Optional | Set to `true` to publish the results as comment to the PR (applicable if workflow run is triggered by PR). Defaults to `true`.
Requires `pull-requests: write` permission +| `comment_on_pr` | Optional | Set to `true` to publish the results as comment to the PR (applicable if workflow run is triggered by PR). Set to `update` to update existing telemetry comment. Defaults to `true`.
Requires `pull-requests: write` permission | `job_summary` | Optional | Set to `true` to publish the results as part of the [job summary page](https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/) of the workflow run. Defaults to `true`. | `theme` | Optional | Set to `dark` to generate charts compatible with Github **dark** mode. Defaults to `light`. diff --git a/src/post.ts b/src/post.ts index 04bab7c..8b35088 100644 --- a/src/post.ts +++ b/src/post.ts @@ -94,16 +94,39 @@ async function reportAll( } const commentOnPR: string = core.getInput('comment_on_pr') - if (pull_request && 'true' === commentOnPR) { + if (pull_request && ['true', 'update'].indexOf(commentOnPR) >= 0) { if (logger.isDebugEnabled()) { logger.debug(`Found Pull Request: ${JSON.stringify(pull_request)}`) } - await octokit.rest.issues.createComment({ - ...github.context.repo, - issue_number: Number(github.context.payload.pull_request?.number), - body: postContent - }) + const issueNumber = Number(github.context.payload.pull_request?.number); + let createComment: boolean = true + if ('update' === commentOnPR) { + const comments = await octokit.rest.issues.listComments({ + ...github.context.repo, + issue_number: issueNumber, + }) + const existingComment = comments.data.find(comment => comment.body.startsWith(title)) + if (existingComment) { + if (logger.isDebugEnabled()) { + logger.debug(`Found Comment: ${existingComment.id}`) + } + createComment = false + await octokit.rest.issues.updateComment({ + ...github.context.repo, + comment_id: existingComment.id, + body: postContent, + }) + } + } + + if (createComment) { + await octokit.rest.issues.createComment({ + ...github.context.repo, + issue_number: issueNumber, + body: postContent + }) + } } else { logger.debug(`Couldn't find Pull Request`) } From be91765f3811a2a9c895dc59048bedf65dba80b6 Mon Sep 17 00:00:00 2001 From: Bojan Rajh Date: Mon, 19 Feb 2024 09:53:31 +0100 Subject: [PATCH 2/4] feat: check multiple pages for comments --- src/post.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/post.ts b/src/post.ts index 8b35088..5999853 100644 --- a/src/post.ts +++ b/src/post.ts @@ -102,11 +102,17 @@ async function reportAll( const issueNumber = Number(github.context.payload.pull_request?.number); let createComment: boolean = true if ('update' === commentOnPR) { - const comments = await octokit.rest.issues.listComments({ - ...github.context.repo, - issue_number: issueNumber, - }) - const existingComment = comments.data.find(comment => comment.body.startsWith(title)) + let existingComment, comments, page = 1; + do { + comments = await octokit.rest.issues.listComments({ + ...github.context.repo, + issue_number: issueNumber, + page, + }) + existingComment = comments.data.find(comment => comment.body.startsWith(title)) + page++ + } while (!existingComment && comments.data.length > 0); + if (existingComment) { if (logger.isDebugEnabled()) { logger.debug(`Found Comment: ${existingComment.id}`) From 3a11fc819df4be1f4fc8ab69c74e2d826e0254b2 Mon Sep 17 00:00:00 2001 From: Bojan Rajh Date: Mon, 4 Mar 2024 09:19:48 +0100 Subject: [PATCH 3/4] fix: codestyle --- src/post.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/post.ts b/src/post.ts index 5999853..78e20d4 100644 --- a/src/post.ts +++ b/src/post.ts @@ -99,19 +99,23 @@ async function reportAll( logger.debug(`Found Pull Request: ${JSON.stringify(pull_request)}`) } - const issueNumber = Number(github.context.payload.pull_request?.number); + const issueNumber = Number(github.context.payload.pull_request?.number) let createComment: boolean = true if ('update' === commentOnPR) { - let existingComment, comments, page = 1; + let existingComment, + comments, + page = 1 do { comments = await octokit.rest.issues.listComments({ ...github.context.repo, issue_number: issueNumber, - page, + page }) - existingComment = comments.data.find(comment => comment.body.startsWith(title)) + existingComment = comments.data.find(comment => + comment.body.startsWith(title) + ) page++ - } while (!existingComment && comments.data.length > 0); + } while (!existingComment && comments.data.length > 0) if (existingComment) { if (logger.isDebugEnabled()) { @@ -121,7 +125,7 @@ async function reportAll( await octokit.rest.issues.updateComment({ ...github.context.repo, comment_id: existingComment.id, - body: postContent, + body: postContent }) } } From 4b4f729b0326fe422292686a20d6c630d05457d4 Mon Sep 17 00:00:00 2001 From: Bojan Rajh Date: Mon, 4 Mar 2024 17:04:40 +0100 Subject: [PATCH 4/4] fix: undefined --- src/post.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/post.ts b/src/post.ts index 78e20d4..9bb7ca5 100644 --- a/src/post.ts +++ b/src/post.ts @@ -112,7 +112,7 @@ async function reportAll( page }) existingComment = comments.data.find(comment => - comment.body.startsWith(title) + comment.body?.startsWith(title) ) page++ } while (!existingComment && comments.data.length > 0)