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: update existing comment #51

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`. <br/> 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`. <br/> 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`.
45 changes: 39 additions & 6 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,49 @@ 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) {
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}`)
}
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
Loading