diff --git a/packages/ci/README.md b/packages/ci/README.md index 801422e3b..c0522c5aa 100644 --- a/packages/ci/README.md +++ b/packages/ci/README.md @@ -14,10 +14,10 @@ This package exports **provider-agnostic core logic for running Code PushUp in CI pipelines**. It serves as the base for the following provider integrations: -| | | -| :------------- | :-------------------------------------------------------------------------------- | -| GitHub Actions | [`code-pushup/github-action`](https://github.com/marketplace/actions/code-pushup) | -| GitLab CI/CD | _coming soon_ | +| | | +| :------------- | :-------------------------------------------------------------------------------------------------- | +| GitHub Actions | [`code-pushup/github-action`](https://github.com/marketplace/actions/code-pushup) | +| GitLab CI/CD | [`code-pushup/gitlab-pipelines-template`](https://gitlab.com/code-pushup/gitlab-pipelines-template) | ## Setup @@ -74,13 +74,13 @@ This will additionally compare reports from both source and target branches and The PR flow requires interacting with the Git provider's API to post a comparison comment. Wrap these requests in functions and pass them in as an object which configures the provider. -| Property | Required | Type | Description | -| :----------------------- | :------: | :----------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- | -| `createComment` | yes | `(body: string) => Promise` | Posts a new comment to PR | -| `updateComment` | yes | `(id: number, body: string) => Promise` | Updates existing PR comment | -| `listComments` | yes | `() => Promise` | Fetches all comments from PR | -| `maxCommentChars` | yes | `number` | Character limit for comment body | -| `downloadReportArtifact` | no | `() => Promise` | Fetches previous report for base branch (returns path to downloaded `report.json`), used as cache to speed up comparison | +| Property | Required | Type | Description | +| :----------------------- | :------: | :----------------------------------------------- | :------------------------------------------------------------------------------------------------------------------- | +| `createComment` | yes | `(body: string) => Promise` | Posts a new comment to PR | +| `updateComment` | yes | `(id: number, body: string) => Promise` | Updates existing PR comment | +| `listComments` | yes | `() => Promise` | Fetches all comments from PR | +| `maxCommentChars` | yes | `number` | Character limit for comment body | +| `downloadReportArtifact` | no | `(project?: string) => Promise` | Fetches previous (root/project) `report.json` for base branch and returns path, used as cache to speed up comparison | A `Comment` object has the following required properties: diff --git a/packages/ci/src/lib/models.ts b/packages/ci/src/lib/models.ts index 99a59f9c3..258a1101a 100644 --- a/packages/ci/src/lib/models.ts +++ b/packages/ci/src/lib/models.ts @@ -37,14 +37,14 @@ export type GitRefs = { */ export type ProviderAPIClient = { maxCommentChars: number; - downloadReportArtifact?: () => Promise; + downloadReportArtifact?: (project?: string) => Promise; listComments: () => Promise; updateComment: (id: number, body: string) => Promise; createComment: (body: string) => Promise; }; /** - * PR comment from {@link ProviderAPIClient} + * PR/MR comment from {@link ProviderAPIClient} */ export type Comment = { id: number; diff --git a/packages/ci/src/lib/run.integration.test.ts b/packages/ci/src/lib/run.integration.test.ts index f306f0d70..dc3371c59 100644 --- a/packages/ci/src/lib/run.integration.test.ts +++ b/packages/ci/src/lib/run.integration.test.ts @@ -328,7 +328,7 @@ describe('runInCI', () => { expect.stringContaining(diffMdString), ); expect(api.createComment).not.toHaveBeenCalled(); - expect(api.downloadReportArtifact).toHaveBeenCalledWith(); + expect(api.downloadReportArtifact).toHaveBeenCalledWith(undefined); expect(utils.executeProcess).toHaveBeenCalledTimes(2); expect(utils.executeProcess).toHaveBeenNthCalledWith(1, { diff --git a/packages/ci/src/lib/run.ts b/packages/ci/src/lib/run.ts index bba1c4da7..276f50ce0 100644 --- a/packages/ci/src/lib/run.ts +++ b/packages/ci/src/lib/run.ts @@ -101,14 +101,16 @@ export async function runInCI( return { mode: 'standalone', artifacts, newIssues }; } -// eslint-disable-next-line max-lines-per-function -async function runOnProject(args: { +type RunOnProjectArgs = { project: ProjectConfig | null; refs: GitRefs; api: ProviderAPIClient; settings: Settings; git: SimpleGit; -}): Promise { +}; + +// eslint-disable-next-line max-lines-per-function +async function runOnProject(args: RunOnProjectArgs): Promise { const { project, refs: { head, base }, @@ -140,7 +142,7 @@ async function runOnProject(args: { } logger.info( - `PR detected, preparing to compare base branch ${base.ref} to head ${head.ref}`, + `PR/MR detected, preparing to compare base branch ${base.ref} to head ${head.ref}`, ); const prevReport = await collectPreviousReport({ ...args, base, ctx }); @@ -188,17 +190,24 @@ async function runOnProject(args: { return { ...diffOutput, newIssues }; } -async function collectPreviousReport(args: { +type CollectPreviousReportArgs = RunOnProjectArgs & { base: GitBranch; - api: ProviderAPIClient; - settings: Settings; ctx: CommandContext; - git: SimpleGit; -}): Promise { - const { base, api, settings, ctx, git } = args; +}; + +async function collectPreviousReport( + args: CollectPreviousReportArgs, +): Promise { + const { project, base, api, settings, ctx, git } = args; const logger = settings.logger; - const cachedBaseReport = await api.downloadReportArtifact?.(); + const cachedBaseReport = await api + .downloadReportArtifact?.(project?.name) + .catch((error: unknown) => { + logger.warn( + `Error when downloading previous report artifact, skipping - ${stringifyError(error)}`, + ); + }); if (api.downloadReportArtifact != null) { logger.info( `Previous report artifact ${cachedBaseReport ? 'found' : 'not found'}`, @@ -235,7 +244,7 @@ async function collectPreviousReport(args: { logger.debug(`Collected previous report at ${prevReportPath}`); await git.checkout(['-f', '-']); - logger.info('Switched back to PR branch'); + logger.info('Switched back to PR/MR branch'); return prevReport; } @@ -266,7 +275,7 @@ async function findNewIssues(args: { logger.debug( `Found ${issues.length} relevant issues for ${ Object.keys(changedFiles).length - } changed files and created GitHub annotations`, + } changed files`, ); return issues;