From cf2b08184f001b80ea7c7df555b45a1e8c004d53 Mon Sep 17 00:00:00 2001 From: Dane Williams Date: Wed, 16 Aug 2023 21:43:12 -0400 Subject: [PATCH] Remove survey (#5) --- apps/cli/src/actions/submit/submit_action.ts | 6 - apps/cli/src/actions/survey.ts | 195 ------------------- 2 files changed, 201 deletions(-) delete mode 100644 apps/cli/src/actions/survey.ts diff --git a/apps/cli/src/actions/submit/submit_action.ts b/apps/cli/src/actions/submit/submit_action.ts index 85659abc8..55a609c1f 100644 --- a/apps/cli/src/actions/submit/submit_action.ts +++ b/apps/cli/src/actions/submit/submit_action.ts @@ -4,7 +4,6 @@ import { TScopeSpec } from '../../lib/engine/scope_spec'; import { ExitFailedError, KilledError } from '../../lib/errors'; import { CommandFailedError } from '../../lib/git/runner'; import { cliAuthPrecondition } from '../../lib/preconditions'; -import { getSurvey, showSurvey } from '../survey'; import { getPRInfoForBranches } from './prepare_branches'; import { submitPullRequest } from './submit_prs'; import { validateBranchesToSubmit } from './validate_branches'; @@ -145,11 +144,6 @@ export async function submitAction( if (!context.interactive) { return; } - - const survey = await getSurvey(context); - if (survey) { - await showSurvey(survey, context); - } } async function selectBranches( diff --git a/apps/cli/src/actions/survey.ts b/apps/cli/src/actions/survey.ts deleted file mode 100644 index e8a34fcd6..000000000 --- a/apps/cli/src/actions/survey.ts +++ /dev/null @@ -1,195 +0,0 @@ -import { API_ROUTES } from '@withgraphite/graphite-cli-routes'; -import { default as t } from '@withgraphite/retype'; -import { postSurveyResponse } from '../background_tasks/post_survey'; -import { requestWithArgs } from '../lib/api/request'; -import { TContext } from '../lib/context'; -import { cliAuthPrecondition } from '../lib/preconditions'; -import { TSurveyResponse } from '../lib/spiffy/survey_responses_spf'; -import { assertUnreachable } from '../lib/utils/assert_unreachable'; - -type SurveyT = t.UnwrapSchemaMap< - typeof API_ROUTES.cliSurvey.response ->['survey']; - -export async function getSurvey( - context: TContext -): Promise { - try { - cliAuthPrecondition(context); - const response = await requestWithArgs( - context.userConfig, - API_ROUTES.cliSurvey, - {}, - {} - ); - if (response._response.status === 200) { - return response.survey; - } - } catch (e) { - // silence any error - this shouldn't crash any part of the CLI - } - - // If we didn't get a definitive answer, let's be conservative and err on - // the side of *not* showing the survey in potentially incorrect situations. - return undefined; -} - -class ExitedSurveyError extends Error { - constructor() { - super(`User exited Graphite survey early`); - this.name = 'Killed'; - } -} - -export async function showSurvey( - survey: SurveyT, - context: TContext -): Promise { - if (process.env.GRAPHITE_INTERACTIVE) { - return; - } - - const responses: TSurveyResponse = { - timestamp: Date.now(), - responses: [], - exitedEarly: false, - }; - try { - if (survey === undefined) { - return; - } - - context.splog.newline(); - if (survey?.introMessage !== undefined) { - context.splog.message(survey.introMessage); - } - - context.splog.newline(); - await askSurveyQuestions( - { - questions: survey.questions, - responses: responses, - }, - context - ); - - context.splog.newline(); - await logAnswers( - { - responses: responses, - completionMessage: survey?.completionMessage, - }, - context - ); - } catch (err) { - switch (err.constructor) { - case ExitedSurveyError: - responses.exitedEarly = true; - context.splog.newline(); - await logAnswers( - { - responses: responses, - completionMessage: survey?.completionMessage, - }, - context - ); - break; - default: - throw err; - } - } -} - -/** - * While capturing the responses, mutate the passed-in object so we can always - * capture and potential responses before the user decided to exit the survey - * early. - */ -async function askSurveyQuestions( - args: { - questions: ( - | { - type: 'TEXT'; - question: string; - } - | { - type: 'OPTIONS'; - question: string; - options: string[]; - } - )[]; - responses: TSurveyResponse; - }, - context: TContext -): Promise { - for (const [index, question] of args.questions.entries()) { - const onCancel = { - onCancel: () => { - throw new ExitedSurveyError(); - }, - }; - - let promptResponse; - const questionText = `Question [${index + 1}/${args.questions.length}]: ${ - question.question - }`; - - switch (question.type) { - case 'TEXT': - promptResponse = await context.prompts( - { - type: 'text', - name: 'answer', - message: questionText, - }, - onCancel - ); - break; - case 'OPTIONS': - promptResponse = await context.prompts( - { - type: 'select', - name: 'answer', - message: questionText, - choices: question.options.map((option) => { - return { - title: option, - value: option, - }; - }), - }, - onCancel - ); - break; - default: - assertUnreachable(question); - continue; - } - - // Add newline after each response to create visual separation to next - // question. - context.splog.newline(); - - args.responses.responses.push({ - question: question.question, - answer: promptResponse.answer, - }); - } -} - -async function logAnswers( - args: { - responses: TSurveyResponse; - completionMessage: string | undefined; - }, - context: TContext -): Promise { - context.surveyConfig.setSurveyResponses(args.responses); - - await postSurveyResponse(); - - if (args.completionMessage !== undefined) { - context.splog.message(args.completionMessage); - } - return; -}