Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Comment>` | Posts a new comment to PR |
| `updateComment` | yes | `(id: number, body: string) => Promise<Comment>` | Updates existing PR comment |
| `listComments` | yes | `() => Promise<Comment[]>` | Fetches all comments from PR |
| `maxCommentChars` | yes | `number` | Character limit for comment body |
| `downloadReportArtifact` | no | `() => Promise<string \| null>` | 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<Comment>` | Posts a new comment to PR |
| `updateComment` | yes | `(id: number, body: string) => Promise<Comment>` | Updates existing PR comment |
| `listComments` | yes | `() => Promise<Comment[]>` | Fetches all comments from PR |
| `maxCommentChars` | yes | `number` | Character limit for comment body |
| `downloadReportArtifact` | no | `(project?: string) => Promise<string \| null>` | 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:

Expand Down
4 changes: 2 additions & 2 deletions packages/ci/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ export type GitRefs = {
*/
export type ProviderAPIClient = {
maxCommentChars: number;
downloadReportArtifact?: () => Promise<string | null>;
downloadReportArtifact?: (project?: string) => Promise<string | null>;
listComments: () => Promise<Comment[]>;
updateComment: (id: number, body: string) => Promise<Comment>;
createComment: (body: string) => Promise<Comment>;
};

/**
* PR comment from {@link ProviderAPIClient}
* PR/MR comment from {@link ProviderAPIClient}
*/
export type Comment = {
id: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/ci/src/lib/run.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
35 changes: 22 additions & 13 deletions packages/ci/src/lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectRunResult> {
};

// eslint-disable-next-line max-lines-per-function
async function runOnProject(args: RunOnProjectArgs): Promise<ProjectRunResult> {
const {
project,
refs: { head, base },
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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<string | null> {
const { base, api, settings, ctx, git } = args;
};

async function collectPreviousReport(
args: CollectPreviousReportArgs,
): Promise<string | null> {
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'}`,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down