Skip to content

Commit

Permalink
Merge pull request #1024 from mikepenz/feature/1020_2
Browse files Browse the repository at this point in the history
Introduce setting to disable annotations for checks
  • Loading branch information
mikepenz authored Jan 29, 2024
2 parents b1b7f65 + 21fada7 commit 2d2e937
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ jobs:
| `check_retries` | Optional. If a testcase is retried, ignore the original failure. |
| `check_title_template` | Optional. Template to configure the title format. Placeholders: {{FILE_NAME}}, {{SUITE_NAME}}, {{TEST_NAME}}. |
| `summary` | Optional. Additional text to summary output |
| `check_annotations` | Optional. Defines if the checks will include annotations. If disabled skips all annotations for the check. (This does not affect `annotate_only`, which uses no checks). |
| `update_check` | Optional. Uses an alternative API to update checks, use for cases with more than 50 annotations. Default: `false`. |
| `annotate_only` | Optional. Will only annotate the results on the files, won't create a check run. Defaults to `false`. |
| `transformers` | Optional. Array of `Transformer`s offering the ability to adjust the fileName. Defaults to: `[{"searchValue":"::","replaceValue":"/"}]` |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ inputs:
description: 'Enable to only annotate the results on the files, will not create a check run.'
required: false
default: 'false'
check_annotations:
description: 'Defines if the checks will include annotations (This is different than `annotate_only`).'
required: false
default: 'true'
update_check:
description: 'Defines if the active check should be updated instead'
required: false
Expand Down
44 changes: 30 additions & 14 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.

62 changes: 40 additions & 22 deletions src/annotator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as core from '@actions/core'
import {TestResult} from './testParser'
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'

export async function annotateTestResult(
testResult: TestResult,
token: string,
headSha: string,
checkAnnotations: boolean,
annotateOnly: boolean,
updateCheck: boolean,
annotateNotice: boolean,
Expand All @@ -32,6 +34,7 @@ export async function annotateTestResult(

const octokit = github.getOctokit(token)
if (annotateOnly) {
// only create annotaitons, no check
for (const annotation of annotations) {
const properties: core.AnnotationProperties = {
title: annotation.title,
Expand All @@ -50,6 +53,7 @@ export async function annotateTestResult(
}
}
} else {
// check status is being created, annotations are included in this (if not diasbled by "checkAnnotations")
if (updateCheck) {
const checks = await octokit.rest.checks.listForRef({
...github.context.repo,
Expand All @@ -63,27 +67,20 @@ export async function annotateTestResult(

const check_run_id = checks.data.check_runs[0].id

core.info(`ℹ️ - ${testResult.checkName} - Updating checks ${annotations.length}`)
for (let i = 0; i < annotations.length; i = i + 50) {
const sliced = annotations.slice(i, i + 50)

const updateCheckRequest = {
...github.context.repo,
check_run_id,
output: {
title,
summary: testResult.summary,
annotations: sliced
}
if (checkAnnotations) {
core.info(`ℹ️ - ${testResult.checkName} - Updating checks (Annotations: ${annotations.length})`)
for (let i = 0; i < annotations.length; i = i + 50) {
const sliced = annotations.slice(i, i + 50)
updateChecks(octokit, check_run_id, title, testResult.summary, sliced)
}

core.debug(JSON.stringify(updateCheckRequest, null, 2))

await octokit.rest.checks.update(updateCheckRequest)
} else {
core.info(`ℹ️ - ${testResult.checkName} - Updating checks (disabled annotations)`)
updateChecks(octokit, check_run_id, title, testResult.summary, [])
}
} else {
const status: 'completed' | 'in_progress' | 'queued' | undefined = 'completed'

// don't send annotations if disabled
const adjsutedAnnotations = checkAnnotations ? annotations : []
const createCheckRequest = {
...github.context.repo,
name: testResult.checkName,
Expand All @@ -93,18 +90,39 @@ export async function annotateTestResult(
output: {
title,
summary: testResult.summary,
annotations: annotations.slice(0, 50)
annotations: adjsutedAnnotations.slice(0, 50)
}
}

core.debug(JSON.stringify(createCheckRequest, null, 2))

core.info(`ℹ️ - ${testResult.checkName} - Creating check for`)
core.info(`ℹ️ - ${testResult.checkName} - Creating check (Annotations: ${adjsutedAnnotations.length})`)
await octokit.rest.checks.create(createCheckRequest)
}
}
}

async function updateChecks(
octokit: InstanceType<typeof GitHub>,
check_run_id: number,
title: string,
summary: string,
annotations: Annotation[]
): Promise<void> {
const updateCheckRequest = {
...github.context.repo,
check_run_id,
output: {
title,
summary,
annotations
}
}

core.debug(JSON.stringify(updateCheckRequest, null, 2))
await octokit.rest.checks.update(updateCheckRequest)
}

export async function attachSummary(
testResults: TestResult[],
detailedSummary: boolean,
Expand Down Expand Up @@ -158,8 +176,8 @@ export async function attachSummary(
annotation.status === 'success'
? '✅ pass'
: annotation.status === 'skipped'
? `⏭️ skipped`
: `❌ ${annotation.annotation_level}`
? `⏭️ skipped`
: `❌ ${annotation.annotation_level}`
}`
])
}
Expand Down
12 changes: 11 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function run(): Promise<void> {

const annotateOnly = core.getInput('annotate_only') === 'true'
const updateCheck = core.getInput('update_check') === 'true'
const checkAnnotations = core.getInput('check_annotations') === 'true'
const commit = core.getInput('commit')
const failOnFailure = core.getInput('fail_on_failure') === 'true'
const requireTests = core.getInput('require_tests') === 'true'
Expand Down Expand Up @@ -109,7 +110,16 @@ export async function run(): Promise<void> {

try {
for (const testResult of testResults) {
await annotateTestResult(testResult, token, headSha, annotateOnly, updateCheck, annotateNotice, jobName)
await annotateTestResult(
testResult,
token,
headSha,
checkAnnotations,
annotateOnly,
updateCheck,
annotateNotice,
jobName
)
}
} catch (error) {
core.error(`❌ Failed to create checks using the provided token. (${error})`)
Expand Down

0 comments on commit 2d2e937

Please sign in to comment.