Skip to content
Snippets Groups Projects
Commit 8ee76c99 authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

fix: check for file content before converting to buffer (github) (#925)

parent f91aa142
No related branches found
No related tags found
No related merge requests found
......@@ -663,7 +663,10 @@ async function getFileContent(filePath, branchName) {
);
try {
const file = await getFile(filePath, branchName);
if (file) {
return Buffer.from(file, 'base64').toString();
}
return null;
} catch (error) {
if (error.statusCode === 404) {
// If file not found, then return null JSON
......
......@@ -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 [
......
......@@ -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(() => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment