-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
45 lines (40 loc) · 1.32 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
import * as core from '@actions/core';
import { context, getOctokit } from '@actions/github';
import parseLCOV from 'parse-lcov';
import fs from 'fs';
interface CoverageMetric {
found: number;
hit: number;
percent: number;
}
function getMetric(coverages: any[], kind: string): CoverageMetric {
let hit = 0, found = 0;
for (const cov of coverages) {
hit += cov[kind].hit;
found += cov[kind].found;
}
const percent = ((hit / found) * 100) || 0;
return {hit, found, percent};
}
function getSummary({hit, found, percent}: CoverageMetric) {
return `${hit}/${found} (${percent.toFixed(1)}%)`;
}
async function main() {
if (context.eventName != 'pull_request') {
return;
}
const file = fs.readFileSync(core.getInput('lcov-path'));
const coverages = parseLCOV(file.toString());
const branches = getMetric(coverages, 'branches');
const lines = getMetric(coverages, 'lines');
const body = `<p>Covered ${getSummary(branches)} branches, ${getSummary(lines)} lines</p>`;
const issue_number = context.payload.pull_request?.number;
if (!issue_number) {
return;
}
const {repo, owner} = context.repo;
const octokit = getOctokit(core.getInput('github-token'));
await octokit.issues.createComment({repo, owner, body, issue_number});
}
main().catch(e => core.setFailed(e.message));
export default main;