From 29a6f088fe166fa34bfb79ae63726801210812e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Chalk?= Date: Tue, 23 Sep 2025 15:38:11 +0200 Subject: [PATCH 1/2] chore: remove unused upload env vars --- .env.example | 3 --- .github/workflows/code-pushup.yml | 3 --- 2 files changed, 6 deletions(-) diff --git a/.env.example b/.env.example index a7f0c55e2..64dd9ba97 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,2 @@ # for uploading to portal -CP_SERVER= CP_API_KEY= -CP_ORGANIZATION= -CP_PROJECT= \ No newline at end of file diff --git a/.github/workflows/code-pushup.yml b/.github/workflows/code-pushup.yml index a15c6c3e7..9cf5477ca 100644 --- a/.github/workflows/code-pushup.yml +++ b/.github/workflows/code-pushup.yml @@ -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 From 9f5b640c0e925594a776638bf9996aa99c290450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Chalk?= Date: Tue, 23 Sep 2025 16:04:37 +0200 Subject: [PATCH 2/2] fix(nx-plugin): remove undocument env vars parsing from executor --- packages/nx-plugin/mock/fixtures/env.ts | 6 +-- packages/nx-plugin/package.json | 3 +- .../src/executors/cli/executor.unit.test.ts | 17 ++++---- .../executors/internal/config.unit.test.ts | 4 -- .../nx-plugin/src/executors/internal/env.ts | 38 +++--------------- .../src/executors/internal/env.unit.test.ts | 40 ++++++------------- 6 files changed, 27 insertions(+), 81 deletions(-) diff --git a/packages/nx-plugin/mock/fixtures/env.ts b/packages/nx-plugin/mock/fixtures/env.ts index 09297696e..a97146a40 100644 --- a/packages/nx-plugin/mock/fixtures/env.ts +++ b/packages/nx-plugin/mock/fixtures/env.ts @@ -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', }; diff --git a/packages/nx-plugin/package.json b/packages/nx-plugin/package.json index f5b779192..4bd5b425e 100644 --- a/packages/nx-plugin/package.json +++ b/packages/nx-plugin/package.json @@ -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", diff --git a/packages/nx-plugin/src/executors/cli/executor.unit.test.ts b/packages/nx-plugin/src/executors/cli/executor.unit.test.ts index daab1594c..acad51ac5 100644 --- a/packages/nx-plugin/src/executors/cli/executor.unit.test.ts +++ b/packages/nx-plugin/src/executors/cli/executor.unit.test.ts @@ -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({ @@ -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); @@ -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'), @@ -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"'); diff --git a/packages/nx-plugin/src/executors/internal/config.unit.test.ts b/packages/nx-plugin/src/executors/internal/config.unit.test.ts index c38554459..cd8eb0aab 100644 --- a/packages/nx-plugin/src/executors/internal/config.unit.test.ts +++ b/packages/nx-plugin/src/executors/internal/config.unit.test.ts @@ -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), }), ); }); diff --git a/packages/nx-plugin/src/executors/internal/env.ts b/packages/nx-plugin/src/executors/internal/env.ts index 16806a09e..2fa28f64b 100644 --- a/packages/nx-plugin/src/executors/internal/env.ts +++ b/packages/nx-plugin/src/executors/internal/env.ts @@ -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; - -export function parseEnv(env: unknown = {}): Partial { - 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>) { + return { + ...(env.CP_API_KEY && { + apiKey: env.CP_API_KEY, }), - ); + } satisfies Partial; } diff --git a/packages/nx-plugin/src/executors/internal/env.unit.test.ts b/packages/nx-plugin/src/executors/internal/env.unit.test.ts index 4f5af94ab..c7d623901 100644 --- a/packages/nx-plugin/src/executors/internal/env.unit.test.ts +++ b/packages/nx-plugin/src/executors/internal/env.unit.test.ts @@ -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({}); }); });