-
Notifications
You must be signed in to change notification settings - Fork 76
Validate Discord app distribution config #611
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,12 +27,24 @@ interface DiscordCommand { | |||||||||||||
| options?: unknown[]; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const DISTRIBUTIONS = ['private', 'public', 'directory'] as const; | ||||||||||||||
| const SCOPES = ['bot', 'applications.commands'] as const; | ||||||||||||||
|
|
||||||||||||||
| function requireApplicationId(config: Config): string { | ||||||||||||||
| const applicationId = config.applicationId?.trim(); | ||||||||||||||
| if (!applicationId) throw new Error('chat-discord requires applicationId'); | ||||||||||||||
| if (!/^\d+$/.test(applicationId)) throw new Error('chat-discord applicationId must be a numeric Discord snowflake'); | ||||||||||||||
| return applicationId; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| function requireDistribution(config: Config): Config['distribution'] { | ||||||||||||||
| const distribution = String(config.distribution ?? '').trim(); | ||||||||||||||
| if (!DISTRIBUTIONS.includes(distribution as Config['distribution'])) { | ||||||||||||||
| throw new Error(`chat-discord distribution must be one of: ${DISTRIBUTIONS.join(', ')}`); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+42
to
+44
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.
Suggested change
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! |
||||||||||||||
| return distribution as Config['distribution']; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| function normalizeCommands(commands: Config['slashCommands']): DiscordCommand[] { | ||||||||||||||
| return (commands ?? []).map((command) => { | ||||||||||||||
| const name = command.name.replace(/^\//, '').trim().toLowerCase(); | ||||||||||||||
|
|
@@ -49,7 +61,10 @@ function normalizeCommands(commands: Config['slashCommands']): DiscordCommand[] | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| function scopesFor(config: Config): string[] { | ||||||||||||||
| return config.scopes?.length ? config.scopes : ['bot', 'applications.commands']; | ||||||||||||||
| const scopes = config.scopes?.length ? config.scopes : ['bot', 'applications.commands']; | ||||||||||||||
| const invalid = scopes.find((scope) => !SCOPES.includes(scope as typeof SCOPES[number])); | ||||||||||||||
| if (invalid) throw new Error(`chat-discord scope must be one of: ${SCOPES.join(', ')}`); | ||||||||||||||
|
Comment on lines
+65
to
+66
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.
Suggested change
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! |
||||||||||||||
| return scopes; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| function inviteUrl(config: Config): string { | ||||||||||||||
|
|
@@ -63,17 +78,18 @@ function inviteUrl(config: Config): string { | |||||||||||||
|
|
||||||||||||||
| function manifestFor(config: Config, version: string) { | ||||||||||||||
| const commands = normalizeCommands(config.slashCommands); | ||||||||||||||
| const distribution = requireDistribution(config); | ||||||||||||||
| return { | ||||||||||||||
| provider: 'discord', | ||||||||||||||
| applicationId: requireApplicationId(config), | ||||||||||||||
| version, | ||||||||||||||
| distribution: config.distribution, | ||||||||||||||
| distribution, | ||||||||||||||
| interactionsEndpointUrl: config.interactionsEndpointUrl, | ||||||||||||||
| scopes: scopesFor(config), | ||||||||||||||
| permissions: config.permissions ?? 0, | ||||||||||||||
| inviteUrl: inviteUrl(config), | ||||||||||||||
| commands, | ||||||||||||||
| directoryReviewRequired: config.distribution === 'directory', | ||||||||||||||
| directoryReviewRequired: distribution === 'directory', | ||||||||||||||
| }; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
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.
build-path coverage for invalid distributionThe new distribution test only exercises
adapter.ship. BothbuildandshipreachmanifestFor → requireDistribution, so the validation fires on both paths. Adding a parallel test foradapter.buildwith an invalid distribution would close the gap and guard against a future refactor that separates the two paths.