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

fix: improve logging and remove try/catch block #146

Merged
merged 1 commit into from
Feb 28, 2025
Merged
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
114 changes: 58 additions & 56 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.

144 changes: 69 additions & 75 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,99 +21,93 @@
getOctokit = github.getOctokit,
git = simpleGit()
): Promise<void> {
try {
const inputs = parseInputs()
const options = createOptions(inputs)
const inputs = parseInputs()
const options = createOptions(inputs)

const [nodeMajorString] = (
process.version.startsWith('v')
? process.version.slice(1)
: process.version
).split('.')
const majorVersion = parseInt(nodeMajorString, 10)
const isUnsupportedVersion = majorVersion < 20
if (options.debug) {

Check failure on line 27 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
core.info(

Check warning on line 28 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Line coverage

Line 28 is not covered in any test case.
`${LOG_PREFIX} Debug in actions in enabled, setting CP_VERBOSE env variable to true`
)
process.env['CP_VERBOSE'] = 'true'
}

if (isUnsupportedVersion) {
core.warning(
`${LOG_PREFIX} Internal runner is using unsupported NodeJS version ${process.version}`
)
} else if (options.debug) {
core.info(
`${LOG_PREFIX} Internal runner is using NodeJS version ${process.version}`
)
}
const [nodeMajorString] = (
process.version.startsWith('v') ? process.version.slice(1) : process.version

Check failure on line 35 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

2nd branch is not taken in any test case.
).split('.')
const majorVersion = parseInt(nodeMajorString, 10)
const isUnsupportedVersion = majorVersion < 20

const refs = parseGitRefs()
const api = new GitHubApiClient(inputs.token, refs, artifact, getOctokit)
if (isUnsupportedVersion) {

Check failure on line 40 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
core.warning(

Check warning on line 41 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Line coverage

Line 41 is not covered in any test case.
`${LOG_PREFIX} Internal runner is using unsupported NodeJS version ${process.version}`
)
} else if (options.debug) {

Check failure on line 44 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
core.info(

Check warning on line 45 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Line coverage

Line 45 is not covered in any test case.
`${LOG_PREFIX} Internal runner is using NodeJS version ${process.version}`
)
}

const result = await runInCI(refs, api, options, git)
const refs = parseGitRefs()
const api = new GitHubApiClient(inputs.token, refs, artifact, getOctokit)

if (result.commentId != null) {
core.setOutput('comment-id', result.commentId)
core.info(`Commented on PR #${github.context.issue.number}`)
}
const result = await runInCI(refs, api, options, git)

const diffFiles =
result.mode === 'standalone'
? Object.values(result.files.diff ?? {})
: result.diffPath
? [result.diffPath]
: []
if (diffFiles.length > 0) {
await uploadArtifact(
artifact,
createDiffArtifactName(),
diffFiles,
inputs
)
}
if (result.commentId != null) {
core.setOutput('comment-id', result.commentId)
core.info(`Commented on PR #${github.context.issue.number}`)
}

const diffFiles =
result.mode === 'standalone'

Check failure on line 61 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

2nd branch is not taken in any test case.
? Object.values(result.files.diff ?? {})
: result.diffPath

Check failure on line 63 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.

Check failure on line 63 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

2nd branch is not taken in any test case.
? [result.diffPath]
: []

if (diffFiles.length > 0) {
await uploadArtifact(artifact, createDiffArtifactName(), diffFiles, inputs)
}

if (result.mode === 'standalone') {
const id = await uploadArtifact(
if (result.mode === 'standalone') {
const id = await uploadArtifact(
artifact,
createReportArtifactName(),
Object.values(result.files.report),
inputs
)
core.setOutput('artifact-id', id)
} else {
for (const project of result.projects) {
await uploadArtifact(

Check warning on line 81 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Line coverage

Lines 80-81 are not covered in any test case.
artifact,
createReportArtifactName(),
Object.values(result.files.report),
createReportArtifactName(project.name),
Object.values(project.files.report),
inputs
)
core.setOutput('artifact-id', id)
} else {
for (const project of result.projects) {
if (project.files.diff) {

Check failure on line 87 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
await uploadArtifact(
artifact,
createReportArtifactName(project.name),
Object.values(project.files.report),
createDiffArtifactName(project.name),
Object.values(project.files.diff),
inputs
)
if (project.files.diff) {
await uploadArtifact(
artifact,
createDiffArtifactName(project.name),
Object.values(project.files.diff),
inputs
)
}
}
}
}

if (inputs.annotations) {
const issues =
result.mode === 'standalone'
? (result.newIssues ?? [])
: result.projects.flatMap(project => project.newIssues ?? [])
if (issues.length > 0) {
core.info(
`Found ${issues.length} new issues, creating GitHub annotations`
)
createAnnotationsFromIssues(issues)
} else {
core.info('No new issues found, skipping GitHub annotations')
}
if (inputs.annotations) {
const issues =
result.mode === 'standalone'

Check failure on line 100 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Branch coverage

2nd branch is not taken in any test case.
? (result.newIssues ?? [])
: result.projects.flatMap(project => project.newIssues ?? [])

Check failure on line 102 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Function coverage

Function (anonymous_1) is not called in any test case.

Check warning on line 102 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Line coverage

Line 102 is not covered in any test case.
if (issues.length > 0) {
core.info(

Check warning on line 104 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Line coverage

Line 104 is not covered in any test case.
`Found ${issues.length} new issues, creating GitHub annotations`
)
createAnnotationsFromIssues(issues)

Check warning on line 107 in src/main.ts

View workflow job for this annotation

GitHub Actions / GitHub Actions Test

<✓> Code coverage | Line coverage

Line 107 is not covered in any test case.
} else {
core.info('No new issues found, skipping GitHub annotations')
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : `${error}`
core.error(errorMessage)
core.setFailed(errorMessage)
return
}

core.info(`${LOG_PREFIX} Finished running successfully`)
Expand Down
12 changes: 11 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import * as core from '@actions/core'
import type { Options } from '@code-pushup/ci'
import type { ActionInputs } from './inputs'

function isDebugActive(): boolean {
return (
// checks just RUNNER_DEBUG env variable
core.isDebug() ||
// docs mention ACTIONS prefixed debug variables https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/troubleshooting-workflows/enabling-debug-logging
process.env['ACTIONS_RUNNER_DEBUG'] === 'true' ||
process.env['ACTIONS_STEP_DEBUG'] === 'true'
)
}

export function createOptions(inputs: ActionInputs): Required<Options> {
return {
monorepo: inputs.monorepo,
Expand All @@ -15,7 +25,7 @@ export function createOptions(inputs: ActionInputs): Required<Options> {
detectNewIssues: inputs.annotations,
skipComment: inputs.skipComment,
silent: inputs.silent,
debug: core.isDebug(),
debug: isDebugActive(),
logger: {
error: core.error,
warn: core.warning,
Expand Down
Loading