diff --git a/README.md b/README.md index 27ae132..6cb1d18 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`. +| `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`. | `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 654975a..ecd2432 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`) }