Skip to content

Commit

Permalink
allow passing in a custom upload function (#2849)
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim authored Jul 16, 2024
1 parent 07cc93a commit ea4d29f
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion projects/fastify-capture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/fastify-capture",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.13",
"version": "0.55.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/json-pointer-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/json-pointer-helpers",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.13",
"version": "0.55.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-io",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.13",
"version": "0.55.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-utilities",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.13",
"version": "0.55.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/optic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/optic",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.13",
"version": "0.55.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
54 changes: 34 additions & 20 deletions projects/optic/src/commands/diff/diff-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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({
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -729,7 +742,8 @@ const getDiffAllAction =
const { warnings, results } = await computeAll(
candidateMap,
config,
options
options,
customOptions
);

for (const result of results) {
Expand Down
45 changes: 27 additions & 18 deletions projects/optic/src/commands/diff/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -287,7 +292,7 @@ const runDiff = async (
};

const getDiffAction =
(config: OpticCliConfig) =>
(config: OpticCliConfig, customOptions: { customUpload?: CustomUploadFn }) =>
async (
file1: string | undefined,
file2: string | undefined,
Expand Down Expand Up @@ -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(
Expand Down
53 changes: 33 additions & 20 deletions projects/optic/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -375,13 +380,15 @@ const runDiffs = async ({
localSpec,
currentBranch,
specDetails,
customUpload,
}: {
specPath: string;
cloudTag: string;
config: OpticCliConfig;
localSpec: ParseResult;
currentBranch: string;
specDetails: Exclude<ReturnType<typeof getApiFromOpticUrl>, null>;
customUpload?: CustomUploadFn;
}) => {
let specResults: CompareSpecResults,
warnings: string[],
Expand Down Expand Up @@ -444,22 +451,27 @@ const runDiffs = async ({

let upload: Awaited<ReturnType<typeof uploadDiff>>;
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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -800,14 +812,15 @@ export const getRunAction =
localSpec,
specDetails,
specPath,
customUpload: customOptions.customUpload,
});

const captureReport = await runCapture({
config,
localSpec,
specPath,
specDetails,
runId: diffsReport.success ? diffsReport.runId : undefined,
runId: diffsReport?.success ? diffsReport.runId : undefined,
organizationId: generatedDetails.organization_id,
});

Expand Down
8 changes: 5 additions & 3 deletions projects/optic/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -65,6 +66,7 @@ export const initCli = async (
cli: Command = cliInstance,
options: {
hideNotifier?: boolean;
customUpload?: CustomUploadFn;
} = {}
): Promise<Command> => {
cli.name('optic');
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions projects/optic/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ParseResult } from './utils/spec-loaders';

export type CustomUploadFn = (spec: ParseResult) => Promise<void>;
Loading

0 comments on commit ea4d29f

Please sign in to comment.