Skip to content

Commit

Permalink
- ensure we update the existing comment instead of crating a new one …
Browse files Browse the repository at this point in the history
…each time
  • Loading branch information
mikepenz authored Aug 30, 2024
1 parent c436525 commit 5147ec6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 15 deletions.
38 changes: 32 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

41 changes: 34 additions & 7 deletions src/annotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,18 @@ export async function attachSummary(
}
}

export function attachComment(
token: string,
export function buildCommentIdentifier(checkName: string[]): string {
return `<!-- Summary comment for ${JSON.stringify(checkName)} by mikepenz/action-junit-report -->`
}

export async function attachComment(
octokit: InstanceType<typeof GitHub>,
checkName: string[],
table: SummaryTableRow[],
detailsTable: SummaryTableRow[],
flakySummary: SummaryTableRow[]
): void {
const octokit = github.getOctokit(token)
): Promise<void> {
const identifier = buildCommentIdentifier(checkName)

let comment = buildTable(table)
if (detailsTable.length > 0) {
Expand All @@ -235,11 +240,33 @@ export function attachComment(
comment += '\n\n'
comment += buildTable(flakySummary)
}
comment += `\n\n${identifier}`

octokit.rest.issues.createComment({
const priorComment = await findPriorComment(octokit, identifier)
if (priorComment) {
await octokit.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: priorComment,
body: comment
})
} else {
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
})
}
}

async function findPriorComment(octokit: InstanceType<typeof GitHub>, identifier: string): Promise<number | undefined> {
const comments = await octokit.paginate(octokit.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
issue_number: context.issue.number
})

const foundComment = comments.find(comment => comment.body?.endsWith(identifier))
return foundComment?.id
}
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as github from '@actions/github'
import {annotateTestResult, attachComment, attachSummary, buildSummaryTables} from './annotator'
import {parseTestReports, TestResult} from './testParser'
import {buildTable, readTransformers, retrieve} from './utils'
import {GitHub} from '@actions/github/lib/utils'

export async function run(): Promise<void> {
try {
Expand Down Expand Up @@ -152,7 +153,8 @@ export async function run(): Promise<void> {
}

if (comment) {
attachComment(token, table, detailTable, flakyTable)
const octokit: InstanceType<typeof GitHub> = github.getOctokit(token)
attachComment(octokit, checkName, table, detailTable, flakyTable)
}

core.setOutput('summary', buildTable(table))
Expand Down

0 comments on commit 5147ec6

Please sign in to comment.