diff --git a/lib/api/github.js b/lib/api/github.js index e0ca936639c0864b14782e107c80f3ed14652a6f..5f901a568dd026ccba51222eef6b353301406b2b 100644 --- a/lib/api/github.js +++ b/lib/api/github.js @@ -233,19 +233,26 @@ async function getFileList(branchName) { if (config.fileList) { return config.fileList; } - const res = await get( - `repos/${config.repoName}/git/trees/${branchName}?recursive=true` - ); - if (res.body.truncated) { - logger.warn( - { repository: config.repoName }, - 'repository tree is truncated' + try { + const res = await get( + `repos/${config.repoName}/git/trees/${branchName}?recursive=true` ); + if (res.body.truncated) { + logger.warn( + { repository: config.repoName }, + 'repository tree is truncated' + ); + } + config.fileList = res.body.tree + .filter(item => item.type === 'blob') + .map(item => item.path) + .sort(); + } catch (err) { + // TODO: change this from warn to info once we know exactly why it happens + logger.warn({ repository: config.repoName }, 'Error retrieving git tree'); + config.fileList = []; } - config.fileList = res.body.tree - .filter(item => item.type === 'blob') - .map(item => item.path) - .sort(); + return config.fileList; } diff --git a/lib/api/gitlab.js b/lib/api/gitlab.js index 55ac109ad2c39de0461e672c9e8c3f646d59456a..d22716f406cf01c48e3ff0b555bc1560635326e0 100644 --- a/lib/api/gitlab.js +++ b/lib/api/gitlab.js @@ -128,14 +128,19 @@ async function getFileList(branchName) { if (config.fileList) { return config.fileList; } - const res = await get( - `projects/${config.repoName}/repository/tree?ref=${branchName}&recursive=true&per_page=100`, - { paginate: true } - ); - config.fileList = res.body - .filter(item => item.type === 'blob') - .map(item => item.path) - .sort(); + try { + const res = await get( + `projects/${config.repoName}/repository/tree?ref=${branchName}&recursive=true&per_page=100`, + { paginate: true } + ); + config.fileList = res.body + .filter(item => item.type === 'blob') + .map(item => item.path) + .sort(); + } catch (err) { + logger.warn('Error retrieving git tree'); + config.fileList = []; + } return config.fileList; } diff --git a/test/api/github.spec.js b/test/api/github.spec.js index 6d13a27aae0958cf7bf871d75f62de2793d116a6..92e48fc18a3e8b467d1789f5bf9b961ccd73e903 100644 --- a/test/api/github.spec.js +++ b/test/api/github.spec.js @@ -469,6 +469,16 @@ describe('api/github', () => { expect(get.mock.calls).toMatchSnapshot(); }); }); + describe('getFileList', () => { + it('returns empty array if error', async () => { + await initRepo('some/repo', 'token'); + get.mockImplementationOnce(() => { + throw new Error('some error'); + }); + const files = await github.findFilePaths('someething'); + expect(files).toEqual([]); + }); + }); describe('findFilePaths(fileName)', () => { it('warns if truncated result', async () => { await initRepo('some/repo', 'token'); diff --git a/test/api/gitlab.spec.js b/test/api/gitlab.spec.js index e8eb9e2aadadbc90cbf01b8872f4fedb95d3ce6d..4ff05f92dcf95f635587fa5f4531313c957fb37e 100644 --- a/test/api/gitlab.spec.js +++ b/test/api/gitlab.spec.js @@ -191,6 +191,14 @@ describe('api/gitlab', () => { }); }); describe('findFilePaths(fileName)', () => { + it('returns empty array if error', async () => { + await initRepo('some/repo', 'token'); + get.mockImplementationOnce(() => { + throw new Error('some error'); + }); + const files = await gitlab.findFilePaths('someething'); + expect(files).toEqual([]); + }); it('warns if truncated result', async () => { await initRepo('some/repo', 'token'); get.mockImplementationOnce(() => ({