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
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# for uploading to portal
CP_SERVER=
CP_API_KEY=
CP_ORGANIZATION=
CP_PROJECT=
3 changes: 0 additions & 3 deletions .github/workflows/code-pushup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ jobs:
# ignore PRs from forks, handled by code-pushup-fork.yml
if: ${{ !github.event.pull_request.head.repo.fork }}
env:
CP_SERVER: ${{ secrets.CP_SERVER }}
CP_API_KEY: ${{ secrets.CP_API_KEY }}
CP_ORGANIZATION: code-pushup
CP_PROJECT: cli
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
6 changes: 1 addition & 5 deletions packages/nx-plugin/mock/fixtures/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export const ENV = {
CP_SERVER: 'https://portal.code.pushup.dev',
CP_ORGANIZATION: 'code-pushup',
CP_PROJECT: 'utils',
CP_API_KEY: '23456789098765432345678909876543',
CP_TIMEOUT: '9',
CP_API_KEY: 'cp_0123456789abcdefghijklmnopqrstuvwxyz',
};
3 changes: 1 addition & 2 deletions packages/nx-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
"@code-pushup/utils": "0.79.1",
"@nx/devkit": ">=17.0.0",
"ansis": "^3.3.0",
"nx": ">=17.0.0",
"zod": "^4.0.5"
"nx": ">=17.0.0"
},
"files": [
"src",
Expand Down
17 changes: 8 additions & 9 deletions packages/nx-plugin/src/executors/cli/executor.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ describe('runAutorunExecutor', () => {
const loggerWarnSpy = vi.spyOn(logger, 'warn');
const executeProcessSpy = vi.spyOn(executeProcessModule, 'executeProcess');

/* eslint-disable functional/immutable-data, @typescript-eslint/no-dynamic-delete */
beforeAll(() => {
Object.entries(process.env)
.filter(([k]) => k.startsWith('CP_'))
.forEach(([k]) => delete process.env[k]);
});

afterAll(() => {
Object.entries(processEnvCP).forEach(([k, v]) => (process.env[k] = v));
});

beforeEach(() => {
vi.unstubAllEnvs();
executeProcessSpy.mockResolvedValue({
Expand All @@ -37,11 +40,6 @@ describe('runAutorunExecutor', () => {
executeProcessSpy.mockReset();
});

afterAll(() => {
Object.entries(processEnvCP).forEach(([k, v]) => (process.env[k] = v));
});
/* eslint-enable functional/immutable-data, @typescript-eslint/no-dynamic-delete */

it('should call executeProcess with return result', async () => {
const output = await runAutorunExecutor({}, executorContext('utils'));
expect(output.success).toBe(true);
Expand Down Expand Up @@ -89,7 +87,6 @@ describe('runAutorunExecutor', () => {
});

it('should create command from context and options if no api key is set', async () => {
vi.stubEnv('CP_PROJECT', 'CLI');
const output = await runAutorunExecutor(
{ persist: { filename: 'REPORT', format: ['md', 'json'] } },
executorContext('core'),
Expand All @@ -102,9 +99,11 @@ describe('runAutorunExecutor', () => {

it('should create command from context, options and arguments if api key is set', async () => {
vi.stubEnv('CP_API_KEY', 'cp_1234567');
vi.stubEnv('CP_PROJECT', 'CLI');
const output = await runAutorunExecutor(
{ persist: { filename: 'REPORT', format: ['md', 'json'] } },
{
persist: { filename: 'REPORT', format: ['md', 'json'] },
upload: { project: 'CLI' },
},
executorContext('core'),
);
expect(output.command).toMatch('--persist.filename="REPORT"');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,7 @@ describe('uploadConfig', () => {
),
).toEqual(
expect.objectContaining({
server: ENV.CP_SERVER,
apiKey: ENV.CP_API_KEY,
organization: ENV.CP_ORGANIZATION,
project: ENV.CP_PROJECT,
timeout: Number(ENV.CP_TIMEOUT),
}),
);
});
Expand Down
38 changes: 5 additions & 33 deletions packages/nx-plugin/src/executors/internal/env.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
import { z } from 'zod';
import type { UploadConfig } from '@code-pushup/models';

// load upload configuration from environment
const envSchema = z
.object({
CP_SERVER: z.string().url().optional(),
CP_API_KEY: z.string().min(1).optional(),
CP_ORGANIZATION: z.string().min(1).optional(),
CP_PROJECT: z.string().min(1).optional(),
CP_TIMEOUT: z.string().regex(/^\d+$/).optional(),
})
.partial();

type UploadEnvVars = z.infer<typeof envSchema>;

export function parseEnv(env: unknown = {}): Partial<UploadConfig> {
const upload: UploadEnvVars = envSchema.parse(env);
return Object.fromEntries(
Object.entries(upload).map(([envKey, value]) => {
switch (envKey) {
case 'CP_SERVER':
return ['server', value];
case 'CP_API_KEY':
return ['apiKey', value];
case 'CP_ORGANIZATION':
return ['organization', value];
case 'CP_PROJECT':
return ['project', value];
case 'CP_TIMEOUT':
return Number(value) >= 0 ? ['timeout', Number(value)] : [];
default:
return [];
}
export function parseEnv(env: Partial<Record<string, string>>) {
return {
...(env.CP_API_KEY && {
apiKey: env.CP_API_KEY,
}),
);
} satisfies Partial<UploadConfig>;
}
40 changes: 12 additions & 28 deletions packages/nx-plugin/src/executors/internal/env.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,23 @@ import { describe, expect } from 'vitest';
import { parseEnv } from './env.js';

describe('parseEnv', () => {
it('should parse empty env vars', () => {
expect(parseEnv({})).toEqual({});
it('should parse CP_API_KEY environment variable', () => {
expect(parseEnv({ CP_API_KEY: 'cp_0123456789' })).toStrictEqual({
apiKey: 'cp_0123456789',
});
});

it('should parse process.env.CP_SERVER option', () => {
expect(parseEnv({ CP_SERVER: 'https://portal.code.pushup.dev' })).toEqual(
expect.objectContaining({ server: 'https://portal.code.pushup.dev' }),
);
it('should not parse API key if missing CP_API_KEY in environment', () => {
expect(parseEnv({})).toStrictEqual({});
});

it('should parse process.env.CP_ORGANIZATION option', () => {
expect(parseEnv({ CP_ORGANIZATION: 'code-pushup' })).toEqual(
expect.objectContaining({ organization: 'code-pushup' }),
);
it('should ignore CP_API_KEY if empty string', () => {
expect(parseEnv({ CP_API_KEY: '' })).toStrictEqual({});
});

it('should parse process.env.CP_PROJECT option', () => {
expect(parseEnv({ CP_PROJECT: 'cli-utils' })).toEqual(
expect.objectContaining({ project: 'cli-utils' }),
);
});

it('should parse process.env.CP_TIMEOUT option', () => {
expect(parseEnv({ CP_TIMEOUT: '3' })).toEqual(
expect.objectContaining({ timeout: 3 }),
);
});

it('should throw for process.env.CP_TIMEOUT option < 0', () => {
expect(() => parseEnv({ CP_TIMEOUT: '-1' })).toThrow('Invalid string');
});

it('should throw for invalid URL in process.env.CP_SERVER option', () => {
expect(() => parseEnv({ CP_SERVER: 'httptpt' })).toThrow('Invalid URL');
it('should ignore other environment variables', () => {
expect(
parseEnv({ CP_SERVER: 'https://api.code-pushup.example.com/graphql' }),
).toStrictEqual({});
});
});
Loading