Skip to content

Commit 918eb0d

Browse files
committed
feat(ci): download previous report from portal if available
1 parent a73bf21 commit 918eb0d

File tree

3 files changed

+91
-22
lines changed

3 files changed

+91
-22
lines changed
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { mkdir, writeFile } from 'node:fs/promises';
2+
import path from 'node:path';
13
import {
24
type PortalDownloadArgs,
35
downloadFromPortal,
@@ -6,11 +8,23 @@ import { transformGQLReport } from './transform.js';
68

79
export async function downloadReportFromPortal(
810
args: PortalDownloadArgs,
9-
): Promise<string | undefined> {
11+
): Promise<string | null> {
1012
const gqlReport = await downloadFromPortal(args);
1113
if (!gqlReport) {
12-
return undefined;
14+
return null;
1315
}
16+
1417
const report = transformGQLReport(gqlReport);
15-
return JSON.stringify(report, null, 2);
18+
19+
const outputFile = path.join(
20+
'tmp',
21+
'code-pushup',
22+
'portal',
23+
args.parameters.organization,
24+
args.parameters.project,
25+
'report.json',
26+
);
27+
await mkdir(path.dirname(outputFile), { recursive: true });
28+
await writeFile(outputFile, JSON.stringify(report, null, 2));
29+
return outputFile;
1630
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ async function compareProjectsInBulk(
163163
): Promise<ProjectRunResult[]> {
164164
const projectReportsWithCache = await Promise.all(
165165
currProjectReports.map(async ({ project, ctx, reports, config }) => {
166-
const args = { project, base, ctx, env };
166+
const args = { project, config, base, ctx, env };
167167
const [currReport, prevReport] = await Promise.all([
168168
readFile(reports.json, 'utf8').then(
169169
(content): ReportData<'current'> => ({

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

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import type {
3434
} from './models.js';
3535
import type { ProjectConfig } from './monorepo/index.js';
3636
import { saveOutputFiles } from './output-files.js';
37+
import { downloadReportFromPortal } from './portal/download.js';
3738

3839
export type RunEnv = {
3940
refs: NormalizedGitRefs;
@@ -58,6 +59,7 @@ export type CompareReportsArgs = {
5859

5960
export type BaseReportArgs = {
6061
project: ProjectConfig | null;
62+
config: EnhancedPersistConfig;
6163
env: RunEnv;
6264
base: GitBranch;
6365
ctx: CommandContext;
@@ -139,7 +141,8 @@ export async function runOnProject(
139141
`PR/MR detected, preparing to compare base branch ${base.ref} to head ${head.ref}`,
140142
);
141143

142-
const prevReport = await collectPreviousReport({ project, env, base, ctx });
144+
const baseArgs: BaseReportArgs = { project, env, base, config, ctx };
145+
const prevReport = await collectPreviousReport(baseArgs);
143146
if (!prevReport) {
144147
return noDiffOutput;
145148
}
@@ -243,37 +246,89 @@ export async function loadCachedBaseReport(
243246
): Promise<ReportData<'previous'> | null> {
244247
const {
245248
project,
249+
env: { settings },
250+
} = args;
251+
252+
const cachedBaseReport =
253+
(await loadCachedBaseReportFromPortal(args)) ??
254+
(await loadCachedBaseReportFromArtifacts(args));
255+
256+
if (!cachedBaseReport) {
257+
return null;
258+
}
259+
return saveReportFiles({
260+
project,
261+
type: 'previous',
262+
files: { json: cachedBaseReport },
263+
settings,
264+
});
265+
}
266+
267+
async function loadCachedBaseReportFromArtifacts(
268+
args: BaseReportArgs,
269+
): Promise<string | null> {
270+
const {
246271
env: { api, settings },
272+
project,
247273
} = args;
248274
const { logger } = settings;
249275

250-
const cachedBaseReport = await api
251-
.downloadReportArtifact?.(project?.name)
276+
if (api.downloadReportArtifact == null) {
277+
return null;
278+
}
279+
280+
const reportPath = await api
281+
.downloadReportArtifact(project?.name)
252282
.catch((error: unknown) => {
253283
logger.warn(
254284
`Error when downloading previous report artifact, skipping - ${stringifyError(error)}`,
255285
);
286+
return null;
256287
});
257-
if (api.downloadReportArtifact != null) {
258-
logger.info(
259-
`Previous report artifact ${cachedBaseReport ? 'found' : 'not found'}`,
260-
);
261-
if (cachedBaseReport) {
262-
logger.debug(
263-
`Previous report artifact downloaded to ${cachedBaseReport}`,
264-
);
265-
}
288+
289+
logger.info(`Previous report artifact ${reportPath ? 'found' : 'not found'}`);
290+
if (reportPath) {
291+
logger.debug(`Previous report artifact downloaded to ${reportPath}`);
266292
}
267293

268-
if (!cachedBaseReport) {
294+
return reportPath;
295+
}
296+
297+
async function loadCachedBaseReportFromPortal(
298+
args: BaseReportArgs,
299+
): Promise<string | null> {
300+
const {
301+
config,
302+
env: { settings },
303+
} = args;
304+
const { logger } = settings;
305+
306+
if (!config.upload) {
269307
return null;
270308
}
271-
return saveReportFiles({
272-
project,
273-
type: 'previous',
274-
files: { json: cachedBaseReport },
275-
settings,
309+
310+
const reportPath = await downloadReportFromPortal({
311+
server: config.upload.server,
312+
apiKey: config.upload.apiKey,
313+
parameters: {
314+
organization: config.upload.organization,
315+
project: config.upload.project,
316+
},
317+
}).catch((error: unknown) => {
318+
logger.warn(
319+
`Error when downloading previous report from portal, skipping - ${stringifyError(error)}`,
320+
);
321+
return null;
276322
});
323+
324+
logger.info(
325+
`Previous report ${reportPath ? 'found' : 'not found'} in Code PushUp portal`,
326+
);
327+
if (reportPath) {
328+
logger.debug(`Previous report downloaded from portal to ${reportPath}`);
329+
}
330+
331+
return reportPath;
277332
}
278333

279334
export async function runInBaseBranch<T>(

0 commit comments

Comments
 (0)