From 8ee76c99e28ef3fa259e21d2ef22a0ad5d54bd13 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@keylocation.sg> Date: Fri, 13 Oct 2017 06:14:29 +0200 Subject: [PATCH] fix: check for file content before converting to buffer (github) (#925) --- lib/api/github.js | 5 +++- test/api/__snapshots__/github.spec.js.snap | 29 ++++++++++++++++++++++ test/api/github.spec.js | 9 +++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/api/github.js b/lib/api/github.js index 7e1260ad31..13e727057f 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 183b078fa5..d291793c82 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 6ec0b2c498..8bbd55995e 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(() => { -- GitLab