Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: commit media with post #2823

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 54 additions & 35 deletions packages/netlify-cms-backend-github/src/API.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Base64 } from 'js-base64';
import semaphore from 'semaphore';
import { find, flow, get, hasIn, initial, last, partial, result, uniq } from 'lodash';
import { find, flow, get, hasIn, initial, last, partial, result, differenceBy } from 'lodash';
import trimStart from 'lodash/trimStart';
import { map } from 'lodash/fp';
import {
getAllResponses,
Expand Down Expand Up @@ -293,7 +294,7 @@ export default class API {
return text;
}

async getMediaDisplayURL(sha, path) {
async getMediaAsBlob(sha, path) {
const response = await this.fetchBlob(sha, this.repoURL);
let blob;
if (path.match(/.svg$/)) {
Expand All @@ -302,6 +303,11 @@ export default class API {
} else {
blob = await response.blob();
}
return blob;
}

async getMediaDisplayURL(sha, path) {
const blob = await this.getMediaAsBlob(sha, path);

return URL.createObjectURL(blob);
}
Expand Down Expand Up @@ -498,14 +504,13 @@ export default class API {
const fileTree = {};

files.forEach(file => {
if (file.uploaded) {
if (file.skip) {
return;
}
parts = file.path.split('/').filter(part => part);
filename = parts.pop();
subtree = fileTree;
while ((part = parts.shift())) {
// eslint-disable-line no-cond-assign
subtree[part] = subtree[part] || {};
subtree = subtree[part];
}
Expand All @@ -516,30 +521,35 @@ export default class API {
return fileTree;
}

persistFiles(entry, mediaFiles, options) {
const uploadPromises = [];
async persistFiles(entry, mediaFiles, options) {
const files = entry ? mediaFiles.concat(entry) : mediaFiles;

// mark files to skip, has to be done here as uploadBlob sets the uploaded flag
files.forEach(file => {
if (file.uploaded) {
return;
file.skip = true;
} else {
file.skip = false;
}
uploadPromises.push(this.uploadBlob(file));
});

const fileTree = this.composeFileTree(files);
const uploadPromises = files.filter(file => !file.skip).map(file => this.uploadBlob(file));
await Promise.all(uploadPromises);

return Promise.all(uploadPromises).then(() => {
if (!options.useWorkflow) {
return this.getBranch()
.then(branchData => this.updateTree(branchData.commit.sha, '/', fileTree))
.then(changeTree => this.commit(options.commitMessage, changeTree))
.then(response => this.patchBranch(this.branch, response.sha));
} else {
const mediaFilesList = mediaFiles.map(file => ({ path: file.path, sha: file.sha }));
return this.editorialWorkflowGit(fileTree, entry, mediaFilesList, options);
}
});
if (!options.useWorkflow) {
const fileTree = this.composeFileTree(files);

return this.getBranch()
.then(branchData => this.updateTree(branchData.commit.sha, '/', fileTree))
.then(changeTree => this.commit(options.commitMessage, changeTree))
.then(response => this.patchBranch(this.branch, response.sha));
} else {
const mediaFilesList = mediaFiles.map(({ sha, path }) => ({
path: trimStart(path, '/'),
sha,
}));
return this.editorialWorkflowGit(files, entry, mediaFilesList, options);
}
}

getFileSha(path, branch) {
Expand Down Expand Up @@ -586,12 +596,13 @@ export default class API {
return this.createPR(commitMessage, branchName);
}

async editorialWorkflowGit(fileTree, entry, filesList, options) {
async editorialWorkflowGit(files, entry, mediaFilesList, options) {
const contentKey = this.generateContentKey(options.collectionName, entry.slug);
const branchName = this.generateBranchName(contentKey);
const unpublished = options.unpublished || false;
if (!unpublished) {
// Open new editorial review workflow for this entry - Create new metadata and commit to new branch
const fileTree = this.composeFileTree(files);
const userPromise = this.user();
const branchData = await this.getBranch();
const changeTree = await this.updateTree(branchData.commit.sha, '/', fileTree);
Expand Down Expand Up @@ -629,24 +640,28 @@ export default class API {
path: entry.path,
sha: entry.sha,
},
files: filesList,
files: mediaFilesList,
},
timeStamp: new Date().toISOString(),
});
} else {
// Entry is already on editorial review workflow - just update metadata and commit to existing branch
const metadata = await this.retrieveMetadata(contentKey);
// mark media files to remove
const metadataMediaFiles = get(metadata, 'objects.files', []);
const mediaFilesToRemove = differenceBy(metadataMediaFiles, mediaFilesList, 'path').map(
file => ({ ...file, remove: true }),
);
const fileTree = this.composeFileTree(files.concat(mediaFilesToRemove));
const branchData = await this.getBranch(branchName);
const changeTree = await this.updateTree(branchData.commit.sha, '/', fileTree);
const commitPromise = this.commit(options.commitMessage, changeTree);
const metadataPromise = this.retrieveMetadata(contentKey);
const [commit, metadata] = await Promise.all([commitPromise, metadataPromise]);
const commit = await this.commit(options.commitMessage, changeTree);
const { title, description } = options.parsedData || {};
const metadataFiles = get(metadata.objects, 'files', []);
const files = [...metadataFiles, ...filesList];

const pr = metadata.pr ? { ...metadata.pr, head: commit.sha } : undefined;
const objects = {
entry: { path: entry.path, sha: entry.sha },
files: uniq(files),
files: mediaFilesList,
};
const updatedMetadata = { ...metadata, pr, title, description, objects };

Expand Down Expand Up @@ -1062,26 +1077,30 @@ export default class API {
for (let i = 0, len = tree.tree.length; i < len; i++) {
obj = tree.tree[i];
if ((fileOrDir = fileTree[obj.path])) {
// eslint-disable-line no-cond-assign
added[obj.path] = true;

if (fileOrDir.file) {
updates.push({ path: obj.path, mode: obj.mode, type: obj.type, sha: fileOrDir.sha });
const sha = fileOrDir.remove ? null : fileOrDir.sha;
updates.push({ path: obj.path, mode: obj.mode, type: obj.type, sha });
} else {
updates.push(this.updateTree(obj.sha, obj.path, fileOrDir));
}
}
}

for (filename in fileTree) {
fileOrDir = fileTree[filename];
if (added[filename]) {
continue;
}
updates.push(
fileOrDir.file
? { path: filename, mode: '100644', type: 'blob', sha: fileOrDir.sha }
: this.updateTree(null, filename, fileOrDir),
);

if (fileOrDir.file) {
updates.push({ path: filename, mode: '100644', type: 'blob', sha: fileOrDir.sha });
} else {
updates.push(this.updateTree(null, filename, fileOrDir));
}
}

return Promise.all(updates)
.then(tree => this.createTree(sha, tree))
.then(response => ({
Expand Down
Loading