@@ -34,6 +34,7 @@ import type {
3434} from './models.js' ;
3535import type { ProjectConfig } from './monorepo/index.js' ;
3636import { saveOutputFiles } from './output-files.js' ;
37+ import { downloadReportFromPortal } from './portal/download.js' ;
3738
3839export type RunEnv = {
3940 refs : NormalizedGitRefs ;
@@ -58,6 +59,7 @@ export type CompareReportsArgs = {
5859
5960export 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
279334export async function runInBaseBranch < T > (
0 commit comments