diff --git a/lib/api/github.js b/lib/api/github.js index e78039155f41e9aeb38958c8f02ca53d596b8cfc..cccc9d9eff0f811c71784f1c08395772651c8493 100644 --- a/lib/api/github.js +++ b/lib/api/github.js @@ -169,7 +169,11 @@ async function findFilePaths(fileName) { `search/code?q=repo:${config.repoName}+filename:${fileName}` ); const exactMatches = res.body.items.filter(item => item.name === fileName); - + if (exactMatches.length === 0) { + // This may happen if the repository is a fork, as forks are not indexed unless they have more stars than the original repo + logger.warn('Could not find any package.json files'); + return ['package.json']; + } // GitHub seems to return files in the root with a leading `/` // which then breaks things later on down the line return exactMatches.map(item => item.path.replace(/^\//, '')); diff --git a/test/api/__snapshots__/github.spec.js.snap b/test/api/__snapshots__/github.spec.js.snap index 0ad77e3f154fbb78c54e78f9b32c0bc15bf46c81..44cbd4da0d07e6ae8c3d5470918455539b62fb2e 100644 --- a/test/api/__snapshots__/github.spec.js.snap +++ b/test/api/__snapshots__/github.spec.js.snap @@ -316,6 +316,20 @@ Array [ ] `; +exports[`api/github findFilePaths(fileName) should return default value if none found 1`] = ` +Array [ + Array [ + "repos/some/repo", + ], + Array [ + "repos/some/repo/git/refs/heads/master", + ], + Array [ + "search/code?q=repo:some/repo+filename:package.json", + ], +] +`; + exports[`api/github findFilePaths(fileName) should return the files matching the fileName 1`] = ` Array [ Array [ diff --git a/test/api/github.spec.js b/test/api/github.spec.js index b7d6a366a28c4d38dd1ffc175a344ac2367ec7f5..4c88e44931cd8d6a44dfcd713ce50d7f89b3770a 100644 --- a/test/api/github.spec.js +++ b/test/api/github.spec.js @@ -298,6 +298,17 @@ describe('api/github', () => { }); }); describe('findFilePaths(fileName)', () => { + it('should return default value if none found', async () => { + await initRepo('some/repo', 'token'); + ghGot.mockImplementationOnce(() => ({ + body: { + items: [], + }, + })); + const files = await github.findFilePaths('package.json'); + expect(ghGot.mock.calls).toMatchSnapshot(); + expect(files).toMatchObject(['package.json']); + }); it('should return the files matching the fileName', async () => { await initRepo('some/repo', 'token'); ghGot.mockImplementationOnce(() => ({