fix(git-gateway-gitlab): fix large media support for editorial workflow (#3105)

This commit is contained in:
Erez Rokah 2020-01-19 14:25:29 +02:00 committed by GitHub
parent 07fa5b316c
commit 038803c9f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 7147 additions and 12 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,28 @@
import fixture from './common/media_library';
import { entry1 } from './common/entries';
import * as specUtils from './common/spec_utils';
const backend = 'git-gateway';
const provider = 'gitlab';
describe('Git Gateway (GitLab) Backend Media Library - Large Media', () => {
let taskResult = { data: {} };
before(() => {
specUtils.before(taskResult, { publish_mode: 'editorial_workflow', provider }, backend);
});
after(() => {
specUtils.after(taskResult, backend);
});
beforeEach(() => {
specUtils.beforeEach(taskResult, backend);
});
afterEach(() => {
specUtils.afterEach(taskResult, backend);
});
fixture({ entries: [entry1], getUser: () => taskResult.data.user });
});

View File

@ -60,6 +60,7 @@ async function prepareTestGitLabRepo() {
console.log('Creating repository', testRepoName);
await client.Projects.create({
name: testRepoName,
lfs_enabled: false,
});
const tempDir = path.join('.temp', testRepoName);

View File

@ -279,7 +279,10 @@ export default class GitGateway implements Implementation {
files.map(async file => {
if (client.matchPath(file.path)) {
const { id, path } = file;
const largeMediaDisplayURLs = await this.getLargeMediaDisplayURLs([{ ...file, id }]);
const largeMediaDisplayURLs = await this.getLargeMediaDisplayURLs(
[{ ...file, id }],
branch,
);
const url = await client.getDownloadURL(largeMediaDisplayURLs[id]);
return {
id,
@ -379,13 +382,18 @@ export default class GitGateway implements Implementation {
},
);
}
async getLargeMediaDisplayURLs(mediaFiles: { path: string; id: string | null }[]) {
async getLargeMediaDisplayURLs(
mediaFiles: { path: string; id: string | null }[],
branch = this.branch,
) {
const client = await this.getLargeMediaClient();
const filesPromise = entriesByFiles(
mediaFiles,
this.api!.readFile.bind(this.api!),
'Git-Gateway',
);
const readFile = (
path: string,
id: string | null | undefined,
{ parseText }: { parseText: boolean },
) => this.api!.readFile(path, id, { branch, parseText });
const filesPromise = entriesByFiles(mediaFiles, readFile, 'Git-Gateway');
return filesPromise
.then(items =>

View File

@ -453,6 +453,18 @@ export default class API {
return branches;
}
async getFileId(path: string, branch: string) {
const request = await this.request({
method: 'HEAD',
url: `${this.repoURL}/repository/files/${encodeURIComponent(path)}`,
params: { ref: branch },
cache: 'no-store',
});
const blobId = request.headers.get('X-Gitlab-Blob-Id') as string;
return blobId;
}
async isFileExists(path: string, branch: string) {
const fileExists = await this.requestText({
method: 'HEAD',
@ -498,9 +510,14 @@ export default class API {
const mergeRequest = await this.getBranchMergeRequest(branch);
const diff = await this.getDifferences(mergeRequest.sha);
const path = diff.find(d => d.old_path.includes(slug))?.old_path as string;
// TODO: get real file id
const mediaFiles = await Promise.all(
diff.filter(d => d.old_path !== path).map(d => ({ path: d.new_path, id: null })),
diff
.filter(d => d.old_path !== path)
.map(async d => {
const path = d.new_path;
const id = await this.getFileId(path, branch);
return { path, id };
}),
);
const label = mergeRequest.labels.find(isCMSLabel) as string;
const status = labelToStatus(label);

View File

@ -308,9 +308,6 @@ export default class GitLab implements Implementation {
const data = await this.api!.readUnpublishedBranchFile(contentKey);
const mediaFiles = await loadEntryMediaFiles(
data.metaData.branch,
// TODO: fix this
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
data.metaData.objects.entry.mediaFiles,
);
return {