-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.ts
89 lines (76 loc) · 2.82 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
import { FileCoverageMode } from './inputs/FileCoverageMode.js'
import { generateFileCoverageHtml } from './report/generateFileCoverageHtml.js';
import { generateHeadline } from './report/generateHeadline.js';
import { generateSummaryTableHtml } from './report/generateSummaryTableHtml.js';
import { getPullChanges } from './inputs/getPullChanges.js';
import { parseVitestJsonFinal, parseVitestJsonSummary } from './inputs/parseJsonReports.js';
import { readOptions } from './inputs/options.js';
import { RequestError } from '@octokit/request-error'
import { writeSummaryToPR } from './writeSummaryToPR.js';
import * as core from '@actions/core';
import * as github from '@actions/github';
const run = async () => {
const {
fileCoverageMode,
jsonFinalPath,
jsonSummaryPath,
jsonSummaryComparePath,
name,
thresholds,
workingDirectory,
processedPrNumber
} = await readOptions();
const jsonSummary = await parseVitestJsonSummary(jsonSummaryPath);
let jsonSummaryCompare;
if (jsonSummaryComparePath) {
jsonSummaryCompare = await parseVitestJsonSummary(jsonSummaryComparePath);
}
const tableData = generateSummaryTableHtml(jsonSummary.total, thresholds, jsonSummaryCompare?.total);
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 }),
userDefinedPrNumber: processedPrNumber
});
} 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);
});