Skip to content

Commit

Permalink
feat: update existing comment
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanrajh committed Nov 9, 2023
1 parent 6705383 commit 7655e4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
35 changes: 29 additions & 6 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
}
Expand Down

0 comments on commit 7655e4e

Please sign in to comment.