Skip to content

Commit

Permalink
fix(versions/update): various edge cases (#965)
Browse files Browse the repository at this point in the history
| 🚥 Resolves readmeio/readme#10989 |
| :------------------- |

## 🧰 Changes

Client-side fixes for readmeio/readme#10989:
- [x] Always includes `version` in body payload for `PUT /version`
requests
- [x] When updating stable version, certain flag prompts (`hidden`,
`deprecated`) are now properly skipped since it's impossible to set
those flags on the stable version.

## 🧬 QA & Testing

Added more test coverage to ensure that these edge cases are accounted
for.
  • Loading branch information
kanadgupta authored Mar 7, 2024
1 parent f1fe8b1 commit 580e307
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
55 changes: 53 additions & 2 deletions __tests__/cmds/versions/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,34 @@ describe('rdme versions:update', () => {
delete process.env.TEST_RDME_CI;
});

it('should update a specific version object', async () => {
it('should update a specific version object using prompts', async () => {
const versionToChange = '1.1.0';
prompts.inject([versionToChange, undefined, false, true, false, false]);

const updatedVersionObject = {
version: versionToChange,
is_stable: false,
is_beta: true,
is_deprecated: false,
is_hidden: false,
};

const mockRequest = getAPIMock()
.get('/api/v1/version')
.basicAuth({ user: key })
.reply(200, [{ version }, { version: versionToChange }])
.get(`/api/v1/version/${versionToChange}`)
.basicAuth({ user: key })
.reply(200, { version: versionToChange })
.put(`/api/v1/version/${versionToChange}`, updatedVersionObject)
.basicAuth({ user: key })
.reply(201, updatedVersionObject);

await expect(updateVersion.run({ key })).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
});

it('should rename a specific version object using prompts', async () => {
const versionToChange = '1.1.0';
const renamedVersion = '1.1.0-update';
prompts.inject([versionToChange, renamedVersion, false, true, false, false]);
Expand All @@ -54,7 +81,31 @@ describe('rdme versions:update', () => {
.reply(200, [{ version }, { version: versionToChange }])
.get(`/api/v1/version/${versionToChange}`)
.basicAuth({ user: key })
.reply(200, { version })
.reply(200, { version: versionToChange })
.put(`/api/v1/version/${versionToChange}`, updatedVersionObject)
.basicAuth({ user: key })
.reply(201, updatedVersionObject);

await expect(updateVersion.run({ key })).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
});

it('should use subset of prompts when updating stable version', async () => {
const versionToChange = '1.1.0';
prompts.inject([versionToChange, undefined, true]);

const updatedVersionObject = {
version: versionToChange,
is_beta: true,
};

const mockRequest = getAPIMock()
.get('/api/v1/version')
.basicAuth({ user: key })
.reply(200, [{ version }, { version: versionToChange, is_stable: true }])
.get(`/api/v1/version/${versionToChange}`)
.basicAuth({ user: key })
.reply(200, { version: versionToChange, is_stable: true })
.put(`/api/v1/version/${versionToChange}`, updatedVersionObject)
.basicAuth({ user: key })
.reply(201, updatedVersionObject);
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/versions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class UpdateVersionCommand extends Command {

// TODO: I think this fetch here is unnecessary but
// it will require a bigger refactor of getProjectVersion
const foundVersion = await readmeAPIFetch(`/api/v1/version/${selectedVersion}`, {
const foundVersion: Version = await readmeAPIFetch(`/api/v1/version/${selectedVersion}`, {
method: 'get',
headers: cleanHeaders(key),
}).then(handleRes);
Expand All @@ -66,7 +66,7 @@ export default class UpdateVersionCommand extends Command {
const body: Version = {
codename,
// fallback to existing version if user was prompted to rename the version but didn't enter anything
version: promptResponse.newVersion || version,
version: promptResponse.newVersion || selectedVersion,
is_beta: promptResponse.is_beta,
is_deprecated: promptResponse.is_deprecated,
is_hidden: promptResponse.is_hidden,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function versionPrompt(
type: (prev, values) => {
// if user wants this version to be the main version
// it can't also be hidden.
return values.is_stable ? null : 'confirm';
return values.is_stable || existingVersion?.is_stable ? null : 'confirm';
},
name: 'is_hidden',
message: 'Should this version be hidden?',
Expand All @@ -193,7 +193,7 @@ export function versionPrompt(
type: (prev, values) => {
// if user wants this version to be the main version
// it can't also be deprecated.
return values.is_stable ? null : 'confirm';
return values.is_stable || existingVersion?.is_stable ? null : 'confirm';
},
name: 'is_deprecated',
message: 'Should this version be deprecated?',
Expand Down

0 comments on commit 580e307

Please sign in to comment.