-
Notifications
You must be signed in to change notification settings - Fork 76
Validate Netlify deploy config #641
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,12 +9,41 @@ interface Config { | |
| message?: string; | ||
| } | ||
|
|
||
| function requireText(value: string | undefined, field: string): string { | ||
| const text = value?.trim(); | ||
| if (!text) throw new Error(`deploy-netlify requires ${field}`); | ||
| return text; | ||
| } | ||
|
|
||
| function optionalText(value: string | undefined, field: string): string | undefined { | ||
| return value === undefined ? undefined : requireText(value, field); | ||
| } | ||
|
|
||
| function optionalSiteId(value: string | undefined): string | undefined { | ||
| const id = optionalText(value, 'siteId'); | ||
| if (id && /[\\/?#\x00-\x1F\x7F]/.test(id)) { | ||
| throw new Error('deploy-netlify siteId must be a single URL path segment'); | ||
| } | ||
| return id; | ||
| } | ||
|
|
||
| function normalizedConfig(config: Config): Config { | ||
| return { | ||
| ...config, | ||
| siteId: optionalSiteId(config.siteId), | ||
| dir: optionalText(config.dir, 'dir'), | ||
| message: optionalText(config.message, 'message'), | ||
| }; | ||
| } | ||
|
|
||
| function deployDir(ctx: { projectDir: string }, config: Config): string { | ||
| config = normalizedConfig(config); | ||
| if (!config.dir) return ctx.projectDir; | ||
| return isAbsolute(config.dir) ? config.dir : join(ctx.projectDir, config.dir); | ||
| } | ||
|
|
||
| function deployArgs(ctx: { channel: string; projectDir: string; version: string }, config: Config, token?: string): string[] { | ||
| config = normalizedConfig(config); | ||
| const prod = config.prod ?? ctx.channel === 'stable'; | ||
| const args = ['--yes', 'netlify-cli', 'deploy', '--json', '--dir', deployDir(ctx, config)]; | ||
| if (prod) args.push('--prod'); | ||
|
|
@@ -26,6 +55,7 @@ function deployArgs(ctx: { channel: string; projectDir: string; version: string | |
| } | ||
|
Comment on lines
45
to
55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
|
|
||
| function renderPlan(ctx: { channel: string; projectDir: string; version: string }, config: Config): string { | ||
| config = normalizedConfig(config); | ||
| const prod = config.prod ?? ctx.channel === 'stable'; | ||
| return `${JSON.stringify({ | ||
| provider: 'netlify', | ||
|
|
@@ -66,6 +96,7 @@ export default defineTarget<Config>({ | |
| return { artifact: planPath }; | ||
| }, | ||
| async ship(ctx, config) { | ||
| config = normalizedConfig(config); | ||
| const prod = config.prod ?? ctx.channel === 'stable'; | ||
| ctx.log(`netlify deploy ${prod ? '--prod' : ''} · site=${config.siteId ?? 'linked'}`); | ||
| if (ctx.dryRun) return { id: 'dry-run', meta: { command: ['npx', ...deployArgs(ctx, config)] } }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
siteIdregex does not reject embedded spaces. A value like'my site'trims cleanly, passes the regex, and is accepted as valid. It is a semantically invalid Netlify site ID that silently slips through the stated "single URL path segment" guard. Adding\sto the character class would close the gap.