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
1 change: 1 addition & 0 deletions packages/ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Optionally, you can override default options for further customization:
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
| `logger` | `Logger` | `console` | Logger for reporting progress and encountered problems |
| `skipComment` | `boolean` | `false` | Toggles if comparison comment is posted to PR |

[^1]: By default, the `code-pushup.config` file is autodetected as described in [`@code-pushup/cli` docs](../cli/README.md#configuration).

Expand Down
1 change: 1 addition & 0 deletions packages/ci/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const DEFAULT_SETTINGS: Settings = {
detectNewIssues: true,
logger: console,
nxProjectsFilter: '--with-target={task}',
skipComment: false,
};
1 change: 1 addition & 0 deletions packages/ci/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Options = {
debug?: boolean;
detectNewIssues?: boolean;
logger?: Logger;
skipComment?: boolean;
};

/**
Expand Down
10 changes: 8 additions & 2 deletions packages/ci/src/lib/run-monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
type RunEnv,
checkPrintConfig,
compareReports,
ensureHeadBranch,
loadCachedBaseReport,
printPersistConfig,
runInBaseBranch,
Expand All @@ -47,6 +48,8 @@ export async function runInMonorepoMode(

logger.info('Running Code PushUp in monorepo mode');

await ensureHeadBranch(env);

const { projects, runManyCommand } = await listMonorepoProjects(settings);
const projectResults = runManyCommand
? await runProjectsInBulk(projects, runManyCommand, env)
Expand All @@ -55,6 +58,7 @@ export async function runInMonorepoMode(
const diffJsonPaths = projectResults
.map(({ files }) => files.diff?.json)
.filter((file): file is string => file != null);

if (diffJsonPaths.length > 0) {
const tmpDiffPath = await runMergeDiffs(
diffJsonPaths,
Expand All @@ -70,12 +74,14 @@ export async function runInMonorepoMode(
await copyFile(tmpDiffPath, diffPath);
logger.debug(`Copied ${tmpDiffPath} to ${diffPath}`);
}
const commentId = await commentOnPR(tmpDiffPath, api, logger);
const commentId = settings.skipComment
? null
: await commentOnPR(tmpDiffPath, api, logger);
return {
mode: 'monorepo',
projects: projectResults,
commentId,
diffPath,
...(commentId != null && { commentId }),
};
}

Expand Down
12 changes: 6 additions & 6 deletions packages/ci/src/lib/run-standalone.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { commentOnPR } from './comment.js';
import type { StandaloneRunResult } from './models.js';
import { type RunEnv, runOnProject } from './run-utils.js';
import { type RunEnv, ensureHeadBranch, runOnProject } from './run-utils.js';

export async function runInStandaloneMode(
env: RunEnv,
): Promise<StandaloneRunResult> {
const {
api,
settings: { logger },
} = env;
const { api, settings } = env;
const { logger } = settings;

logger.info('Running Code PushUp in standalone project mode');

await ensureHeadBranch(env);

const { files, newIssues } = await runOnProject(null, env);

const commentMdPath = files.diff?.md;
if (commentMdPath) {
if (!settings.skipComment && commentMdPath) {
const commentId = await commentOnPR(commentMdPath, api, logger);
return {
mode: 'standalone',
Expand Down
7 changes: 7 additions & 0 deletions packages/ci/src/lib/run-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@
return null;
}

export async function ensureHeadBranch({ refs, git }: RunEnv): Promise<void> {

Check warning on line 226 in packages/ci/src/lib/run-utils.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Functions coverage

Missing functions documentation for ensureHeadBranch
const { head } = refs;
if ((await git.revparse('HEAD')) !== (await git.revparse(head.ref))) {

Check failure on line 228 in packages/ci/src/lib/run-utils.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
await git.checkout(['-f', head.ref]);
}

Check warning on line 230 in packages/ci/src/lib/run-utils.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 229-230 are not covered in any test case.
}

export async function runInBaseBranch<T>(
base: GitBranch,
env: RunEnv,
Expand Down
91 changes: 91 additions & 0 deletions packages/ci/src/lib/run.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,43 @@ describe('runInCI', () => {
expect(logger.info).toHaveBeenCalled();
expect(logger.debug).toHaveBeenCalled();
});

it('should skip comment if disabled', async () => {
const api: ProviderAPIClient = {
maxCommentChars: 1_000_000,
createComment: vi.fn(),
updateComment: vi.fn(),
listComments: vi.fn().mockResolvedValue([]),
};

await expect(
runInCI(refs, api, { ...options, skipComment: true }, git),
).resolves.toEqual({
mode: 'standalone',
commentId: undefined,
newIssues: [],
files: {
report: {
json: path.join(outputDir, 'report.json'),
md: path.join(outputDir, 'report.md'),
},
diff: {
json: path.join(outputDir, 'report-diff.json'),
md: path.join(outputDir, 'report-diff.md'),
},
},
} satisfies RunResult);

expect(api.listComments).not.toHaveBeenCalled();
expect(api.createComment).not.toHaveBeenCalled();
expect(api.updateComment).not.toHaveBeenCalled();

expect(utils.executeProcess).toHaveBeenCalledWith({
command: options.bin,
args: expect.arrayContaining(['compare']),
cwd: workDir,
} satisfies utils.ProcessConfig);
});
});
});

Expand Down Expand Up @@ -743,6 +780,60 @@ describe('runInCI', () => {
expect(logger.info).toHaveBeenCalled();
expect(logger.debug).toHaveBeenCalled();
});

it('should skip comment if disabled', async () => {
const api: ProviderAPIClient = {
maxCommentChars: 1_000_000,
createComment: vi.fn(),
updateComment: vi.fn(),
listComments: vi.fn(),
};

await expect(
runInCI(
refs,
api,
{ ...options, monorepo: tool, skipComment: true },
git,
),
).resolves.toEqual({
mode: 'monorepo',
commentId: undefined,
diffPath: path.join(workDir, '.code-pushup/merged-report-diff.md'),
projects: [
expect.objectContaining({ name: 'cli' }),
expect.objectContaining({ name: 'core' }),
expect.objectContaining({ name: 'utils' }),
],
} satisfies RunResult);

await expect(
readFile(
path.join(workDir, '.code-pushup/merged-report-diff.md'),
'utf8',
),
).resolves.toBe(diffMdString);

expect(api.listComments).not.toHaveBeenCalled();
expect(api.createComment).not.toHaveBeenCalled();
expect(api.updateComment).not.toHaveBeenCalled();

expect(utils.executeProcess).toHaveBeenCalledWith({
command: runMany,
args: expect.any(Array),
cwd: expect.stringContaining(workDir),
} satisfies utils.ProcessConfig);
expect(utils.executeProcess).toHaveBeenCalledWith({
command: run,
args: expect.arrayContaining(['compare']),
cwd: expect.stringContaining(workDir),
} satisfies utils.ProcessConfig);
expect(utils.executeProcess).toHaveBeenCalledWith({
command: run,
args: expect.arrayContaining(['merge-diffs']),
cwd: expect.stringContaining(workDir),
} satisfies utils.ProcessConfig);
});
});
});

Expand Down
Loading