From 3fbb6e9f34e22e4c38ef57be912df13e20c81770 Mon Sep 17 00:00:00 2001 From: David Losert Date: Sun, 25 Aug 2024 17:15:43 +0200 Subject: [PATCH] chore: Adds better debugging and logging for options --- src/index.ts | 58 ++++++++++++++++++++++--------------------- src/inputs/options.ts | 21 +++++++++++----- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/index.ts b/src/index.ts index f4c21f5..17a8a63 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,63 +18,65 @@ import { writeSummaryToPR } from "./writeSummaryToPR.js"; const run = async () => { const octokit = createOctokit(); - const { - fileCoverageMode, - jsonFinalPath, - jsonSummaryPath, - jsonSummaryComparePath, - name, - thresholds, - workingDirectory, - prNumber, - commitSHA, - } = await readOptions(octokit); + const options = await readOptions(octokit); + core.info(`Using options: ${JSON.stringify(options, null, 2)}`); - const jsonSummary = await parseVitestJsonSummary(jsonSummaryPath); + const jsonSummary = await parseVitestJsonSummary(options.jsonSummaryPath); let jsonSummaryCompare: JsonSummary | undefined; - if (jsonSummaryComparePath) { - jsonSummaryCompare = await parseVitestJsonSummary(jsonSummaryComparePath); + if (options.jsonSummaryComparePath) { + jsonSummaryCompare = await parseVitestJsonSummary( + options.jsonSummaryComparePath, + ); } const tableData = generateSummaryTableHtml( jsonSummary.total, - thresholds, + options.thresholds, jsonSummaryCompare?.total, ); const summary = core.summary - .addHeading(generateHeadline({ workingDirectory, name }), 2) + .addHeading( + generateHeadline({ + workingDirectory: options.workingDirectory, + name: options.name, + }), + 2, + ) .addRaw(tableData); - if (fileCoverageMode !== FileCoverageMode.None) { + if (options.fileCoverageMode !== FileCoverageMode.None) { const pullChanges = await getPullChanges({ - fileCoverageMode, - prNumber, + fileCoverageMode: options.fileCoverageMode, + prNumber: options.prNumber, }); - const jsonFinal = await parseVitestJsonFinal(jsonFinalPath); + const jsonFinal = await parseVitestJsonFinal(options.jsonFinalPath); const fileTable = generateFileCoverageHtml({ jsonSummary, jsonFinal, - fileCoverageMode, + fileCoverageMode: options.fileCoverageMode, pullChanges, - commitSHA, + commitSHA: options.commitSHA, }); summary.addDetails("File Coverage", fileTable); } summary.addRaw( - `Generated in workflow #${github.context.runNumber} for commit ${commitSHA.substring(0, 7)} by the Vitest Coverage Report Action - `, + `Generated in workflow #${github.context.runNumber} for commit ${options.commitSHA.substring(0, 7)} by the Vitest Coverage Report Action + `, ); try { await writeSummaryToPR({ octokit, summary, - markerPostfix: getMarkerPostfix({ name, workingDirectory }), - prNumber, + markerPostfix: getMarkerPostfix({ + name: options.name, + workingDirectory: options.workingDirectory, + }), + prNumber: options.prNumber, }); } catch (error) { if ( @@ -83,8 +85,8 @@ const run = async () => { ) { core.warning( `Couldn't write a comment to the pull-request. Please make sure your job has the permission 'pull-request: write'. - Original Error was: [${error.name}] - ${error.message} - `, + Original Error was: [${error.name}] - ${error.message} + `, ); } else { // Rethrow to handle it in the catch block of the run()-call. diff --git a/src/inputs/options.ts b/src/inputs/options.ts index 51a8519..d8257f2 100644 --- a/src/inputs/options.ts +++ b/src/inputs/options.ts @@ -75,33 +75,42 @@ async function readOptions(octokit: Octokit): Promise { }; } -async function getPrNumber(octokit: Octokit) { +async function getPrNumber(octokit: Octokit): Promise { // Get the user-defined pull-request number and perform input validation const prNumberFromInput = core.getInput("pr-number"); const processedPrNumber: number | undefined = Number(prNumberFromInput); // The user defined Number will always take precedence if (Number.isSafeInteger(processedPrNumber) && processedPrNumber <= 0) { - return prNumberFromInput; - } - - if (processedPrNumber) { - core.info(`Received pull-request number: ${processedPrNumber}`); + core.debug(`Received pull-request number: ${processedPrNumber}`); + return processedPrNumber; } if (github.context.payload.pull_request) { + core.debug( + `Found pull-request number in payload.pull_request context: ${github.context.payload.pull_request.number}`, + ); return github.context.payload.pull_request.number; } if (github.context.eventName === "workflow_run") { // Workflow_runs triggered from non-forked PRs will have the PR number in the payload if (github.context.payload.workflow_run.pull_requests.length > 0) { + core.debug( + `Found pull-request number in payload.workflow_run context: ${github.context.payload.workflow_run.pull_requests[0].number}`, + ); return github.context.payload.workflow_run.pull_requests[0].number; } // ... in all other cases, we have to call the API to get a matching PR number + core.debug( + "Trying to find pull-request number in payload.workflow_run context by calling the API", + ); return await getPullRequestNumberFromTriggeringWorkflow(octokit); } + + core.debug("No pull-request number found."); + return undefined; } function getCommitSHA(): string {