fast forward to github implementation

This commit is contained in:
Cássio Zen
2016-09-19 15:31:01 -03:00
parent ded39fc1bc
commit b56bd24abb

View File

@ -1,7 +1,6 @@
import LocalForage from 'localforage'; import LocalForage from 'localforage';
import MediaProxy from '../../valueObjects/MediaProxy'; import MediaProxy from '../../valueObjects/MediaProxy';
import { Base64 } from 'js-base64'; import { Base64 } from 'js-base64';
import { EDITORIAL_WORKFLOW } from '../../constants/publishModes';
export default class API { export default class API {
constructor(token, url, branch) { constructor(token, url, branch) {
@ -55,7 +54,7 @@ export default class API {
} }
checkMetadataRef() { checkMetadataRef() {
return this.request(`${this.repoURL}/refs/meta/_netlify_cms?${Date.now()}`, { return this.request(`${this.repoURL}/refs/meta/_netlify_cms`, {
cache: 'no-store', cache: 'no-store',
}) })
.then(response => response.object) .then(response => response.object)
@ -91,6 +90,11 @@ export default class API {
.then(item => this.updateTree(branchData.sha, '/', fileTree)) .then(item => this.updateTree(branchData.sha, '/', fileTree))
.then(changeTree => this.commit(`Updating “${key}” metadata`, changeTree)) .then(changeTree => this.commit(`Updating “${key}” metadata`, changeTree))
.then(response => this.patchRef('meta', '_netlify_cms', response.sha)); .then(response => this.patchRef('meta', '_netlify_cms', response.sha));
}).then(() => {
LocalForage.setItem(`gh.meta.${key}`, {
expires: Date.now() + 300000, // In 5 minutes
data
});
}); });
} }
@ -103,31 +107,25 @@ export default class API {
params: { ref: 'refs/meta/_netlify_cms' }, params: { ref: 'refs/meta/_netlify_cms' },
headers: { 'Content-Type': 'application/vnd.netlify.raw' }, headers: { 'Content-Type': 'application/vnd.netlify.raw' },
cache: 'no-store', cache: 'no-store',
}).then((result) => { })
LocalForage.setItem(`gh.meta.${key}`, { .then(response => JSON.parse(response));
expires: Date.now() + 300000, // In 5 minutes
data: result,
});
return result;
});
}); });
} }
readFile(path, sha) { readFile(path, sha, branch = this.branch) {
const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null); const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null);
return cache.then((cached) => { return cache.then((cached) => {
if (cached) { return cached; } if (cached) { return cached; }
return this.request(`${this.repoURL}/files/${path}`, { return this.request(`${this.repoURL}/files/${path}`, {
headers: { 'Content-Type': 'application/vnd.netlify.raw' }, headers: { 'Content-Type': 'application/vnd.netlify.raw' },
params: { ref: this.branch }, params: { ref: branch },
cache: false, cache: false,
raw: true raw: true
}).then((result) => { }).then((result) => {
if (sha) { if (sha) {
LocalForage.setItem(`gh.${sha}`, result); LocalForage.setItem(`gh.${sha}`, result);
} }
return result; return result;
}); });
}); });
@ -142,10 +140,13 @@ export default class API {
persistFiles(entry, mediaFiles, options) { persistFiles(entry, mediaFiles, options) {
let filename, part, parts, subtree; let filename, part, parts, subtree;
const fileTree = {}; const fileTree = {};
const files = []; const uploadPromises = [];
mediaFiles.concat(entry).forEach((file) => {
const files = mediaFiles.concat(entry);
files.forEach((file) => {
if (file.uploaded) { return; } if (file.uploaded) { return; }
files.push(this.uploadBlob(file)); uploadPromises.push(this.uploadBlob(file));
parts = file.path.split('/').filter((part) => part); parts = file.path.split('/').filter((part) => part);
filename = parts.pop(); filename = parts.pop();
subtree = fileTree; subtree = fileTree;
@ -156,20 +157,11 @@ export default class API {
subtree[filename] = file; subtree[filename] = file;
file.file = true; file.file = true;
}); });
return Promise.all(files) return Promise.all(uploadPromises)
.then(() => this.getBranch()) .then(() => this.getBranch())
.then(branchData => this.updateTree(branchData.commit.sha, '/', fileTree)) .then(branchData => this.updateTree(branchData.commit.sha, '/', fileTree))
.then(changeTree => this.commit(options.commitMessage, changeTree)) .then(changeTree => this.commit(options.commitMessage, changeTree))
.then((response) => { .then((response) => this.patchBranch(this.branch, response.sha));
if (options.mode && options.mode === EDITORIAL_WORKFLOW) {
const contentKey = options.collectionName ? `${options.collectionName}-${entry.slug}` : entry.slug;
return this.createBranch(`cms/${contentKey}`, response.sha)
.then(this.storeMetadata(contentKey, { status: 'draft' }))
.then(this.createPR(options.commitMessage, `cms/${contentKey}`));
} else {
return this.patchBranch(this.branch, response.sha);
}
});
} }
createRef(type, name, sha) { createRef(type, name, sha) {
@ -179,10 +171,6 @@ export default class API {
}); });
} }
createBranch(branchName, sha) {
return this.createRef('heads', branchName, sha);
}
patchRef(type, name, sha) { patchRef(type, name, sha) {
return this.request(`${this.repoURL}/refs/${type}/${name}`, { return this.request(`${this.repoURL}/refs/${type}/${name}`, {
method: 'PATCH', method: 'PATCH',
@ -190,12 +178,26 @@ export default class API {
}); });
} }
deleteRef(type, name, sha) {
return this.request(`${this.repoURL}/refs/${type}/${name}`, {
method: 'DELETE',
});
}
getBranch(branch = this.branch) {
return this.request(`${this.repoURL}/refs/heads/${this.branch}`);
}
createBranch(branchName, sha) {
return this.createRef('heads', branchName, sha);
}
patchBranch(branchName, sha) { patchBranch(branchName, sha) {
return this.patchRef('heads', branchName, sha); return this.patchRef('heads', branchName, sha);
} }
getBranch() { deleteBranch(branchName) {
return this.request(`${this.repoURL}/refs/heads/${this.branch}`); return this.deleteRef('heads', branchName);
} }
createPR(title, head, base = 'master') { createPR(title, head, base = 'master') {