From 2820b5eef31f201fca048bbc9cb8bbac5a53d35f Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Sun, 15 Oct 2017 22:50:59 +0200
Subject: [PATCH] feat: implement findFilePaths on gitlab (#945)

---
 lib/api/gitlab.js                          | 24 ++++++++++++++---
 test/api/__snapshots__/gitlab.spec.js.snap |  8 ++++++
 test/api/gitlab.spec.js                    | 31 +++++++++++++++++++++-
 3 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/lib/api/gitlab.js b/lib/api/gitlab.js
index 443aa3c64c..bc6ca21975 100644
--- a/lib/api/gitlab.js
+++ b/lib/api/gitlab.js
@@ -123,10 +123,26 @@ async function setBaseBranch(branchName) {
 
 // Search
 
-// Returns an array of file paths in current repo matching the fileName
-async function findFilePaths() {
-  logger.debug("Can't find multiple package.json files in GitLab");
-  return [];
+// Get full file list
+async function getFileList(branchName) {
+  if (config.fileList) {
+    return config.fileList;
+  }
+  const res = await glGot(
+    `projects/${config.repoName}/repository/tree?ref=${branchName}&recursive=1`
+  );
+  config.fileList = res.body
+    .filter(item => item.type === 'blob')
+    .map(item => item.path)
+    .sort();
+  return config.fileList;
+}
+
+// Return all files in the repository matching the filename
+async function findFilePaths(fileName, branchName = config.baseBranch) {
+  return (await getFileList(branchName)).filter(fullFilePath =>
+    fullFilePath.endsWith(fileName)
+  );
 }
 
 // Branch
diff --git a/test/api/__snapshots__/gitlab.spec.js.snap b/test/api/__snapshots__/gitlab.spec.js.snap
index a3f769b1fc..4b1036017a 100644
--- a/test/api/__snapshots__/gitlab.spec.js.snap
+++ b/test/api/__snapshots__/gitlab.spec.js.snap
@@ -193,6 +193,14 @@ Array [
 ]
 `;
 
+exports[`api/gitlab findFilePaths(fileName) should return the files matching the fileName 1`] = `
+Array [
+  "package.json",
+  "src/app/package.json",
+  "src/otherapp/package.json",
+]
+`;
+
 exports[`api/gitlab getBranch returns a branch 1`] = `"foo"`;
 
 exports[`api/gitlab getBranchLastCommitTime should return a Date 1`] = `2012-09-20T08:50:22.000Z`;
diff --git a/test/api/gitlab.spec.js b/test/api/gitlab.spec.js
index 1780f09412..aaa65a26df 100644
--- a/test/api/gitlab.spec.js
+++ b/test/api/gitlab.spec.js
@@ -191,11 +191,40 @@ describe('api/gitlab', () => {
     });
   });
   describe('findFilePaths(fileName)', () => {
-    it('should return empty array', async () => {
+    it('warns if truncated result', async () => {
       await initRepo('some/repo', 'token');
+      glGot.mockImplementationOnce(() => ({
+        body: [],
+      }));
       const files = await gitlab.findFilePaths('package.json');
       expect(files.length).toBe(0);
     });
+    it('caches the result', async () => {
+      await initRepo('some/repo', 'token');
+      glGot.mockImplementationOnce(() => ({
+        body: [],
+      }));
+      let files = await gitlab.findFilePaths('package.json');
+      expect(files.length).toBe(0);
+      files = await gitlab.findFilePaths('package.js');
+      expect(files.length).toBe(0);
+    });
+    it('should return the files matching the fileName', async () => {
+      await initRepo('some/repo', 'token');
+      glGot.mockImplementationOnce(() => ({
+        body: [
+          { type: 'blob', path: 'package.json' },
+          {
+            type: 'blob',
+            path: 'some-dir/package.json.some-thing-else',
+          },
+          { type: 'blob', path: 'src/app/package.json' },
+          { type: 'blob', path: 'src/otherapp/package.json' },
+        ],
+      }));
+      const files = await gitlab.findFilePaths('package.json');
+      expect(files).toMatchSnapshot();
+    });
   });
   describe('branchExists(branchName)', () => {
     it('should return true if 200 OK', async () => {
-- 
GitLab