diff --git a/lib/api/github.js b/lib/api/github.js index 7e1260ad310e607e1325e4cc642532e6c479546d..13e727057fd8909c70ebc0d1d22a0474b709bc60 100644 --- a/lib/api/github.js +++ b/lib/api/github.js @@ -663,7 +663,10 @@ async function getFileContent(filePath, branchName) { ); try { const file = await getFile(filePath, branchName); - return Buffer.from(file, 'base64').toString(); + if (file) { + return Buffer.from(file, 'base64').toString(); + } + return null; } catch (error) { if (error.statusCode === 404) { // If file not found, then return null JSON diff --git a/test/api/__snapshots__/github.spec.js.snap b/test/api/__snapshots__/github.spec.js.snap index 183b078fa5e2b2af813b03c94b6ca5aa4f410d38..d291793c825491dbad2a36c9bc56e22290e28a85 100644 --- a/test/api/__snapshots__/github.spec.js.snap +++ b/test/api/__snapshots__/github.spec.js.snap @@ -832,6 +832,35 @@ Array [ ] `; +exports[`api/github getFileContent(filePatch, branchName) should return null if getFile returns nothing 1`] = ` +Array [ + Array [ + "repos/some/repo", + Object { + "headers": Object { + "accept": "application/vnd.github.loki-preview+json", + }, + }, + ], + Array [ + "repos/some/repo/git/refs/heads/master", + undefined, + ], + Array [ + "repos/some/repo/branches/master/protection/required_status_checks", + Object { + "headers": Object { + "accept": "application/vnd.github.loki-preview+json", + }, + }, + ], + Array [ + "repos/some/repo/contents/package.json?ref=master", + undefined, + ], +] +`; + exports[`api/github getFileContent(filePatch, branchName) should return the encoded file content 1`] = ` Array [ Array [ diff --git a/test/api/github.spec.js b/test/api/github.spec.js index 6ec0b2c4982f94eb80329d95ddf4c21f34afb7f9..8bbd55995edbee8d578d201586efba54f6dcc90e 100644 --- a/test/api/github.spec.js +++ b/test/api/github.spec.js @@ -1497,6 +1497,15 @@ describe('api/github', () => { expect(ghGot.mock.calls).toMatchSnapshot(); expect(content).toBe(null); }); + it('should return null if getFile returns nothing', async () => { + await initRepo('some/repo', 'token'); + ghGot.mockImplementationOnce(() => ({ + body: {}, + })); + const content = await github.getFileContent('package.json'); + expect(ghGot.mock.calls).toMatchSnapshot(); + expect(content).toBe(null); + }); it('should return propagate unknown errors', async () => { await initRepo('some/repo', 'token'); ghGot.mockImplementationOnce(() => {