Skip to content

Commit

Permalink
Merge pull request #1167 from mikepenz/feature/1166
Browse files Browse the repository at this point in the history
Introduce flag to comment summary on PR
  • Loading branch information
mikepenz authored Aug 30, 2024
2 parents 5f36538 + 0c0ff35 commit bf92eb2
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
report_paths: test_results/python/report_flaky.xml
include_passed: true
detailed_summary: true
comment: true
annotate_only: ${{ github.event_name == 'workflow_dispatch' }}

- name: Test JUnit flaky test import
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ jobs:
| `job_summary` | Optional. Enables the publishing of the job summary for the results. Defaults to `true`. May be required to disable [Enterprise Server](https://github.com/mikepenz/action-junit-report/issues/637) |
| `detailed_summary` | Optional. Include table with all test results in the summary. Defaults to `false`. |
| `flaky_summary` | Optional. Include table with all falky results in the summary. Defaults to `false`. |
| `comment` | Optional. Enables a comment being added to the PR with the summary tables (Respects the summary configuration flags). Defaults to `false`. |
| `updateComment` | Optional. If a prior action run comment exists, it is updated. If disabled, new comments are creted for each run. Defaults to `true`. |
| `annotate_notice` | Optional. Annotate passed test results along with warning/failed ones. Defaults to `false`. (Changed in v3.5.0) |
| `follow_symlink` | Optional. Enables to follow symlinks when searching test files via the globber. Defaults to `false`. |
| `job_name` | Optional. Specify the name of a check to update |
Expand Down Expand Up @@ -164,6 +166,7 @@ configuration) please enable `write` permission via:
```yml
permissions:
checks: write
pull-requests: write # only required if `comment: true` was enabled
```
Additionally for [security reasons], the github token used for `pull_request` workflows is [marked as read-only].
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ inputs:
description: 'Include table with all flaky results in summary'
required: false
default: 'false'
comment:
description: 'Enables a comment being added to the PR with the summary tables (summary has to be enabled). Default: false'
required: false
default: 'false'
updateComment:
description: 'Enables updating the prior comment if one already exists. Default: true'
required: false
default: 'true'
annotate_notice:
description: 'Annotate passed tests along with warning and failed ones'
required: false
Expand Down
52 changes: 52 additions & 0 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.

1 change: 1 addition & 0 deletions package-lock.json

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

57 changes: 56 additions & 1 deletion src/annotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as core from '@actions/core'
import {Annotation, TestResult} from './testParser'
import * as github from '@actions/github'
import {SummaryTableRow} from '@actions/core/lib/summary'
import {GitHub} from '@actions/github/lib/utils'
import {context, GitHub} from '@actions/github/lib/utils'
import {buildTable} from './utils'

export async function annotateTestResult(
testResult: TestResult,
Expand Down Expand Up @@ -216,3 +217,57 @@ export async function attachSummary(
await core.summary.addTable(flakySummary).write()
}
}

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[],
updateComment: boolean,
table: SummaryTableRow[],
detailsTable: SummaryTableRow[],
flakySummary: SummaryTableRow[]
): Promise<void> {
const identifier = buildCommentIdentifier(checkName)

let comment = buildTable(table)
if (detailsTable.length > 0) {
comment += '\n\n'
comment += buildTable(detailsTable)
}
if (flakySummary.length > 1) {
comment += '\n\n'
comment += buildTable(flakySummary)
}
comment += `\n\n${identifier}`

const priorComment = updateComment ? await findPriorComment(octokit, identifier) : undefined
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
})

const foundComment = comments.find(comment => comment.body?.endsWith(identifier))
return foundComment?.id
}
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {annotateTestResult, attachSummary, buildSummaryTables} from './annotator'
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 All @@ -27,6 +28,8 @@ export async function run(): Promise<void> {
const jobSummary = core.getInput('job_summary') === 'true'
const detailedSummary = core.getInput('detailed_summary') === 'true'
const flakySummary = core.getInput('flaky_summary') === 'true'
const comment = core.getInput('comment') === 'true'
const updateComment = core.getInput('updateComment') === 'true'
const jobName = core.getInput('job_name')

const reportPaths = core.getMultilineInput('report_paths')
Expand Down Expand Up @@ -150,6 +153,11 @@ export async function run(): Promise<void> {
core.info('⏩ Skipped creation of job summary')
}

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

core.setOutput('summary', buildTable(table))
core.setOutput('detailed_summary', buildTable(detailTable))
core.setOutput('flaky_summary', buildTable(flakyTable))
Expand Down

0 comments on commit bf92eb2

Please sign in to comment.