Skip to content

Commit

Permalink
chore: Adds better debugging and logging for options
Browse files Browse the repository at this point in the history
  • Loading branch information
davelosert committed Aug 25, 2024
1 parent e23a523 commit 3fbb6e9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
58 changes: 30 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`<em>Generated in workflow <a href=${getWorkflowSummaryURL()}>#${github.context.runNumber}</a> for commit ${commitSHA.substring(0, 7)} by the <a href="https://github.com/davelosert/vitest-coverage-report-action">Vitest Coverage Report Action</a></em>
`,
`<em>Generated in workflow <a href=${getWorkflowSummaryURL()}>#${github.context.runNumber}</a> for commit ${options.commitSHA.substring(0, 7)} by the <a href="https://github.com/davelosert/vitest-coverage-report-action">Vitest Coverage Report Action</a></em>
`,
);

try {
await writeSummaryToPR({
octokit,
summary,
markerPostfix: getMarkerPostfix({ name, workingDirectory }),
prNumber,
markerPostfix: getMarkerPostfix({
name: options.name,
workingDirectory: options.workingDirectory,
}),
prNumber: options.prNumber,
});
} catch (error) {
if (
Expand All @@ -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.
Expand Down
21 changes: 15 additions & 6 deletions src/inputs/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,42 @@ async function readOptions(octokit: Octokit): Promise<Options> {
};
}

async function getPrNumber(octokit: Octokit) {
async function getPrNumber(octokit: Octokit): Promise<number | undefined> {
// 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 {
Expand Down

0 comments on commit 3fbb6e9

Please sign in to comment.