-
Notifications
You must be signed in to change notification settings - Fork 16
feat(ci): add helper function for parsing configPatterns from json string #1079
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ba1149a
feat(models): export default persist config
matejchalk 5f88d59
feat(ci): add helper function for parsing configPatterns from json st…
matejchalk 6d8a69e
feat(ci): export default settings and min/max limits
matejchalk 7e906d6
feat(models): export default persist.skipReports value
matejchalk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
export type { SourceFileIssue } from './lib/issues.js'; | ||
export type * from './lib/models.js'; | ||
export { | ||
MONOREPO_TOOLS, | ||
isMonorepoTool, | ||
MONOREPO_TOOLS, | ||
type MonorepoTool, | ||
} from './lib/monorepo/index.js'; | ||
export { runInCI } from './lib/run.js'; | ||
export { configPatternsSchema } from './lib/schemas.js'; | ||
export { | ||
DEFAULT_SETTINGS, | ||
MAX_SEARCH_COMMITS, | ||
MIN_SEARCH_COMMITS, | ||
parseConfigPatternsFromString, | ||
} from './lib/settings.js'; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ZodError, z } from 'zod'; | ||
import { | ||
DEFAULT_PERSIST_CONFIG, | ||
persistConfigSchema, | ||
slugSchema, | ||
uploadConfigSchema, | ||
} from '@code-pushup/models'; | ||
import { interpolate } from '@code-pushup/utils'; | ||
|
||
// eslint-disable-next-line unicorn/prefer-top-level-await, unicorn/catch-error-name | ||
export const interpolatedSlugSchema = slugSchema.catch(ctx => { | ||
// allow {projectName} interpolation (invalid slug) | ||
if ( | ||
typeof ctx.value === 'string' && | ||
ctx.issues.length === 1 && | ||
ctx.issues[0]?.code === 'invalid_format' | ||
) { | ||
// if only regex failed, try if it would pass once we insert known variables | ||
const { success } = slugSchema.safeParse( | ||
interpolate(ctx.value, { projectName: 'example' }), | ||
); | ||
if (success) { | ||
return ctx.value; | ||
} | ||
} | ||
throw new ZodError(ctx.error.issues); | ||
}); | ||
|
||
export const configPatternsSchema = z.object({ | ||
persist: persistConfigSchema.transform(persist => ({ | ||
...DEFAULT_PERSIST_CONFIG, | ||
...persist, | ||
})), | ||
upload: uploadConfigSchema | ||
.omit({ organization: true, project: true }) | ||
.extend({ | ||
organization: interpolatedSlugSchema, | ||
project: interpolatedSlugSchema, | ||
}) | ||
.optional(), | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { ZodError } from 'zod'; | ||
import type { ConfigPatterns } from './models.js'; | ||
import { configPatternsSchema, interpolatedSlugSchema } from './schemas.js'; | ||
|
||
describe('interpolatedSlugSchema', () => { | ||
it('should accept a valid slug', () => { | ||
expect(interpolatedSlugSchema.parse('valid-slug')).toBe('valid-slug'); | ||
}); | ||
|
||
it('should accept a slug with {projectName} interpolation', () => { | ||
expect(interpolatedSlugSchema.parse('{projectName}-slug')).toBe( | ||
'{projectName}-slug', | ||
); | ||
}); | ||
|
||
it('should reject an invalid slug that cannot be fixed by interpolation', () => { | ||
expect(() => interpolatedSlugSchema.parse('Invalid Slug!')).toThrow( | ||
ZodError, | ||
); | ||
}); | ||
|
||
it('should reject a non-string value', () => { | ||
expect(() => interpolatedSlugSchema.parse(123)).toThrow(ZodError); | ||
}); | ||
}); | ||
|
||
describe('configPatternsSchema', () => { | ||
it('should accept valid persist and upload configs', () => { | ||
const configPatterns: Required<ConfigPatterns> = { | ||
persist: { | ||
outputDir: '.code-pushup/{projectName}', | ||
filename: 'report', | ||
format: ['json', 'md'], | ||
skipReports: false, | ||
}, | ||
upload: { | ||
server: 'https://api.code-pushup.example.com/graphql', | ||
apiKey: 'cp_...', | ||
organization: 'example', | ||
project: '{projectName}', | ||
}, | ||
}; | ||
expect(configPatternsSchema.parse(configPatterns)).toEqual(configPatterns); | ||
}); | ||
|
||
it('should accept persist config without upload', () => { | ||
const configPatterns: ConfigPatterns = { | ||
persist: { | ||
outputDir: '.code-pushup/{projectName}', | ||
filename: 'report', | ||
format: ['json', 'md'], | ||
skipReports: false, | ||
}, | ||
}; | ||
expect(configPatternsSchema.parse(configPatterns)).toEqual(configPatterns); | ||
}); | ||
|
||
it('fills in default persist values if missing', () => { | ||
expect( | ||
configPatternsSchema.parse({ | ||
persist: { | ||
filename: '{projectName}-report', | ||
}, | ||
}), | ||
).toEqual<ConfigPatterns>({ | ||
persist: { | ||
outputDir: '.code-pushup', | ||
filename: '{projectName}-report', | ||
format: ['json', 'md'], | ||
skipReports: false, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should reject if persist is missing', () => { | ||
expect(() => configPatternsSchema.parse({})).toThrow(ZodError); | ||
}); | ||
|
||
it('should reject if persist has invalid values', () => { | ||
expect(() => | ||
configPatternsSchema.parse({ | ||
persist: { | ||
format: 'json', // should be array | ||
}, | ||
}), | ||
).toThrow(ZodError); | ||
}); | ||
|
||
it('should reject if upload is missing required fields', () => { | ||
expect(() => | ||
configPatternsSchema.parse({ | ||
persist: { | ||
outputDir: '.code-pushup/{projectName}', | ||
filename: 'report', | ||
format: ['json', 'md'], | ||
skipReports: false, | ||
}, | ||
upload: { | ||
server: 'https://api.code-pushup.example.com/graphql', | ||
organization: 'example', | ||
project: '{projectName}', | ||
// missing apiKey | ||
}, | ||
}), | ||
).toThrow(ZodError); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ZodError, z } from 'zod'; | ||
import type { ConfigPatterns, Settings } from './models.js'; | ||
import { configPatternsSchema } from './schemas.js'; | ||
|
||
export const DEFAULT_SETTINGS: Settings = { | ||
monorepo: false, | ||
parallel: false, | ||
projects: null, | ||
task: 'code-pushup', | ||
bin: 'npx --no-install code-pushup', | ||
config: null, | ||
directory: process.cwd(), | ||
silent: false, | ||
debug: false, | ||
detectNewIssues: true, | ||
logger: console, | ||
nxProjectsFilter: '--with-target={task}', | ||
skipComment: false, | ||
configPatterns: null, | ||
searchCommits: false, | ||
}; | ||
|
||
export const MIN_SEARCH_COMMITS = 1; | ||
export const MAX_SEARCH_COMMITS = 100; | ||
|
||
export function parseConfigPatternsFromString( | ||
value: string, | ||
): ConfigPatterns | null { | ||
if (!value) { | ||
matejchalk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
} | ||
|
||
try { | ||
const json = JSON.parse(value); | ||
return configPatternsSchema.parse(json); | ||
} catch (error) { | ||
if (error instanceof SyntaxError) { | ||
throw new TypeError( | ||
`Invalid JSON value for configPatterns input - ${error.message}`, | ||
); | ||
} | ||
if (error instanceof ZodError) { | ||
throw new TypeError( | ||
`Invalid shape of configPatterns input:\n${z.prettifyError(error)}`, | ||
); | ||
} | ||
throw error; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import type { CoreConfig } from '@code-pushup/models'; | ||
import type { ConfigPatterns } from './models.js'; | ||
import { parseConfigPatternsFromString } from './settings.js'; | ||
|
||
describe('parseConfigPatternsFromString', () => { | ||
it('should return for empty string', () => { | ||
expect(parseConfigPatternsFromString('')).toBeNull(); | ||
}); | ||
|
||
it('should parse full persist and upload configs', () => { | ||
const configPatterns: Required<ConfigPatterns> = { | ||
persist: { | ||
outputDir: '.code-pushup/{projectName}', | ||
filename: 'report', | ||
format: ['json', 'md'], | ||
skipReports: false, | ||
}, | ||
upload: { | ||
server: 'https://api.code-pushup.example.com/graphql', | ||
apiKey: 'cp_...', | ||
organization: 'example', | ||
project: '{projectName}', | ||
}, | ||
}; | ||
expect( | ||
parseConfigPatternsFromString(JSON.stringify(configPatterns)), | ||
).toEqual(configPatterns); | ||
}); | ||
|
||
it('should parse full persist config without upload config', () => { | ||
const configPatterns: ConfigPatterns = { | ||
persist: { | ||
outputDir: '.code-pushup/{projectName}', | ||
filename: 'report', | ||
format: ['json', 'md'], | ||
skipReports: false, | ||
}, | ||
}; | ||
expect( | ||
parseConfigPatternsFromString(JSON.stringify(configPatterns)), | ||
).toEqual(configPatterns); | ||
}); | ||
|
||
it('should fill in default persist values where missing', () => { | ||
expect( | ||
parseConfigPatternsFromString( | ||
JSON.stringify({ | ||
persist: { | ||
filename: '{projectName}-report', | ||
}, | ||
} satisfies Pick<CoreConfig, 'persist'>), | ||
), | ||
).toEqual<ConfigPatterns>({ | ||
persist: { | ||
outputDir: '.code-pushup', | ||
filename: '{projectName}-report', | ||
format: ['json', 'md'], | ||
skipReports: false, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should throw if input string is not valid JSON', () => { | ||
expect(() => | ||
parseConfigPatternsFromString('outputDir: .code-pushup/{projectName}'), | ||
).toThrow('Invalid JSON value for configPatterns input - Unexpected token'); | ||
}); | ||
|
||
it('should throw if persist config is missing', () => { | ||
expect(() => parseConfigPatternsFromString('{}')).toThrow( | ||
/Invalid shape of configPatterns input.*expected object, received undefined.*at persist/s, | ||
); | ||
}); | ||
|
||
it('should throw if persist config has invalid values', () => { | ||
expect(() => | ||
parseConfigPatternsFromString( | ||
JSON.stringify({ persist: { format: 'json' } }), | ||
), | ||
).toThrow( | ||
/Invalid shape of configPatterns input.*expected array, received string.*at persist\.format/s, | ||
); | ||
}); | ||
|
||
it('should throw if upload config has missing values', () => { | ||
expect(() => | ||
parseConfigPatternsFromString( | ||
JSON.stringify({ | ||
persist: {}, | ||
upload: { | ||
server: 'https://api.code-pushup.example.com/graphql', | ||
organization: 'example', | ||
project: '{projectName}', | ||
}, | ||
}), | ||
), | ||
).toThrow( | ||
/Invalid shape of configPatterns input.*expected string, received undefined.*at upload\.apiKey/s, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.