-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
186 lines (169 loc) · 5.12 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
const core = require("@actions/core");
const github = require("@actions/github");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const { context } = github;
const COMMENT_HEADER = "## Flow Coverage\n";
async function getFilesInPR(octokit, pattern) {
const res = await octokit.request(
"GET /repos/{owner}/{repo}/pulls/{pull_number}/files",
{
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
}
);
if (res.status !== 200) {
throw "Cannot find PR";
}
return res.data
.map(({ filename, status }) => {
return {
filename,
status,
};
})
.filter(({ filename }) => {
const regex = new RegExp(pattern, "gmi");
return regex.test(filename);
});
}
async function getCoverageData(directory, path, filenames, packageManager) {
const outputs = await Promise.all(
filenames.map((filename) => {
let extendedDirectory = directory;
let clippedFilename = filename;
if (path !== "") {
extendedDirectory += `/${path}`;
const regex = new RegExp("^(" + path + ")");
clippedFilename = filename.replace(regex, "");
}
return exec(
`cd ${extendedDirectory} && ${packageManager} flow coverage ${clippedFilename} --quiet`
);
})
);
const coverageData = {};
outputs.forEach(async ({ stdout, stderr }, index) => {
const begin = stdout.indexOf(": ") + 2;
const end = stdout.indexOf("%");
coverageData[filenames[index]] = stdout.substring(begin, end);
});
return coverageData;
}
function getMarkdownTableAndThresholdPass(coverageDifference, threshold) {
const floatThreshold = parseFloat(threshold);
let passesThreshold = true;
let table = "| File | Difference |\n| --- | --- |";
Object.keys(coverageDifference).forEach((filename) => {
if (isNaN(coverageDifference[filename])) {
table += `\n| ${filename} | ${coverageDifference[filename]}`;
} else {
const roundedDifference =
Math.round(
(parseFloat(coverageDifference[filename]) + Number.EPSILON) * 100
) / 100;
passesThreshold =
passesThreshold &&
!isNaN(floatThreshold) &&
Math.sign(roundedDifference) === -1
? roundedDifference > -Math.abs(threshold)
: passesThreshold;
let differenceString = roundedDifference.toString();
if (differenceString.charAt(0) !== "-") {
differenceString = "+" + differenceString;
}
table += `\n| ${filename} | ${differenceString}%`;
}
});
return { table, passesThreshold };
}
async function postComment(octokit, context, table) {
await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: COMMENT_HEADER + table,
}
);
}
async function run() {
const pattern = core.getInput("pattern");
const myToken = core.getInput("github-token");
const octokit = github.getOctokit(myToken);
const filenamesInPR = await getFilesInPR(octokit, pattern);
if (filenamesInPR.length === 0) {
return;
}
const modifiedFiles = filenamesInPR
.filter(({ filename, status }) => status === "modified")
.map(({ filename }) => filename);
const packageManager = core.getInput("package-manager");
const path = core.getInput("path");
const prCoverageData = await getCoverageData(
"head",
path,
modifiedFiles,
packageManager
);
const baseCoverageData = await getCoverageData(
"base",
path,
modifiedFiles,
packageManager
);
const coverageDifference = {};
modifiedFiles.forEach((filename) => {
coverageDifference[filename] = (
prCoverageData[filename] - baseCoverageData[filename]
).toString();
});
filenamesInPR.forEach(({ filename, status }) => {
if (status !== "modified") {
coverageDifference[filename] = status;
}
});
const threshold = core.getInput("threshold");
const { table, passesThreshold } = getMarkdownTableAndThresholdPass(
coverageDifference,
threshold
);
const comments = await octokit.request(
"GET /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
}
);
if (comments.data.length !== 0) {
const commentToEdit = comments.data.find((comment) =>
comment.body.startsWith(COMMENT_HEADER)
);
if (commentToEdit != null) {
await octokit.request(
"PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}",
{
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: commentToEdit.id,
body: COMMENT_HEADER + table,
}
);
} else {
postComment(octokit, context, table);
}
} else {
postComment(octokit, context, table);
}
if (passesThreshold === false) {
core.setFailed(`A file does not pass the flow threshold of ${threshold}`);
}
}
try {
run();
} catch (error) {
core.setFailed(error.message);
}