From a50e65e1432bfe767f895fec7c69619f0e11438b Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@keylocation.sg> Date: Sun, 4 Jun 2017 08:06:40 +0200 Subject: [PATCH] Add robustness to package.json parsing (#250) * Add robustness to package.json parsing * Add tests --- lib/api/github.js | 8 +++++++- lib/worker.js | 6 ++++++ test/api/__snapshots__/github.spec.js.snap | 17 +++++++++++++++++ test/api/github.spec.js | 13 +++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/api/github.js b/lib/api/github.js index 4feeb7de2a..e9a3302c51 100644 --- a/lib/api/github.js +++ b/lib/api/github.js @@ -438,7 +438,13 @@ async function getFileContent(filePath, branchName = config.baseBranch) { } async function getFileJson(filePath, branchName = config.baseBranch) { - return JSON.parse(await getFileContent(filePath, branchName)); + let fileJson = null; + try { + fileJson = JSON.parse(await getFileContent(filePath, branchName)); + } catch (err) { + logger.error(`Failed to parse JSON for ${filePath}`); + } + return fileJson; } // Add a new commit, create branch if not existing diff --git a/lib/worker.js b/lib/worker.js index 5994e3ccca..d8de4ffd90 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -28,6 +28,12 @@ async function processPackageFile(repoName, packageFile, packageConfig) { logger.info(`Processing ${repoName} ${packageFile}`); const packageContent = await config.api.getFileJson(packageFile); + + if (!packageContent) { + logger.verbose('No package.json content found - skipping'); + return []; + } + // Check for renovate config inside the package.json if (packageContent.renovate) { logger.debug( diff --git a/test/api/__snapshots__/github.spec.js.snap b/test/api/__snapshots__/github.spec.js.snap index ee24817ab1..31795af9e9 100644 --- a/test/api/__snapshots__/github.spec.js.snap +++ b/test/api/__snapshots__/github.spec.js.snap @@ -540,6 +540,23 @@ Array [ ] `; +exports[`api/github getFileJson(filePatch, branchName) should return null if invalid JSON 1`] = ` +Array [ + Array [ + "repos/some/repo", + ], + Array [ + "repos/some/repo/git/refs/heads/master", + ], + Array [ + "repos/some/repo/git/commits/1234", + ], + Array [ + "repos/some/repo/contents/package.json?ref=master", + ], +] +`; + exports[`api/github getFileJson(filePatch, branchName) should return the file contents parsed as JSON 1`] = ` Array [ Array [ diff --git a/test/api/github.spec.js b/test/api/github.spec.js index 6abc21adbf..dd02013fc6 100644 --- a/test/api/github.spec.js +++ b/test/api/github.spec.js @@ -869,6 +869,19 @@ describe('api/github', () => { expect(content).toMatchSnapshot(); }); }); + describe('getFileJson(filePatch, branchName)', () => { + it('should return null if invalid JSON', async () => { + await initRepo('some/repo', 'token'); + ghGot.mockImplementationOnce(() => ({ + body: { + content: Buffer.from('{hello: "world"}').toString('base64'), + }, + })); + const content = await github.getFileJson('package.json'); + expect(ghGot.mock.calls).toMatchSnapshot(); + expect(content).toBeNull(); + }); + }); describe('commitFilesToBranch(branchName, files, message, parentBranch)', () => { beforeEach(async () => { await initRepo('some/repo', 'token'); -- GitLab