Skip to content

Commit 2adcf9d

Browse files
committed
feat(core): copy label from report.json inputs to report-diff.json output
1 parent 77d33b5 commit 2adcf9d

File tree

8 files changed

+186
-104
lines changed

8 files changed

+186
-104
lines changed

packages/cli/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,12 @@ In addition to the [Common Command Options](#common-command-options), the follow
293293
| -------------- | -------- | -------------------------------------- | ----------------------------------- |
294294
| **`--before`** | `string` | `.code-pushup/report-before.json` [^1] | Path to source `report.json`. |
295295
| **`--after`** | `string` | `.code-pushup/report-after.json` [^1] | Path to target `report.json`. |
296-
| **`--label`** | `string` | n/a | Label for diff (e.g. project name). |
296+
| **`--label`** | `string` | n/a [^2] | Label for diff (e.g. project name). |
297297

298298
[^1]: Uses `persist` config to determine report paths, so default file paths are actually `${persist.outputDir}/${persist.filename}-before.json` and `${persist.outputDir}/${persist.filename}-after.json`.
299299

300+
[^2]: Uses `label` from input `report.json` files is they both have the same value.
301+
300302
#### `print-config` command
301303

302304
Usage:

packages/cli/src/lib/compare/compare-command.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { bold, gray } from 'ansis';
22
import type { CommandModule } from 'yargs';
3-
import { compareReportFiles } from '@code-pushup/core';
3+
import { type CompareOptions, compareReportFiles } from '@code-pushup/core';
44
import type { PersistConfig, UploadConfig } from '@code-pushup/models';
55
import { ui } from '@code-pushup/utils';
66
import { CLI_NAME } from '../constants.js';
7-
import type { CompareOptions } from '../implementation/compare.model.js';
87
import { yargsCompareOptionsDefinition } from '../implementation/compare.options.js';
98

109
export function yargsCompareCommandObject() {
@@ -25,10 +24,8 @@ export function yargsCompareCommandObject() {
2524
const { before, after, label, persist, upload } = options;
2625

2726
const outputPaths = await compareReportFiles(
28-
{ before, after },
29-
persist,
30-
upload,
31-
label,
27+
{ persist, upload },
28+
{ before, after, label },
3229
);
3330

3431
ui().logger.info(

packages/cli/src/lib/compare/compare-command.unit.test.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ describe('compare-command', () => {
3636
expect(compareReportFiles).toHaveBeenCalledWith<
3737
Parameters<typeof compareReportFiles>
3838
>(
39-
{ before: 'source-report.json', after: 'target-report.json' },
4039
{
41-
outputDir: DEFAULT_PERSIST_OUTPUT_DIR,
42-
filename: DEFAULT_PERSIST_FILENAME,
43-
format: DEFAULT_PERSIST_FORMAT,
40+
persist: {
41+
outputDir: DEFAULT_PERSIST_OUTPUT_DIR,
42+
filename: DEFAULT_PERSIST_FILENAME,
43+
format: DEFAULT_PERSIST_FORMAT,
44+
},
45+
upload: expect.any(Object),
46+
},
47+
{
48+
before: 'source-report.json',
49+
after: 'target-report.json',
4450
},
45-
expect.any(Object),
46-
undefined,
4751
);
4852
});
4953

@@ -60,19 +64,18 @@ describe('compare-command', () => {
6064

6165
expect(compareReportFiles).toHaveBeenCalledWith<
6266
Parameters<typeof compareReportFiles>
63-
>(
64-
{ before: 'source-report.json', after: 'target-report.json' },
65-
expect.any(Object),
66-
expect.any(Object),
67-
'core',
68-
);
67+
>(expect.any(Object), {
68+
before: 'source-report.json',
69+
after: 'target-report.json',
70+
label: 'core',
71+
});
6972
});
7073

7174
it('should log output paths to stdout', async () => {
72-
await yargsCli(
73-
['compare', '--before=source-report.json', '--after=target-report.json'],
74-
{ ...DEFAULT_CLI_CONFIGURATION, commands: [yargsCompareCommandObject()] },
75-
).parseAsync();
75+
await yargsCli(['compare'], {
76+
...DEFAULT_CLI_CONFIGURATION,
77+
commands: [yargsCompareCommandObject()],
78+
}).parseAsync();
7679

7780
expect(ui()).toHaveLogged(
7881
'info',

packages/cli/src/lib/implementation/compare.model.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/cli/src/lib/implementation/compare.options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Options } from 'yargs';
2-
import type { CompareOptions } from './compare.model.js';
2+
import type { CompareOptions } from '@code-pushup/core';
33

44
export function yargsCompareOptionsDefinition(): Record<
55
keyof CompareOptions,

packages/core/src/index.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
export {
2-
type CollectAndPersistReportsOptions,
32
collectAndPersistReports,
3+
type CollectAndPersistReportsOptions,
44
} from './lib/collect-and-persist.js';
5-
export { compareReportFiles, compareReports } from './lib/compare.js';
6-
export { type CollectOptions, collect } from './lib/implementation/collect.js';
5+
export {
6+
compareReportFiles,
7+
compareReports,
8+
type CompareOptions,
9+
} from './lib/compare.js';
10+
export {
11+
history,
12+
type HistoryOnlyOptions,
13+
type HistoryOptions,
14+
} from './lib/history.js';
15+
export { collect, type CollectOptions } from './lib/implementation/collect.js';
716
export type { ReportsToCompare } from './lib/implementation/compare-scorables.js';
817
export {
9-
PluginOutputMissingAuditError,
1018
executePlugin,
1119
executePlugins,
20+
PluginOutputMissingAuditError,
1221
} from './lib/implementation/execute-plugin.js';
1322
export {
1423
PersistDirError,
1524
PersistError,
1625
persistReport,
1726
} from './lib/implementation/persist.js';
1827
export {
19-
history,
20-
type HistoryOptions,
21-
type HistoryOnlyOptions,
22-
} from './lib/history.js';
23-
export {
24-
ConfigPathError,
2528
autoloadRc,
29+
ConfigPathError,
2630
readRcByPath,
2731
} from './lib/implementation/read-rc-file.js';
28-
export type { GlobalOptions } from './lib/types.js';
29-
export { type UploadOptions, upload } from './lib/upload.js';
3032
export { mergeDiffs } from './lib/merge-diffs.js';
33+
export type { GlobalOptions } from './lib/types.js';
34+
export { upload, type UploadOptions } from './lib/upload.js';

packages/core/src/lib/compare.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,45 @@ import {
2626
} from './implementation/compare-scorables.js';
2727
import { loadPortalClient } from './load-portal-client.js';
2828

29+
export type CompareOptions = {
30+
before?: string;
31+
after?: string;
32+
label?: string;
33+
};
34+
2935
export async function compareReportFiles(
30-
inputPaths: Partial<Diff<string>>,
31-
persistConfig: Required<PersistConfig>,
32-
uploadConfig: UploadConfig | undefined,
33-
label?: string,
36+
config: {
37+
persist: Required<PersistConfig>;
38+
upload?: UploadConfig;
39+
},
40+
options?: CompareOptions,
3441
): Promise<string[]> {
35-
const { outputDir, filename, format } = persistConfig;
42+
const { outputDir, filename, format } = config.persist;
3643

3744
const defaultInputPath = (suffix: keyof Diff<string>) =>
3845
createReportPath({ outputDir, filename, format: 'json', suffix });
3946

4047
const [reportBefore, reportAfter] = await Promise.all([
41-
readJsonFile(inputPaths.before ?? defaultInputPath('before')),
42-
readJsonFile(inputPaths.after ?? defaultInputPath('after')),
48+
readJsonFile(options?.before ?? defaultInputPath('before')),
49+
readJsonFile(options?.after ?? defaultInputPath('after')),
4350
]);
4451
const reports: Diff<Report> = {
4552
before: reportSchema.parse(reportBefore),
4653
after: reportSchema.parse(reportAfter),
4754
};
4855

4956
const diff = compareReports(reports);
50-
if (label) {
51-
// eslint-disable-next-line functional/immutable-data
52-
diff.label = label;
53-
}
54-
if (uploadConfig && diff.commits) {
55-
// eslint-disable-next-line functional/immutable-data
56-
diff.portalUrl = await fetchPortalComparisonLink(
57-
uploadConfig,
58-
diff.commits,
59-
);
60-
}
57+
58+
const label = options?.label ?? getLabelFromReports(reports);
59+
const portalUrl =
60+
config.upload &&
61+
diff.commits &&
62+
(await fetchPortalComparisonLink(config.upload, diff.commits));
63+
64+
const diffWithLinks: ReportsDiff =
65+
label || portalUrl
66+
? { ...diff, ...(label && { label }), ...(portalUrl && { portalUrl }) }
67+
: diff;
6168

6269
return Promise.all(
6370
format.map(async fmt => {
@@ -67,7 +74,7 @@ export async function compareReportFiles(
6774
format: fmt,
6875
suffix: 'diff',
6976
});
70-
const content = reportsDiffToFileContent(diff, fmt);
77+
const content = reportsDiffToFileContent(diffWithLinks, fmt);
7178
await ensureDirectoryExists(outputDir);
7279
await writeFile(outputPath, content);
7380
return outputPath;
@@ -154,3 +161,14 @@ async function fetchPortalComparisonLink(
154161
throw error;
155162
}
156163
}
164+
165+
function getLabelFromReports(reports: Diff<Report>): string | undefined {
166+
if (
167+
reports.before.label &&
168+
reports.after.label &&
169+
reports.before.label === reports.after.label
170+
) {
171+
return reports.after.label;
172+
}
173+
return undefined;
174+
}

0 commit comments

Comments
 (0)