-
Notifications
You must be signed in to change notification settings - Fork 1
feat(cli): add feedback, the anonymous CLI feedback command #119
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
5 commits
Select commit
Hold shift + click to select a range
4cf4367
feat(cli): add feedback, the anonymous CLI feedback command
AmanVarshney01 ed4be46
test(cli): cover the feedback message length limit
AmanVarshney01 bdc19c3
fix(cli): address codex review of the feedback command
AmanVarshney01 4d05b6b
docs(cli): add the FEEDBACK_SEND_FAILED recommended meaning
AmanVarshney01 ea5b339
feat(cli): point unexpected crashes at the feedback command
AmanVarshney01 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
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
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,43 @@ | ||
| import { Command, Option } from "commander"; | ||
|
|
||
| import { type FeedbackFlags, runFeedback } from "../../controllers/feedback"; | ||
| import { renderFeedbackSuccess } from "../../presenters/feedback"; | ||
| import { attachCommandDescriptor } from "../../shell/command-meta"; | ||
| import { runCommand } from "../../shell/command-runner"; | ||
| import { addGlobalFlags } from "../../shell/global-flags"; | ||
| import { type CliRuntime, configureRuntimeCommand } from "../../shell/runtime"; | ||
| import type { FeedbackResult } from "../../types/feedback"; | ||
|
|
||
| export function createFeedbackCommand(runtime: CliRuntime): Command { | ||
| const command = attachCommandDescriptor( | ||
| configureRuntimeCommand(new Command("feedback"), runtime), | ||
| "feedback", | ||
| ); | ||
|
|
||
| command | ||
| .argument("<message>", "Feedback text (up to 4000 characters)") | ||
| .addOption( | ||
| new Option( | ||
| "--email <address>", | ||
| "Contact email if you want a reply; feedback is anonymous without it", | ||
| ), | ||
| ); | ||
| addGlobalFlags(command); | ||
|
|
||
| command.action(async (message: string, options) => { | ||
| const flags = options as FeedbackFlags; | ||
|
|
||
| await runCommand<FeedbackResult>( | ||
| runtime, | ||
| "feedback", | ||
| options as Record<string, unknown>, | ||
| (context) => runFeedback(context, message, { email: flags.email }), | ||
| { | ||
| renderHuman: (context, descriptor, result) => | ||
| renderFeedbackSuccess(context, descriptor, result), | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| return command; | ||
| } |
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,175 @@ | ||
| import { getCliVersion } from "../lib/version"; | ||
| import { CliError, usageError } from "../shell/errors"; | ||
| import type { CommandSuccess } from "../shell/output"; | ||
| import type { CommandContext } from "../shell/runtime"; | ||
| import type { FeedbackContext, FeedbackResult } from "../types/feedback"; | ||
|
|
||
| const DEFAULT_FEEDBACK_ENDPOINT = | ||
| "https://hiieirp2pwqnjvq9axzyg6d0.fra.prisma.build/feedback"; | ||
| // Feedback must never feel slower than the thought it carries; a service | ||
| // that cannot answer quickly is treated as unavailable. | ||
| const FEEDBACK_TIMEOUT_MS = 3_000; | ||
| // Mirrors the feedback service's own limits so refusals happen before the | ||
| // network round trip. | ||
| const MAX_MESSAGE_LENGTH = 4_000; | ||
| const MAX_EMAIL_LENGTH = 320; | ||
| const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
|
|
||
| export interface FeedbackFlags { | ||
| email?: string; | ||
| } | ||
|
|
||
| export async function runFeedback( | ||
| context: CommandContext, | ||
| messageArg: string, | ||
| flags: FeedbackFlags, | ||
| ): Promise<CommandSuccess<FeedbackResult>> { | ||
| const message = messageArg.trim(); | ||
| if (!message) { | ||
| throw usageError( | ||
| "Feedback message required", | ||
| "The message argument is empty.", | ||
| "Pass a non-empty message.", | ||
| ['prisma-cli feedback "the deploy flow is great"'], | ||
| ); | ||
| } | ||
| if (message.length > MAX_MESSAGE_LENGTH) { | ||
| throw usageError( | ||
| "Feedback message too long", | ||
| `The message is ${message.length} characters; the limit is ${MAX_MESSAGE_LENGTH}.`, | ||
| "Shorten the message.", | ||
| ); | ||
| } | ||
|
|
||
| const email = flags.email?.trim(); | ||
| if ( | ||
| email !== undefined && | ||
| (email.length > MAX_EMAIL_LENGTH || !EMAIL_PATTERN.test(email)) | ||
| ) { | ||
| throw usageError( | ||
| "Invalid email", | ||
| `"${flags.email}" is not a valid email address of at most ${MAX_EMAIL_LENGTH} characters.`, | ||
| "Pass a valid address with --email, or drop the flag to stay anonymous.", | ||
| ['prisma-cli feedback "please add X" --email you@example.com'], | ||
| ); | ||
| } | ||
|
|
||
| const feedbackContext: FeedbackContext = { | ||
| cliVersion: getCliVersion(), | ||
| nodeVersion: process.version, | ||
| platform: process.platform, | ||
| arch: process.arch, | ||
| }; | ||
|
|
||
| const endpoint = | ||
| context.runtime.env.PRISMA_CLI_FEEDBACK_URL || DEFAULT_FEEDBACK_ENDPOINT; | ||
| const id = await postFeedback(context, endpoint, { | ||
| message, | ||
| ...(email ? { email } : {}), | ||
| meta: { ...feedbackContext }, | ||
| }); | ||
|
|
||
| return { | ||
| command: "feedback", | ||
| result: { | ||
| id, | ||
| email: email ?? null, | ||
| context: feedbackContext, | ||
| }, | ||
| warnings: [], | ||
| nextSteps: [], | ||
| }; | ||
| } | ||
|
|
||
| async function postFeedback( | ||
| context: CommandContext, | ||
| endpoint: string, | ||
| body: Record<string, unknown>, | ||
| ): Promise<string | null> { | ||
| let response: Response; | ||
| try { | ||
| response = await fetch(endpoint, { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| "user-agent": `prisma-cli/${getCliVersion()}`, | ||
| }, | ||
| body: JSON.stringify(body), | ||
| signal: AbortSignal.any([ | ||
| context.runtime.signal, | ||
| AbortSignal.timeout(FEEDBACK_TIMEOUT_MS), | ||
| ]), | ||
| }); | ||
| } catch (error) { | ||
| if (context.runtime.signal.aborted) { | ||
| throw error; | ||
| } | ||
| const detail = | ||
| error instanceof Error && error.name === "TimeoutError" | ||
| ? TIMEOUT_DETAIL | ||
| : `The feedback service could not be reached${error instanceof Error && error.cause instanceof Error ? ` (${error.cause.message})` : ""}.`; | ||
| throw feedbackSendFailed(detail); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| if (!response.ok) { | ||
| throw feedbackSendFailed( | ||
| `The feedback service responded with HTTP ${response.status}${await readServiceError(context, response)}.`, | ||
| ); | ||
| } | ||
|
|
||
| // The body read runs under the same abort signal as the request, so a | ||
| // stalled response or a user cancellation here must not be mistaken for a | ||
| // fully received non-JSON body. | ||
| let payload: { id?: unknown } | null; | ||
| try { | ||
| payload = (await response.json()) as { id?: unknown } | null; | ||
| } catch (error) { | ||
| if (context.runtime.signal.aborted) { | ||
| throw error; | ||
| } | ||
| if (!(error instanceof SyntaxError)) { | ||
| throw feedbackSendFailed( | ||
| error instanceof Error && error.name === "TimeoutError" | ||
| ? TIMEOUT_DETAIL | ||
| : "The feedback service response could not be read.", | ||
| ); | ||
| } | ||
| // The body arrived but was not JSON; the submission itself succeeded. | ||
| payload = null; | ||
| } | ||
| return typeof payload?.id === "string" ? payload.id : null; | ||
| } | ||
|
|
||
| const TIMEOUT_DETAIL = `The feedback service did not answer within ${FEEDBACK_TIMEOUT_MS / 1000} seconds.`; | ||
|
|
||
| async function readServiceError( | ||
| context: CommandContext, | ||
| response: Response, | ||
| ): Promise<string> { | ||
| let payload: { error?: { message?: unknown } } | null; | ||
| try { | ||
| payload = (await response.json()) as { | ||
| error?: { message?: unknown }; | ||
| } | null; | ||
| } catch (error) { | ||
| if (context.runtime.signal.aborted) { | ||
| throw error; | ||
| } | ||
| return ""; | ||
| } | ||
| return typeof payload?.error?.message === "string" | ||
| ? ` (${payload.error.message})` | ||
| : ""; | ||
| } | ||
|
|
||
| function feedbackSendFailed(detail: string): CliError { | ||
| return new CliError({ | ||
| code: "FEEDBACK_SEND_FAILED", | ||
| domain: "cli", | ||
| summary: "Feedback could not be delivered", | ||
| why: detail, | ||
| fix: "Check your network and rerun the command.", | ||
| exitCode: 1, | ||
| nextSteps: [], | ||
| }); | ||
| } | ||
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.