-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
254 lines (221 loc) · 8.83 KB
/
index.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { DefaultArtifactClient } from '@actions/artifact';
import core from '@actions/core';
import exec from '@actions/exec';
import github from '@actions/github';
import glob from '@actions/glob';
import lcovParse from 'lcov-parse';
import { markdownTable } from 'markdown-table';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';
async function run() {
try {
const lcovFile = path.resolve(core.getInput('lcov-file'));
const commentTitle = core.getInput('comment-title');
const workingDirectory = path.resolve(core.getInput('working-directory'));
const allFilesMinimumCoverage = parseFloat(core.getInput('all-files-minimum-coverage')) || 0;
const changedFilesMinimumCoverage = parseFloat(core.getInput('changed-files-minimum-coverage')) || 0;
const artifactName = core.getInput('artifact-name');
const githubToken = core.getInput('github-token');
const octokit = github.getOctokit(githubToken);
console.log(`lcovFile: ${lcovFile}`);
console.log(`commentTitle: ${commentTitle}`);
console.log(`workingDirectory: ${workingDirectory}`);
console.log(`allFilesMinimumCoverage: ${allFilesMinimumCoverage}`);
console.log(`changedFilesMinimumCoverage: ${changedFilesMinimumCoverage}`);
console.log(`artifactName: ${artifactName}`);
console.log(`githubToken: ${githubToken}`);
const data = await parseLcov(lcovFile, workingDirectory);
const changedFiles = await getChangedFiles(octokit);
const allFilesLcov = sumLcov(data);
const allFilesPassed = isPassed(allFilesLcov, allFilesMinimumCoverage);
const changedFilesLcov = sumLcov(data, changedFiles);
const hasChangedFiles = changedFilesLcov != undefined;
const changedFilesPassed = isPassed(changedFilesLcov, changedFilesMinimumCoverage);
const bothPassed = allFilesPassed && (!hasChangedFiles || changedFilesPassed);
if (!bothPassed) {
core.setFailed('Coverage is below the minimum');
}
console.log(`allFilesPassed: ${allFilesPassed}`);
console.log(`hasChangedFiles: ${hasChangedFiles}`);
console.log(`changedFilesPassed: ${changedFilesPassed}`);
console.log(`bothPassed: ${bothPassed}`);
const commentId = renderCommentId(commentTitle);
const comment = commentId +
renderCommentHeader(commentTitle, bothPassed) +
renderSectionHeader('All Files') +
renderLcovOverall(allFilesLcov, allFilesMinimumCoverage, allFilesPassed) +
renderSectionHeader('Changed Files') +
renderLcovOverall(changedFilesLcov, changedFilesMinimumCoverage, changedFilesPassed) +
renderLcovFiles(data, changedFiles);
console.log(comment);
if (github.context.eventName == 'pull_request') {
await postComment(octokit, commentId, comment);
} else {
console.log('Skipped posting comment');
};
if (artifactName != '') {
uploadArtifact(lcovFile, artifactName, workingDirectory);
} else {
console.log('Skipped uploading artifact');
}
} catch (error) {
core.setFailed(error.message);
}
}
run();
async function getChangedFiles(octokit) {
if (github.context.eventName != 'pull_request') return new Set();
console.log('Getting changed files...');
const { data: { files: files } } = await octokit.rest.repos.compareCommitsWithBasehead({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
basehead: `${github.context.payload.pull_request.base.sha}...${github.context.payload.pull_request.head.sha}`,
per_page: 1,
});
const fileNames = files.map((file) => path.resolve(file.filename));
console.log(`Changed files: ${JSON.stringify(fileNames)}`)
return new Set(fileNames);
}
async function postComment(octokit, commentId, comment) {
const comments = await getComments(octokit);
const existingComment = comments.find((comment) => comment.body.startsWith(commentId));
if (existingComment == undefined) {
console.log(`Existing comment is not found, creating new comment...`);
await createNewComment(octokit, comment);
} else {
console.log(`Existing comment is found, updating existing comment...`);
await updateComment(octokit, existingComment, comment);
}
}
async function getComments(octokit) {
if (github.context.eventName != 'pull_request') return [];
console.log('Getting comments...');
const { data: comments } = await octokit.rest.issues.listComments({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.pull_request.number,
per_page: 100,
});
console.log(`Comments: ${JSON.stringify(comments.map((comment) => comment.body))}`);
return comments;
}
async function createNewComment(octokit, comment) {
await octokit.rest.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.pull_request.number,
body: comment,
});
}
async function updateComment(octokit, existingComment, comment) {
await octokit.rest.issues.updateComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
comment_id: existingComment.id,
body: comment,
});
}
async function parseLcov(lcovFile, workingDirectory) {
console.log('Parsing coverage file...');
return new Promise(resolve => {
lcovParse(lcovFile, function (err, data) {
if (err != null) {
throw new Error(`Error parsing lcov file ${lcovFile}: ${err}`);
}
resolve(data.map((item) => {
item.file = path.resolve(workingDirectory, item.file);
return item;
}));
});
})
}
function sumLcov(data, files) {
if (files != undefined) {
data = data.filter((item) => files.has(item.file));
}
if (data.length == 0) {
return undefined;
}
const sum = data.reduce((total, file) => {
const newTotal = JSON.parse(JSON.stringify(total));
newTotal.lines.found += file.lines.found;
newTotal.lines.hit += file.lines.hit;
newTotal.functions.found += file.functions.found;
newTotal.functions.hit += file.functions.hit;
newTotal.branches.found += file.branches.found;
newTotal.branches.hit += file.branches.hit;
return newTotal;
});
return sum;
}
function isPassed(data, minimumCoverage) {
var coverage;
if (data == undefined || data.lines.found == 0) {
coverage = 0.0;
} else {
coverage = data.lines.hit * 100 / data.lines.found
}
return coverage >= minimumCoverage;
}
function renderLcovOverall(data, minimumCoverage, isPassed) {
if (data == undefined) {
return 'N/A\n'
}
const output =
`- Lines: ${renderLcovPercentage(data.lines)} ${renderPassed(isPassed)} (Minimum coverage is ${minimumCoverage}%)\n` +
`- Functions: ${renderLcovPercentage(data.functions)}\n` +
`- Branches: ${renderLcovPercentage(data.branches)}\n` +
'\n';
return output;
}
function renderLcovFiles(data, files) {
if (files != undefined) {
data = data.filter((item) => files.has(item.file));
}
if (data.length == 0) {
return '';
}
const table = [['File', 'Lines', 'Functions', 'Branches']];
data.forEach(item => {
table.push(
[
path.basename(item.file),
renderLcovPercentage(item.lines),
renderLcovPercentage(item.functions),
renderLcovPercentage(item.branches),
]
)
});
return markdownTable(table);
}
function renderLcovPercentage(lcov) {
const hit = lcov.hit;
const found = lcov.found;
if (found == 0) return 'N/A';
const percentage = (hit * 100 / found).toFixed(1);
return `${hit}/${found} (${percentage}%)`
}
function renderCommentId(title) {
return `[lcov-comment-id]: <> (${title})\n`
}
function renderCommentHeader(title, isPassed) {
return `## LCOV Report${title !== '' ? ` - ${title}` : ''} ${renderPassed(isPassed)}\n`
}
function renderSectionHeader(title) {
return `### ${title}\n`;
}
function renderPassed(isPassed) {
if (isPassed) {
return '✅';
} else {
return '❌';
}
}
async function uploadArtifact(lcovFile, artifactName, workingDirectory) {
const artifact = new DefaultArtifactClient();
const artifactPath = path.resolve(uuidv4());
await exec.exec(`genhtml ${lcovFile} -o ${artifactPath}`, [], { cwd: workingDirectory })
const globber = await glob.create(`${artifactPath}/**/*.*`);
const files = await globber.glob();
await artifact.uploadArtifact(artifactName, files, artifactPath);
}