diff --git a/src/backends/netlify-git/API.js b/src/backends/netlify-git/API.js index 873286e7..886c9f0b 100644 --- a/src/backends/netlify-git/API.js +++ b/src/backends/netlify-git/API.js @@ -1,7 +1,6 @@ import LocalForage from 'localforage'; import MediaProxy from '../../valueObjects/MediaProxy'; import { Base64 } from 'js-base64'; -import { EDITORIAL_WORKFLOW } from '../../constants/publishModes'; export default class API { constructor(token, url, branch) { @@ -55,7 +54,7 @@ export default class API { } checkMetadataRef() { - return this.request(`${this.repoURL}/refs/meta/_netlify_cms?${Date.now()}`, { + return this.request(`${this.repoURL}/refs/meta/_netlify_cms`, { cache: 'no-store', }) .then(response => response.object) @@ -91,6 +90,11 @@ export default class API { .then(item => this.updateTree(branchData.sha, '/', fileTree)) .then(changeTree => this.commit(`Updating “${key}” metadata`, changeTree)) .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' }, headers: { 'Content-Type': 'application/vnd.netlify.raw' }, cache: 'no-store', - }).then((result) => { - LocalForage.setItem(`gh.meta.${key}`, { - expires: Date.now() + 300000, // In 5 minutes - data: result, - }); - return result; - }); + }) + .then(response => JSON.parse(response)); }); } - readFile(path, sha) { + readFile(path, sha, branch = this.branch) { const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null); return cache.then((cached) => { if (cached) { return cached; } return this.request(`${this.repoURL}/files/${path}`, { headers: { 'Content-Type': 'application/vnd.netlify.raw' }, - params: { ref: this.branch }, + params: { ref: branch }, cache: false, raw: true }).then((result) => { if (sha) { LocalForage.setItem(`gh.${sha}`, result); } - return result; }); }); @@ -142,10 +140,13 @@ export default class API { persistFiles(entry, mediaFiles, options) { let filename, part, parts, subtree; const fileTree = {}; - const files = []; - mediaFiles.concat(entry).forEach((file) => { + const uploadPromises = []; + + const files = mediaFiles.concat(entry); + + files.forEach((file) => { if (file.uploaded) { return; } - files.push(this.uploadBlob(file)); + uploadPromises.push(this.uploadBlob(file)); parts = file.path.split('/').filter((part) => part); filename = parts.pop(); subtree = fileTree; @@ -156,20 +157,11 @@ export default class API { subtree[filename] = file; file.file = true; }); - return Promise.all(files) + return Promise.all(uploadPromises) .then(() => this.getBranch()) .then(branchData => this.updateTree(branchData.commit.sha, '/', fileTree)) .then(changeTree => this.commit(options.commitMessage, changeTree)) - .then((response) => { - 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); - } - }); + .then((response) => this.patchBranch(this.branch, response.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) { return this.request(`${this.repoURL}/refs/${type}/${name}`, { 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) { return this.patchRef('heads', branchName, sha); } - getBranch() { - return this.request(`${this.repoURL}/refs/heads/${this.branch}`); + deleteBranch(branchName) { + return this.deleteRef('heads', branchName); } createPR(title, head, base = 'master') {