diff --git a/lib/api/github.js b/lib/api/github.js index 4feeb7de2acd1abc9b6037836ba43d3626e0bfe9..e9a3302c5199a0896b7a503edd51be6a98dcb355 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 5994e3ccca7906ce537f4a043fe6702eaf5e8abc..d8de4ffd90892ad0431a50a85f0408662feb2d14 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 ee24817ab125e8001db37df9da9121c85a85d495..31795af9e91700e65836305db993c1350ce9dde0 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 6abc21adbffc9cc7a2df26a1efe527f5ba04964c..dd02013fc68f0c44fcb89ea7495242930cf0b04e 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');