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
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export default tseslint.config(
],
},
},
{
files: ['**/*.type.test.ts'],
rules: {
'vitest/expect-expect': 'off',
},
},
{
files: ['**/*.json'],
languageOptions: { parser: jsoncParser },
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"node": ">=22.14"
},
"dependencies": {
"@code-pushup/portal-client": "^0.13.0",
"@code-pushup/portal-client": "^0.14.3",
"@isaacs/cliui": "^8.0.2",
"@nx/devkit": "19.8.13",
"@poppinss/cliui": "6.4.1",
Expand Down
27 changes: 27 additions & 0 deletions packages/ci/mocks/fixtures/outputs/config.portal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"persist": {
"outputDir": ".code-pushup",
"filename": "report",
"format": ["json", "md"]
},
"upload": {
"server": "https://api.code-pushup.dunder-mifflin.org/graphql",
"apiKey": "cp_abcdef0123456789",
"organization": "dunder-mifflin",
"project": "website"
},
"categories": [],
"plugins": [
{
"title": "TypeScript migration",
"slug": "ts-migration",
"icon": "typescript",
"audits": [
{
"slug": "ts-files",
"title": "Source files converted from JavaScript to TypeScript"
}
]
}
]
}
4 changes: 2 additions & 2 deletions packages/ci/mocks/fixtures/outputs/diff-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"hash": "efed65b3ffab808176c4f8670d77f8d69f71490e",
"message": "Initial commit",
"date": "2024-10-14T09:00:13.000Z",
"author": "John Doe"
"author": "John Doe <john.doe@example.com>"
},
"after": {
"hash": "2f3b5365432abb7c9949aa50ff3aa8b7a02256de",
"message": "Convert JS file to TS",
"date": "2024-10-14T09:00:13.000Z",
"author": "John Doe"
"author": "John Doe <john.doe@example.com>"
}
},
"categories": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ci/mocks/fixtures/outputs/report-after.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"hash": "99781f731759ef36b2fb7e4a39703965904f5376",
"message": "Convert JS file to TS",
"date": "2024-10-14T09:55:21.000Z",
"author": "John Doe"
"author": "John Doe <john.doe@example.com>"
},
"packageName": "@code-pushup/core",
"version": "0.51.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ci/mocks/fixtures/outputs/report-before.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"hash": "efed65b3ffab808176c4f8670d77f8d69f71490e",
"message": "Initial commit",
"date": "2024-10-14T09:00:13.000Z",
"author": "John Doe"
"author": "John Doe <john.doe@example.com>"
},
"packageName": "@code-pushup/core",
"version": "0.51.0",
Expand Down
82 changes: 82 additions & 0 deletions packages/ci/mocks/fixtures/outputs/report-before.portal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"__typename": "Report",
"commit": {
"__typename": "Commit",
"sha": "efed65b3ffab808176c4f8670d77f8d69f71490e",
"message": "Initial commit",
"date": "2024-10-14T09:00:13.000Z",
"author": {
"__typename": "CommitAuthor",
"name": "John Doe",
"email": "john.doe@example.com"
}
},
"packageName": "@code-pushup/core",
"packageVersion": "0.51.0",
"commandStartDate": "2024-10-14T09:00:14.969Z",
"commandDuration": 14,
"categories": [],
"plugins": [
{
"__typename": "Plugin",
"slug": "ts-migration",
"title": "TypeScript migration",
"icon": "typescript",
"description": null,
"docsUrl": null,
"packageName": null,
"packageVersion": null,
"runnerStartDate": "2024-10-14T09:00:14.978Z",
"runnerDuration": 2,
"audits": {
"edges": [
{
"node": {
"__typename": "Audit",
"slug": "ts-files",
"title": "Source files converted from JavaScript to TypeScript",
"description": null,
"docsUrl": null,
"score": 0.5,
"value": 50,
"formattedValue": "50% converted",
"details": {
"enabled": true,
"tables": [],
"trees": []
}
}
}
]
},
"groups": []
}
],
"issues": {
"edges": [
{
"node": {
"__typename": "Issue",
"message": "Use .ts file extension instead of .js",
"severity": "WARNING",
"audit": {
"__typename": "Audit",
"slug": "ts-files",
"plugin": {
"__typename": "Plugin",
"slug": "ts-migration"
}
},
"source": {
"__typename": "SourceCodeLocation",
"filePath": "index.js",
"startLine": null,
"startColumn": null,
"endLine": null,
"endColumn": null
}
}
}
]
}
}
1 change: 1 addition & 0 deletions packages/ci/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"type": "module",
"dependencies": {
"@code-pushup/models": "0.70.0",
"@code-pushup/portal-client": "^0.14.3",
"@code-pushup/utils": "0.70.0",
"glob": "^11.0.1",
"simple-git": "^3.20.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/ci/src/lib/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ export { runCompare } from './commands/compare.js';
export { runMergeDiffs } from './commands/merge-diffs.js';
export { runPrintConfig } from './commands/print-config.js';
export { createCommandContext, type CommandContext } from './context.js';
export { persistedFilesFromConfig } from './persist.js';
export {
parsePersistConfig,
persistedFilesFromConfig,
type EnhancedPersistConfig,
} from './persist.js';
18 changes: 13 additions & 5 deletions packages/ci/src/lib/cli/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
DEFAULT_PERSIST_OUTPUT_DIR,
type Format,
persistConfigSchema,
uploadConfigSchema,
} from '@code-pushup/models';
import { objectFromEntries, stringifyError } from '@code-pushup/utils';
import { objectFromEntries } from '@code-pushup/utils';

