-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.ts
124 lines (110 loc) · 3.58 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import * as core from "@actions/core";
import * as github from "@actions/github";
import { RequestError } from "@octokit/request-error";
import { FileCoverageMode } from "./inputs/FileCoverageMode.js";
import { getPullChanges } from "./inputs/getPullChanges.js";
import { readOptions } from "./inputs/options.js";
import {
parseVitestJsonFinal,
parseVitestJsonSummary,
} from "./inputs/parseJsonReports.js";
import { createOctokit } from "./octokit.js";
import { generateCommitSHAUrl } from "./report/generateCommitSHAUrl.js";
import { generateFileCoverageHtml } from "./report/generateFileCoverageHtml.js";
import { generateHeadline } from "./report/generateHeadline.js";
import { generateSummaryTableHtml } from "./report/generateSummaryTableHtml.js";
import type { JsonSummary } from "./types/JsonSummary.js";
import { writeSummaryToPR } from "./writeSummaryToPR.js";
const run = async () => {
const octokit = createOctokit();
const options = await readOptions(octokit);
core.info(`Using options: ${JSON.stringify(options, null, 2)}`);
const jsonSummary = await parseVitestJsonSummary(options.jsonSummaryPath);
let jsonSummaryCompare: JsonSummary | undefined;
if (options.jsonSummaryComparePath) {
jsonSummaryCompare = await parseVitestJsonSummary(
options.jsonSummaryComparePath,
);
}
const summary = core.summary
.addHeading(
generateHeadline({
workingDirectory: options.workingDirectory,
name: options.name,
}),
2,
)
.addRaw(
generateSummaryTableHtml(
jsonSummary.total,
options.thresholds,
jsonSummaryCompare?.total,
),
);
if (options.fileCoverageMode !== FileCoverageMode.None) {
const pullChanges = await getPullChanges({
fileCoverageMode: options.fileCoverageMode,
prNumber: options.prNumber,
octokit,
});
const jsonFinal = await parseVitestJsonFinal(options.jsonFinalPath);
const fileTable = generateFileCoverageHtml({
jsonSummary,
jsonFinal,
fileCoverageMode: options.fileCoverageMode,
pullChanges,
commitSHA: options.commitSHA,
});
summary.addDetails("File Coverage", fileTable);
}
const commitSHAUrl = generateCommitSHAUrl(options.commitSHA);
summary.addRaw(
`<em>Generated in workflow <a href=${getWorkflowSummaryURL()}>#${github.context.runNumber}</a> for commit <a href="${commitSHAUrl}">${options.commitSHA.substring(0, 7)}</a> 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: options.name,
workingDirectory: options.workingDirectory,
}),
prNumber: options.prNumber,
});
} 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'.
Original Error was: [${error.name}] - ${error.message}
`,
);
} else {
// Rethrow to handle it in the catch block of the run()-call.
throw error;
}
}
await summary.write();
};
function getMarkerPostfix({
name,
workingDirectory,
}: { name: string; workingDirectory: string }) {
if (name) return name;
if (workingDirectory !== "./") return workingDirectory;
return "root";
}
function getWorkflowSummaryURL() {
const { owner, repo } = github.context.repo;
const { runId } = github.context;
return `${github.context.serverUrl}/${owner}/${repo}/actions/runs/${runId}`;
}
run()
.then(() => {
core.info("Report generated successfully.");
})
.catch((err) => {
core.error(err);
});