Skip to content

Commit 59a913f

Browse files
feat(openapi/upload): add --confirm-overwrite flag (#1168)
| 🚥 Resolves #1167 | | :------------------- | ## 🧰 Changes If an automated system runs `openapi upload` on a pre-existing file, then an overwrite prompt is shown. There is no way to skip this prompt which can lock up automated systems. This PR adds a flag to skip the prompt. ## 🧬 QA & Testing Provide as much information as you can on how to test what you've done. **Case 1 - confirm existing behavior** - Upload a new API spec to readme using `openapi upload {spec} --key={key} --slug={slug}` - Try uploading the API spec again to trigger the overwrite prompt **Case 2 - test new behavior** - Upload a new API spec to readme using `openapi upload {spec} --key={key} --slug={slug}` - Try uploading the API spec again using `openapi upload {spec} --key={key} --slug={slug} --overwrite` to skip the overwrite prompt --------- Co-authored-by: Kanad Gupta <[email protected]>
1 parent 4661e93 commit 59a913f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

__tests__/commands/openapi/upload.test.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import nock from 'nock';
22
import prompts from 'prompts';
33
import slugify from 'slugify';
4-
import { describe, beforeAll, beforeEach, afterEach, it, expect } from 'vitest';
4+
import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
55

66
import Command from '../../../src/commands/openapi/upload.js';
77
import petstore from '../../__fixtures__/petstore-simple-weird-version.json' with { type: 'json' };
@@ -428,4 +428,26 @@ describe('rdme openapi upload', () => {
428428
fileMock.done();
429429
});
430430
});
431+
432+
describe('given that the confirm overwrite flag is passed', () => {
433+
it('should overwrite an existing API definition without asking for confirmation', async () => {
434+
const mock = getAPIv2Mock({ authorization: `Bearer ${key}` })
435+
.get(`/versions/${version}/apis`)
436+
.reply(200, { data: [{ filename: slugifiedFilename }] })
437+
.put(`/versions/${version}/apis/${slugifiedFilename}`, body =>
438+
body.match(`form-data; name="schema"; filename="${slugifiedFilename}"`),
439+
)
440+
.reply(200, {
441+
data: {
442+
upload: { status: 'done' },
443+
uri: `/versions/${version}/apis/${slugifiedFilename}`,
444+
},
445+
});
446+
447+
const result = await run(['--version', version, filename, '--key', key, '--confirm-overwrite']);
448+
expect(result.stdout).toContain('was successfully updated in ReadMe!');
449+
450+
mock.done();
451+
});
452+
});
431453
});

src/commands/openapi/upload.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export default class OpenAPIUploadCommand extends BaseCommand<typeof OpenAPIUplo
3030

3131
static flags = {
3232
key: keyFlag,
33+
'confirm-overwrite': Flags.boolean({
34+
description:
35+
'If set, file overwrites will be made without a confirmation prompt. This flag can be a useful in automated environments where prompts cannot be responded to.',
36+
hidden: true,
37+
}),
3338
slug: Flags.string({
3439
summary: 'Override the slug (i.e., the unique identifier) for your API definition.',
3540
description: [
@@ -146,7 +151,7 @@ export default class OpenAPIUploadCommand extends BaseCommand<typeof OpenAPIUplo
146151
if (method === 'PUT') {
147152
// bypass the prompt if we're in a CI environment
148153
prompts.override({
149-
confirm: isCI() ? true : undefined,
154+
confirm: isCI() || this.flags['confirm-overwrite'] ? true : undefined,
150155
});
151156

152157
const { confirm } = await promptTerminal({

0 commit comments

Comments
 (0)