-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·124 lines (115 loc) · 3.72 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
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
#!/usr/bin/env ts-node
import { join } from "path";
import { existsSync, readFileSync } from "fs";
import diff, { JsonSummary } from "coverage-diff";
import { GithubUtils } from "./utils/github-utils";
import { Options, initProgram } from "./utils/options";
import {
COMMENT_HEADER,
coverageToMarkdown,
updateComment,
} from "./utils/pr-comment";
export async function run(): Promise<void> {
const options: Options = initProgram();
const rootPath = options.basePath || process.cwd();
const coveragePath = join(rootPath, options.coveragePath);
const github = new GithubUtils(
options.repo,
options.owner,
options.githubToken,
);
const pullRequest = await github.findOpenPR(options.branch);
const shortCommitSha = options.commit.substring(0, 7);
console.info("------------ Coverage Guard -------------");
console.info(`Repo: ${options.owner}/${options.repo}`);
console.info(options.monorepoPath ? `Module: ${options.monorepoPath}` : "");
console.info(`Branch: ${options.branch} - Commit: ${shortCommitSha}`);
console.info(`Base branches: ${options.baseBranch.toString()}`);
console.info(`Coverage file: ${options.coveragePath}`);
console.info(`Base path: ${rootPath}`);
console.info("-------------------------------------------\n");
if (!existsSync(coveragePath)) {
console.error(
`[CovergeGuard] The coverage file doesn't exists: ${coveragePath}`,
);
process.exit(0);
}
const coverageSummary = parseResults(readFileSync(coveragePath, "utf-8"));
if (options.baseBranch.includes(options.branch)) {
console.info(
"[CovergeGuard] We are in a base branch, let's upload the coverage",
);
// TODO
} else {
console.info(
"[CovergeGuard] We are in a feature branch, let's compare the coverage",
);
const baseBranch =
pullRequest?.base.ref || (await github.getDefaultBranch());
const baseSummary = await baseBranchSummary(
options.repo,
baseBranch,
options.monorepoPath,
);
const difference = diff(baseSummary || {}, coverageSummary, {
customFormatter: coverageToMarkdown(
options.monorepoPath,
!!baseSummary,
baseBranch,
),
});
if (pullRequest && options.createComment) {
console.info(
"[CovergeGuard] We found an open PR for the branch, let's create a comment",
);
const comment = await github.findPreviousComment(pullRequest.number);
if (comment && comment.content) {
const newComment = updateComment(
comment.content,
difference.results,
options.monorepoPath,
);
await github.updateComment(comment.id, newComment);
} else {
await github.comment(
pullRequest.number,
COMMENT_HEADER + difference.results,
);
}
console.info(
`[CovergeGuard] Added the results to the PR :)\nLink: ${pullRequest.html_url}`,
);
} else {
console.info(
`[CovergeGuard] No open PR for branch: ${options.branch}. Printing difference here:`,
);
console.table(difference.diff.total);
}
}
}
export async function baseBranchSummary(
repo: string,
branch: string,
monorepoPath: string,
): Promise<JsonSummary | undefined> {
try {
return parseResults("{}");
} catch (err) {
console.error(
`[CovergeGuard] Couldn't find the coverage summary in the S3 bucket`,
);
console.error(
"[CovergeGuard] We can't create a comparison. We will just print the results",
);
}
}
function parseResults(resultsFile: string): JsonSummary {
try {
return JSON.parse(resultsFile);
} catch (error) {
console.error(`[CovergeGuard] Error parsing the coverage file.`);
console.error(error);
process.exit(0);
}
}
run();