Skip to content
Snippets Groups Projects
Commit 2820b5ee authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

feat: implement findFilePaths on gitlab (#945)

parent 33851a5a
Branches
No related tags found
No related merge requests found
...@@ -123,10 +123,26 @@ async function setBaseBranch(branchName) { ...@@ -123,10 +123,26 @@ async function setBaseBranch(branchName) {
// Search // Search
// Returns an array of file paths in current repo matching the fileName // Get full file list
async function findFilePaths() { async function getFileList(branchName) {
logger.debug("Can't find multiple package.json files in GitLab"); if (config.fileList) {
return []; 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 // Branch
......
...@@ -193,6 +193,14 @@ Array [ ...@@ -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 getBranch returns a branch 1`] = `"foo"`;
exports[`api/gitlab getBranchLastCommitTime should return a Date 1`] = `2012-09-20T08:50:22.000Z`; exports[`api/gitlab getBranchLastCommitTime should return a Date 1`] = `2012-09-20T08:50:22.000Z`;
......
...@@ -191,11 +191,40 @@ describe('api/gitlab', () => { ...@@ -191,11 +191,40 @@ describe('api/gitlab', () => {
}); });
}); });
describe('findFilePaths(fileName)', () => { describe('findFilePaths(fileName)', () => {
it('should return empty array', async () => { it('warns if truncated result', async () => {
await initRepo('some/repo', 'token'); await initRepo('some/repo', 'token');
glGot.mockImplementationOnce(() => ({
body: [],
}));
const files = await gitlab.findFilePaths('package.json'); const files = await gitlab.findFilePaths('package.json');
expect(files.length).toBe(0); 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)', () => { describe('branchExists(branchName)', () => {
it('should return true if 200 OK', async () => { it('should return true if 200 OK', async () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment