diff --git a/src/index.ts b/src/index.ts index 64521ba..3804f87 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import path from 'node:path'; import { parseVitestJsonFinal, parseVitestJsonSummary } from './parseJsonReports.js'; import { writeSummaryToPR } from './writeSummaryToPR.js'; import * as core from '@actions/core'; +import {RequestError} from '@octokit/request-error' import { parseCoverageThresholds } from './parseCoverageThresholds.js'; import { generateFileCoverageHtml } from './generateFileCoverageHtml.js'; @@ -33,7 +34,19 @@ const run = async () => { .addRaw(tableData) .addDetails('File Coverage', fileTable) - await writeSummaryToPR(summary); + try { + await writeSummaryToPR(summary); + } catch (error) { + if (error instanceof RequestError && (error.status === 404 || error.status === 403)) { + core.warning( + `Couldn't write a comment to the pull-request. Please make sure your job has the permission 'pull-request: write'.` + ) + } else { + // Rethrow to handle it in the catch block of the run()-call. + throw error; + } + } + await summary.write(); }; diff --git a/src/writeSummaryToPR.ts b/src/writeSummaryToPR.ts index 9689ece..0f54929 100644 --- a/src/writeSummaryToPR.ts +++ b/src/writeSummaryToPR.ts @@ -7,7 +7,7 @@ type Octokit = ReturnType; const writeSummaryToPR = async (summary: typeof core.summary) => { if (!github.context.payload.pull_request) { - console.log('[vitest-coverage-report] Not in the context of a pull request. Skipping comment creation.'); + core.info('[vitest-coverage-report] Not in the context of a pull request. Skipping comment creation.'); return; }