-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.ts
80 lines (69 loc) · 2.52 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
import { generateSummaryTableHtml } from './generateSummaryTableHtml.js';
import { parseVitestJsonFinal, parseVitestJsonSummary } from './parseJsonReports.js';
import { writeSummaryToPR } from './writeSummaryToPR.js';
import * as core from '@actions/core';
import * as github from '@actions/github';
import { RequestError } from '@octokit/request-error'
import { generateFileCoverageHtml } from './generateFileCoverageHtml.js';
import { getPullChanges } from './getPullChanges.js';
import { FileCoverageMode } from './FileCoverageMode.js'
import { readOptions } from './options.js';
import { generateHeadline } from './generateHeadline.js';
const run = async () => {
const {
fileCoverageMode,
jsonFinalPath,
jsonSummaryPath,
name,
thresholds,
workingDirectory
} = await readOptions();
const jsonSummary = await parseVitestJsonSummary(jsonSummaryPath);
const tableData = generateSummaryTableHtml(jsonSummary.total, thresholds);
const summary = core.summary
.addHeading(generateHeadline({ workingDirectory, name }), 2)
.addRaw(tableData)
if (fileCoverageMode !== FileCoverageMode.None) {
const pullChanges = await getPullChanges(fileCoverageMode);
const jsonFinal = await parseVitestJsonFinal(jsonFinalPath);
const fileTable = generateFileCoverageHtml({
jsonSummary, jsonFinal, fileCoverageMode, pullChanges
});
summary.addDetails('File Coverage', fileTable)
}
summary
.addRaw(`<em>Generated in workflow <a href=${getWorkflowSummaryURL()}>#${github.context.runNumber}</a></em>`)
try {
await writeSummaryToPR({
summary,
markerPostfix: getMarkerPostfix({ name, workingDirectory })
});
} 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);
});