From 15b2083913522a81e22cdecdc3e1e8025acc9778 Mon Sep 17 00:00:00 2001 From: Kanad Gupta Date: Tue, 10 Dec 2024 17:44:38 -0600 Subject: [PATCH] test: more merge cleanup --- __tests__/helpers/get-api-mock.ts | 6 - .../lib/__snapshots__/createGHA.test.ts.snap | 82 ----------- __tests__/lib/createGHA.test.ts | 1 - __tests__/lib/prompts.test.ts | 70 ---------- src/lib/apiError.ts | 2 +- src/lib/flags.ts | 9 -- src/lib/prompts.ts | 132 ------------------ src/lib/versionSelect.ts | 80 ----------- 8 files changed, 1 insertion(+), 381 deletions(-) delete mode 100644 __tests__/lib/prompts.test.ts delete mode 100644 src/lib/prompts.ts delete mode 100644 src/lib/versionSelect.ts diff --git a/__tests__/helpers/get-api-mock.ts b/__tests__/helpers/get-api-mock.ts index 3b8e6252b..8ffd07f36 100644 --- a/__tests__/helpers/get-api-mock.ts +++ b/__tests__/helpers/get-api-mock.ts @@ -15,9 +15,3 @@ export function getAPIv1Mock(reqHeaders = {}) { }, }); } - -export function getAPIv1MockWithVersionHeader(v: string) { - return getAPIv1Mock({ - 'x-readme-version': v, - }); -} diff --git a/__tests__/lib/__snapshots__/createGHA.test.ts.snap b/__tests__/lib/__snapshots__/createGHA.test.ts.snap index 79d9e2e84..1937da436 100644 --- a/__tests__/lib/__snapshots__/createGHA.test.ts.snap +++ b/__tests__/lib/__snapshots__/createGHA.test.ts.snap @@ -164,88 +164,6 @@ jobs: " `; -exports[`#createGHA > command inputs > 'openapi' '' > should run GHA creation workflow and generate valid workflow file 1`] = ` -" -Your GitHub Actions workflow file has been created! ✨ - -Almost done! Just a couple more steps: -1. Push your newly created file (.github/workflows/rdme-openapi.yml) to GitHub 🚀 -2. Create a GitHub secret called README_API_KEY and populate the value with your ReadMe API key (••••••••••••I_KEY) 🔑 - -🔐 Check out GitHub's docs for more info on creating encrypted secrets (https://docs.github.com/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) - -🦉 If you have any more questions, feel free to drop us a line! support@readme.io -" -`; - -exports[`#createGHA > command inputs > 'openapi' '' > should run GHA creation workflow and generate valid workflow file 2`] = ` -"# This GitHub Actions workflow was auto-generated by the \`rdme\` cli on 2022-01-01T00:00:00.000Z -# You can view our full documentation here: https://docs.readme.com/docs/rdme -name: ReadMe GitHub Action 🦉 - -on: - push: - branches: - # This workflow will run every time you push code to the following branch: \`some-branch\` - # Check out GitHub's docs for more info on configuring this: - # https://docs.github.com/actions/using-workflows/events-that-trigger-workflows - - some-branch - -jobs: - rdme-openapi: - runs-on: ubuntu-latest - steps: - - name: Check out repo 📚 - uses: actions/checkout@v4 - - - name: Run \`openapi\` command 🚀 - uses: readmeio/rdme@v7 - with: - rdme: openapi petstore.json --key=\${{ secrets.README_API_KEY }} --id=spec_id -" -`; - -exports[`#createGHA > command inputs > 'openapi' '' > should run GHA creation workflow with \`--github\` flag and messy file name and generate valid workflow file 1`] = ` -" -Your GitHub Actions workflow file has been created! ✨ - -Almost done! Just a couple more steps: -1. Push your newly created file (.github/workflows/rdme-openapi-with-github-flag.yml) to GitHub 🚀 -2. Create a GitHub secret called README_API_KEY and populate the value with your ReadMe API key (••••••••••••I_KEY) 🔑 - -🔐 Check out GitHub's docs for more info on creating encrypted secrets (https://docs.github.com/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) - -🦉 If you have any more questions, feel free to drop us a line! support@readme.io -" -`; - -exports[`#createGHA > command inputs > 'openapi' '' > should run GHA creation workflow with \`--github\` flag and messy file name and generate valid workflow file 2`] = ` -"# This GitHub Actions workflow was auto-generated by the \`rdme\` cli on 2022-01-01T00:00:00.000Z -# You can view our full documentation here: https://docs.readme.com/docs/rdme -name: ReadMe GitHub Action 🦉 - -on: - push: - branches: - # This workflow will run every time you push code to the following branch: \`another-branch\` - # Check out GitHub's docs for more info on configuring this: - # https://docs.github.com/actions/using-workflows/events-that-trigger-workflows - - another-branch - -jobs: - rdme-openapi: - runs-on: ubuntu-latest - steps: - - name: Check out repo 📚 - uses: actions/checkout@v4 - - - name: Run \`openapi\` command 🚀 - uses: readmeio/rdme@v7 - with: - rdme: openapi petstore.json --key=\${{ secrets.README_API_KEY }} --id=spec_id -" -`; - exports[`#createGHA > command inputs > 'openapi:validate' '' > should run GHA creation workflow and generate valid workflow file 1`] = ` " Your GitHub Actions workflow file has been created! ✨ diff --git a/__tests__/lib/createGHA.test.ts b/__tests__/lib/createGHA.test.ts index 99f14053a..2f78f0954 100644 --- a/__tests__/lib/createGHA.test.ts +++ b/__tests__/lib/createGHA.test.ts @@ -58,7 +58,6 @@ describe('#createGHA', () => { // `openapi:validate` is the ID we define in src/index.ts for backwards compatibility, // hence we're using this command ID here { cmd: 'openapi:validate', opts: { spec: 'petstore.json' }, label: '' }, - { cmd: 'openapi', opts: { key, spec: 'petstore.json', id: 'spec_id' }, label: '' }, { cmd: 'changelogs', opts: { key, path: './changelogs' }, label: '' }, { cmd: 'changelogs', diff --git a/__tests__/lib/prompts.test.ts b/__tests__/lib/prompts.test.ts deleted file mode 100644 index 94b8287a6..000000000 --- a/__tests__/lib/prompts.test.ts +++ /dev/null @@ -1,70 +0,0 @@ -import prompts from 'prompts'; -import { describe, it, expect } from 'vitest'; - -import * as promptHandler from '../../src/lib/prompts.js'; -import promptTerminal from '../../src/lib/promptWrapper.js'; - -const specList = [ - { - _id: 'spec1', - title: 'spec1_title', - }, - { - _id: 'spec2', - title: 'spec2_title', - }, -]; - -const getSpecs = () => { - return { - body: [ - { - _id: 'spec3', - title: 'spec3_title', - }, - ], - } as unknown as Promise; -}; - -describe('prompt test bed', () => { - describe('createOasPrompt()', () => { - it('should return a create option if selected', async () => { - prompts.inject(['create']); - - const answer = await promptTerminal( - promptHandler.createOasPrompt( - [ - { - _id: '1234', - title: 'buster', - }, - ], - {}, - 1, - null, - ), - ); - - expect(answer).toStrictEqual({ option: 'create' }); - }); - - it('should return specId if user chooses to update file', async () => { - prompts.inject(['update', 'spec1']); - - const parsedDocs = { - next: { - page: 2, - url: '', - }, - prev: { - page: 1, - url: '', - }, - }; - - const answer = await promptTerminal(promptHandler.createOasPrompt(specList, parsedDocs, 1, getSpecs)); - - expect(answer).toStrictEqual({ option: 'spec1' }); - }); - }); -}); diff --git a/src/lib/apiError.ts b/src/lib/apiError.ts index b7c7970b5..0aab9ab87 100644 --- a/src/lib/apiError.ts +++ b/src/lib/apiError.ts @@ -1,7 +1,7 @@ /** * APIv1ErrorResponse is the shape of the error response we get from ReadMe API v1. */ -export interface APIv1ErrorResponse { +interface APIv1ErrorResponse { docs?: string; error: string; help?: string; diff --git a/src/lib/flags.ts b/src/lib/flags.ts index 07ac99ce0..07ef320e6 100644 --- a/src/lib/flags.ts +++ b/src/lib/flags.ts @@ -22,15 +22,6 @@ export const titleFlag = Flags.string({ description: 'An override value for the `info.title` field in the API definition', }); -/** - * Used in any command where `version` is a flag. - */ -export const versionFlag = Flags.string({ - description: - 'If running command in a CI environment and this option is not passed, the main project version will be used. See our docs for more information: https://docs.readme.com/main/docs/versions', - summary: 'ReadMe project version', -}); - /** * Used in the `openapi` family of commands where `workingDirectory` is an option. */ diff --git a/src/lib/prompts.ts b/src/lib/prompts.ts deleted file mode 100644 index 0ba6c0b23..000000000 --- a/src/lib/prompts.ts +++ /dev/null @@ -1,132 +0,0 @@ -import type { Choice, PromptObject } from 'prompts'; - -import parse from 'parse-link-header'; - -import { debug } from './logger.js'; -import promptTerminal from './promptWrapper.js'; -import { handleAPIv1Res } from './readmeAPIFetch.js'; - -interface Spec { - _id: string; - title: string; -} - -export type OpenAPIPromptOptions = 'create' | 'update'; - -type SpecList = Spec[]; - -interface ParsedDocs { - next?: { - page: number; - url: string; - }; - prev?: { - page: number; - url: string; - }; -} - -function specOptions( - specList: SpecList, - parsedDocs: ParsedDocs | null, - currPage: number, - totalPages: number, -): Choice[] { - const specs = specList.map(s => { - return { - description: `API Definition ID: ${s._id}`, // eslint-disable-line no-underscore-dangle - title: s.title, - value: s._id, // eslint-disable-line no-underscore-dangle - }; - }); - if (parsedDocs?.prev?.page) { - specs.push({ - description: 'Go to the previous page', - title: `< Prev (page ${currPage - 1} of ${totalPages})`, - value: 'prev', - }); - } - if (parsedDocs?.next?.page) { - specs.push({ - description: 'Go to the next page', - title: `Next (page ${currPage + 1} of ${totalPages}) >`, - value: 'next', - }); - } - return specs; -} - -const updateOasPrompt = ( - specList: SpecList, - parsedDocs: ParsedDocs | null, - currPage: number, - totalPages: number, - getSpecs: (url: string) => Promise, -): PromptObject<'specId'>[] => [ - { - type: 'select', - name: 'specId', - message: 'Select your desired file to update', - choices: specOptions(specList, parsedDocs, currPage, totalPages), - async format(spec: string) { - if (spec === 'prev') { - try { - const newSpecs = await getSpecs(`${parsedDocs?.prev?.url || ''}`); - const newParsedDocs = parse(newSpecs.headers.get('link')); - const newSpecList = await handleAPIv1Res(newSpecs); - const { specId }: { specId: string } = await promptTerminal( - updateOasPrompt(newSpecList, newParsedDocs, currPage - 1, totalPages, getSpecs), - ); - return specId; - } catch (e) { - debug(`error retrieving previous specs: ${e.message}`); - return null; - } - } else if (spec === 'next') { - try { - const newSpecs = await getSpecs(`${parsedDocs?.next?.url || ''}`); - const newParsedDocs = parse(newSpecs.headers.get('link')); - const newSpecList = await handleAPIv1Res(newSpecs); - const { specId }: { specId: string } = await promptTerminal( - updateOasPrompt(newSpecList, newParsedDocs, currPage + 1, totalPages, getSpecs), - ); - return specId; - } catch (e) { - debug(`error retrieving next specs: ${e.message}`); - return null; - } - } - - return spec; - }, - }, -]; - -export function createOasPrompt( - specList: SpecList, - parsedDocs: ParsedDocs | null, - totalPages: number, - getSpecs: (url: string) => Promise, -): PromptObject<'option'>[] { - return [ - { - type: 'select', - name: 'option', - message: 'Would you like to update an existing OAS file or create a new one?', - choices: [ - { title: 'Update existing', value: 'update' }, - { title: 'Create a new spec', value: 'create' }, - ], - async format(picked: OpenAPIPromptOptions) { - if (picked === 'update') { - const { specId }: { specId: string } = await promptTerminal( - updateOasPrompt(specList, parsedDocs, 1, totalPages, getSpecs), - ); - return specId; - } - - return picked; - }, - }, - ]; -} diff --git a/src/lib/versionSelect.ts b/src/lib/versionSelect.ts deleted file mode 100644 index 58646e12c..000000000 --- a/src/lib/versionSelect.ts +++ /dev/null @@ -1,80 +0,0 @@ -import type { APIv1ErrorResponse } from './apiError.js'; - -import { APIv1Error } from './apiError.js'; -import isCI from './isCI.js'; -import { warn } from './logger.js'; -import promptTerminal from './promptWrapper.js'; -import { cleanAPIv1Headers, handleAPIv1Res, readmeAPIv1Fetch } from './readmeAPIFetch.js'; - -interface Version { - codename?: string; - createdAt?: string; - from?: string; - is_beta?: boolean; - is_deprecated?: boolean; - is_hidden?: boolean; - is_stable: boolean; - version: string; -} - -/** - * Validates and returns a project version. - * - * @param versionFlag version input parameter - * @param key project API key - * @returns a cleaned up project version - */ -export async function getProjectVersion( - versionFlag: string | undefined, - key: string, - returnStable = false, -): Promise { - try { - if (versionFlag) { - return await readmeAPIv1Fetch(`/api/v1/version/${versionFlag}`, { - method: 'get', - headers: cleanAPIv1Headers(key), - }) - .then(handleAPIv1Res) - .then((res: Version) => res.version); - } - - if (isCI()) { - warn('No `--version` parameter detected in current CI environment. Defaulting to main version.'); - return undefined; - } - - const versionList: Version[] = await readmeAPIv1Fetch('/api/v1/version', { - method: 'get', - headers: cleanAPIv1Headers(key), - }).then(handleAPIv1Res); - - if (versionList.length === 1) { - return versionList[0].version; - } - - if (returnStable) { - const stableVersion = versionList.find(v => v.is_stable === true); - if (!stableVersion) { - throw new Error('Unexpected version response from the ReadMe API. Get in touch with us at support@readme.io!'); - } - return stableVersion.version; - } - - const { versionSelection } = await promptTerminal({ - type: 'select', - name: 'versionSelection', - message: 'Select your desired version', - choices: versionList.map(v => { - return { - title: v.version, - value: v.version, - }; - }), - }); - - return versionSelection; - } catch (err) { - return Promise.reject(new APIv1Error(err as APIv1ErrorResponse)); - } -}