Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make portal-client dependency optional #837

Merged
merged 2 commits into from
Oct 15, 2024
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
3 changes: 2 additions & 1 deletion e2e/plugin-lighthouse-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ describe('collect report with lighthouse-plugin NPM package', () => {

const { code, stdout } = await executeProcess({
command: 'npx',
args: ['@code-pushup/cli', 'collect', '--no-progress'],
// verbose exposes audits with perfect scores that are hidden in the default stdout
args: ['@code-pushup/cli', 'collect', '--no-progress', '--verbose'],
BioPhoton marked this conversation as resolved.
Show resolved Hide resolved
cwd,
});

Expand Down
21 changes: 20 additions & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,26 @@ _If you're looking for programmatic usage, then refer to the underlying [@code-p

## Portal integration

If you have access to the Code PushUp portal, provide credentials in order to upload reports.
If you have access to the Code PushUp portal, you can enable report uploads by installing the `@code-pushup/portal-client` package.

<details>
<summary>Installation command for <code>npm</code>, <code>yarn</code> and <code>pnpm</code></summary>

```sh
npm install --save-dev @code-pushup/portal-client
```

```sh
yarn add --dev @code-pushup/portal-client
```

```sh
pnpm add --save-dev @code-pushup/portal-client
```

</details>

Once the package is installed, update your configuration file to include your portal credentials:

```ts
const config: CoreConfig = {
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/lib/autorun/autorun-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export function yargsAutorunCommandObject() {
}

if (options.upload) {
const { url } = await upload(options);
uploadSuccessfulLog(url);
const report = await upload(options);
if (report?.url) {
uploadSuccessfulLog(report.url);
}
} else {
ui().logger.warning('Upload skipped because configuration is not set.');
renderIntegratePortalHint();
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/lib/upload/upload-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export function yargsUploadCommandObject() {
renderIntegratePortalHint();
throw new Error('Upload configuration not set');
}
const { url } = await upload(options);
uploadSuccessfulLog(url);
const report = await upload(options);
if (report?.url) {
uploadSuccessfulLog(report.url);
}
},
} satisfies CommandModule;
}
9 changes: 8 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
"dependencies": {
"@code-pushup/models": "0.51.0",
"@code-pushup/utils": "0.51.0",
"@code-pushup/portal-client": "^0.9.0",
"ansis": "^3.3.0"
},
"peerDependencies": {
"@code-pushup/portal-client": "^0.9.0"
},
"peerDependenciesMeta": {
"@code-pushup/portal-client": {
"optional": true
}
}
}
10 changes: 6 additions & 4 deletions packages/core/src/lib/compare.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import {
PortalOperationError,
getPortalComparisonLink,
} from '@code-pushup/portal-client';
import {
type Format,
type PersistConfig,
Expand All @@ -28,6 +24,7 @@
compareCategories,
compareGroups,
} from './implementation/compare-scorables';
import { loadPortalClient } from './load-portal-client';

export async function compareReportFiles(
inputPaths: Diff<string>,
Expand Down Expand Up @@ -119,6 +116,11 @@
commits: NonNullable<ReportsDiff['commits']>,
): Promise<string | undefined> {
const { server, apiKey, organization, project } = uploadConfig;
const portalClient = await loadPortalClient();
if (!portalClient) {

Check failure on line 120 in packages/core/src/lib/compare.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
return;
}

Check warning on line 122 in packages/core/src/lib/compare.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 121-122 are not covered in any test case.
const { PortalOperationError, getPortalComparisonLink } = portalClient;
try {
return await getPortalComparisonLink({
server,
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/lib/load-portal-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ui } from '@code-pushup/utils';

Check failure on line 1 in packages/core/src/lib/load-portal-client.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.

export async function loadPortalClient(): Promise<
typeof import('@code-pushup/portal-client') | null
> {
try {
return await import('@code-pushup/portal-client');
} catch {

Check failure on line 8 in packages/core/src/lib/load-portal-client.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
ui().logger.error(
'Optional peer dependency @code-pushup/portal-client is not available. Make sure it is installed to enable upload functionality.',
);
return null;
}

Check warning on line 13 in packages/core/src/lib/load-portal-client.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 9-13 are not covered in any test case.
}
18 changes: 9 additions & 9 deletions packages/core/src/lib/upload.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
type SaveReportMutationVariables,
uploadToPortal,
} from '@code-pushup/portal-client';
import type { SaveReportMutationVariables } from '@code-pushup/portal-client';
import type { PersistConfig, Report, UploadConfig } from '@code-pushup/models';
import { loadReport } from '@code-pushup/utils';
import { reportToGQL } from './implementation/report-to-gql';
import { loadPortalClient } from './load-portal-client';
import type { GlobalOptions } from './types';

export type UploadOptions = { upload?: UploadConfig } & {
Expand All @@ -16,13 +14,15 @@
* @param options
* @param uploadFn
*/
export async function upload(
options: UploadOptions,
uploadFn: typeof uploadToPortal = uploadToPortal,
) {
export async function upload(options: UploadOptions) {
if (options.upload == null) {
throw new Error('Upload configuration is not set.');
}
const portalClient = await loadPortalClient();
if (!portalClient) {

Check failure on line 22 in packages/core/src/lib/upload.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
return;
}

Check warning on line 24 in packages/core/src/lib/upload.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 23-24 are not covered in any test case.
const { uploadToPortal } = portalClient;
const { apiKey, server, organization, project, timeout } = options.upload;
const report: Report = await loadReport({
...options.persist,
Expand All @@ -39,5 +39,5 @@
...reportToGQL(report),
};

return uploadFn({ apiKey, server, data, timeout });
return uploadToPortal({ apiKey, server, data, timeout });
}