@code-pushup/ci 0.56.0 → 0.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -8
- package/package.json +10 -8
- package/src/index.d.ts +4 -4
- package/src/index.js +3 -0
- package/src/index.js.map +1 -0
- package/src/lib/cli/commands/collect.d.ts +2 -3
- package/src/lib/cli/commands/collect.js +16 -0
- package/src/lib/cli/commands/collect.js.map +1 -0
- package/src/lib/cli/commands/compare.d.ts +2 -3
- package/src/lib/cli/commands/compare.js +20 -0
- package/src/lib/cli/commands/compare.js.map +1 -0
- package/src/lib/cli/commands/merge-diffs.d.ts +2 -3
- package/src/lib/cli/commands/merge-diffs.js +23 -0
- package/src/lib/cli/commands/merge-diffs.js.map +1 -0
- package/src/lib/cli/commands/print-config.d.ts +2 -2
- package/src/lib/cli/commands/print-config.js +26 -0
- package/src/lib/cli/commands/print-config.js.map +1 -0
- package/src/lib/cli/context.d.ts +3 -5
- package/src/lib/cli/context.js +9 -0
- package/src/lib/cli/context.js.map +1 -0
- package/src/lib/cli/index.d.ts +6 -6
- package/src/lib/cli/index.js +7 -0
- package/src/lib/cli/index.js.map +1 -0
- package/src/lib/cli/persist.d.ts +5 -26
- package/src/lib/cli/persist.js +24 -0
- package/src/lib/cli/persist.js.map +1 -0
- package/src/lib/comment.d.ts +1 -1
- package/src/lib/comment.js +29 -0
- package/src/lib/comment.js.map +1 -0
- package/src/lib/constants.d.ts +1 -1
- package/src/lib/constants.js +15 -0
- package/src/lib/constants.js.map +1 -0
- package/src/lib/git.js +99 -0
- package/src/lib/git.js.map +1 -0
- package/src/lib/issues.d.ts +1 -1
- package/src/lib/issues.js +96 -0
- package/src/lib/issues.js.map +1 -0
- package/src/lib/models.d.ts +9 -11
- package/src/lib/models.js +2 -0
- package/src/lib/models.js.map +1 -0
- package/src/lib/monorepo/detect-tool.d.ts +1 -1
- package/src/lib/monorepo/detect-tool.js +11 -0
- package/src/lib/monorepo/detect-tool.js.map +1 -0
- package/src/lib/monorepo/handlers/index.d.ts +1 -1
- package/src/lib/monorepo/handlers/index.js +20 -0
- package/src/lib/monorepo/handlers/index.js.map +1 -0
- package/src/lib/monorepo/handlers/npm.d.ts +1 -1
- package/src/lib/monorepo/handlers/npm.js +29 -0
- package/src/lib/monorepo/handlers/npm.js.map +1 -0
- package/src/lib/monorepo/handlers/nx.d.ts +1 -1
- package/src/lib/monorepo/handlers/nx.js +62 -0
- package/src/lib/monorepo/handlers/nx.js.map +1 -0
- package/src/lib/monorepo/handlers/pnpm.d.ts +1 -1
- package/src/lib/monorepo/handlers/pnpm.js +47 -0
- package/src/lib/monorepo/handlers/pnpm.js.map +1 -0
- package/src/lib/monorepo/handlers/turbo.d.ts +1 -1
- package/src/lib/monorepo/handlers/turbo.js +48 -0
- package/src/lib/monorepo/handlers/turbo.js.map +1 -0
- package/src/lib/monorepo/handlers/yarn.d.ts +1 -1
- package/src/lib/monorepo/handlers/yarn.js +44 -0
- package/src/lib/monorepo/handlers/yarn.js.map +1 -0
- package/src/lib/monorepo/index.d.ts +2 -2
- package/src/lib/monorepo/index.js +3 -0
- package/src/lib/monorepo/index.js.map +1 -0
- package/src/lib/monorepo/list-projects.d.ts +9 -3
- package/src/lib/monorepo/list-projects.js +99 -0
- package/src/lib/monorepo/list-projects.js.map +1 -0
- package/src/lib/monorepo/packages.js +50 -0
- package/src/lib/monorepo/packages.js.map +1 -0
- package/src/lib/monorepo/tools.d.ts +6 -0
- package/src/lib/monorepo/tools.js +5 -0
- package/src/lib/monorepo/tools.js.map +1 -0
- package/src/lib/run-monorepo.d.ts +3 -0
- package/src/lib/run-monorepo.js +141 -0
- package/src/lib/run-monorepo.js.map +1 -0
- package/src/lib/run-standalone.d.ts +3 -0
- package/src/lib/run-standalone.js +19 -0
- package/src/lib/run-standalone.js.map +1 -0
- package/src/lib/run-utils.d.ts +45 -0
- package/src/lib/run-utils.js +158 -0
- package/src/lib/run-utils.js.map +1 -0
- package/src/lib/run.d.ts +1 -1
- package/src/lib/run.js +24 -0
- package/src/lib/run.js.map +1 -0
- package/index.js +0 -1742
package/index.js
DELETED
|
@@ -1,1742 +0,0 @@
|
|
|
1
|
-
// packages/ci/src/lib/monorepo/list-projects.ts
|
|
2
|
-
import { glob as glob2 } from "glob";
|
|
3
|
-
import { join as join7 } from "node:path";
|
|
4
|
-
|
|
5
|
-
// packages/ci/src/lib/monorepo/handlers/npm.ts
|
|
6
|
-
import { join as join2 } from "node:path";
|
|
7
|
-
|
|
8
|
-
// packages/models/src/lib/implementation/schemas.ts
|
|
9
|
-
import { MATERIAL_ICONS } from "vscode-material-icons";
|
|
10
|
-
import { z } from "zod";
|
|
11
|
-
|
|
12
|
-
// packages/models/src/lib/implementation/limits.ts
|
|
13
|
-
var MAX_SLUG_LENGTH = 128;
|
|
14
|
-
var MAX_TITLE_LENGTH = 256;
|
|
15
|
-
var MAX_DESCRIPTION_LENGTH = 65536;
|
|
16
|
-
var MAX_ISSUE_MESSAGE_LENGTH = 1024;
|
|
17
|
-
|
|
18
|
-
// packages/models/src/lib/implementation/utils.ts
|
|
19
|
-
var slugRegex = /^[a-z\d]+(?:-[a-z\d]+)*$/;
|
|
20
|
-
var filenameRegex = /^(?!.*[ \\/:*?"<>|]).+$/;
|
|
21
|
-
function hasDuplicateStrings(strings) {
|
|
22
|
-
const sortedStrings = strings.toSorted();
|
|
23
|
-
const duplStrings = sortedStrings.filter(
|
|
24
|
-
(item, index) => index !== 0 && item === sortedStrings[index - 1]
|
|
25
|
-
);
|
|
26
|
-
return duplStrings.length === 0 ? false : [...new Set(duplStrings)];
|
|
27
|
-
}
|
|
28
|
-
function hasMissingStrings(toCheck, existing) {
|
|
29
|
-
const nonExisting = toCheck.filter((s) => !existing.includes(s));
|
|
30
|
-
return nonExisting.length === 0 ? false : nonExisting;
|
|
31
|
-
}
|
|
32
|
-
function errorItems(items, transform = (itemArr) => itemArr.join(", ")) {
|
|
33
|
-
return transform(items || []);
|
|
34
|
-
}
|
|
35
|
-
function exists(value) {
|
|
36
|
-
return value != null;
|
|
37
|
-
}
|
|
38
|
-
function getMissingRefsForCategories(categories, plugins) {
|
|
39
|
-
if (!categories || categories.length === 0) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
const auditRefsFromCategory = categories.flatMap(
|
|
43
|
-
({ refs }) => refs.filter(({ type }) => type === "audit").map(({ plugin, slug }) => `${plugin}/${slug}`)
|
|
44
|
-
);
|
|
45
|
-
const auditRefsFromPlugins = plugins.flatMap(
|
|
46
|
-
({ audits, slug: pluginSlug }) => audits.map(({ slug }) => `${pluginSlug}/${slug}`)
|
|
47
|
-
);
|
|
48
|
-
const missingAuditRefs = hasMissingStrings(
|
|
49
|
-
auditRefsFromCategory,
|
|
50
|
-
auditRefsFromPlugins
|
|
51
|
-
);
|
|
52
|
-
const groupRefsFromCategory = categories.flatMap(
|
|
53
|
-
({ refs }) => refs.filter(({ type }) => type === "group").map(({ plugin, slug }) => `${plugin}#${slug} (group)`)
|
|
54
|
-
);
|
|
55
|
-
const groupRefsFromPlugins = plugins.flatMap(
|
|
56
|
-
({ groups, slug: pluginSlug }) => Array.isArray(groups) ? groups.map(({ slug }) => `${pluginSlug}#${slug} (group)`) : []
|
|
57
|
-
);
|
|
58
|
-
const missingGroupRefs = hasMissingStrings(
|
|
59
|
-
groupRefsFromCategory,
|
|
60
|
-
groupRefsFromPlugins
|
|
61
|
-
);
|
|
62
|
-
const missingRefs = [missingAuditRefs, missingGroupRefs].filter((refs) => Array.isArray(refs) && refs.length > 0).flat();
|
|
63
|
-
return missingRefs.length > 0 ? missingRefs : false;
|
|
64
|
-
}
|
|
65
|
-
function missingRefsForCategoriesErrorMsg(categories, plugins) {
|
|
66
|
-
const missingRefs = getMissingRefsForCategories(categories, plugins);
|
|
67
|
-
return `The following category references need to point to an audit or group: ${errorItems(
|
|
68
|
-
missingRefs
|
|
69
|
-
)}`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// packages/models/src/lib/implementation/schemas.ts
|
|
73
|
-
var tableCellValueSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]).default(null);
|
|
74
|
-
function executionMetaSchema(options = {
|
|
75
|
-
descriptionDate: "Execution start date and time",
|
|
76
|
-
descriptionDuration: "Execution duration in ms"
|
|
77
|
-
}) {
|
|
78
|
-
return z.object({
|
|
79
|
-
date: z.string({ description: options.descriptionDate }),
|
|
80
|
-
duration: z.number({ description: options.descriptionDuration })
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
var slugSchema = z.string({ description: "Unique ID (human-readable, URL-safe)" }).regex(slugRegex, {
|
|
84
|
-
message: "The slug has to follow the pattern [0-9a-z] followed by multiple optional groups of -[0-9a-z]. e.g. my-slug"
|
|
85
|
-
}).max(MAX_SLUG_LENGTH, {
|
|
86
|
-
message: `slug can be max ${MAX_SLUG_LENGTH} characters long`
|
|
87
|
-
});
|
|
88
|
-
var descriptionSchema = z.string({ description: "Description (markdown)" }).max(MAX_DESCRIPTION_LENGTH).optional();
|
|
89
|
-
var urlSchema = z.string().url();
|
|
90
|
-
var docsUrlSchema = urlSchema.optional().or(z.literal("")).describe("Documentation site");
|
|
91
|
-
var titleSchema = z.string({ description: "Descriptive name" }).max(MAX_TITLE_LENGTH);
|
|
92
|
-
var scoreSchema = z.number({
|
|
93
|
-
description: "Value between 0 and 1"
|
|
94
|
-
}).min(0).max(1);
|
|
95
|
-
function metaSchema(options) {
|
|
96
|
-
const {
|
|
97
|
-
descriptionDescription,
|
|
98
|
-
titleDescription,
|
|
99
|
-
docsUrlDescription,
|
|
100
|
-
description
|
|
101
|
-
} = options ?? {};
|
|
102
|
-
return z.object(
|
|
103
|
-
{
|
|
104
|
-
title: titleDescription ? titleSchema.describe(titleDescription) : titleSchema,
|
|
105
|
-
description: descriptionDescription ? descriptionSchema.describe(descriptionDescription) : descriptionSchema,
|
|
106
|
-
docsUrl: docsUrlDescription ? docsUrlSchema.describe(docsUrlDescription) : docsUrlSchema
|
|
107
|
-
},
|
|
108
|
-
{ description }
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
var filePathSchema = z.string().trim().min(1, { message: "path is invalid" });
|
|
112
|
-
var fileNameSchema = z.string().trim().regex(filenameRegex, {
|
|
113
|
-
message: `The filename has to be valid`
|
|
114
|
-
}).min(1, { message: "file name is invalid" });
|
|
115
|
-
var positiveIntSchema = z.number().int().positive();
|
|
116
|
-
var nonnegativeNumberSchema = z.number().nonnegative();
|
|
117
|
-
function packageVersionSchema(options) {
|
|
118
|
-
const { versionDescription = "NPM version of the package", required } = options ?? {};
|
|
119
|
-
const packageSchema = z.string({ description: "NPM package name" });
|
|
120
|
-
const versionSchema = z.string({ description: versionDescription });
|
|
121
|
-
return z.object(
|
|
122
|
-
{
|
|
123
|
-
packageName: required ? packageSchema : packageSchema.optional(),
|
|
124
|
-
version: required ? versionSchema : versionSchema.optional()
|
|
125
|
-
},
|
|
126
|
-
{ description: "NPM package name and version of a published package" }
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
var weightSchema = nonnegativeNumberSchema.describe(
|
|
130
|
-
"Coefficient for the given score (use weight 0 if only for display)"
|
|
131
|
-
);
|
|
132
|
-
function weightedRefSchema(description, slugDescription) {
|
|
133
|
-
return z.object(
|
|
134
|
-
{
|
|
135
|
-
slug: slugSchema.describe(slugDescription),
|
|
136
|
-
weight: weightSchema.describe("Weight used to calculate score")
|
|
137
|
-
},
|
|
138
|
-
{ description }
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
function scorableSchema(description, refSchema, duplicateCheckFn, duplicateMessageFn) {
|
|
142
|
-
return z.object(
|
|
143
|
-
{
|
|
144
|
-
slug: slugSchema.describe('Human-readable unique ID, e.g. "performance"'),
|
|
145
|
-
refs: z.array(refSchema).min(1).refine(
|
|
146
|
-
(refs) => !duplicateCheckFn(refs),
|
|
147
|
-
(refs) => ({
|
|
148
|
-
message: duplicateMessageFn(refs)
|
|
149
|
-
})
|
|
150
|
-
).refine(hasNonZeroWeightedRef, () => ({
|
|
151
|
-
message: "In a category there has to be at least one ref with weight > 0"
|
|
152
|
-
}))
|
|
153
|
-
},
|
|
154
|
-
{ description }
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
var materialIconSchema = z.enum(MATERIAL_ICONS, {
|
|
158
|
-
description: "Icon from VSCode Material Icons extension"
|
|
159
|
-
});
|
|
160
|
-
function hasNonZeroWeightedRef(refs) {
|
|
161
|
-
return refs.reduce((acc, { weight }) => weight + acc, 0) !== 0;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// packages/models/src/lib/source.ts
|
|
165
|
-
import { z as z2 } from "zod";
|
|
166
|
-
var sourceFileLocationSchema = z2.object(
|
|
167
|
-
{
|
|
168
|
-
file: filePathSchema.describe("Relative path to source file in Git repo"),
|
|
169
|
-
position: z2.object(
|
|
170
|
-
{
|
|
171
|
-
startLine: positiveIntSchema.describe("Start line"),
|
|
172
|
-
startColumn: positiveIntSchema.describe("Start column").optional(),
|
|
173
|
-
endLine: positiveIntSchema.describe("End line").optional(),
|
|
174
|
-
endColumn: positiveIntSchema.describe("End column").optional()
|
|
175
|
-
},
|
|
176
|
-
{ description: "Location in file" }
|
|
177
|
-
).optional()
|
|
178
|
-
},
|
|
179
|
-
{ description: "Source file location" }
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
// packages/models/src/lib/audit.ts
|
|
183
|
-
import { z as z3 } from "zod";
|
|
184
|
-
var auditSchema = z3.object({
|
|
185
|
-
slug: slugSchema.describe("ID (unique within plugin)")
|
|
186
|
-
}).merge(
|
|
187
|
-
metaSchema({
|
|
188
|
-
titleDescription: "Descriptive name",
|
|
189
|
-
descriptionDescription: "Description (markdown)",
|
|
190
|
-
docsUrlDescription: "Link to documentation (rationale)",
|
|
191
|
-
description: "List of scorable metrics for the given plugin"
|
|
192
|
-
})
|
|
193
|
-
);
|
|
194
|
-
var pluginAuditsSchema = z3.array(auditSchema, {
|
|
195
|
-
description: "List of audits maintained in a plugin"
|
|
196
|
-
}).min(1).refine(
|
|
197
|
-
(auditMetadata) => !getDuplicateSlugsInAudits(auditMetadata),
|
|
198
|
-
(auditMetadata) => ({
|
|
199
|
-
message: duplicateSlugsInAuditsErrorMsg(auditMetadata)
|
|
200
|
-
})
|
|
201
|
-
);
|
|
202
|
-
function duplicateSlugsInAuditsErrorMsg(audits) {
|
|
203
|
-
const duplicateRefs = getDuplicateSlugsInAudits(audits);
|
|
204
|
-
return `In plugin audits the following slugs are not unique: ${errorItems(
|
|
205
|
-
duplicateRefs
|
|
206
|
-
)}`;
|
|
207
|
-
}
|
|
208
|
-
function getDuplicateSlugsInAudits(audits) {
|
|
209
|
-
return hasDuplicateStrings(audits.map(({ slug }) => slug));
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// packages/models/src/lib/audit-output.ts
|
|
213
|
-
import { z as z6 } from "zod";
|
|
214
|
-
|
|
215
|
-
// packages/models/src/lib/issue.ts
|
|
216
|
-
import { z as z4 } from "zod";
|
|
217
|
-
var issueSeveritySchema = z4.enum(["info", "warning", "error"], {
|
|
218
|
-
description: "Severity level"
|
|
219
|
-
});
|
|
220
|
-
var issueSchema = z4.object(
|
|
221
|
-
{
|
|
222
|
-
message: z4.string({ description: "Descriptive error message" }).max(MAX_ISSUE_MESSAGE_LENGTH),
|
|
223
|
-
severity: issueSeveritySchema,
|
|
224
|
-
source: sourceFileLocationSchema.optional()
|
|
225
|
-
},
|
|
226
|
-
{ description: "Issue information" }
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
// packages/models/src/lib/table.ts
|
|
230
|
-
import { z as z5 } from "zod";
|
|
231
|
-
var tableAlignmentSchema = z5.enum(["left", "center", "right"], {
|
|
232
|
-
description: "Cell alignment"
|
|
233
|
-
});
|
|
234
|
-
var tableColumnObjectSchema = z5.object({
|
|
235
|
-
key: z5.string(),
|
|
236
|
-
label: z5.string().optional(),
|
|
237
|
-
align: tableAlignmentSchema.optional()
|
|
238
|
-
});
|
|
239
|
-
var tableRowObjectSchema = z5.record(tableCellValueSchema, {
|
|
240
|
-
description: "Object row"
|
|
241
|
-
});
|
|
242
|
-
var tableRowPrimitiveSchema = z5.array(tableCellValueSchema, {
|
|
243
|
-
description: "Primitive row"
|
|
244
|
-
});
|
|
245
|
-
var tableSharedSchema = z5.object({
|
|
246
|
-
title: z5.string().optional().describe("Display title for table")
|
|
247
|
-
});
|
|
248
|
-
var tablePrimitiveSchema = tableSharedSchema.merge(
|
|
249
|
-
z5.object(
|
|
250
|
-
{
|
|
251
|
-
columns: z5.array(tableAlignmentSchema).optional(),
|
|
252
|
-
rows: z5.array(tableRowPrimitiveSchema)
|
|
253
|
-
},
|
|
254
|
-
{ description: "Table with primitive rows and optional alignment columns" }
|
|
255
|
-
)
|
|
256
|
-
);
|
|
257
|
-
var tableObjectSchema = tableSharedSchema.merge(
|
|
258
|
-
z5.object(
|
|
259
|
-
{
|
|
260
|
-
columns: z5.union([
|
|
261
|
-
z5.array(tableAlignmentSchema),
|
|
262
|
-
z5.array(tableColumnObjectSchema)
|
|
263
|
-
]).optional(),
|
|
264
|
-
rows: z5.array(tableRowObjectSchema)
|
|
265
|
-
},
|
|
266
|
-
{
|
|
267
|
-
description: "Table with object rows and optional alignment or object columns"
|
|
268
|
-
}
|
|
269
|
-
)
|
|
270
|
-
);
|
|
271
|
-
var tableSchema = (description = "Table information") => z5.union([tablePrimitiveSchema, tableObjectSchema], { description });
|
|
272
|
-
|
|
273
|
-
// packages/models/src/lib/audit-output.ts
|
|
274
|
-
var auditValueSchema = nonnegativeNumberSchema.describe("Raw numeric value");
|
|
275
|
-
var auditDisplayValueSchema = z6.string({ description: "Formatted value (e.g. '0.9 s', '2.1 MB')" }).optional();
|
|
276
|
-
var auditDetailsSchema = z6.object(
|
|
277
|
-
{
|
|
278
|
-
issues: z6.array(issueSchema, { description: "List of findings" }).optional(),
|
|
279
|
-
table: tableSchema("Table of related findings").optional()
|
|
280
|
-
},
|
|
281
|
-
{ description: "Detailed information" }
|
|
282
|
-
);
|
|
283
|
-
var auditOutputSchema = z6.object(
|
|
284
|
-
{
|
|
285
|
-
slug: slugSchema.describe("Reference to audit"),
|
|
286
|
-
displayValue: auditDisplayValueSchema,
|
|
287
|
-
value: auditValueSchema,
|
|
288
|
-
score: scoreSchema,
|
|
289
|
-
details: auditDetailsSchema.optional()
|
|
290
|
-
},
|
|
291
|
-
{ description: "Audit information" }
|
|
292
|
-
);
|
|
293
|
-
var auditOutputsSchema = z6.array(auditOutputSchema, {
|
|
294
|
-
description: "List of JSON formatted audit output emitted by the runner process of a plugin"
|
|
295
|
-
}).refine(
|
|
296
|
-
(audits) => !getDuplicateSlugsInAudits2(audits),
|
|
297
|
-
(audits) => ({ message: duplicateSlugsInAuditsErrorMsg2(audits) })
|
|
298
|
-
);
|
|
299
|
-
function duplicateSlugsInAuditsErrorMsg2(audits) {
|
|
300
|
-
const duplicateRefs = getDuplicateSlugsInAudits2(audits);
|
|
301
|
-
return `In plugin audits the slugs are not unique: ${errorItems(
|
|
302
|
-
duplicateRefs
|
|
303
|
-
)}`;
|
|
304
|
-
}
|
|
305
|
-
function getDuplicateSlugsInAudits2(audits) {
|
|
306
|
-
return hasDuplicateStrings(audits.map(({ slug }) => slug));
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// packages/models/src/lib/category-config.ts
|
|
310
|
-
import { z as z7 } from "zod";
|
|
311
|
-
var categoryRefSchema = weightedRefSchema(
|
|
312
|
-
"Weighted references to audits and/or groups for the category",
|
|
313
|
-
"Slug of an audit or group (depending on `type`)"
|
|
314
|
-
).merge(
|
|
315
|
-
z7.object({
|
|
316
|
-
type: z7.enum(["audit", "group"], {
|
|
317
|
-
description: "Discriminant for reference kind, affects where `slug` is looked up"
|
|
318
|
-
}),
|
|
319
|
-
plugin: slugSchema.describe(
|
|
320
|
-
"Plugin slug (plugin should contain referenced audit or group)"
|
|
321
|
-
)
|
|
322
|
-
})
|
|
323
|
-
);
|
|
324
|
-
var categoryConfigSchema = scorableSchema(
|
|
325
|
-
"Category with a score calculated from audits and groups from various plugins",
|
|
326
|
-
categoryRefSchema,
|
|
327
|
-
getDuplicateRefsInCategoryMetrics,
|
|
328
|
-
duplicateRefsInCategoryMetricsErrorMsg
|
|
329
|
-
).merge(
|
|
330
|
-
metaSchema({
|
|
331
|
-
titleDescription: "Category Title",
|
|
332
|
-
docsUrlDescription: "Category docs URL",
|
|
333
|
-
descriptionDescription: "Category description",
|
|
334
|
-
description: "Meta info for category"
|
|
335
|
-
})
|
|
336
|
-
).merge(
|
|
337
|
-
z7.object({
|
|
338
|
-
isBinary: z7.boolean({
|
|
339
|
-
description: 'Is this a binary category (i.e. only a perfect score considered a "pass")?'
|
|
340
|
-
}).optional()
|
|
341
|
-
})
|
|
342
|
-
);
|
|
343
|
-
function duplicateRefsInCategoryMetricsErrorMsg(metrics) {
|
|
344
|
-
const duplicateRefs = getDuplicateRefsInCategoryMetrics(metrics);
|
|
345
|
-
return `In the categories, the following audit or group refs are duplicates: ${errorItems(
|
|
346
|
-
duplicateRefs
|
|
347
|
-
)}`;
|
|
348
|
-
}
|
|
349
|
-
function getDuplicateRefsInCategoryMetrics(metrics) {
|
|
350
|
-
return hasDuplicateStrings(
|
|
351
|
-
metrics.map(({ slug, type, plugin }) => `${type} :: ${plugin} / ${slug}`)
|
|
352
|
-
);
|
|
353
|
-
}
|
|
354
|
-
var categoriesSchema = z7.array(categoryConfigSchema, {
|
|
355
|
-
description: "Categorization of individual audits"
|
|
356
|
-
}).refine(
|
|
357
|
-
(categoryCfg) => !getDuplicateSlugCategories(categoryCfg),
|
|
358
|
-
(categoryCfg) => ({
|
|
359
|
-
message: duplicateSlugCategoriesErrorMsg(categoryCfg)
|
|
360
|
-
})
|
|
361
|
-
);
|
|
362
|
-
function duplicateSlugCategoriesErrorMsg(categories) {
|
|
363
|
-
const duplicateStringSlugs = getDuplicateSlugCategories(categories);
|
|
364
|
-
return `In the categories, the following slugs are duplicated: ${errorItems(
|
|
365
|
-
duplicateStringSlugs
|
|
366
|
-
)}`;
|
|
367
|
-
}
|
|
368
|
-
function getDuplicateSlugCategories(categories) {
|
|
369
|
-
return hasDuplicateStrings(categories.map(({ slug }) => slug));
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
// packages/models/src/lib/commit.ts
|
|
373
|
-
import { z as z8 } from "zod";
|
|
374
|
-
var commitSchema = z8.object(
|
|
375
|
-
{
|
|
376
|
-
hash: z8.string({ description: "Commit SHA (full)" }).regex(
|
|
377
|
-
/^[\da-f]{40}$/,
|
|
378
|
-
"Commit SHA should be a 40-character hexadecimal string"
|
|
379
|
-
),
|
|
380
|
-
message: z8.string({ description: "Commit message" }),
|
|
381
|
-
date: z8.coerce.date({
|
|
382
|
-
description: "Date and time when commit was authored"
|
|
383
|
-
}),
|
|
384
|
-
author: z8.string({
|
|
385
|
-
description: "Commit author name"
|
|
386
|
-
}).trim()
|
|
387
|
-
},
|
|
388
|
-
{ description: "Git commit" }
|
|
389
|
-
);
|
|
390
|
-
|
|
391
|
-
// packages/models/src/lib/core-config.ts
|
|
392
|
-
import { z as z14 } from "zod";
|
|
393
|
-
|
|
394
|
-
// packages/models/src/lib/persist-config.ts
|
|
395
|
-
import { z as z9 } from "zod";
|
|
396
|
-
var formatSchema = z9.enum(["json", "md"]);
|
|
397
|
-
var persistConfigSchema = z9.object({
|
|
398
|
-
outputDir: filePathSchema.describe("Artifacts folder").optional(),
|
|
399
|
-
filename: fileNameSchema.describe("Artifacts file name (without extension)").optional(),
|
|
400
|
-
format: z9.array(formatSchema).optional()
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
// packages/models/src/lib/plugin-config.ts
|
|
404
|
-
import { z as z12 } from "zod";
|
|
405
|
-
|
|
406
|
-
// packages/models/src/lib/group.ts
|
|
407
|
-
import { z as z10 } from "zod";
|
|
408
|
-
var groupRefSchema = weightedRefSchema(
|
|
409
|
-
"Weighted reference to a group",
|
|
410
|
-
"Reference slug to a group within this plugin (e.g. 'max-lines')"
|
|
411
|
-
);
|
|
412
|
-
var groupMetaSchema = metaSchema({
|
|
413
|
-
titleDescription: "Descriptive name for the group",
|
|
414
|
-
descriptionDescription: "Description of the group (markdown)",
|
|
415
|
-
docsUrlDescription: "Group documentation site",
|
|
416
|
-
description: "Group metadata"
|
|
417
|
-
});
|
|
418
|
-
var groupSchema = scorableSchema(
|
|
419
|
-
'A group aggregates a set of audits into a single score which can be referenced from a category. E.g. the group slug "performance" groups audits and can be referenced in a category',
|
|
420
|
-
groupRefSchema,
|
|
421
|
-
getDuplicateRefsInGroups,
|
|
422
|
-
duplicateRefsInGroupsErrorMsg
|
|
423
|
-
).merge(groupMetaSchema);
|
|
424
|
-
var groupsSchema = z10.array(groupSchema, {
|
|
425
|
-
description: "List of groups"
|
|
426
|
-
}).optional().refine(
|
|
427
|
-
(groups) => !getDuplicateSlugsInGroups(groups),
|
|
428
|
-
(groups) => ({
|
|
429
|
-
message: duplicateSlugsInGroupsErrorMsg(groups)
|
|
430
|
-
})
|
|
431
|
-
);
|
|
432
|
-
function duplicateRefsInGroupsErrorMsg(groups) {
|
|
433
|
-
const duplicateRefs = getDuplicateRefsInGroups(groups);
|
|
434
|
-
return `In plugin groups the following references are not unique: ${errorItems(
|
|
435
|
-
duplicateRefs
|
|
436
|
-
)}`;
|
|
437
|
-
}
|
|
438
|
-
function getDuplicateRefsInGroups(groups) {
|
|
439
|
-
return hasDuplicateStrings(groups.map(({ slug: ref }) => ref).filter(exists));
|
|
440
|
-
}
|
|
441
|
-
function duplicateSlugsInGroupsErrorMsg(groups) {
|
|
442
|
-
const duplicateRefs = getDuplicateSlugsInGroups(groups);
|
|
443
|
-
return `In groups the following slugs are not unique: ${errorItems(
|
|
444
|
-
duplicateRefs
|
|
445
|
-
)}`;
|
|
446
|
-
}
|
|
447
|
-
function getDuplicateSlugsInGroups(groups) {
|
|
448
|
-
return Array.isArray(groups) ? hasDuplicateStrings(groups.map(({ slug }) => slug)) : false;
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// packages/models/src/lib/runner-config.ts
|
|
452
|
-
import { z as z11 } from "zod";
|
|
453
|
-
var outputTransformSchema = z11.function().args(z11.unknown()).returns(z11.union([auditOutputsSchema, z11.promise(auditOutputsSchema)]));
|
|
454
|
-
var runnerConfigSchema = z11.object(
|
|
455
|
-
{
|
|
456
|
-
command: z11.string({
|
|
457
|
-
description: "Shell command to execute"
|
|
458
|
-
}),
|
|
459
|
-
args: z11.array(z11.string({ description: "Command arguments" })).optional(),
|
|
460
|
-
outputFile: filePathSchema.describe("Output path"),
|
|
461
|
-
outputTransform: outputTransformSchema.optional()
|
|
462
|
-
},
|
|
463
|
-
{
|
|
464
|
-
description: "How to execute runner"
|
|
465
|
-
}
|
|
466
|
-
);
|
|
467
|
-
var onProgressSchema = z11.function().args(z11.unknown()).returns(z11.void());
|
|
468
|
-
var runnerFunctionSchema = z11.function().args(onProgressSchema.optional()).returns(z11.union([auditOutputsSchema, z11.promise(auditOutputsSchema)]));
|
|
469
|
-
|
|
470
|
-
// packages/models/src/lib/plugin-config.ts
|
|
471
|
-
var pluginMetaSchema = packageVersionSchema().merge(
|
|
472
|
-
metaSchema({
|
|
473
|
-
titleDescription: "Descriptive name",
|
|
474
|
-
descriptionDescription: "Description (markdown)",
|
|
475
|
-
docsUrlDescription: "Plugin documentation site",
|
|
476
|
-
description: "Plugin metadata"
|
|
477
|
-
})
|
|
478
|
-
).merge(
|
|
479
|
-
z12.object({
|
|
480
|
-
slug: slugSchema.describe("Unique plugin slug within core config"),
|
|
481
|
-
icon: materialIconSchema
|
|
482
|
-
})
|
|
483
|
-
);
|
|
484
|
-
var pluginDataSchema = z12.object({
|
|
485
|
-
runner: z12.union([runnerConfigSchema, runnerFunctionSchema]),
|
|
486
|
-
audits: pluginAuditsSchema,
|
|
487
|
-
groups: groupsSchema
|
|
488
|
-
});
|
|
489
|
-
var pluginConfigSchema = pluginMetaSchema.merge(pluginDataSchema).refine(
|
|
490
|
-
(pluginCfg) => !getMissingRefsFromGroups(pluginCfg),
|
|
491
|
-
(pluginCfg) => ({
|
|
492
|
-
message: missingRefsFromGroupsErrorMsg(pluginCfg)
|
|
493
|
-
})
|
|
494
|
-
);
|
|
495
|
-
function missingRefsFromGroupsErrorMsg(pluginCfg) {
|
|
496
|
-
const missingRefs = getMissingRefsFromGroups(pluginCfg);
|
|
497
|
-
return `The following group references need to point to an existing audit in this plugin config: ${errorItems(
|
|
498
|
-
missingRefs
|
|
499
|
-
)}`;
|
|
500
|
-
}
|
|
501
|
-
function getMissingRefsFromGroups(pluginCfg) {
|
|
502
|
-
return hasMissingStrings(
|
|
503
|
-
pluginCfg.groups?.flatMap(
|
|
504
|
-
({ refs: audits }) => audits.map(({ slug: ref }) => ref)
|
|
505
|
-
) ?? [],
|
|
506
|
-
pluginCfg.audits.map(({ slug }) => slug)
|
|
507
|
-
);
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
// packages/models/src/lib/upload-config.ts
|
|
511
|
-
import { z as z13 } from "zod";
|
|
512
|
-
var uploadConfigSchema = z13.object({
|
|
513
|
-
server: urlSchema.describe("URL of deployed portal API"),
|
|
514
|
-
apiKey: z13.string({
|
|
515
|
-
description: "API key with write access to portal (use `process.env` for security)"
|
|
516
|
-
}),
|
|
517
|
-
organization: slugSchema.describe(
|
|
518
|
-
"Organization slug from Code PushUp portal"
|
|
519
|
-
),
|
|
520
|
-
project: slugSchema.describe("Project slug from Code PushUp portal"),
|
|
521
|
-
timeout: z13.number({ description: "Request timeout in minutes (default is 5)" }).positive().int().optional()
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
// packages/models/src/lib/core-config.ts
|
|
525
|
-
var unrefinedCoreConfigSchema = z14.object({
|
|
526
|
-
plugins: z14.array(pluginConfigSchema, {
|
|
527
|
-
description: "List of plugins to be used (official, community-provided, or custom)"
|
|
528
|
-
}).min(1),
|
|
529
|
-
/** portal configuration for persisting results */
|
|
530
|
-
persist: persistConfigSchema.optional(),
|
|
531
|
-
/** portal configuration for uploading results */
|
|
532
|
-
upload: uploadConfigSchema.optional(),
|
|
533
|
-
categories: categoriesSchema.optional()
|
|
534
|
-
});
|
|
535
|
-
var coreConfigSchema = refineCoreConfig(unrefinedCoreConfigSchema);
|
|
536
|
-
function refineCoreConfig(schema) {
|
|
537
|
-
return schema.refine(
|
|
538
|
-
({ categories, plugins }) => !getMissingRefsForCategories(categories, plugins),
|
|
539
|
-
({ categories, plugins }) => ({
|
|
540
|
-
message: missingRefsForCategoriesErrorMsg(categories, plugins)
|
|
541
|
-
})
|
|
542
|
-
);
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
// packages/models/src/lib/implementation/constants.ts
|
|
546
|
-
var DEFAULT_PERSIST_OUTPUT_DIR = ".code-pushup";
|
|
547
|
-
var DEFAULT_PERSIST_FILENAME = "report";
|
|
548
|
-
var DEFAULT_PERSIST_FORMAT = ["json", "md"];
|
|
549
|
-
|
|
550
|
-
// packages/models/src/lib/report.ts
|
|
551
|
-
import { z as z15 } from "zod";
|
|
552
|
-
var auditReportSchema = auditSchema.merge(auditOutputSchema);
|
|
553
|
-
var pluginReportSchema = pluginMetaSchema.merge(
|
|
554
|
-
executionMetaSchema({
|
|
555
|
-
descriptionDate: "Start date and time of plugin run",
|
|
556
|
-
descriptionDuration: "Duration of the plugin run in ms"
|
|
557
|
-
})
|
|
558
|
-
).merge(
|
|
559
|
-
z15.object({
|
|
560
|
-
audits: z15.array(auditReportSchema).min(1),
|
|
561
|
-
groups: z15.array(groupSchema).optional()
|
|
562
|
-
})
|
|
563
|
-
).refine(
|
|
564
|
-
(pluginReport) => !getMissingRefsFromGroups2(pluginReport.audits, pluginReport.groups ?? []),
|
|
565
|
-
(pluginReport) => ({
|
|
566
|
-
message: missingRefsFromGroupsErrorMsg2(
|
|
567
|
-
pluginReport.audits,
|
|
568
|
-
pluginReport.groups ?? []
|
|
569
|
-
)
|
|
570
|
-
})
|
|
571
|
-
);
|
|
572
|
-
function missingRefsFromGroupsErrorMsg2(audits, groups) {
|
|
573
|
-
const missingRefs = getMissingRefsFromGroups2(audits, groups);
|
|
574
|
-
return `group references need to point to an existing audit in this plugin report: ${errorItems(
|
|
575
|
-
missingRefs
|
|
576
|
-
)}`;
|
|
577
|
-
}
|
|
578
|
-
function getMissingRefsFromGroups2(audits, groups) {
|
|
579
|
-
return hasMissingStrings(
|
|
580
|
-
groups.flatMap(
|
|
581
|
-
({ refs: auditRefs }) => auditRefs.map(({ slug: ref }) => ref)
|
|
582
|
-
),
|
|
583
|
-
audits.map(({ slug }) => slug)
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
var reportSchema = packageVersionSchema({
|
|
587
|
-
versionDescription: "NPM version of the CLI",
|
|
588
|
-
required: true
|
|
589
|
-
}).merge(
|
|
590
|
-
executionMetaSchema({
|
|
591
|
-
descriptionDate: "Start date and time of the collect run",
|
|
592
|
-
descriptionDuration: "Duration of the collect run in ms"
|
|
593
|
-
})
|
|
594
|
-
).merge(
|
|
595
|
-
z15.object(
|
|
596
|
-
{
|
|
597
|
-
plugins: z15.array(pluginReportSchema).min(1),
|
|
598
|
-
categories: z15.array(categoryConfigSchema).optional(),
|
|
599
|
-
commit: commitSchema.describe("Git commit for which report was collected").nullable()
|
|
600
|
-
},
|
|
601
|
-
{ description: "Collect output data" }
|
|
602
|
-
)
|
|
603
|
-
).refine(
|
|
604
|
-
({ categories, plugins }) => !getMissingRefsForCategories(categories, plugins),
|
|
605
|
-
({ categories, plugins }) => ({
|
|
606
|
-
message: missingRefsForCategoriesErrorMsg(categories, plugins)
|
|
607
|
-
})
|
|
608
|
-
);
|
|
609
|
-
|
|
610
|
-
// packages/models/src/lib/reports-diff.ts
|
|
611
|
-
import { z as z16 } from "zod";
|
|
612
|
-
function makeComparisonSchema(schema) {
|
|
613
|
-
const sharedDescription = schema.description || "Result";
|
|
614
|
-
return z16.object({
|
|
615
|
-
before: schema.describe(`${sharedDescription} (source commit)`),
|
|
616
|
-
after: schema.describe(`${sharedDescription} (target commit)`)
|
|
617
|
-
});
|
|
618
|
-
}
|
|
619
|
-
function makeArraysComparisonSchema(diffSchema, resultSchema, description) {
|
|
620
|
-
return z16.object(
|
|
621
|
-
{
|
|
622
|
-
changed: z16.array(diffSchema),
|
|
623
|
-
unchanged: z16.array(resultSchema),
|
|
624
|
-
added: z16.array(resultSchema),
|
|
625
|
-
removed: z16.array(resultSchema)
|
|
626
|
-
},
|
|
627
|
-
{ description }
|
|
628
|
-
);
|
|
629
|
-
}
|
|
630
|
-
var scorableMetaSchema = z16.object({
|
|
631
|
-
slug: slugSchema,
|
|
632
|
-
title: titleSchema,
|
|
633
|
-
docsUrl: docsUrlSchema
|
|
634
|
-
});
|
|
635
|
-
var scorableWithPluginMetaSchema = scorableMetaSchema.merge(
|
|
636
|
-
z16.object({
|
|
637
|
-
plugin: pluginMetaSchema.pick({ slug: true, title: true, docsUrl: true }).describe("Plugin which defines it")
|
|
638
|
-
})
|
|
639
|
-
);
|
|
640
|
-
var scorableDiffSchema = scorableMetaSchema.merge(
|
|
641
|
-
z16.object({
|
|
642
|
-
scores: makeComparisonSchema(scoreSchema).merge(
|
|
643
|
-
z16.object({
|
|
644
|
-
diff: z16.number().min(-1).max(1).describe("Score change (`scores.after - scores.before`)")
|
|
645
|
-
})
|
|
646
|
-
).describe("Score comparison")
|
|
647
|
-
})
|
|
648
|
-
);
|
|
649
|
-
var scorableWithPluginDiffSchema = scorableDiffSchema.merge(
|
|
650
|
-
scorableWithPluginMetaSchema
|
|
651
|
-
);
|
|
652
|
-
var categoryDiffSchema = scorableDiffSchema;
|
|
653
|
-
var groupDiffSchema = scorableWithPluginDiffSchema;
|
|
654
|
-
var auditDiffSchema = scorableWithPluginDiffSchema.merge(
|
|
655
|
-
z16.object({
|
|
656
|
-
values: makeComparisonSchema(auditValueSchema).merge(
|
|
657
|
-
z16.object({
|
|
658
|
-
diff: z16.number().describe("Value change (`values.after - values.before`)")
|
|
659
|
-
})
|
|
660
|
-
).describe("Audit `value` comparison"),
|
|
661
|
-
displayValues: makeComparisonSchema(auditDisplayValueSchema).describe(
|
|
662
|
-
"Audit `displayValue` comparison"
|
|
663
|
-
)
|
|
664
|
-
})
|
|
665
|
-
);
|
|
666
|
-
var categoryResultSchema = scorableMetaSchema.merge(
|
|
667
|
-
z16.object({ score: scoreSchema })
|
|
668
|
-
);
|
|
669
|
-
var groupResultSchema = scorableWithPluginMetaSchema.merge(
|
|
670
|
-
z16.object({ score: scoreSchema })
|
|
671
|
-
);
|
|
672
|
-
var auditResultSchema = scorableWithPluginMetaSchema.merge(
|
|
673
|
-
auditOutputSchema.pick({ score: true, value: true, displayValue: true })
|
|
674
|
-
);
|
|
675
|
-
var reportsDiffSchema = z16.object({
|
|
676
|
-
commits: makeComparisonSchema(commitSchema).nullable().describe("Commits identifying compared reports"),
|
|
677
|
-
portalUrl: urlSchema.optional().describe("Link to comparison page in Code PushUp portal"),
|
|
678
|
-
label: z16.string().optional().describe("Label (e.g. project name)"),
|
|
679
|
-
categories: makeArraysComparisonSchema(
|
|
680
|
-
categoryDiffSchema,
|
|
681
|
-
categoryResultSchema,
|
|
682
|
-
"Changes affecting categories"
|
|
683
|
-
),
|
|
684
|
-
groups: makeArraysComparisonSchema(
|
|
685
|
-
groupDiffSchema,
|
|
686
|
-
groupResultSchema,
|
|
687
|
-
"Changes affecting groups"
|
|
688
|
-
),
|
|
689
|
-
audits: makeArraysComparisonSchema(
|
|
690
|
-
auditDiffSchema,
|
|
691
|
-
auditResultSchema,
|
|
692
|
-
"Changes affecting audits"
|
|
693
|
-
)
|
|
694
|
-
}).merge(
|
|
695
|
-
packageVersionSchema({
|
|
696
|
-
versionDescription: "NPM version of the CLI (when `compare` was run)",
|
|
697
|
-
required: true
|
|
698
|
-
})
|
|
699
|
-
).merge(
|
|
700
|
-
executionMetaSchema({
|
|
701
|
-
descriptionDate: "Start date and time of the compare run",
|
|
702
|
-
descriptionDuration: "Duration of the compare run in ms"
|
|
703
|
-
})
|
|
704
|
-
);
|
|
705
|
-
|
|
706
|
-
// packages/utils/src/lib/errors.ts
|
|
707
|
-
function stringifyError(error) {
|
|
708
|
-
if (error instanceof Error) {
|
|
709
|
-
if (error.name === "Error" || error.message.startsWith(error.name)) {
|
|
710
|
-
return error.message;
|
|
711
|
-
}
|
|
712
|
-
return `${error.name}: ${error.message}`;
|
|
713
|
-
}
|
|
714
|
-
if (typeof error === "string") {
|
|
715
|
-
return error;
|
|
716
|
-
}
|
|
717
|
-
return JSON.stringify(error);
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
// packages/utils/src/lib/execute-process.ts
|
|
721
|
-
import {
|
|
722
|
-
spawn
|
|
723
|
-
} from "node:child_process";
|
|
724
|
-
|
|
725
|
-
// packages/utils/src/lib/reports/utils.ts
|
|
726
|
-
import ansis from "ansis";
|
|
727
|
-
import { md } from "build-md";
|
|
728
|
-
function calcDuration(start, stop) {
|
|
729
|
-
return Math.round((stop ?? performance.now()) - start);
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
// packages/utils/src/lib/execute-process.ts
|
|
733
|
-
var ProcessError = class extends Error {
|
|
734
|
-
code;
|
|
735
|
-
stderr;
|
|
736
|
-
stdout;
|
|
737
|
-
constructor(result) {
|
|
738
|
-
super(result.stderr);
|
|
739
|
-
this.code = result.code;
|
|
740
|
-
this.stderr = result.stderr;
|
|
741
|
-
this.stdout = result.stdout;
|
|
742
|
-
}
|
|
743
|
-
};
|
|
744
|
-
function executeProcess(cfg) {
|
|
745
|
-
const { command, args, observer, ignoreExitCode = false, ...options } = cfg;
|
|
746
|
-
const { onStdout, onStderr, onError, onComplete } = observer ?? {};
|
|
747
|
-
const date = (/* @__PURE__ */ new Date()).toISOString();
|
|
748
|
-
const start = performance.now();
|
|
749
|
-
return new Promise((resolve, reject) => {
|
|
750
|
-
const spawnedProcess = spawn(command, args ?? [], {
|
|
751
|
-
shell: true,
|
|
752
|
-
windowsHide: true,
|
|
753
|
-
...options
|
|
754
|
-
});
|
|
755
|
-
let stdout = "";
|
|
756
|
-
let stderr = "";
|
|
757
|
-
spawnedProcess.stdout.on("data", (data) => {
|
|
758
|
-
stdout += String(data);
|
|
759
|
-
onStdout?.(String(data), spawnedProcess);
|
|
760
|
-
});
|
|
761
|
-
spawnedProcess.stderr.on("data", (data) => {
|
|
762
|
-
stderr += String(data);
|
|
763
|
-
onStderr?.(String(data), spawnedProcess);
|
|
764
|
-
});
|
|
765
|
-
spawnedProcess.on("error", (err) => {
|
|
766
|
-
stderr += err.toString();
|
|
767
|
-
});
|
|
768
|
-
spawnedProcess.on("close", (code2) => {
|
|
769
|
-
const timings = { date, duration: calcDuration(start) };
|
|
770
|
-
if (code2 === 0 || ignoreExitCode) {
|
|
771
|
-
onComplete?.();
|
|
772
|
-
resolve({ code: code2, stdout, stderr, ...timings });
|
|
773
|
-
} else {
|
|
774
|
-
const errorMsg = new ProcessError({ code: code2, stdout, stderr, ...timings });
|
|
775
|
-
onError?.(errorMsg);
|
|
776
|
-
reject(errorMsg);
|
|
777
|
-
}
|
|
778
|
-
});
|
|
779
|
-
});
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
// packages/utils/src/lib/file-system.ts
|
|
783
|
-
import { bold, gray } from "ansis";
|
|
784
|
-
import { bundleRequire } from "bundle-require";
|
|
785
|
-
import { mkdir, readFile, readdir, rm, stat } from "node:fs/promises";
|
|
786
|
-
|
|
787
|
-
// packages/utils/src/lib/logging.ts
|
|
788
|
-
import isaacs_cliui from "@isaacs/cliui";
|
|
789
|
-
import { cliui } from "@poppinss/cliui";
|
|
790
|
-
import { underline } from "ansis";
|
|
791
|
-
|
|
792
|
-
// packages/utils/src/lib/file-system.ts
|
|
793
|
-
async function readTextFile(path3) {
|
|
794
|
-
const buffer = await readFile(path3);
|
|
795
|
-
return buffer.toString();
|
|
796
|
-
}
|
|
797
|
-
async function readJsonFile(path3) {
|
|
798
|
-
const text = await readTextFile(path3);
|
|
799
|
-
return JSON.parse(text);
|
|
800
|
-
}
|
|
801
|
-
async function fileExists(path3) {
|
|
802
|
-
try {
|
|
803
|
-
const stats = await stat(path3);
|
|
804
|
-
return stats.isFile();
|
|
805
|
-
} catch {
|
|
806
|
-
return false;
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
function projectToFilename(project) {
|
|
810
|
-
return project.replace(/[/\\\s]+/g, "-").replace(/@/g, "");
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
// packages/utils/src/lib/git/git.ts
|
|
814
|
-
import { simpleGit } from "simple-git";
|
|
815
|
-
|
|
816
|
-
// packages/utils/src/lib/transform.ts
|
|
817
|
-
function toArray(val) {
|
|
818
|
-
return Array.isArray(val) ? val : [val];
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
// packages/utils/src/lib/git/git.commits-and-tags.ts
|
|
822
|
-
import { simpleGit as simpleGit2 } from "simple-git";
|
|
823
|
-
|
|
824
|
-
// packages/utils/src/lib/semver.ts
|
|
825
|
-
import { rcompare, valid } from "semver";
|
|
826
|
-
|
|
827
|
-
// packages/utils/src/lib/progress.ts
|
|
828
|
-
import { black, bold as bold2, gray as gray2, green } from "ansis";
|
|
829
|
-
import { MultiProgressBars } from "multi-progress-bars";
|
|
830
|
-
|
|
831
|
-
// packages/utils/src/lib/reports/generate-md-report.ts
|
|
832
|
-
import { MarkdownDocument as MarkdownDocument3, md as md4 } from "build-md";
|
|
833
|
-
|
|
834
|
-
// packages/utils/src/lib/reports/formatting.ts
|
|
835
|
-
import {
|
|
836
|
-
MarkdownDocument,
|
|
837
|
-
md as md2
|
|
838
|
-
} from "build-md";
|
|
839
|
-
|
|
840
|
-
// packages/utils/src/lib/reports/generate-md-report-categoy-section.ts
|
|
841
|
-
import { MarkdownDocument as MarkdownDocument2, md as md3 } from "build-md";
|
|
842
|
-
|
|
843
|
-
// packages/utils/src/lib/reports/generate-md-reports-diff.ts
|
|
844
|
-
import {
|
|
845
|
-
MarkdownDocument as MarkdownDocument5,
|
|
846
|
-
md as md6
|
|
847
|
-
} from "build-md";
|
|
848
|
-
|
|
849
|
-
// packages/utils/src/lib/reports/generate-md-reports-diff-utils.ts
|
|
850
|
-
import { MarkdownDocument as MarkdownDocument4, md as md5 } from "build-md";
|
|
851
|
-
|
|
852
|
-
// packages/utils/src/lib/reports/log-stdout-summary.ts
|
|
853
|
-
import { bold as bold4, cyan, cyanBright, green as green2, red } from "ansis";
|
|
854
|
-
|
|
855
|
-
// packages/ci/src/lib/monorepo/packages.ts
|
|
856
|
-
import { glob } from "glob";
|
|
857
|
-
import { basename, dirname, join } from "node:path";
|
|
858
|
-
async function listPackages(cwd, patterns = ["**"]) {
|
|
859
|
-
const files = await glob(
|
|
860
|
-
patterns.map((pattern) => pattern.replace(/\/?$/, "/package.json")),
|
|
861
|
-
{ cwd }
|
|
862
|
-
);
|
|
863
|
-
return Promise.all(
|
|
864
|
-
files.toSorted().map(async (file) => {
|
|
865
|
-
const packageJson = await readJsonFile(join(cwd, file));
|
|
866
|
-
const directory = join(cwd, dirname(file));
|
|
867
|
-
const name = packageJson.name || basename(directory);
|
|
868
|
-
return { name, directory, packageJson };
|
|
869
|
-
})
|
|
870
|
-
);
|
|
871
|
-
}
|
|
872
|
-
async function listWorkspaces(cwd) {
|
|
873
|
-
const rootPackageJson = await readRootPackageJson(cwd);
|
|
874
|
-
const patterns = Array.isArray(rootPackageJson.workspaces) ? rootPackageJson.workspaces : rootPackageJson.workspaces?.packages;
|
|
875
|
-
return {
|
|
876
|
-
workspaces: await listPackages(cwd, patterns),
|
|
877
|
-
rootPackageJson
|
|
878
|
-
};
|
|
879
|
-
}
|
|
880
|
-
async function hasWorkspacesEnabled(cwd) {
|
|
881
|
-
const packageJson = await readRootPackageJson(cwd);
|
|
882
|
-
if (!packageJson.private) {
|
|
883
|
-
return false;
|
|
884
|
-
}
|
|
885
|
-
if (Array.isArray(packageJson.workspaces)) {
|
|
886
|
-
return packageJson.workspaces.length > 0;
|
|
887
|
-
}
|
|
888
|
-
if (typeof packageJson.workspaces === "object") {
|
|
889
|
-
return Boolean(packageJson.workspaces.packages?.length);
|
|
890
|
-
}
|
|
891
|
-
return false;
|
|
892
|
-
}
|
|
893
|
-
async function readRootPackageJson(cwd) {
|
|
894
|
-
return await readJsonFile(join(cwd, "package.json"));
|
|
895
|
-
}
|
|
896
|
-
function hasDependency(packageJson, name) {
|
|
897
|
-
const { dependencies = {}, devDependencies = {} } = packageJson;
|
|
898
|
-
return name in devDependencies || name in dependencies;
|
|
899
|
-
}
|
|
900
|
-
function hasScript(packageJson, script) {
|
|
901
|
-
const { scripts = {} } = packageJson;
|
|
902
|
-
return script in scripts;
|
|
903
|
-
}
|
|
904
|
-
function hasCodePushUpDependency(packageJson) {
|
|
905
|
-
return hasDependency(packageJson, "@code-pushup/cli");
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
// packages/ci/src/lib/monorepo/handlers/npm.ts
|
|
909
|
-
var npmHandler = {
|
|
910
|
-
tool: "npm",
|
|
911
|
-
async isConfigured(options) {
|
|
912
|
-
return await fileExists(join2(options.cwd, "package-lock.json")) && await hasWorkspacesEnabled(options.cwd);
|
|
913
|
-
},
|
|
914
|
-
async listProjects(options) {
|
|
915
|
-
const { workspaces, rootPackageJson } = await listWorkspaces(options.cwd);
|
|
916
|
-
return workspaces.filter(
|
|
917
|
-
({ packageJson }) => hasScript(packageJson, options.task) || hasCodePushUpDependency(packageJson) || hasCodePushUpDependency(rootPackageJson)
|
|
918
|
-
).map(({ name, packageJson }) => ({
|
|
919
|
-
name,
|
|
920
|
-
bin: hasScript(packageJson, options.task) ? `npm -w ${name} run ${options.task} --` : `npm -w ${name} exec ${options.task} --`
|
|
921
|
-
}));
|
|
922
|
-
}
|
|
923
|
-
};
|
|
924
|
-
|
|
925
|
-
// packages/ci/src/lib/monorepo/handlers/nx.ts
|
|
926
|
-
import { join as join3 } from "node:path";
|
|
927
|
-
var nxHandler = {
|
|
928
|
-
tool: "nx",
|
|
929
|
-
async isConfigured(options) {
|
|
930
|
-
return await fileExists(join3(options.cwd, "nx.json")) && (await executeProcess({
|
|
931
|
-
command: "npx",
|
|
932
|
-
args: ["nx", "report"],
|
|
933
|
-
cwd: options.cwd,
|
|
934
|
-
observer: options.observer
|
|
935
|
-
})).code === 0;
|
|
936
|
-
},
|
|
937
|
-
async listProjects(options) {
|
|
938
|
-
const { stdout } = await executeProcess({
|
|
939
|
-
command: "npx",
|
|
940
|
-
args: [
|
|
941
|
-
"nx",
|
|
942
|
-
"show",
|
|
943
|
-
"projects",
|
|
944
|
-
...toArray(options.nxProjectsFilter).map(
|
|
945
|
-
(arg) => arg.replaceAll("{task}", options.task)
|
|
946
|
-
),
|
|
947
|
-
"--json"
|
|
948
|
-
],
|
|
949
|
-
cwd: options.cwd,
|
|
950
|
-
observer: options.observer
|
|
951
|
-
});
|
|
952
|
-
const projects = parseProjects(stdout);
|
|
953
|
-
return projects.map((project) => ({
|
|
954
|
-
name: project,
|
|
955
|
-
bin: `npx nx run ${project}:${options.task} --`
|
|
956
|
-
}));
|
|
957
|
-
}
|
|
958
|
-
};
|
|
959
|
-
function parseProjects(stdout) {
|
|
960
|
-
let json;
|
|
961
|
-
try {
|
|
962
|
-
json = JSON.parse(stdout);
|
|
963
|
-
} catch (error) {
|
|
964
|
-
throw new Error(
|
|
965
|
-
`Invalid non-JSON output from 'nx show projects' - ${stringifyError(
|
|
966
|
-
error
|
|
967
|
-
)}`
|
|
968
|
-
);
|
|
969
|
-
}
|
|
970
|
-
if (Array.isArray(json) && json.every((item) => typeof item === "string")) {
|
|
971
|
-
return json;
|
|
972
|
-
}
|
|
973
|
-
throw new Error(
|
|
974
|
-
`Invalid JSON output from 'nx show projects', expected array of strings, received ${JSON.stringify(
|
|
975
|
-
json
|
|
976
|
-
)}`
|
|
977
|
-
);
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
// packages/ci/src/lib/monorepo/handlers/pnpm.ts
|
|
981
|
-
import { join as join4 } from "node:path";
|
|
982
|
-
import * as YAML from "yaml";
|
|
983
|
-
var WORKSPACE_FILE = "pnpm-workspace.yaml";
|
|
984
|
-
var pnpmHandler = {
|
|
985
|
-
tool: "pnpm",
|
|
986
|
-
async isConfigured(options) {
|
|
987
|
-
return await fileExists(join4(options.cwd, WORKSPACE_FILE)) && await fileExists(join4(options.cwd, "package.json"));
|
|
988
|
-
},
|
|
989
|
-
async listProjects(options) {
|
|
990
|
-
const yaml = await readTextFile(join4(options.cwd, WORKSPACE_FILE));
|
|
991
|
-
const workspace = YAML.parse(yaml);
|
|
992
|
-
const packages = await listPackages(options.cwd, workspace.packages);
|
|
993
|
-
const rootPackageJson = await readRootPackageJson(options.cwd);
|
|
994
|
-
return packages.filter(
|
|
995
|
-
({ packageJson }) => hasScript(packageJson, options.task) || hasCodePushUpDependency(packageJson) || hasCodePushUpDependency(rootPackageJson)
|
|
996
|
-
).map(({ name, packageJson }) => ({
|
|
997
|
-
name,
|
|
998
|
-
bin: hasScript(packageJson, options.task) ? `pnpm -F ${name} run ${options.task}` : `pnpm -F ${name} exec ${options.task}`
|
|
999
|
-
}));
|
|
1000
|
-
}
|
|
1001
|
-
};
|
|
1002
|
-
|
|
1003
|
-
// packages/ci/src/lib/monorepo/handlers/turbo.ts
|
|
1004
|
-
import { join as join6 } from "node:path";
|
|
1005
|
-
|
|
1006
|
-
// packages/ci/src/lib/monorepo/handlers/yarn.ts
|
|
1007
|
-
import { join as join5 } from "node:path";
|
|
1008
|
-
var yarnHandler = {
|
|
1009
|
-
tool: "yarn",
|
|
1010
|
-
async isConfigured(options) {
|
|
1011
|
-
return await fileExists(join5(options.cwd, "yarn.lock")) && await hasWorkspacesEnabled(options.cwd);
|
|
1012
|
-
},
|
|
1013
|
-
async listProjects(options) {
|
|
1014
|
-
const { workspaces, rootPackageJson } = await listWorkspaces(options.cwd);
|
|
1015
|
-
return workspaces.filter(
|
|
1016
|
-
({ packageJson }) => hasScript(packageJson, options.task) || hasCodePushUpDependency(packageJson) || hasCodePushUpDependency(rootPackageJson)
|
|
1017
|
-
).map(({ name, packageJson }) => ({
|
|
1018
|
-
name,
|
|
1019
|
-
bin: hasScript(packageJson, options.task) ? `yarn workspace ${name} run ${options.task}` : `yarn workspace ${name} exec ${options.task}`
|
|
1020
|
-
}));
|
|
1021
|
-
}
|
|
1022
|
-
};
|
|
1023
|
-
|
|
1024
|
-
// packages/ci/src/lib/monorepo/handlers/turbo.ts
|
|
1025
|
-
var WORKSPACE_HANDLERS = [pnpmHandler, yarnHandler, npmHandler];
|
|
1026
|
-
var turboHandler = {
|
|
1027
|
-
tool: "turbo",
|
|
1028
|
-
async isConfigured(options) {
|
|
1029
|
-
const configPath = join6(options.cwd, "turbo.json");
|
|
1030
|
-
return await fileExists(configPath) && options.task in (await readJsonFile(configPath)).tasks;
|
|
1031
|
-
},
|
|
1032
|
-
async listProjects(options) {
|
|
1033
|
-
for (const handler of WORKSPACE_HANDLERS) {
|
|
1034
|
-
if (await handler.isConfigured(options)) {
|
|
1035
|
-
const projects = await handler.listProjects(options);
|
|
1036
|
-
return projects.filter(({ bin }) => bin.includes(`run ${options.task}`)).map(({ name }) => ({
|
|
1037
|
-
name,
|
|
1038
|
-
bin: `npx turbo run ${options.task} -F ${name} --`
|
|
1039
|
-
}));
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
throw new Error(
|
|
1043
|
-
`Package manager for Turborepo not found, expected one of ${WORKSPACE_HANDLERS.map(
|
|
1044
|
-
({ tool }) => tool
|
|
1045
|
-
).join("/")}`
|
|
1046
|
-
);
|
|
1047
|
-
}
|
|
1048
|
-
};
|
|
1049
|
-
|
|
1050
|
-
// packages/ci/src/lib/monorepo/handlers/index.ts
|
|
1051
|
-
var MONOREPO_TOOL_HANDLERS = [
|
|
1052
|
-
nxHandler,
|
|
1053
|
-
turboHandler,
|
|
1054
|
-
yarnHandler,
|
|
1055
|
-
pnpmHandler,
|
|
1056
|
-
npmHandler
|
|
1057
|
-
];
|
|
1058
|
-
function getToolHandler(tool) {
|
|
1059
|
-
const matchedHandler = MONOREPO_TOOL_HANDLERS.find(
|
|
1060
|
-
(handler) => handler.tool === tool
|
|
1061
|
-
);
|
|
1062
|
-
if (!matchedHandler) {
|
|
1063
|
-
throw new Error(`No handler available for monorepo tool "${tool}"`);
|
|
1064
|
-
}
|
|
1065
|
-
return matchedHandler;
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
|
-
// packages/ci/src/lib/monorepo/detect-tool.ts
|
|
1069
|
-
async function detectMonorepoTool(options) {
|
|
1070
|
-
for (const handler of MONOREPO_TOOL_HANDLERS) {
|
|
1071
|
-
if (await handler.isConfigured(options)) {
|
|
1072
|
-
return handler.tool;
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
return null;
|
|
1076
|
-
}
|
|
1077
|
-
|
|
1078
|
-
// packages/ci/src/lib/monorepo/list-projects.ts
|
|
1079
|
-
async function listMonorepoProjects(settings) {
|
|
1080
|
-
if (!settings.monorepo) {
|
|
1081
|
-
throw new Error("Monorepo mode not enabled");
|
|
1082
|
-
}
|
|
1083
|
-
const logger = settings.logger;
|
|
1084
|
-
const options = createMonorepoHandlerOptions(settings);
|
|
1085
|
-
const tool = settings.monorepo === true ? await detectMonorepoTool(options) : settings.monorepo;
|
|
1086
|
-
if (settings.monorepo === true) {
|
|
1087
|
-
if (tool) {
|
|
1088
|
-
logger.info(`Auto-detected monorepo tool ${tool}`);
|
|
1089
|
-
} else {
|
|
1090
|
-
logger.info("Couldn't auto-detect any supported monorepo tool");
|
|
1091
|
-
}
|
|
1092
|
-
} else {
|
|
1093
|
-
logger.info(`Using monorepo tool "${tool}" from inputs`);
|
|
1094
|
-
}
|
|
1095
|
-
if (tool) {
|
|
1096
|
-
const handler = getToolHandler(tool);
|
|
1097
|
-
const projects = await handler.listProjects(options);
|
|
1098
|
-
logger.info(`Found ${projects.length} projects in ${tool} monorepo`);
|
|
1099
|
-
logger.debug(`Projects: ${projects.map(({ name }) => name).join(", ")}`);
|
|
1100
|
-
return projects;
|
|
1101
|
-
}
|
|
1102
|
-
if (settings.projects) {
|
|
1103
|
-
return listProjectsByGlobs({
|
|
1104
|
-
patterns: settings.projects,
|
|
1105
|
-
cwd: options.cwd,
|
|
1106
|
-
bin: settings.bin,
|
|
1107
|
-
logger
|
|
1108
|
-
});
|
|
1109
|
-
}
|
|
1110
|
-
return listProjectsByNpmPackages({
|
|
1111
|
-
cwd: options.cwd,
|
|
1112
|
-
bin: settings.bin,
|
|
1113
|
-
logger
|
|
1114
|
-
});
|
|
1115
|
-
}
|
|
1116
|
-
function createMonorepoHandlerOptions(settings) {
|
|
1117
|
-
return {
|
|
1118
|
-
task: settings.task,
|
|
1119
|
-
cwd: settings.directory,
|
|
1120
|
-
nxProjectsFilter: settings.nxProjectsFilter,
|
|
1121
|
-
...!settings.silent && {
|
|
1122
|
-
observer: {
|
|
1123
|
-
onStdout: (stdout) => {
|
|
1124
|
-
console.info(stdout);
|
|
1125
|
-
},
|
|
1126
|
-
onStderr: (stderr) => {
|
|
1127
|
-
console.warn(stderr);
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
};
|
|
1132
|
-
}
|
|
1133
|
-
async function listProjectsByGlobs(args) {
|
|
1134
|
-
const { patterns, cwd, bin, logger } = args;
|
|
1135
|
-
const directories = await glob2(
|
|
1136
|
-
patterns.map((path3) => path3.replace(/\/$/, "/")),
|
|
1137
|
-
{ cwd }
|
|
1138
|
-
);
|
|
1139
|
-
logger.info(
|
|
1140
|
-
`Found ${directories.length} project folders matching "${patterns.join(
|
|
1141
|
-
", "
|
|
1142
|
-
)}" from configuration`
|
|
1143
|
-
);
|
|
1144
|
-
logger.debug(`Projects: ${directories.join(", ")}`);
|
|
1145
|
-
return directories.toSorted().map((directory) => ({
|
|
1146
|
-
name: directory,
|
|
1147
|
-
bin,
|
|
1148
|
-
directory: join7(cwd, directory)
|
|
1149
|
-
}));
|
|
1150
|
-
}
|
|
1151
|
-
async function listProjectsByNpmPackages(args) {
|
|
1152
|
-
const { cwd, bin, logger } = args;
|
|
1153
|
-
const packages = await listPackages(cwd);
|
|
1154
|
-
logger.info(`Found ${packages.length} NPM packages in repository`);
|
|
1155
|
-
logger.debug(`Projects: ${packages.map(({ name }) => name).join(", ")}`);
|
|
1156
|
-
return packages.map(({ name, directory }) => ({
|
|
1157
|
-
name,
|
|
1158
|
-
bin,
|
|
1159
|
-
directory
|
|
1160
|
-
}));
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
// packages/ci/src/lib/monorepo/tools.ts
|
|
1164
|
-
var MONOREPO_TOOLS = ["nx", "turbo", "yarn", "pnpm", "npm"];
|
|
1165
|
-
function isMonorepoTool(value) {
|
|
1166
|
-
return MONOREPO_TOOLS.includes(value);
|
|
1167
|
-
}
|
|
1168
|
-
|
|
1169
|
-
// packages/ci/src/lib/run.ts
|
|
1170
|
-
import fs from "node:fs/promises";
|
|
1171
|
-
import path2 from "node:path";
|
|
1172
|
-
import { simpleGit as simpleGit4 } from "simple-git";
|
|
1173
|
-
|
|
1174
|
-
// packages/ci/src/lib/cli/persist.ts
|
|
1175
|
-
import path from "node:path";
|
|
1176
|
-
function persistCliOptions({
|
|
1177
|
-
directory,
|
|
1178
|
-
project,
|
|
1179
|
-
output
|
|
1180
|
-
}) {
|
|
1181
|
-
return [
|
|
1182
|
-
`--persist.outputDir=${path.join(directory, output)}`,
|
|
1183
|
-
`--persist.filename=${createFilename(project)}`,
|
|
1184
|
-
...DEFAULT_PERSIST_FORMAT.map((format) => `--persist.format=${format}`)
|
|
1185
|
-
];
|
|
1186
|
-
}
|
|
1187
|
-
function persistedCliFiles({
|
|
1188
|
-
directory,
|
|
1189
|
-
isDiff,
|
|
1190
|
-
project,
|
|
1191
|
-
formats,
|
|
1192
|
-
output
|
|
1193
|
-
}) {
|
|
1194
|
-
const rootDir = path.join(directory, output);
|
|
1195
|
-
const filename = isDiff ? `${createFilename(project)}-diff` : createFilename(project);
|
|
1196
|
-
const filePaths = (formats ?? DEFAULT_PERSIST_FORMAT).reduce(
|
|
1197
|
-
(acc, format) => ({
|
|
1198
|
-
...acc,
|
|
1199
|
-
[`${format}FilePath`]: path.join(rootDir, `${filename}.${format}`)
|
|
1200
|
-
}),
|
|
1201
|
-
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter, @typescript-eslint/consistent-type-assertions
|
|
1202
|
-
{}
|
|
1203
|
-
);
|
|
1204
|
-
const files = Object.values(filePaths);
|
|
1205
|
-
return {
|
|
1206
|
-
...filePaths,
|
|
1207
|
-
artifactData: {
|
|
1208
|
-
rootDir,
|
|
1209
|
-
files
|
|
1210
|
-
}
|
|
1211
|
-
};
|
|
1212
|
-
}
|
|
1213
|
-
function createFilename(project) {
|
|
1214
|
-
if (!project) {
|
|
1215
|
-
return DEFAULT_PERSIST_FILENAME;
|
|
1216
|
-
}
|
|
1217
|
-
const prefix = projectToFilename(project);
|
|
1218
|
-
return `${prefix}-${DEFAULT_PERSIST_FILENAME}`;
|
|
1219
|
-
}
|
|
1220
|
-
|
|
1221
|
-
// packages/ci/src/lib/cli/commands/collect.ts
|
|
1222
|
-
async function runCollect({
|
|
1223
|
-
bin,
|
|
1224
|
-
config,
|
|
1225
|
-
directory,
|
|
1226
|
-
silent,
|
|
1227
|
-
project,
|
|
1228
|
-
output
|
|
1229
|
-
}) {
|
|
1230
|
-
const { stdout } = await executeProcess({
|
|
1231
|
-
command: bin,
|
|
1232
|
-
args: [
|
|
1233
|
-
...config ? [`--config=${config}`] : [],
|
|
1234
|
-
...persistCliOptions({ directory, project, output })
|
|
1235
|
-
],
|
|
1236
|
-
cwd: directory
|
|
1237
|
-
});
|
|
1238
|
-
if (!silent) {
|
|
1239
|
-
console.info(stdout);
|
|
1240
|
-
}
|
|
1241
|
-
return persistedCliFiles({ directory, project, output });
|
|
1242
|
-
}
|
|
1243
|
-
|
|
1244
|
-
// packages/ci/src/lib/cli/commands/compare.ts
|
|
1245
|
-
async function runCompare({ before, after, label }, { bin, config, directory, silent, project, output }) {
|
|
1246
|
-
const { stdout } = await executeProcess({
|
|
1247
|
-
command: bin,
|
|
1248
|
-
args: [
|
|
1249
|
-
"compare",
|
|
1250
|
-
`--before=${before}`,
|
|
1251
|
-
`--after=${after}`,
|
|
1252
|
-
...label ? [`--label=${label}`] : [],
|
|
1253
|
-
...config ? [`--config=${config}`] : [],
|
|
1254
|
-
...persistCliOptions({ directory, project, output })
|
|
1255
|
-
],
|
|
1256
|
-
cwd: directory
|
|
1257
|
-
});
|
|
1258
|
-
if (!silent) {
|
|
1259
|
-
console.info(stdout);
|
|
1260
|
-
}
|
|
1261
|
-
return persistedCliFiles({ directory, isDiff: true, project, output });
|
|
1262
|
-
}
|
|
1263
|
-
|
|
1264
|
-
// packages/ci/src/lib/cli/commands/merge-diffs.ts
|
|
1265
|
-
async function runMergeDiffs(files, { bin, config, directory, silent, output }) {
|
|
1266
|
-
const { stdout } = await executeProcess({
|
|
1267
|
-
command: bin,
|
|
1268
|
-
args: [
|
|
1269
|
-
"merge-diffs",
|
|
1270
|
-
...files.map((file) => `--files=${file}`),
|
|
1271
|
-
...config ? [`--config=${config}`] : [],
|
|
1272
|
-
...persistCliOptions({ directory, output })
|
|
1273
|
-
],
|
|
1274
|
-
cwd: directory
|
|
1275
|
-
});
|
|
1276
|
-
if (!silent) {
|
|
1277
|
-
console.info(stdout);
|
|
1278
|
-
}
|
|
1279
|
-
return persistedCliFiles({
|
|
1280
|
-
directory,
|
|
1281
|
-
isDiff: true,
|
|
1282
|
-
formats: ["md"],
|
|
1283
|
-
output
|
|
1284
|
-
});
|
|
1285
|
-
}
|
|
1286
|
-
|
|
1287
|
-
// packages/ci/src/lib/cli/commands/print-config.ts
|
|
1288
|
-
async function runPrintConfig({
|
|
1289
|
-
bin,
|
|
1290
|
-
config,
|
|
1291
|
-
directory,
|
|
1292
|
-
silent
|
|
1293
|
-
}) {
|
|
1294
|
-
const { stdout } = await executeProcess({
|
|
1295
|
-
command: bin,
|
|
1296
|
-
args: [...config ? [`--config=${config}`] : [], "print-config"],
|
|
1297
|
-
cwd: directory
|
|
1298
|
-
});
|
|
1299
|
-
if (!silent) {
|
|
1300
|
-
console.info(stdout);
|
|
1301
|
-
}
|
|
1302
|
-
}
|
|
1303
|
-
|
|
1304
|
-
// packages/ci/src/lib/cli/context.ts
|
|
1305
|
-
function createCommandContext(settings, project) {
|
|
1306
|
-
return {
|
|
1307
|
-
project: project?.name,
|
|
1308
|
-
bin: project?.bin ?? settings.bin,
|
|
1309
|
-
directory: project?.directory ?? settings.directory,
|
|
1310
|
-
config: settings.config,
|
|
1311
|
-
silent: settings.silent,
|
|
1312
|
-
output: settings.output.replaceAll("{project}", project?.name ?? "")
|
|
1313
|
-
};
|
|
1314
|
-
}
|
|
1315
|
-
|
|
1316
|
-
// packages/ci/src/lib/comment.ts
|
|
1317
|
-
import { readFile as readFile2 } from "node:fs/promises";
|
|
1318
|
-
async function commentOnPR(mdPath, api, logger) {
|
|
1319
|
-
const markdown = await readFile2(mdPath, "utf8");
|
|
1320
|
-
const identifier = `<!-- generated by @code-pushup/ci -->`;
|
|
1321
|
-
const body = truncateBody(
|
|
1322
|
-
`${markdown}
|
|
1323
|
-
|
|
1324
|
-
${identifier}
|
|
1325
|
-
`,
|
|
1326
|
-
api.maxCommentChars,
|
|
1327
|
-
logger
|
|
1328
|
-
);
|
|
1329
|
-
const comments = await api.listComments();
|
|
1330
|
-
logger.debug(`Fetched ${comments.length} comments for pull request`);
|
|
1331
|
-
const prevComment = comments.find(
|
|
1332
|
-
(comment) => comment.body.includes(identifier)
|
|
1333
|
-
);
|
|
1334
|
-
logger.debug(
|
|
1335
|
-
prevComment ? `Found previous comment ${prevComment.id} from Code PushUp` : "Previous Code PushUp comment not found"
|
|
1336
|
-
);
|
|
1337
|
-
if (prevComment) {
|
|
1338
|
-
const updatedComment = await api.updateComment(prevComment.id, body);
|
|
1339
|
-
logger.info(`Updated body of comment ${updatedComment.url}`);
|
|
1340
|
-
return updatedComment.id;
|
|
1341
|
-
}
|
|
1342
|
-
const createdComment = await api.createComment(body);
|
|
1343
|
-
logger.info(`Created new comment ${createdComment.url}`);
|
|
1344
|
-
return createdComment.id;
|
|
1345
|
-
}
|
|
1346
|
-
function truncateBody(body, max, logger) {
|
|
1347
|
-
const truncateWarning = "...*[Comment body truncated]*";
|
|
1348
|
-
if (body.length > max) {
|
|
1349
|
-
logger.warn(`Comment body is too long. Truncating to ${max} characters.`);
|
|
1350
|
-
return body.slice(0, max - truncateWarning.length) + truncateWarning;
|
|
1351
|
-
}
|
|
1352
|
-
return body;
|
|
1353
|
-
}
|
|
1354
|
-
|
|
1355
|
-
// packages/ci/src/lib/constants.ts
|
|
1356
|
-
var DEFAULT_SETTINGS = {
|
|
1357
|
-
monorepo: false,
|
|
1358
|
-
projects: null,
|
|
1359
|
-
task: "code-pushup",
|
|
1360
|
-
bin: "npx --no-install code-pushup",
|
|
1361
|
-
config: null,
|
|
1362
|
-
directory: process.cwd(),
|
|
1363
|
-
silent: false,
|
|
1364
|
-
debug: false,
|
|
1365
|
-
detectNewIssues: true,
|
|
1366
|
-
logger: console,
|
|
1367
|
-
output: DEFAULT_PERSIST_OUTPUT_DIR,
|
|
1368
|
-
nxProjectsFilter: "--with-target={task}"
|
|
1369
|
-
};
|
|
1370
|
-
|
|
1371
|
-
// packages/ci/src/lib/git.ts
|
|
1372
|
-
import { DiffNameStatus, simpleGit as simpleGit3 } from "simple-git";
|
|
1373
|
-
async function listChangedFiles(refs, git = simpleGit3()) {
|
|
1374
|
-
const statuses = [
|
|
1375
|
-
DiffNameStatus.ADDED,
|
|
1376
|
-
DiffNameStatus.COPIED,
|
|
1377
|
-
DiffNameStatus.MODIFIED,
|
|
1378
|
-
DiffNameStatus.RENAMED
|
|
1379
|
-
];
|
|
1380
|
-
const { files } = await git.diffSummary([
|
|
1381
|
-
refs.base,
|
|
1382
|
-
refs.head,
|
|
1383
|
-
`--diff-filter=${statuses.join("")}`,
|
|
1384
|
-
"--find-renames",
|
|
1385
|
-
"--find-copies"
|
|
1386
|
-
]);
|
|
1387
|
-
const entries = await Promise.all(
|
|
1388
|
-
files.filter(({ binary }) => !binary).map(({ file }) => {
|
|
1389
|
-
const rename = parseFileRename(file);
|
|
1390
|
-
if (rename) {
|
|
1391
|
-
return { file: rename.curr, originalFile: rename.prev };
|
|
1392
|
-
}
|
|
1393
|
-
return { file };
|
|
1394
|
-
}).map(async ({ file, originalFile }) => {
|
|
1395
|
-
const diff = await git.diff([
|
|
1396
|
-
"--unified=0",
|
|
1397
|
-
refs.base,
|
|
1398
|
-
refs.head,
|
|
1399
|
-
"--",
|
|
1400
|
-
file,
|
|
1401
|
-
...originalFile ? [originalFile] : []
|
|
1402
|
-
]);
|
|
1403
|
-
const lineChanges = parseDiff(diff);
|
|
1404
|
-
return [
|
|
1405
|
-
file,
|
|
1406
|
-
{ ...originalFile && { originalFile }, lineChanges }
|
|
1407
|
-
];
|
|
1408
|
-
})
|
|
1409
|
-
);
|
|
1410
|
-
return Object.fromEntries(entries);
|
|
1411
|
-
}
|
|
1412
|
-
function parseFileRename(file) {
|
|
1413
|
-
const partialRenameMatch = file.match(/^(.*){(.*) => (.*)}(.*)$/);
|
|
1414
|
-
if (partialRenameMatch) {
|
|
1415
|
-
const [, prefix = "", prev, curr, suffix] = partialRenameMatch;
|
|
1416
|
-
return {
|
|
1417
|
-
prev: prefix + prev + suffix,
|
|
1418
|
-
curr: prefix + curr + suffix
|
|
1419
|
-
};
|
|
1420
|
-
}
|
|
1421
|
-
const fullRenameMatch = file.match(/^(.*) => (.*)$/);
|
|
1422
|
-
if (fullRenameMatch) {
|
|
1423
|
-
const [, prev = "", curr = ""] = fullRenameMatch;
|
|
1424
|
-
return { prev, curr };
|
|
1425
|
-
}
|
|
1426
|
-
return null;
|
|
1427
|
-
}
|
|
1428
|
-
function parseDiff(diff) {
|
|
1429
|
-
const changeSummaries = diff.match(/@@ [ \d,+-]+ @@/g);
|
|
1430
|
-
if (changeSummaries == null) {
|
|
1431
|
-
return [];
|
|
1432
|
-
}
|
|
1433
|
-
return changeSummaries.map((summary) => summary.match(/^@@ -(\d+|\d+,\d+) \+(\d+|\d+,\d+) @@$/)).filter((matches) => matches != null).map((matches) => {
|
|
1434
|
-
const [prevLine = "", prevAdded = "1"] = matches[1].split(",");
|
|
1435
|
-
const [currLine = "", currAdded = "1"] = matches[2].split(",");
|
|
1436
|
-
return {
|
|
1437
|
-
prev: {
|
|
1438
|
-
line: Number.parseInt(prevLine, 10),
|
|
1439
|
-
count: Number.parseInt(prevAdded, 10)
|
|
1440
|
-
},
|
|
1441
|
-
curr: {
|
|
1442
|
-
line: Number.parseInt(currLine, 10),
|
|
1443
|
-
count: Number.parseInt(currAdded, 10)
|
|
1444
|
-
}
|
|
1445
|
-
};
|
|
1446
|
-
});
|
|
1447
|
-
}
|
|
1448
|
-
function isFileChanged(changedFiles, file) {
|
|
1449
|
-
return file in changedFiles;
|
|
1450
|
-
}
|
|
1451
|
-
function adjustFileName(changedFiles, file) {
|
|
1452
|
-
return Object.entries(changedFiles).find(
|
|
1453
|
-
([, { originalFile }]) => originalFile === file
|
|
1454
|
-
)?.[0] ?? file;
|
|
1455
|
-
}
|
|
1456
|
-
function adjustLine(changedFiles, file, line) {
|
|
1457
|
-
const changedFile = changedFiles[adjustFileName(changedFiles, file)];
|
|
1458
|
-
if (!changedFile) {
|
|
1459
|
-
return line;
|
|
1460
|
-
}
|
|
1461
|
-
const offset = changedFile.lineChanges.filter(({ prev }) => prev.line < line).reduce((acc, { prev, curr }) => acc + (curr.count - prev.count), 0);
|
|
1462
|
-
return line + offset;
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
|
-
// packages/ci/src/lib/issues.ts
|
|
1466
|
-
function filterRelevantIssues({
|
|
1467
|
-
currReport,
|
|
1468
|
-
prevReport,
|
|
1469
|
-
reportsDiff,
|
|
1470
|
-
changedFiles
|
|
1471
|
-
}) {
|
|
1472
|
-
const auditsWithPlugin = [
|
|
1473
|
-
...reportsDiff.audits.changed,
|
|
1474
|
-
...reportsDiff.audits.added
|
|
1475
|
-
].map((auditLink) => {
|
|
1476
|
-
const plugin = currReport.plugins.find(
|
|
1477
|
-
({ slug }) => slug === auditLink.plugin.slug
|
|
1478
|
-
);
|
|
1479
|
-
const audit = plugin?.audits.find(({ slug }) => slug === auditLink.slug);
|
|
1480
|
-
return plugin && audit && [plugin, audit];
|
|
1481
|
-
}).filter((ctx) => ctx != null);
|
|
1482
|
-
const issues = auditsWithPlugin.flatMap(
|
|
1483
|
-
([plugin, audit]) => getAuditIssues(audit, plugin)
|
|
1484
|
-
);
|
|
1485
|
-
const prevIssues = prevReport.plugins.flatMap(
|
|
1486
|
-
(plugin) => plugin.audits.flatMap((audit) => getAuditIssues(audit, plugin))
|
|
1487
|
-
);
|
|
1488
|
-
return issues.filter(
|
|
1489
|
-
(issue) => isFileChanged(changedFiles, issue.source.file) && !prevIssues.some(
|
|
1490
|
-
(prevIssue) => issuesMatch(prevIssue, issue, changedFiles)
|
|
1491
|
-
)
|
|
1492
|
-
).sort(createIssuesSortCompareFn(currReport));
|
|
1493
|
-
}
|
|
1494
|
-
function getAuditIssues(audit, plugin) {
|
|
1495
|
-
return audit.details?.issues?.filter((issue) => issue.source?.file != null).map((issue) => ({ ...issue, audit, plugin })) ?? [];
|
|
1496
|
-
}
|
|
1497
|
-
function issuesMatch(prev, curr, changedFiles) {
|
|
1498
|
-
return prev.plugin.slug === curr.plugin.slug && prev.audit.slug === curr.audit.slug && prev.severity === curr.severity && removeDigits(prev.message) === removeDigits(curr.message) && adjustFileName(changedFiles, prev.source.file) === curr.source.file && positionsMatch(prev.source, curr.source, changedFiles);
|
|
1499
|
-
}
|
|
1500
|
-
function removeDigits(message) {
|
|
1501
|
-
return message.replace(/\d+/g, "");
|
|
1502
|
-
}
|
|
1503
|
-
function positionsMatch(prev, curr, changedFiles) {
|
|
1504
|
-
if (!hasPosition(prev) || !hasPosition(curr)) {
|
|
1505
|
-
return hasPosition(prev) === hasPosition(curr);
|
|
1506
|
-
}
|
|
1507
|
-
return adjustedStartLinesMatch(prev, curr, changedFiles) || adjustedLineSpansMatch(prev, curr, changedFiles);
|
|
1508
|
-
}
|
|
1509
|
-
function hasPosition(source) {
|
|
1510
|
-
return source.position != null;
|
|
1511
|
-
}
|
|
1512
|
-
function adjustedStartLinesMatch(prev, curr, changedFiles) {
|
|
1513
|
-
return adjustLine(changedFiles, prev.file, prev.position.startLine) === curr.position.startLine;
|
|
1514
|
-
}
|
|
1515
|
-
function adjustedLineSpansMatch(prev, curr, changedFiles) {
|
|
1516
|
-
if (prev.position?.endLine == null || curr.position?.endLine == null) {
|
|
1517
|
-
return false;
|
|
1518
|
-
}
|
|
1519
|
-
const prevLineCount = prev.position.endLine - prev.position.startLine;
|
|
1520
|
-
const currLineCount = curr.position.endLine - curr.position.startLine;
|
|
1521
|
-
const currStartLineOffset = adjustLine(changedFiles, curr.file, curr.position.startLine) - curr.position.startLine;
|
|
1522
|
-
return prevLineCount === currLineCount - currStartLineOffset;
|
|
1523
|
-
}
|
|
1524
|
-
function createIssuesSortCompareFn(report) {
|
|
1525
|
-
return (a, b) => getAuditImpactValue(b, report) - getAuditImpactValue(a, report);
|
|
1526
|
-
}
|
|
1527
|
-
function getAuditImpactValue({ audit, plugin }, report) {
|
|
1528
|
-
if (!report.categories) {
|
|
1529
|
-
return 0;
|
|
1530
|
-
}
|
|
1531
|
-
return report.categories.map((category) => {
|
|
1532
|
-
const weights = category.refs.map((ref) => {
|
|
1533
|
-
if (ref.plugin !== plugin.slug) {
|
|
1534
|
-
return 0;
|
|
1535
|
-
}
|
|
1536
|
-
switch (ref.type) {
|
|
1537
|
-
case "audit":
|
|
1538
|
-
return ref.slug === audit.slug ? ref.weight : 0;
|
|
1539
|
-
case "group":
|
|
1540
|
-
return calculateGroupImpact(ref, audit, report);
|
|
1541
|
-
}
|
|
1542
|
-
});
|
|
1543
|
-
return weights.reduce((acc, weight) => acc + weight, 0) / category.refs.reduce((acc, { weight }) => acc + weight, 0);
|
|
1544
|
-
}).reduce((acc, value) => acc + value, 0);
|
|
1545
|
-
}
|
|
1546
|
-
function calculateGroupImpact(ref, audit, report) {
|
|
1547
|
-
const group = report.plugins.find(({ slug }) => slug === ref.plugin)?.groups?.find(({ slug }) => slug === ref.slug);
|
|
1548
|
-
if (!group?.refs.length) {
|
|
1549
|
-
return 0;
|
|
1550
|
-
}
|
|
1551
|
-
const groupRatio = (group.refs.find(({ slug }) => slug === audit.slug)?.weight ?? 0) / group.refs.reduce((acc, { weight }) => acc + weight, 0);
|
|
1552
|
-
return ref.weight * groupRatio;
|
|
1553
|
-
}
|
|
1554
|
-
|
|
1555
|
-
// packages/ci/src/lib/run.ts
|
|
1556
|
-
async function runInCI(refs, api, options, git = simpleGit4()) {
|
|
1557
|
-
const settings = { ...DEFAULT_SETTINGS, ...options };
|
|
1558
|
-
const logger = settings.logger;
|
|
1559
|
-
if (settings.monorepo) {
|
|
1560
|
-
logger.info("Running Code PushUp in monorepo mode");
|
|
1561
|
-
const projects = await listMonorepoProjects(settings);
|
|
1562
|
-
const projectResults = await projects.reduce(
|
|
1563
|
-
async (acc, project) => [
|
|
1564
|
-
...await acc,
|
|
1565
|
-
await runOnProject({ project, settings, refs, api, git })
|
|
1566
|
-
],
|
|
1567
|
-
Promise.resolve([])
|
|
1568
|
-
);
|
|
1569
|
-
const diffJsonPaths = projectResults.map(
|
|
1570
|
-
({ artifacts: { diff } }) => diff?.files.find((file) => file.endsWith(".json"))
|
|
1571
|
-
).filter((file) => file != null);
|
|
1572
|
-
if (diffJsonPaths.length > 0) {
|
|
1573
|
-
const { mdFilePath, artifactData: diffArtifact } = await runMergeDiffs(
|
|
1574
|
-
diffJsonPaths,
|
|
1575
|
-
createCommandContext(settings, projects[0])
|
|
1576
|
-
);
|
|
1577
|
-
logger.debug(`Merged ${diffJsonPaths.length} diffs into ${mdFilePath}`);
|
|
1578
|
-
const commentId = await commentOnPR(mdFilePath, api, logger);
|
|
1579
|
-
return {
|
|
1580
|
-
mode: "monorepo",
|
|
1581
|
-
projects: projectResults,
|
|
1582
|
-
commentId,
|
|
1583
|
-
diffArtifact
|
|
1584
|
-
};
|
|
1585
|
-
}
|
|
1586
|
-
return { mode: "monorepo", projects: projectResults };
|
|
1587
|
-
}
|
|
1588
|
-
logger.info("Running Code PushUp in standalone project mode");
|
|
1589
|
-
const { artifacts, newIssues } = await runOnProject({
|
|
1590
|
-
project: null,
|
|
1591
|
-
settings,
|
|
1592
|
-
api,
|
|
1593
|
-
refs,
|
|
1594
|
-
git
|
|
1595
|
-
});
|
|
1596
|
-
const commentMdPath = artifacts.diff?.files.find(
|
|
1597
|
-
(file) => file.endsWith(".md")
|
|
1598
|
-
);
|
|
1599
|
-
if (commentMdPath) {
|
|
1600
|
-
const commentId = await commentOnPR(commentMdPath, api, logger);
|
|
1601
|
-
return {
|
|
1602
|
-
mode: "standalone",
|
|
1603
|
-
artifacts,
|
|
1604
|
-
commentId,
|
|
1605
|
-
newIssues
|
|
1606
|
-
};
|
|
1607
|
-
}
|
|
1608
|
-
return { mode: "standalone", artifacts, newIssues };
|
|
1609
|
-
}
|
|
1610
|
-
async function runOnProject(args) {
|
|
1611
|
-
const {
|
|
1612
|
-
project,
|
|
1613
|
-
refs: { head, base },
|
|
1614
|
-
settings,
|
|
1615
|
-
git
|
|
1616
|
-
} = args;
|
|
1617
|
-
const logger = settings.logger;
|
|
1618
|
-
const ctx = createCommandContext(settings, project);
|
|
1619
|
-
if (project) {
|
|
1620
|
-
logger.info(`Running Code PushUp on monorepo project ${project.name}`);
|
|
1621
|
-
}
|
|
1622
|
-
const { jsonFilePath: currReportPath, artifactData: reportArtifact } = await runCollect(ctx);
|
|
1623
|
-
const currReport = await fs.readFile(currReportPath, "utf8");
|
|
1624
|
-
logger.debug(`Collected current report at ${currReportPath}`);
|
|
1625
|
-
const noDiffOutput = {
|
|
1626
|
-
name: project?.name ?? "-",
|
|
1627
|
-
artifacts: {
|
|
1628
|
-
report: reportArtifact
|
|
1629
|
-
}
|
|
1630
|
-
};
|
|
1631
|
-
if (base == null) {
|
|
1632
|
-
return noDiffOutput;
|
|
1633
|
-
}
|
|
1634
|
-
logger.info(
|
|
1635
|
-
`PR/MR detected, preparing to compare base branch ${base.ref} to head ${head.ref}`
|
|
1636
|
-
);
|
|
1637
|
-
const prevReport = await collectPreviousReport({ ...args, base, ctx });
|
|
1638
|
-
if (!prevReport) {
|
|
1639
|
-
return noDiffOutput;
|
|
1640
|
-
}
|
|
1641
|
-
const reportsDir = path2.join(settings.directory, ".code-pushup");
|
|
1642
|
-
const currPath = path2.join(reportsDir, "curr-report.json");
|
|
1643
|
-
const prevPath = path2.join(reportsDir, "prev-report.json");
|
|
1644
|
-
await fs.writeFile(currPath, currReport);
|
|
1645
|
-
await fs.writeFile(prevPath, prevReport);
|
|
1646
|
-
logger.debug(`Saved reports to ${currPath} and ${prevPath}`);
|
|
1647
|
-
const comparisonFiles = await runCompare(
|
|
1648
|
-
{ before: prevPath, after: currPath, label: project?.name },
|
|
1649
|
-
ctx
|
|
1650
|
-
);
|
|
1651
|
-
logger.info("Compared reports and generated diff files");
|
|
1652
|
-
logger.debug(
|
|
1653
|
-
`Generated diff files at ${comparisonFiles.jsonFilePath} and ${comparisonFiles.mdFilePath}`
|
|
1654
|
-
);
|
|
1655
|
-
const diffOutput = {
|
|
1656
|
-
...noDiffOutput,
|
|
1657
|
-
artifacts: {
|
|
1658
|
-
...noDiffOutput.artifacts,
|
|
1659
|
-
diff: comparisonFiles.artifactData
|
|
1660
|
-
}
|
|
1661
|
-
};
|
|
1662
|
-
if (!settings.detectNewIssues) {
|
|
1663
|
-
return diffOutput;
|
|
1664
|
-
}
|
|
1665
|
-
const newIssues = await findNewIssues({
|
|
1666
|
-
base,
|
|
1667
|
-
currReport,
|
|
1668
|
-
prevReport,
|
|
1669
|
-
comparisonFiles,
|
|
1670
|
-
logger,
|
|
1671
|
-
git
|
|
1672
|
-
});
|
|
1673
|
-
return { ...diffOutput, newIssues };
|
|
1674
|
-
}
|
|
1675
|
-
async function collectPreviousReport(args) {
|
|
1676
|
-
const { project, base, api, settings, ctx, git } = args;
|
|
1677
|
-
const logger = settings.logger;
|
|
1678
|
-
const cachedBaseReport = await api.downloadReportArtifact?.(project?.name).catch((error) => {
|
|
1679
|
-
logger.warn(
|
|
1680
|
-
`Error when downloading previous report artifact, skipping - ${stringifyError(error)}`
|
|
1681
|
-
);
|
|
1682
|
-
});
|
|
1683
|
-
if (api.downloadReportArtifact != null) {
|
|
1684
|
-
logger.info(
|
|
1685
|
-
`Previous report artifact ${cachedBaseReport ? "found" : "not found"}`
|
|
1686
|
-
);
|
|
1687
|
-
if (cachedBaseReport) {
|
|
1688
|
-
logger.debug(
|
|
1689
|
-
`Previous report artifact downloaded to ${cachedBaseReport}`
|
|
1690
|
-
);
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
1693
|
-
if (cachedBaseReport) {
|
|
1694
|
-
return fs.readFile(cachedBaseReport, "utf8");
|
|
1695
|
-
} else {
|
|
1696
|
-
await git.fetch("origin", base.ref, ["--depth=1"]);
|
|
1697
|
-
await git.checkout(["-f", base.ref]);
|
|
1698
|
-
logger.info(`Switched to base branch ${base.ref}`);
|
|
1699
|
-
try {
|
|
1700
|
-
await runPrintConfig({ ...ctx, silent: !settings.debug });
|
|
1701
|
-
logger.debug(
|
|
1702
|
-
`Executing print-config verified code-pushup installed in base branch ${base.ref}`
|
|
1703
|
-
);
|
|
1704
|
-
} catch (error) {
|
|
1705
|
-
logger.debug(`Error from print-config - ${stringifyError(error)}`);
|
|
1706
|
-
logger.info(
|
|
1707
|
-
`Executing print-config failed, assuming code-pushup not installed in base branch ${base.ref} and skipping comparison`
|
|
1708
|
-
);
|
|
1709
|
-
return null;
|
|
1710
|
-
}
|
|
1711
|
-
const { jsonFilePath: prevReportPath } = await runCollect(ctx);
|
|
1712
|
-
const prevReport = await fs.readFile(prevReportPath, "utf8");
|
|
1713
|
-
logger.debug(`Collected previous report at ${prevReportPath}`);
|
|
1714
|
-
await git.checkout(["-f", "-"]);
|
|
1715
|
-
logger.info("Switched back to PR/MR branch");
|
|
1716
|
-
return prevReport;
|
|
1717
|
-
}
|
|
1718
|
-
}
|
|
1719
|
-
async function findNewIssues(args) {
|
|
1720
|
-
const { base, currReport, prevReport, comparisonFiles, logger, git } = args;
|
|
1721
|
-
await git.fetch("origin", base.ref, ["--depth=1"]);
|
|
1722
|
-
const reportsDiff = await fs.readFile(comparisonFiles.jsonFilePath, "utf8");
|
|
1723
|
-
const changedFiles = await listChangedFiles(
|
|
1724
|
-
{ base: "FETCH_HEAD", head: "HEAD" },
|
|
1725
|
-
git
|
|
1726
|
-
);
|
|
1727
|
-
const issues = filterRelevantIssues({
|
|
1728
|
-
currReport: JSON.parse(currReport),
|
|
1729
|
-
prevReport: JSON.parse(prevReport),
|
|
1730
|
-
reportsDiff: JSON.parse(reportsDiff),
|
|
1731
|
-
changedFiles
|
|
1732
|
-
});
|
|
1733
|
-
logger.debug(
|
|
1734
|
-
`Found ${issues.length} relevant issues for ${Object.keys(changedFiles).length} changed files`
|
|
1735
|
-
);
|
|
1736
|
-
return issues;
|
|
1737
|
-
}
|
|
1738
|
-
export {
|
|
1739
|
-
MONOREPO_TOOLS,
|
|
1740
|
-
isMonorepoTool,
|
|
1741
|
-
runInCI
|
|
1742
|
-
};
|