forked from romeovs/lcov-reporter-action
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (129 loc) · 4.68 KB
/
index.js
File metadata and controls
146 lines (129 loc) · 4.68 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import fs, { promises } from "fs";
import path from "path";
import core from "@actions/core";
import github from "@actions/github";
import { parse } from "./lcov";
import { diff, diffForMonorepo } from "./comment";
import { upsertComment } from "./github";
/**
* Find all files inside a dir, recursively.
* @function getLcovFiles
* @param {string} dir Dir path string.
* @return {string[{<package_name>: <path_to_lcov_file>}]} Array with lcove file names with package names as key.
*/
const getLcovFiles = (dir, filelist) => {
let fileArray = filelist || [];
fs.readdirSync(dir).forEach(file => {
fileArray = fs.statSync(path.join(dir, file)).isDirectory()
? getLcovFiles(path.join(dir, file), fileArray)
: fileArray
.filter(f => f.path.includes("lcov.info"))
.concat({
name: dir.split("/")[1],
path: path.join(dir, file),
});
});
return fileArray;
};
/**
* Find all files inside a dir, recursively for base branch.
* @function getLcovBaseFiles
* @param {string} dir Dir path string.
* @return {string[{<package_name>: <path_to_lcov_file>}]} Array with lcove file names with package names as key.
*/
const getLcovBaseFiles = (dir, filelist) => {
let fileArray = filelist || [];
fs.readdirSync(dir).forEach(file => {
fileArray = fs.statSync(path.join(dir, file)).isDirectory()
? getLcovBaseFiles(path.join(dir, file), fileArray)
: fileArray
.filter(f => f.path.includes("lcov-base"))
.concat({
name: dir.split("/")[1],
path: path.join(dir, file),
});
});
return fileArray;
};
const main = async () => {
const { context = {} } = github || {};
const token = core.getInput("github-token");
const lcovFile = core.getInput("lcov-file") || "./coverage/lcov.info";
const baseFile = core.getInput("lcov-base");
const appName = core.getInput("app-name");
// Add base path for monorepo
const monorepoBasePath = core.getInput("monorepo-base-path");
const raw =
!monorepoBasePath &&
(await promises
.readFile(lcovFile, "utf-8")
.catch(err => console.error(err)));
if (!monorepoBasePath && !raw) {
console.log(`No coverage report found at '${lcovFile}', exiting...`);
return;
}
const baseRaw =
baseFile &&
(await promises
.readFile(baseFile, "utf-8")
.catch(err => console.error(err)));
if (!monorepoBasePath && baseFile && !baseRaw) {
console.log(`No coverage report found at '${baseFile}', ignoring...`);
}
const lcovArray = monorepoBasePath ? getLcovFiles(monorepoBasePath) : [];
const lcovBaseArray = monorepoBasePath
? getLcovBaseFiles(monorepoBasePath)
: [];
const lcovArrayForMonorepo = [];
const lcovBaseArrayForMonorepo = [];
for (const file of lcovArray) {
if (file.path.includes(".info")) {
const rLcove = await promises.readFile(file.path, "utf8");
const data = await parse(rLcove);
lcovArrayForMonorepo.push({
packageName: file.name,
lcov: data,
});
}
}
for (const file of lcovBaseArray) {
if (file.path.includes(".info")) {
const rLcovBase = await promises.readFile(file.path, "utf8");
const data = await parse(rLcovBase);
lcovBaseArrayForMonorepo.push({
packageName: file.name,
lcov: data,
});
}
}
const options = {
repository: context.payload.repository.full_name,
commit: context.payload.pull_request.head.sha,
prefix: `${process.env.GITHUB_WORKSPACE}/`,
head: context.payload.pull_request.head.ref,
base: context.payload.pull_request.base.ref,
appName,
};
const lcov = !monorepoBasePath && (await parse(raw));
const baselcov = baseRaw && (await parse(baseRaw));
const client = github.getOctokit(token);
await upsertComment({
client,
context,
prNumber: context.payload.pull_request.number,
body: !lcovArrayForMonorepo.length
? diff(lcov, baselcov, options)
: diffForMonorepo(
lcovArrayForMonorepo,
lcovBaseArrayForMonorepo,
options,
),
hiddenHeader: appName
? `<!-- ${appName}-code-coverage-assistant -->`
: `<!-- monorepo-code-coverage-assistant -->`,
});
};
main().catch(err => {
console.log(err);
core.setFailed(err.message);
});