Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version selection to all relevant commands #344

Merged
merged 5 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions __tests__/cmds/docs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ describe('rdme docs', () => {
return expect(docs.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
});

it('should error if no version provided', () => {
expect.assertions(1);
return expect(docs.run({ key })).rejects.toThrow('No project version provided. Please use `--version`.');
});

it('should error if no folder provided', () => {
expect.assertions(1);
return expect(docs.run({ key, version: '1.0.0' })).rejects.toThrow(
Expand Down Expand Up @@ -103,13 +98,19 @@ describe('rdme docs', () => {
.basicAuth({ user: key })
.reply(200);

const versionMock = nock(config.host)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return docs.run({ folder: './__tests__/__fixtures__/existing-docs', key, version }).then(skippedDocs => {
// All docs should have been updated because their hashes from the GET request were different from what they
// are currently.
expect(skippedDocs).toHaveLength(0);

getMocks.done();
updateMocks.done();
versionMock.done();
});
});

Expand All @@ -124,13 +125,19 @@ describe('rdme docs', () => {
.basicAuth({ user: key })
.reply(200, { category, slug: anotherDoc.slug, lastUpdatedHash: anotherDoc.hash });

const versionMock = nock(config.host)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return docs.run({ folder: './__tests__/__fixtures__/existing-docs', key, version }).then(skippedDocs => {
expect(skippedDocs).toStrictEqual([
'`simple-doc` was not updated because there were no changes.',
'`another-doc` was not updated because there were no changes.',
]);

getMocks.done();
versionMock.done();
});
});
});
Expand All @@ -156,9 +163,15 @@ describe('rdme docs', () => {
.basicAuth({ user: key })
.reply(201);

const versionMock = nock(config.host)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return docs.run({ folder: './__tests__/__fixtures__/new-docs', key, version }).then(() => {
getMock.done();
postMock.done();
versionMock.done();
});
});

Expand Down Expand Up @@ -219,6 +232,11 @@ describe('rdme docs', () => {
category,
});

const versionMock = nock(config.host)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return docs.run({ folder: './__tests__/__fixtures__/failure-docs', key, version }).then(message => {
expect(console.log).toHaveBeenCalledTimes(1);
expect(message).toStrictEqual([
Expand All @@ -242,6 +260,7 @@ describe('rdme docs', () => {

getMocks.done();
postMocks.done();
versionMock.done();

console.log.mockRestore();
});
Expand All @@ -262,10 +281,6 @@ describe('rdme docs:edit', () => {
return expect(docsEdit.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
});

it('should error if no version provided', () => {
return expect(docsEdit.run({ key })).rejects.toThrow('No project version provided. Please use `--version`.');
});

it('should error if no slug provided', () => {
return expect(docsEdit.run({ key, version: '1.0.0' })).rejects.toThrow(
'No slug provided. Usage `rdme docs:edit <slug> [options]`.'
Expand All @@ -292,6 +307,11 @@ describe('rdme docs:edit', () => {
.basicAuth({ user: key })
.reply(200);

const versionMock = nock(config.host)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

function mockEditor(filename, cb) {
expect(filename).toBe(`${slug}.md`);
expect(fs.existsSync(filename)).toBe(true);
Expand All @@ -301,6 +321,7 @@ describe('rdme docs:edit', () => {
return docsEdit.run({ slug, key, version: '1.0.0', mockEditor }).then(() => {
getMock.done();
putMock.done();
versionMock.done();
expect(fs.existsSync(`${slug}.md`)).toBe(false);

expect(console.log).toHaveBeenCalledWith('Doc successfully updated. Cleaning up local file.');
Expand All @@ -320,8 +341,14 @@ describe('rdme docs:edit', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const versionMock = nock(config.host)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return docsEdit.run({ slug, key, version: '1.0.0' }).catch(err => {
getMock.done();
versionMock.done();
expect(err.code).toBe('DOC_NOTFOUND');
expect(err.message).toContain("The doc with the slug 'no-such-doc' couldn't be found");
});
Expand All @@ -341,6 +368,11 @@ describe('rdme docs:edit', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const versionMock = nock(config.host)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

function mockEditor(filename, cb) {
return cb(0);
}
Expand All @@ -349,6 +381,7 @@ describe('rdme docs:edit', () => {
expect(err.code).toBe('DOC_INVALID');
getMock.done();
putMock.done();
versionMock.done();
expect(fs.existsSync(`${slug}.md`)).toBe(true);
fs.unlinkSync(`${slug}.md`);
});
Expand All @@ -359,7 +392,12 @@ describe('rdme docs:edit', () => {
const slug = 'getting-started';
const body = 'abcdef';

nock(config.host).get(`/api/v1/docs/${slug}`).reply(200, { body });
nock(config.host)
rahulhegdee marked this conversation as resolved.
Show resolved Hide resolved
.get(`/api/v1/docs/${slug}`)
.reply(200, { body })
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

function mockEditor(filename, cb) {
return cb(1);
Expand Down
59 changes: 28 additions & 31 deletions __tests__/cmds/versions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ describe('rdme versions*', () => {
});
});

it('should error if no version provided', () => {
expect.assertions(1);
return createVersion.run({ key }).catch(err => {
expect(err.message).toBe('Please specify a semantic version. See `rdme help versions:create` for help.');
});
});

it('should get a specific version object', () => {
promptHandler.createVersionPrompt.mockResolvedValue({
is_stable: true,
Expand Down Expand Up @@ -158,29 +151,34 @@ describe('rdme versions*', () => {
});
});

it('should error if no version provided', () => {
expect.assertions(1);
return deleteVersion.run({ key }).catch(err => {
expect(err.message).toBe('Please specify a semantic version. See `rdme help versions:delete` for help.');
});
});

it('should delete a specific version', () => {
const mockRequest = nock(config.host).delete(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200);
const mockRequest = nock(config.host)
.delete(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return deleteVersion.run({ key, version }).then(() => {
mockRequest.done();
});
});

it('should catch any request errors', () => {
const mockRequest = nock(config.host).delete(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(404, {
error: 'VERSION_NOTFOUND',
message:
"The version you specified ({version}) doesn't match any of the existing versions ({versions_list}) in ReadMe.",
suggestion: '...a suggestion to resolve the issue...',
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});
const mockRequest = nock(config.host)
.delete(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(404, {
error: 'VERSION_NOTFOUND',
message:
"The version you specified ({version}) doesn't match any of the existing versions ({versions_list}) in ReadMe.",
suggestion: '...a suggestion to resolve the issue...',
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
})
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return deleteVersion.run({ key, version }).catch(err => {
expect(err.message).toContain('The version you specified');
Expand All @@ -197,13 +195,6 @@ describe('rdme versions*', () => {
});
});

it('should error if no version provided', () => {
expect.assertions(1);
return updateVersion.run({ key }).catch(err => {
expect(err.message).toBe('Please specify a semantic version. See `rdme help versions:update` for help.');
});
});

it('should update a specific version object', () => {
promptHandler.createVersionPrompt.mockResolvedValue({
is_stable: false,
Expand All @@ -217,7 +208,10 @@ describe('rdme versions*', () => {
.reply(200, { version })
.put(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(201, { version });
.reply(201, { version })
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return updateVersion.run({ key, version }).then(() => {
mockRequest.done();
Expand All @@ -242,7 +236,10 @@ describe('rdme versions*', () => {
message: "You can't make a stable version non-stable",
suggestion: '...a suggestion to resolve the issue...',
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});
})
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return updateVersion.run({ key, version }).catch(err => {
expect(err.message).toContain("You can't make a stable version non-stable");
Expand Down
2 changes: 1 addition & 1 deletion __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('cli', () => {
conf.set('apiKey', '123456');
return cli(['docs']).catch(err => {
conf.delete('apiKey');
expect(err.message).toBe('No project version provided. Please use `--version`.');
expect(err.message).toBe('No folder provided. Usage `rdme docs <folder> [options]`.');
});
});

Expand Down
11 changes: 6 additions & 5 deletions src/cmds/docs/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const editor = require('editor');
const { promisify } = require('util');
const APIError = require('../../lib/apiError');
const { getSwaggerVersion } = require('../../lib/versionSelect');

const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
Expand Down Expand Up @@ -41,19 +42,19 @@ exports.run = async function (opts) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!version) {
return Promise.reject(new Error('No project version provided. Please use `--version`.'));
}

if (!slug) {
return Promise.reject(new Error(`No slug provided. Usage \`${config.cli} ${exports.usage}\`.`));
}

const selectedVersion = await getSwaggerVersion(version, key, true).catch(e => {
return Promise.reject(e);
});

const filename = `${slug}.md`;
const options = {
auth: { user: key },
headers: {
'x-readme-version': version,
'x-readme-version': selectedVersion,
},
};

Expand Down
11 changes: 6 additions & 5 deletions src/cmds/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const crypto = require('crypto');
const frontMatter = require('gray-matter');
const { promisify } = require('util');
const APIError = require('../../lib/apiError');
const { getSwaggerVersion } = require('../../lib/versionSelect');

const readFile = promisify(fs.readFile);

Expand Down Expand Up @@ -42,14 +43,14 @@ exports.run = async function (opts) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!version) {
return Promise.reject(new Error('No project version provided. Please use `--version`.'));
}

if (!folder) {
return Promise.reject(new Error(`No folder provided. Usage \`${config.cli} ${exports.usage}\`.`));
}

const selectedVersion = await getSwaggerVersion(version, key, true).catch(e => {
return Promise.reject(e);
});

// Find the files to sync
const readdirRecursive = folderToSearch => {
const filesInFolder = fs.readdirSync(folderToSearch, { withFileTypes: true });
Expand All @@ -72,7 +73,7 @@ exports.run = async function (opts) {
const options = {
auth: { user: key },
headers: {
'x-readme-version': version,
'x-readme-version': selectedVersion,
},
};

Expand Down
30 changes: 2 additions & 28 deletions src/cmds/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { prompt } = require('enquirer');
const OASNormalize = require('oas-normalize');
const promptOpts = require('../lib/prompts');
const APIError = require('../lib/apiError');
const { getSwaggerVersion } = require('../lib/versionSelect');

exports.command = 'openapi';
exports.usage = 'openapi [file] [options]';
Expand Down Expand Up @@ -163,35 +164,8 @@ exports.run = async function (opts) {
return updateSpec(id);
}

async function getSwaggerVersion(versionFlag) {
const options = { json: {}, auth: { user: key } };

try {
if (versionFlag) {
options.json.version = versionFlag;
const foundVersion = await request.get(`${config.host}/api/v1/version/${versionFlag}`, options);

return foundVersion.version;
}

const versionList = await request.get(`${config.host}/api/v1/version`, options);
const { option, versionSelection, newVersion } = await prompt(
promptOpts.generatePrompts(versionList, versionFlag)
);

if (option === 'update') return versionSelection;

options.json = { from: versionList[0].version, version: newVersion, is_stable: false };
await request.post(`${config.host}/api/v1/version`, options);

return newVersion;
} catch (err) {
return Promise.reject(new APIError(err));
}
}

if (!id) {
selectedVersion = await getSwaggerVersion(version).catch(e => {
selectedVersion = await getSwaggerVersion(version, key, true).catch(e => {
return Promise.reject(e);
});
}
Expand Down
Loading