Fix: get files by path depth (#2993)

* fix: get files up to depth specified by colletion path

* test(e2e): update mock data

* chore: fix comment
This commit is contained in:
Erez Rokah
2019-12-22 15:20:42 +02:00
committed by GitHub
parent 982fd7b0f8
commit b27748b54f
75 changed files with 4075 additions and 3714 deletions

View File

@ -187,10 +187,10 @@ export default class API {
// while the CMS defaults to sorting by filename _ascending_, at
// least in the current GitHub backend). This should eventually be
// refactored.
listFiles = async path => {
listFiles = async (path, recursive = false) => {
const firstPageCursor = await this.fetchCursor({
url: `${this.repoURL}/repository/tree`,
params: { path, ref: this.branch, recursive: true },
params: { path, ref: this.branch, recursive },
});
const lastPageLink = firstPageCursor.data.getIn(['links', 'last']);
const { entries, cursor } = await this.fetchCursorAndEntries(lastPageLink);
@ -209,12 +209,12 @@ export default class API {
};
};
listAllFiles = async path => {
listAllFiles = async (path, recursive = false) => {
const entries = [];
let { cursor, entries: initialEntries } = await this.fetchCursorAndEntries({
url: `${this.repoURL}/repository/tree`,
// Get the maximum number of entries per page
params: { path, ref: this.branch, per_page: 100 },
params: { path, ref: this.branch, per_page: 100, recursive },
});
entries.push(...initialEntries);
while (cursor && cursor.actions.has('next')) {

View File

@ -484,6 +484,34 @@ describe('gitlab backend', () => {
});
});
describe('filterFile', () => {
it('should return true for nested file with matching depth', () => {
backend = resolveBackend(defaultConfig);
expect(
backend.implementation.filterFile(
'content/posts',
{ name: 'index.md', path: 'content/posts/dir1/dir2/index.md' },
'md',
3,
),
).toBe(true);
});
it('should return false for nested file with non matching depth', () => {
backend = resolveBackend(defaultConfig);
expect(
backend.implementation.filterFile(
'content/posts',
{ name: 'index.md', path: 'content/posts/dir1/dir2/index.md' },
'md',
2,
),
).toBe(false);
});
});
afterEach(() => {
nock.cleanAll();
authStore.logout();

View File

@ -1,7 +1,8 @@
import trimStart from 'lodash/trimStart';
import semaphore from 'semaphore';
import { trim } from 'lodash';
import { stripIndent } from 'common-tags';
import { CURSOR_COMPATIBILITY_SYMBOL, basename } from 'netlify-cms-lib-util';
import { CURSOR_COMPATIBILITY_SYMBOL, basename, getCollectionDepth } from 'netlify-cms-lib-util';
import AuthenticationPage from './AuthenticationPage';
import API from './API';
@ -78,9 +79,17 @@ export default class GitLab {
return Promise.resolve(this.token);
}
filterFile(folder, file, extension, depth) {
// gitlab paths include the root folder
const fileFolder = trim(file.path.split(folder)[1] || '/', '/');
return file.name.endsWith('.' + extension) && fileFolder.split('/').length <= depth;
}
entriesByFolder(collection, extension) {
return this.api.listFiles(collection.get('folder')).then(({ files, cursor }) =>
this.fetchFiles(files.filter(file => file.name.endsWith('.' + extension))).then(
const depth = getCollectionDepth(collection);
const folder = collection.get('folder');
return this.api.listFiles(folder, depth > 1).then(({ files, cursor }) =>
this.fetchFiles(files.filter(file => this.filterFile(folder, file, extension, depth))).then(
fetchedFiles => {
const returnedFiles = fetchedFiles;
returnedFiles[CURSOR_COMPATIBILITY_SYMBOL] = cursor;
@ -91,9 +100,13 @@ export default class GitLab {
}
allEntriesByFolder(collection, extension) {
const depth = getCollectionDepth(collection);
const folder = collection.get('folder');
return this.api
.listAllFiles(collection.get('folder'))
.then(files => this.fetchFiles(files.filter(file => file.name.endsWith('.' + extension))));
.listAllFiles(folder, depth > 1)
.then(files =>
this.fetchFiles(files.filter(file => this.filterFile(folder, file, extension, depth))),
);
}
entriesByFiles(collection) {