Skip to content

Commit

Permalink
fix: improve logging & remove try/catch block preventing error
Browse files Browse the repository at this point in the history
  • Loading branch information
vmasek committed Feb 28, 2025
1 parent efd7f1e commit 4b1819c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 133 deletions.
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 @@ export async function run(
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) {
core.info(
`${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
).split('.')
const majorVersion = parseInt(nodeMajorString, 10)
const isUnsupportedVersion = majorVersion < 20

const refs = parseGitRefs()
const api = new GitHubApiClient(inputs.token, refs, artifact, getOctokit)
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 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'
? Object.values(result.files.diff ?? {})
: result.diffPath
? [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(
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) {
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'
? (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')
}
} 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

0 comments on commit 4b1819c

Please sign in to comment.