forked from AthleticNet/comment-test-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (77 loc) · 2.9 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
const { inspect } = require("util");
const core = require("@actions/core");
const github = require("@actions/github");
const fs = require('fs');
const originMeta = {
commentFrom: 'Comment Test Coverage as table',
}
async function run() {
try {
const inputs = {
token: core.getInput("token"),
path: core.getInput("path"),
title: core.getInput("title"),
};
const {
payload: { pull_request: pullRequest, repository }
} = github.context;
if (!pullRequest) {
core.error("This action only works on pull_request events");
return;
}
const { number: issueNumber } = pullRequest;
const { full_name: repoFullName } = repository;
const [owner, repo] = repoFullName.split("/");
const octokit = new github.getOctokit(inputs.token);
const data = fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/${inputs.path}`, 'utf8');
const json = JSON.parse(data);
const coverage = `<!--json:${JSON.stringify(originMeta)}-->
|${inputs.title}| % | values |
|---------------|:---------------------------:|:-------------------------------------------------------------------:|
|Statements |${json.total.statements.pct}%|( ${json.total.statements.covered} / ${json.total.statements.total} )|
|Branches |${json.total.branches.pct}% |( ${json.total.branches.covered} / ${json.total.branches.total} ) |
|Functions |${json.total.functions.pct}% |( ${json.total.functions.covered} / ${json.total.functions.total} ) |
|Lines |${json.total.lines.pct}% |( ${json.total.lines.covered} / ${json.total.lines.total} ) |
`;
await deletePreviousComments({
issueNumber,
octokit,
owner,
repo,
inputs
});
await octokit.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: coverage,
});
} catch (error) {
core.debug(inspect(error));
core.setFailed(error.message);
}
}
async function deletePreviousComments({ owner, repo, octokit, issueNumber, inputs }) {
const onlyPreviousCoverageComments = (comment) => {
const regexMarker = new RegExp(inputs.title);
const extractMetaFromMarker = (body) => JSON.parse(body.replace(/^<!--json:|-->(.|\n|\r)*$/g, ''));
if (comment.user.type !== 'Bot') return false;
if (!regexMarker.test(comment.body)) return false;
const meta = extractMetaFromMarker(comment.body);
return meta.commentFrom === originMeta.commentFrom;
}
const asyncDeleteComment = (comment) => {
return octokit.issues.deleteComment({ owner, repo, comment_id: comment.id });
}
const commentList = await octokit.issues.listComments({
owner,
repo,
issue_number: issueNumber,
}).then(response => response.data);
await Promise.all(
commentList
.filter(onlyPreviousCoverageComments)
.map(asyncDeleteComment)
);
}
run();