diff --git a/lib/api/gitlab.js b/lib/api/gitlab.js index 443aa3c64c7fe2f5607b79e933e9bec9bef33669..bc6ca21975054c83fdd6638ca77d6c86fa8f775e 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 a3f769b1fcb0794e51688ce15ad93e8ad0f1ba9b..4b1036017a31553ee16ec58f873b6f5e82b1d7be 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 1780f09412a951e8f5d26ac6bd1be59f5ef00416..aaa65a26dff7fcbf2a2503d75bcc7d846e475d84 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 () => {