From 792a2b2202ec6a15db0c6797a98834af3a7a3525 Mon Sep 17 00:00:00 2001 From: Rahul Hegde <53894083+rahulhegdee@users.noreply.github.com> Date: Fri, 9 Jul 2021 10:23:29 -0700 Subject: [PATCH] fix: change docs command to always update all valid docs (#338) * fix: change swagger command to always update all valid docs * test: update test to fit docs update --- .../__fixtures__/failure-docs/fail-doc.md | 1 + .../__fixtures__/failure-docs/new-doc.md | 6 + __tests__/cmds/docs.test.js | 109 ++++++++++++++++++ src/cmds/docs/index.js | 20 +++- 4 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 __tests__/__fixtures__/failure-docs/fail-doc.md create mode 100644 __tests__/__fixtures__/failure-docs/new-doc.md diff --git a/__tests__/__fixtures__/failure-docs/fail-doc.md b/__tests__/__fixtures__/failure-docs/fail-doc.md new file mode 100644 index 000000000..e8eba3dc1 --- /dev/null +++ b/__tests__/__fixtures__/failure-docs/fail-doc.md @@ -0,0 +1 @@ +Body diff --git a/__tests__/__fixtures__/failure-docs/new-doc.md b/__tests__/__fixtures__/failure-docs/new-doc.md new file mode 100644 index 000000000..bd970fbec --- /dev/null +++ b/__tests__/__fixtures__/failure-docs/new-doc.md @@ -0,0 +1,6 @@ +--- +category: 5ae122e10fdf4e39bb34db6f +title: This is the document title +--- + +Body diff --git a/__tests__/cmds/docs.test.js b/__tests__/cmds/docs.test.js index 017fcc8cc..e68ecaaee 100644 --- a/__tests__/cmds/docs.test.js +++ b/__tests__/cmds/docs.test.js @@ -138,6 +138,115 @@ describe('rdme docs', () => { postMock.done(); }); }); + + it('should create only valid docs', () => { + console.log = jest.fn(); + expect.assertions(2); + const slug = 'fail-doc'; + const slugTwo = 'new-doc'; + const doc = frontMatter(fs.readFileSync(path.join(fixturesDir, `/failure-docs/${slug}.md`))); + const docTwo = frontMatter(fs.readFileSync(path.join(fixturesDir, `/failure-docs/${slugTwo}.md`))); + const hash = crypto + .createHash('sha1') + .update(fs.readFileSync(path.join(fixturesDir, `/failure-docs/${slug}.md`))) + .digest('hex'); + + const hashTwo = crypto + .createHash('sha1') + .update(fs.readFileSync(path.join(fixturesDir, `/failure-docs/${slugTwo}.md`))) + .digest('hex'); + + const getMock = nock(config.host, { + reqheaders: { + 'x-readme-version': version, + }, + }) + .get(`/api/v1/docs/${slug}`) + .basicAuth({ user: key }) + .reply(404, { + error: 'DOC_NOTFOUND', + message: `The doc with the slug '${slug}' couldn't be found`, + suggestion: '...a suggestion to resolve the issue...', + help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', + }); + + const getMockTwo = nock(config.host, { + reqheaders: { + 'x-readme-version': version, + }, + }) + .get(`/api/v1/docs/${slugTwo}`) + .basicAuth({ user: key }) + .reply(404, { + error: 'DOC_NOTFOUND', + message: `The doc with the slug '${slugTwo}' couldn't be found`, + suggestion: '...a suggestion to resolve the issue...', + help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', + }); + + const postMock = nock(config.host, { + reqheaders: { + 'x-readme-version': version, + }, + }) + .post(`/api/v1/docs`, { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) + .basicAuth({ user: key }) + .reply(400, { + error: 'DOC_INVALID', + message: "We couldn't save this doc (Path `category` is required.).", + }); + + const postMockTwo = nock(config.host, { + reqheaders: { + 'x-readme-version': version, + }, + }) + .post(`/api/v1/docs`, { slug: slugTwo, body: docTwo.content, ...docTwo.data, lastUpdatedHash: hashTwo }) + .basicAuth({ user: key }) + .reply(201, { + metadata: { image: [], title: '', description: '' }, + api: { + method: 'post', + url: '', + auth: 'required', + params: [], + apiSetting: '60ddf83e30681022753e27af', + }, + title: 'This is the document title', + updates: [], + type: 'endpoint', + slug: slugTwo, + body: 'Body', + category: '5ae122e10fdf4e39bb34db6f', + }); + + return docs.run({ folder: './__tests__/__fixtures__/failure-docs', key, version }).then(message => { + expect(console.log).toHaveBeenCalledTimes(1); + expect(message).toStrictEqual([ + { + metadata: { image: [], title: '', description: '' }, + api: { + method: 'post', + url: '', + auth: 'required', + params: [], + apiSetting: '60ddf83e30681022753e27af', + }, + title: 'This is the document title', + updates: [], + type: 'endpoint', + slug: slugTwo, + body: 'Body', + category: '5ae122e10fdf4e39bb34db6f', + }, + ]); + getMock.done(); + getMockTwo.done(); + postMock.done(); + postMockTwo.done(); + console.log.mockRestore(); + }); + }); }); }); diff --git a/src/cmds/docs/index.js b/src/cmds/docs/index.js index 333034eb7..0f78fb4f5 100644 --- a/src/cmds/docs/index.js +++ b/src/cmds/docs/index.js @@ -1,3 +1,4 @@ +require('colors'); const request = require('request-promise-native'); const fs = require('fs'); const path = require('path'); @@ -34,7 +35,7 @@ exports.args = [ }, ]; -exports.run = function (opts) { +exports.run = async function (opts) { const { folder, key, version } = opts; if (!key) { @@ -89,7 +90,7 @@ exports.run = function (opts) { .catch(err => Promise.reject(new APIError(err))); } - return Promise.all( + const updatedDocs = await Promise.allSettled( files.map(async filename => { const file = await readFile(path.join(folder, filename), 'utf8'); const matter = frontMatter(file); @@ -103,7 +104,20 @@ exports.run = function (opts) { ...options, }) .then(updateDoc.bind(null, slug, matter, hash), createDoc.bind(null, slug, matter, hash)) - .catch(err => Promise.reject(new APIError(err))); + .catch(err => { + console.log(`\n\`${slug}\` failed to upload. ${err.message}\n`.red); + }); }) ); + + for (let i = 0; i < updatedDocs.length; ) { + if (updatedDocs[i].value !== undefined) { + updatedDocs[i] = updatedDocs[i].value; // returns only the value of the response + i += 1; + } else { + updatedDocs.splice(i, 1); // we already displayed the error messages so we can filter those out + } + } + + return updatedDocs; };