1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.dupeReport = void 0;
|
4 | const diff_1 = require("diff");
|
5 | const circle_1 = require("./api/circle");
|
6 | const github_1 = require("./api/github");
|
7 | const format_report_1 = require("./lib/format-report");
|
8 | const DUPE_IDENTIFIER = "<!-- DUPE:REPORT -->";
|
9 | const jobName = "build";
|
10 | const artifactName = "duplicates-report";
|
11 | const dupeReport = async ({ owner, repo, buildNum, dryRun = false }) => {
|
12 | const circle = new circle_1.Circle({ owner, repo });
|
13 | const pullRequestURL = await circle.getPullRequest(buildNum);
|
14 |
|
15 | if (!pullRequestURL) {
|
16 | console.log(`Exiting for build ${owner}/${repo} #${buildNum} because there was no PR`);
|
17 | return;
|
18 | }
|
19 | const github = new github_1.GitHub({
|
20 | owner,
|
21 | repo,
|
22 | pullRequest: pullRequestURL,
|
23 | dryRun
|
24 | });
|
25 |
|
26 | let headReport;
|
27 | try {
|
28 | headReport = await circle.fetchArtifact({
|
29 | jobName,
|
30 | artifactName,
|
31 | buildNum
|
32 | });
|
33 | }
|
34 | catch (_a) {
|
35 | console.error("failed to read head duplicate report");
|
36 | }
|
37 | if (!headReport) {
|
38 | console.log("local report is empty");
|
39 | headReport = "";
|
40 | }
|
41 |
|
42 | let baseReport;
|
43 | try {
|
44 | const buildNum = await github.getBaseCommitBuild(jobName);
|
45 | baseReport = await circle.fetchArtifact({
|
46 | jobName,
|
47 | artifactName,
|
48 | buildNum
|
49 | });
|
50 | }
|
51 | catch (_b) {
|
52 | console.warn(`Failed to get based build, falling back to master`);
|
53 | try {
|
54 | baseReport = await circle.fetchArtifact({
|
55 | jobName,
|
56 | artifactName
|
57 | });
|
58 | }
|
59 | catch (error) {
|
60 | console.error("failed to fetch base report");
|
61 | throw error;
|
62 | }
|
63 | }
|
64 | if (!baseReport) {
|
65 | console.log("base report is empty");
|
66 | baseReport = "";
|
67 | }
|
68 |
|
69 | if (baseReport === "" && headReport === "") {
|
70 | console.log("Nothing to compare");
|
71 | return;
|
72 | }
|
73 |
|
74 | let dupeDiff = "";
|
75 | let headerDiff = false;
|
76 | let hasDiff = false;
|
77 | let change = 0;
|
78 | const masterHeader = baseReport
|
79 | .trim()
|
80 | .split("\n", 5)
|
81 | .slice(0, 4)
|
82 | .join("\n");
|
83 | const localHeader = headReport
|
84 | .trim()
|
85 | .split("\n", 5)
|
86 | .slice(0, 4)
|
87 | .join("\n");
|
88 | if (masterHeader !== localHeader) {
|
89 | headerDiff = true;
|
90 | }
|
91 | diff_1.diffLines(baseReport.trim(), headReport.trim(), {
|
92 | newlineIsToken: true
|
93 | }).forEach((line, lineNumber) => {
|
94 | let lineStart = " ";
|
95 | if (line.added) {
|
96 | hasDiff = true;
|
97 | lineStart = "+";
|
98 | }
|
99 | else if (line.removed) {
|
100 | hasDiff = true;
|
101 | lineStart = "-";
|
102 | }
|
103 | line.value.split("\n").forEach((l, lineOffset) => {
|
104 | lineStart === "+" && change++;
|
105 | lineStart === "-" && change--;
|
106 | dupeDiff += lineStart + l + "\n";
|
107 | });
|
108 | });
|
109 | dupeDiff = "```diff\n" + dupeDiff + "\n```";
|
110 |
|
111 | let existingPRComment;
|
112 | try {
|
113 | existingPRComment = await github.checkForPRCommentWithString(DUPE_IDENTIFIER);
|
114 | }
|
115 | catch (_c) {
|
116 | console.error("failed when trying to check to see if message existed on pr");
|
117 | }
|
118 |
|
119 | console.log("diff info", { hasDiff, headerDiff, change });
|
120 | if (!hasDiff || (change === 0 && !headerDiff)) {
|
121 | if (existingPRComment) {
|
122 | try {
|
123 | await github.deleteComment(existingPRComment.id);
|
124 | }
|
125 | catch (error) {
|
126 | console.error(`Failed to delete comment ${existingPRComment.id} in PR ${pullRequestURL}`, error);
|
127 | }
|
128 | }
|
129 | console.log(`No change in build #${buildNum} in PR ${pullRequestURL}, exiting...`);
|
130 | return;
|
131 | }
|
132 |
|
133 | let assignees = [];
|
134 | try {
|
135 | assignees = await github.getAssignees();
|
136 | }
|
137 | catch (error) {
|
138 | console.error("failed to fetch assignees", error.message);
|
139 | }
|
140 |
|
141 | let prComment = `${DUPE_IDENTIFIER}\n\n`;
|
142 | prComment += format_report_1.formatReport(headReport, dupeDiff, change, assignees);
|
143 |
|
144 | try {
|
145 | if (existingPRComment) {
|
146 | await github.updateComment(existingPRComment.id, prComment);
|
147 | }
|
148 | else {
|
149 | await github.createComment(prComment);
|
150 | }
|
151 | }
|
152 | catch (error) {
|
153 | console.error("failed to create dupe report comment with code", error.status);
|
154 | }
|
155 | };
|
156 | exports.dupeReport = dupeReport;
|