export type EnhancedPersistConfig = Pick<CoreConfig, 'persist' | 'upload'>;

export function persistedFilesFromConfig(
config: Pick<CoreConfig, 'persist'>,
config: EnhancedPersistConfig,
{ isDiff, directory }: { isDiff?: boolean; directory: string },
): Record<Format, string> {
const {
Expand All @@ -36,11 +39,16 @@ export function persistedFilesFromConfig(

export async function parsePersistConfig(
json: unknown,
): Promise<Pick<CoreConfig, 'persist'>> {
const schema = z.object({ persist: persistConfigSchema.optional() });
): Promise<EnhancedPersistConfig> {
const schema = z.object({
persist: persistConfigSchema.optional(),
upload: uploadConfigSchema.optional(),
});
const result = await schema.safeParseAsync(json);
if (result.error) {
throw new Error(`Invalid persist config - ${stringifyError(result.error)}`);
throw new Error(
`Code PushUp config is invalid:\n${z.prettifyError(result.error)}`,
);
}
return result.data;
}
53 changes: 50 additions & 3 deletions packages/ci/src/lib/cli/persist.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import path from 'node:path';
import type { CoreConfig } from '@code-pushup/models';
import { parsePersistConfig, persistedFilesFromConfig } from './persist.js';
import {
type EnhancedPersistConfig,
parsePersistConfig,
persistedFilesFromConfig,
} from './persist.js';

describe('persistedFilesFromConfig', () => {
it('should return default report paths when no config is set', () => {
Expand Down Expand Up @@ -72,17 +76,47 @@ describe('persistedFilesFromConfig', () => {
});

describe('parsePersistConfig', () => {
it('should validate only persist config', async () => {
it('should validate only persist and upload config', async () => {
await expect(
parsePersistConfig({
persist: {
outputDir: '.code-pushup',
filename: 'report',
format: ['json', 'md'],
},
upload: {
server: 'https://code-pushup-api.dunder-mifflin.org/graphql',
apiKey: 'cp_abcdef0123456789',
organization: 'dunder-mifflin',
project: 'website',
},
// missing props (slug, etc.)
plugins: [{ title: 'some plugin', audits: [{ title: 'some audit' }] }],
} as CoreConfig),
).resolves.toEqual({
persist: {
outputDir: '.code-pushup',
filename: 'report',
format: ['json', 'md'],
},
upload: {
server: 'https://code-pushup-api.dunder-mifflin.org/graphql',
apiKey: 'cp_abcdef0123456789',
organization: 'dunder-mifflin',
project: 'website',
},
} satisfies EnhancedPersistConfig);
});

it('should accept missing upload config', async () => {
await expect(
parsePersistConfig({
persist: {
outputDir: '.code-pushup',
filename: 'report',
format: ['json', 'md'],
},
}),
).resolves.toEqual({
persist: {
outputDir: '.code-pushup',
Expand Down Expand Up @@ -114,7 +148,20 @@ describe('parsePersistConfig', () => {
await expect(
parsePersistConfig({ persist: { format: ['json', 'html'] } }),
).rejects.toThrow(
/^Invalid persist config - ZodError:.*Invalid option: expected one of \\"json\\"\|\\"md\\"/s,
/^Code PushUp config is invalid.*Invalid option: expected one of "json"\|"md".*at persist\.format\[1]/s,
);
});

it('should error if upload config is invalid', async () => {
await expect(
parsePersistConfig({
upload: {
organization: 'dunder-mifflin',
project: 'website',
},
}),
).rejects.toThrow(
/^Code PushUp config is invalid.*Invalid input: expected string, received undefined.*at upload\.server.*at upload\.apiKey/s,
);
});
});
Loading
Loading