From 085e0ed3e4467cd51e5a84c445f3a367bd97a939 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Wed, 28 Jun 2017 11:39:51 +0200
Subject: [PATCH] Return default package.json if GitHub api finds no files

---
 lib/api/github.js                          |  6 +++++-
 test/api/__snapshots__/github.spec.js.snap | 14 ++++++++++++++
 test/api/github.spec.js                    | 11 +++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/lib/api/github.js b/lib/api/github.js
index e78039155f..cccc9d9eff 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 0ad77e3f15..44cbd4da0d 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 b7d6a366a2..4c88e44931 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(() => ({
-- 
GitLab