Skip to content

Commit c11db95

Browse files
committed
feat(ci): skip persist.format args if defaults already configured
1 parent ab39b05 commit c11db95

File tree

4 files changed

+78
-38
lines changed

4 files changed

+78
-38
lines changed

packages/ci/src/lib/cli/commands/collect.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
22
import { executeProcess, isVerbose } from '@code-pushup/utils';
33
import type { CommandContext } from '../context.js';
44

5-
export async function runCollect({
6-
bin,
7-
config,
8-
directory,
9-
observer,
10-
}: CommandContext): Promise<void> {
5+
export async function runCollect(
6+
{ bin, config, directory, observer }: CommandContext,
7+
{ hasFormats }: { hasFormats: boolean },
8+
): Promise<void> {
119
await executeProcess({
1210
command: bin,
1311
args: [
1412
...(isVerbose() ? ['--verbose'] : []),
1513
...(config ? [`--config=${config}`] : []),
16-
...DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`),
14+
...(hasFormats
15+
? []
16+
: DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`)),
1717
],
1818
cwd: directory,
1919
observer,

packages/ci/src/lib/run-monorepo.ts

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
type RunEnv,
3333
checkPrintConfig,
3434
compareReports,
35+
hasDefaultPersistFormats,
3536
loadCachedBaseReport,
3637
printPersistConfig,
3738
runInBaseBranch,
@@ -116,24 +117,35 @@ async function runProjectsInBulk(
116117
`Running on ${projects.length} projects in bulk (parallel: ${settings.parallel})`,
117118
);
118119

119-
await collectMany(runManyCommand, env);
120-
121-
const currProjectReports = await asyncSequential(
122-
projects,
123-
async (project): Promise<ProjectReport> => {
124-
const ctx = createCommandContext(settings, project);
125-
const config = await printPersistConfig(ctx);
126-
const reports = await saveOutputFiles({
127-
project,
128-
type: 'current',
129-
files: persistedFilesFromConfig(config, ctx),
130-
settings,
131-
});
132-
return { project, reports, config, ctx };
133-
},
134-
);
120+
const currProjectConfigs = await asyncSequential(projects, async project => {
121+
const ctx = createCommandContext(settings, project);
122+
const config = await printPersistConfig(ctx);
123+
return { project, config, ctx };
124+
});
125+
const hasFormats = allProjectsHaveDefaultPersistFormats(currProjectConfigs);
135126
logger.debug(
136-
`Loaded ${currProjectReports.length} persist configs by running print-config command for each project`,
127+
[
128+
`Loaded ${currProjectConfigs.length} persist configs by running print-config command for each project.`,
129+
hasFormats
130+
? 'Every project has default persist formats.'
131+
: 'Not all projects have default persist formats.',
132+
].join(' '),
133+
);
134+
135+
await collectMany(runManyCommand, env, { hasFormats });
136+
137+
const currProjectReports = await Promise.all(
138+
currProjectConfigs.map(
139+
async ({ project, config, ctx }): Promise<ProjectReport> => {
140+
const reports = await saveOutputFiles({
141+
project,
142+
type: 'current',
143+
files: persistedFilesFromConfig(config, ctx),
144+
settings,
145+
});
146+
return { project, reports, config, ctx };
147+
},
148+
),
137149
);
138150

139151
if (base == null) {
@@ -248,10 +260,12 @@ async function collectPreviousReports(
248260
}
249261

250262
if (onlyProjects.length > 0) {
263+
const hasFormats =
264+
allProjectsHaveDefaultPersistFormats(validProjectConfigs);
251265
logger.info(
252266
`Collecting previous reports for ${onlyProjects.length} projects`,
253267
);
254-
await collectMany(runManyCommand, env, onlyProjects);
268+
await collectMany(runManyCommand, env, { hasFormats, onlyProjects });
255269
}
256270

257271
const projectFiles = validProjectConfigs.map(args =>
@@ -281,16 +295,21 @@ async function savePreviousProjectReport(args: {
281295
async function collectMany(
282296
runManyCommand: RunManyCommand,
283297
env: RunEnv,
284-
onlyProjects?: string[],
298+
options: {
299+
hasFormats: boolean;
300+
onlyProjects?: string[];
301+
},
285302
): Promise<void> {
286303
const { settings } = env;
304+
const { hasFormats, onlyProjects } = options;
305+
287306
const command = await runManyCommand(onlyProjects);
288307
const ctx: CommandContext = {
289308
...createCommandContext(settings, null),
290309
bin: command,
291310
};
292311

293-
await runCollect(ctx);
312+
await runCollect(ctx, { hasFormats });
294313

295314
const countText = onlyProjects
296315
? `${onlyProjects.length} previous`
@@ -299,3 +318,9 @@ async function collectMany(
299318
`Collected ${countText} reports using command \`${command}\``,
300319
);
301320
}
321+
322+
export function allProjectsHaveDefaultPersistFormats(
323+
projects: { config: Pick<CoreConfig, 'persist'> }[],
324+
): boolean {
325+
return projects.every(({ config }) => hasDefaultPersistFormats(config));
326+
}

packages/ci/src/lib/run-utils.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/* eslint-disable max-lines */
22
import { readFile } from 'node:fs/promises';
33
import type { SimpleGit } from 'simple-git';
4-
import type { CoreConfig, Report, ReportsDiff } from '@code-pushup/models';
4+
import {
5+
type CoreConfig,
6+
DEFAULT_PERSIST_FORMAT,
7+
type Report,
8+
type ReportsDiff,
9+
} from '@code-pushup/models';
510
import {
611
removeUndefinedAndEmptyProps,
712
stringifyError,
@@ -112,7 +117,7 @@ export async function runOnProject(
112117
`Loaded persist config from print-config command - ${JSON.stringify(config.persist)}`,
113118
);
114119

115-
await runCollect(ctx);
120+
await runCollect(ctx, { hasFormats: hasDefaultPersistFormats(config) });
116121
const currReport = await saveReportFiles({
117122
project,
118123
type: 'current',
@@ -221,7 +226,7 @@ export async function collectPreviousReport(
221226
return null;
222227
}
223228

224-
await runCollect(ctx);
229+
await runCollect(ctx, { hasFormats: hasDefaultPersistFormats(config) });
225230
const report = await saveReportFiles({
226231
project,
227232
type: 'previous',
@@ -329,6 +334,16 @@ export async function printPersistConfig(
329334
return parsePersistConfig(json);
330335
}
331336

337+
export function hasDefaultPersistFormats(
338+
config: Pick<CoreConfig, 'persist'>,
339+
): boolean {
340+
const formats = config.persist?.format;
341+
return (
342+
formats == null ||
343+
DEFAULT_PERSIST_FORMAT.every(format => formats.includes(format))
344+
);
345+
}
346+
332347
export async function findNewIssues(
333348
args: CompareReportsArgs & { diffFiles: OutputFiles },
334349
): Promise<SourceFileIssue[]> {

packages/ci/src/lib/run.int.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ describe('runInCI', () => {
256256
} satisfies utils.ProcessConfig);
257257
expect(utils.executeProcess).toHaveBeenNthCalledWith(2, {
258258
command: options.bin,
259-
args: ['--persist.format=json', '--persist.format=md'],
259+
args: [],
260260
cwd: workDir,
261261
observer: expectedObserver,
262262
} satisfies utils.ProcessConfig);
@@ -334,7 +334,7 @@ describe('runInCI', () => {
334334
} satisfies utils.ProcessConfig);
335335
expect(utils.executeProcess).toHaveBeenNthCalledWith(2, {
336336
command: options.bin,
337-
args: ['--persist.format=json', '--persist.format=md'],
337+
args: [],
338338
cwd: workDir,
339339
observer: expectedObserver,
340340
} satisfies utils.ProcessConfig);
@@ -346,7 +346,7 @@ describe('runInCI', () => {
346346
} satisfies utils.ProcessConfig);
347347
expect(utils.executeProcess).toHaveBeenNthCalledWith(4, {
348348
command: options.bin,
349-
args: ['--persist.format=json', '--persist.format=md'],
349+
args: [],
350350
cwd: workDir,
351351
observer: expectedObserver,
352352
} satisfies utils.ProcessConfig);
@@ -418,7 +418,7 @@ describe('runInCI', () => {
418418
} satisfies utils.ProcessConfig);
419419
expect(utils.executeProcess).toHaveBeenNthCalledWith(2, {
420420
command: options.bin,
421-
args: ['--persist.format=json', '--persist.format=md'],
421+
args: [],
422422
cwd: workDir,
423423
observer: expectedObserver,
424424
} satisfies utils.ProcessConfig);
@@ -605,7 +605,7 @@ describe('runInCI', () => {
605605
} satisfies utils.ProcessConfig);
606606
expect(utils.executeProcess).toHaveBeenCalledWith({
607607
command: runMany,
608-
args: ['--persist.format=json', '--persist.format=md'],
608+
args: [],
609609
cwd: expect.stringContaining(workDir),
610610
observer: expectedObserver,
611611
} satisfies utils.ProcessConfig);
@@ -763,7 +763,7 @@ describe('runInCI', () => {
763763
} satisfies utils.ProcessConfig);
764764
expect(utils.executeProcess).toHaveBeenCalledWith({
765765
command: runMany,
766-
args: ['--persist.format=json', '--persist.format=md'],
766+
args: [],
767767
cwd: expect.stringContaining(workDir),
768768
observer: expectedObserver,
769769
} satisfies utils.ProcessConfig);
@@ -951,7 +951,7 @@ describe('runInCI', () => {
951951
} satisfies utils.ProcessConfig);
952952
expect(utils.executeProcess).toHaveBeenCalledWith({
953953
command: options.bin,
954-
args: ['--persist.format=json', '--persist.format=md'],
954+
args: [],
955955
cwd: expect.stringContaining(workDir),
956956
observer: expectedObserver,
957957
} satisfies utils.ProcessConfig);
@@ -1120,7 +1120,7 @@ describe('runInCI', () => {
11201120
} satisfies utils.ProcessConfig);
11211121
expect(utils.executeProcess).toHaveBeenCalledWith({
11221122
command: options.bin,
1123-
args: ['--persist.format=json', '--persist.format=md'],
1123+
args: [],
11241124
cwd: expect.stringContaining(workDir),
11251125
observer: expectedObserver,
11261126
} satisfies utils.ProcessConfig);

0 commit comments

Comments
 (0)