From ea4d29f1cf22221c2c66a43f550aeb73f7a94c7f Mon Sep 17 00:00:00 2001 From: Nicholas Lim <18374483+niclim@users.noreply.github.com> Date: Tue, 16 Jul 2024 14:51:38 -0400 Subject: [PATCH] allow passing in a custom upload function (#2849) --- package.json | 2 +- projects/fastify-capture/package.json | 2 +- projects/json-pointer-helpers/package.json | 2 +- projects/openapi-io/package.json | 2 +- projects/openapi-utilities/package.json | 2 +- projects/optic/package.json | 2 +- projects/optic/src/commands/diff/diff-all.ts | 54 ++++++++++++-------- projects/optic/src/commands/diff/diff.ts | 45 +++++++++------- projects/optic/src/commands/run.ts | 53 +++++++++++-------- projects/optic/src/init.ts | 8 +-- projects/optic/src/types.ts | 3 ++ projects/rulesets-base/package.json | 2 +- projects/standard-rulesets/package.json | 2 +- 13 files changed, 110 insertions(+), 69 deletions(-) create mode 100644 projects/optic/src/types.ts diff --git a/package.json b/package.json index c304264f3e..d262026ed9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "openapi-workspaces", "license": "MIT", "private": true, - "version": "0.54.13", + "version": "0.55.0", "workspaces": [ "projects/json-pointer-helpers", "projects/openapi-io", diff --git a/projects/fastify-capture/package.json b/projects/fastify-capture/package.json index 6ecf089dda..3cf597f165 100644 --- a/projects/fastify-capture/package.json +++ b/projects/fastify-capture/package.json @@ -2,7 +2,7 @@ "name": "@useoptic/fastify-capture", "license": "MIT", "packageManager": "yarn@4.1.1", - "version": "0.54.13", + "version": "0.55.0", "main": "build/index.js", "types": "build/index.d.ts", "files": [ diff --git a/projects/json-pointer-helpers/package.json b/projects/json-pointer-helpers/package.json index 44f7ea963b..e060c9d220 100644 --- a/projects/json-pointer-helpers/package.json +++ b/projects/json-pointer-helpers/package.json @@ -2,7 +2,7 @@ "name": "@useoptic/json-pointer-helpers", "license": "MIT", "packageManager": "yarn@4.1.1", - "version": "0.54.13", + "version": "0.55.0", "main": "build/index.js", "types": "build/index.d.ts", "files": [ diff --git a/projects/openapi-io/package.json b/projects/openapi-io/package.json index 7938b9a7af..4ef04a2684 100644 --- a/projects/openapi-io/package.json +++ b/projects/openapi-io/package.json @@ -2,7 +2,7 @@ "name": "@useoptic/openapi-io", "license": "MIT", "packageManager": "yarn@4.1.1", - "version": "0.54.13", + "version": "0.55.0", "main": "build/index.js", "types": "build/index.d.ts", "files": [ diff --git a/projects/openapi-utilities/package.json b/projects/openapi-utilities/package.json index cf911afb14..d147dd8841 100644 --- a/projects/openapi-utilities/package.json +++ b/projects/openapi-utilities/package.json @@ -2,7 +2,7 @@ "name": "@useoptic/openapi-utilities", "license": "MIT", "packageManager": "yarn@4.1.1", - "version": "0.54.13", + "version": "0.55.0", "main": "build/index.js", "types": "build/index.d.ts", "files": [ diff --git a/projects/optic/package.json b/projects/optic/package.json index ddd0c4b1e3..b288bcb620 100644 --- a/projects/optic/package.json +++ b/projects/optic/package.json @@ -2,7 +2,7 @@ "name": "@useoptic/optic", "license": "MIT", "packageManager": "yarn@4.1.1", - "version": "0.54.13", + "version": "0.55.0", "main": "build/index.js", "types": "build/index.d.ts", "files": [ diff --git a/projects/optic/src/commands/diff/diff-all.ts b/projects/optic/src/commands/diff/diff-all.ts index a28c615b47..3b8fc7c4dc 100644 --- a/projects/optic/src/commands/diff/diff-all.ts +++ b/projects/optic/src/commands/diff/diff-all.ts @@ -25,6 +25,7 @@ import { jsonChangelog } from './changelog-renderers/json-changelog'; import * as Types from '../../client/optic-backend-types'; import { openUrl } from '../../utils/open-url'; import { renderCloudSetup } from '../../utils/render-cloud'; +import { CustomUploadFn } from '../../types'; const usage = () => ` optic diff-all @@ -42,7 +43,11 @@ Example usage: $ optic diff-all --standard @org/example-standard --web --check `; -export const registerDiffAll = (cli: Command, config: OpticCliConfig) => { +export const registerDiffAll = ( + cli: Command, + config: OpticCliConfig, + options: { customUpload?: CustomUploadFn } +) => { cli .command('diff-all', { hidden: true }) .configureHelp({ @@ -106,7 +111,9 @@ comma separated values (e.g. "**/*.yml,**/*.json")' '[deprecated] all matching APIs are now added by default', false ) - .action(errorHandler(getDiffAllAction(config), { command: 'diff-all' })); + .action( + errorHandler(getDiffAllAction(config, options), { command: 'diff-all' }) + ); }; type DiffAllActionOptions = { @@ -196,7 +203,8 @@ function matchCandidates( async function computeAll( candidateMap: CandidateMap, config: OpticCliConfig, - options: DiffAllActionOptions + options: DiffAllActionOptions, + customOptions: { customUpload?: CustomUploadFn } ): Promise<{ warnings: Warnings; results: Result[]; @@ -398,21 +406,25 @@ async function computeAll( let changelogUrl: string | null = null; let specUrl: string | null = null; if (options.upload) { - const uploadResults = await uploadDiff( - { - from: fromParseResults, - to: toParseResults, - }, - specResults, - config, - specDetails, - { - headTag: options.headTag, - standard, - } - ); - specUrl = uploadResults?.headSpecUrl ?? null; - changelogUrl = uploadResults?.changelogUrl ?? null; + if (customOptions.customUpload) { + await customOptions.customUpload(toParseResults); + } else { + const uploadResults = await uploadDiff( + { + from: fromParseResults, + to: toParseResults, + }, + specResults, + config, + specDetails, + { + headTag: options.headTag, + standard, + } + ); + specUrl = uploadResults?.headSpecUrl ?? null; + changelogUrl = uploadResults?.changelogUrl ?? null; + } } let sourcemapOptions: GetSourcemapOptions = { @@ -619,7 +631,8 @@ function applyGlobFilter( } const getDiffAllAction = - (config: OpticCliConfig) => async (options: DiffAllActionOptions) => { + (config: OpticCliConfig, customOptions: { customUpload?: CustomUploadFn }) => + async (options: DiffAllActionOptions) => { if (options.generated) { logger.warn( chalk.yellow.bold( @@ -729,7 +742,8 @@ const getDiffAllAction = const { warnings, results } = await computeAll( candidateMap, config, - options + options, + customOptions ); for (const result of results) { diff --git a/projects/optic/src/commands/diff/diff.ts b/projects/optic/src/commands/diff/diff.ts index bc4ae5af24..ff4f37a01b 100644 --- a/projects/optic/src/commands/diff/diff.ts +++ b/projects/optic/src/commands/diff/diff.ts @@ -32,6 +32,7 @@ import { computeChecksumForAws } from '../../utils/checksum'; import { openUrl } from '../../utils/open-url'; import { renderCloudSetup } from '../../utils/render-cloud'; import { getSpinner } from '../../utils/spinner'; +import { CustomUploadFn } from '../../types'; type DiffActionOptions = { base: string; @@ -70,7 +71,11 @@ Examples: $ optic diff openapi-spec-v0.yml openapi-spec-v1.yml --check --standard ./other_config.yml `; -export const registerDiff = (cli: Command, config: OpticCliConfig) => { +export const registerDiff = ( + cli: Command, + config: OpticCliConfig, + options: { customUpload?: CustomUploadFn } +) => { cli .command('diff') .configureHelp({ @@ -120,7 +125,7 @@ export const registerDiff = (cli: Command, config: OpticCliConfig) => { '--generated', '[deprecated] Optic no longer differentiates generated and non-generated specifications' ) - .action(errorHandler(getDiffAction(config), { command: 'diff' })); + .action(errorHandler(getDiffAction(config, options), { command: 'diff' })); }; type SpecDetails = { apiId: string; orgId: string } | null; @@ -287,7 +292,7 @@ const runDiff = async ( }; const getDiffAction = - (config: OpticCliConfig) => + (config: OpticCliConfig, customOptions: { customUpload?: CustomUploadFn }) => async ( file1: string | undefined, file2: string | undefined, @@ -389,21 +394,25 @@ const getDiffAction = let [baseParseResult, headParseResult, specDetails] = parsedFiles; if (options.upload) { - const uploadResults = await uploadDiff( - { - from: baseParseResult, - to: headParseResult, - }, - diffResult.specResults, - config, - specDetails, - { - headTag: options.headTag, - standard: diffResult.standard, - } - ); - specUrl = uploadResults?.headSpecUrl ?? null; - maybeChangelogUrl = uploadResults?.changelogUrl ?? null; + if (customOptions.customUpload) { + await customOptions.customUpload(headParseResult); + } else { + const uploadResults = await uploadDiff( + { + from: baseParseResult, + to: headParseResult, + }, + diffResult.specResults, + config, + specDetails, + { + headTag: options.headTag, + standard: diffResult.standard, + } + ); + specUrl = uploadResults?.headSpecUrl ?? null; + maybeChangelogUrl = uploadResults?.changelogUrl ?? null; + } } if (options.json) { console.log( diff --git a/projects/optic/src/commands/run.ts b/projects/optic/src/commands/run.ts index cf8aaa8b72..bfb29bf685 100644 --- a/projects/optic/src/commands/run.ts +++ b/projects/optic/src/commands/run.ts @@ -51,6 +51,7 @@ import { getCaptureStorage } from './capture/storage'; import { captureRequestsFromProxy } from './capture/actions/captureRequests'; import { processCaptures } from './capture/capture'; import { uploadCoverage } from './capture/actions/upload-coverage'; +import { CustomUploadFn } from '../types'; const usage = () => ` To see how Optic handles changes, run Optic in your repository a first time; then make @@ -118,7 +119,11 @@ async function comment(data: CiRunDetails, commenter: CommentApi, sha: string) { } } -export function registerRunCommand(cli: Command, config: OpticCliConfig) { +export function registerRunCommand( + cli: Command, + config: OpticCliConfig, + options: { customUpload?: CustomUploadFn } +) { cli .command('run') .description( @@ -146,7 +151,7 @@ export function registerRunCommand(cli: Command, config: OpticCliConfig) { '[file_paths]', 'Comma-seperated glob patterns matching specifications to process. When omitted, matches all non-ignored specifications.' ) - .action(errorHandler(getRunAction(config), { command: 'run' })); + .action(errorHandler(getRunAction(config, options), { command: 'run' })); } type RunActionOptions = { @@ -375,6 +380,7 @@ const runDiffs = async ({ localSpec, currentBranch, specDetails, + customUpload, }: { specPath: string; cloudTag: string; @@ -382,6 +388,7 @@ const runDiffs = async ({ localSpec: ParseResult; currentBranch: string; specDetails: Exclude, null>; + customUpload?: CustomUploadFn; }) => { let specResults: CompareSpecResults, warnings: string[], @@ -444,22 +451,27 @@ const runDiffs = async ({ let upload: Awaited>; try { - upload = await uploadDiff( - { - from: cloudSpec, - to: localSpec, - }, - specResults, - config, - specDetails, - { - standard, - silent: true, - currentBranch, - } - ); - specUrl = upload?.headSpecUrl ?? undefined; - changelogUrl = upload?.changelogUrl ?? undefined; + if (customUpload) { + await customUpload(cloudSpec); + return; + } else { + upload = await uploadDiff( + { + from: cloudSpec, + to: localSpec, + }, + specResults, + config, + specDetails, + { + standard, + silent: true, + currentBranch, + } + ); + specUrl = upload?.headSpecUrl ?? undefined; + changelogUrl = upload?.changelogUrl ?? undefined; + } } catch (e) { return { success: false, @@ -604,7 +616,7 @@ const runCapture = async ({ }; export const getRunAction = - (config: OpticCliConfig) => + (config: OpticCliConfig, customOptions: { customUpload?: CustomUploadFn }) => async (matchArg: string | undefined, options: RunActionOptions) => { const commentToken = process.env.GITHUB_TOKEN ?? process.env.OPTIC_GITLAB_TOKEN; @@ -800,6 +812,7 @@ export const getRunAction = localSpec, specDetails, specPath, + customUpload: customOptions.customUpload, }); const captureReport = await runCapture({ @@ -807,7 +820,7 @@ export const getRunAction = localSpec, specPath, specDetails, - runId: diffsReport.success ? diffsReport.runId : undefined, + runId: diffsReport?.success ? diffsReport.runId : undefined, organizationId: generatedDetails.organization_id, }); diff --git a/projects/optic/src/init.ts b/projects/optic/src/init.ts index de94d86083..71ff8857e6 100644 --- a/projects/optic/src/init.ts +++ b/projects/optic/src/init.ts @@ -32,6 +32,7 @@ import { registerApiList } from './commands/api/list'; import { registerHistory } from './commands/history'; import { registerRunCommand } from './commands/run'; import path from 'path'; +import { CustomUploadFn } from './types'; const packageJson = require('../package.json'); @@ -65,6 +66,7 @@ export const initCli = async ( cli: Command = cliInstance, options: { hideNotifier?: boolean; + customUpload?: CustomUploadFn; } = {} ): Promise => { cli.name('optic'); @@ -136,8 +138,8 @@ export const initCli = async ( cli.version(packageJson.version, '-V, --version', 'Display version'); cli.addHelpCommand(false); - registerRunCommand(cli, cliConfig); - registerDiff(cli, cliConfig); + registerRunCommand(cli, cliConfig, options); + registerDiff(cli, cliConfig, options); const betaSubcommands = cli.command('beta', { hidden: true }); registerCaptureCommand(cli, cliConfig); @@ -162,7 +164,7 @@ export const initCli = async ( cli.addCommand(updateCommand(), { hidden: true }); registerLint(cli, cliConfig); - registerDiffAll(cli, cliConfig); + registerDiffAll(cli, cliConfig, options); registerLogin(cli, cliConfig); registerDereference(cli, cliConfig); registerBundle(cli, cliConfig); diff --git a/projects/optic/src/types.ts b/projects/optic/src/types.ts new file mode 100644 index 0000000000..35fad936c0 --- /dev/null +++ b/projects/optic/src/types.ts @@ -0,0 +1,3 @@ +import { ParseResult } from './utils/spec-loaders'; + +export type CustomUploadFn = (spec: ParseResult) => Promise; diff --git a/projects/rulesets-base/package.json b/projects/rulesets-base/package.json index 545a78ee7b..36675b92d5 100644 --- a/projects/rulesets-base/package.json +++ b/projects/rulesets-base/package.json @@ -2,7 +2,7 @@ "name": "@useoptic/rulesets-base", "license": "MIT", "packageManager": "yarn@4.1.1", - "version": "0.54.13", + "version": "0.55.0", "main": "build/index.js", "types": "build/index.d.ts", "files": [ diff --git a/projects/standard-rulesets/package.json b/projects/standard-rulesets/package.json index 3dc7eea44e..073cbbf20d 100644 --- a/projects/standard-rulesets/package.json +++ b/projects/standard-rulesets/package.json @@ -2,7 +2,7 @@ "name": "@useoptic/standard-rulesets", "license": "MIT", "packageManager": "yarn@4.1.1", - "version": "0.54.13", + "version": "0.55.0", "main": "build/index.js", "types": "build/index.d.ts", "files": [