Saving metadata as a custom ref

This commit is contained in:
Cássio Zen 2016-08-31 16:41:29 -03:00
parent 666d66ab81
commit 1406d9225f

View File

@ -46,15 +46,15 @@ export default class API {
}); });
} }
checkMetadataBranch() { checkMetadataRef() {
return this.request(`${this.repoURL}/branches/_netlify_cms?${Date.now()}`, { return this.request(`${this.repoURL}/git/refs/meta/_netlify_cms?${Date.now()}`, {
cache: 'no-store', cache: 'no-store',
}) })
.then(response => response.commit) .then(response => response.object)
.catch(error => { .catch(error => {
//Branch doesn't exist // Meta ref doesn't exist
const readme = { const readme = {
raw: '# Netlify CMS\n\nThis branch is used by the Netlify CMS to store metadata information for specific files and branches.' raw: '# Netlify CMS\n\nThis tree is used by the Netlify CMS to store metadata information for specific files and branches.'
}; };
return this.uploadBlob(readme) return this.uploadBlob(readme)
@ -63,13 +63,13 @@ export default class API {
body: JSON.stringify({ tree: [{ path: 'README.md', mode: '100644', type: 'blob', sha: item.sha }] }) body: JSON.stringify({ tree: [{ path: 'README.md', mode: '100644', type: 'blob', sha: item.sha }] })
})) }))
.then(tree => this.commit('First Commit', tree)) .then(tree => this.commit('First Commit', tree))
.then(response => this.createBranch('_netlify_cms', response.sha)) .then(response => this.createRef('meta', '_netlify_cms', response.sha))
.then(response => response.object); .then(response => response.object);
}); });
} }
storeMetadata(key, data) { storeMetadata(key, data) {
this.checkMetadataBranch() this.checkMetadataRef()
.then((branchData) => { .then((branchData) => {
const fileTree = { const fileTree = {
[`${key}.json`]: { [`${key}.json`]: {
@ -82,7 +82,7 @@ export default class API {
return this.uploadBlob(fileTree[`${key}.json`]) return this.uploadBlob(fileTree[`${key}.json`])
.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.patchBranch('_netlify_cms', response.sha)); .then(response => this.patchRef('meta', '_netlify_cms', response.sha));
}); });
} }
@ -91,7 +91,7 @@ export default class API {
return cache.then((cached) => { return cache.then((cached) => {
if (cached && cached.expires > Date.now()) { return cached.data; } if (cached && cached.expires > Date.now()) { return cached.data; }
return this.request(`${this.repoURL}/contents/${key}.json?ref=_netlify_cms`, { return this.request(`${this.repoURL}/contents/${key}.json?ref=refs/meta/_netlify_cms`, {
headers: { Accept: 'application/vnd.github.VERSION.raw' }, headers: { Accept: 'application/vnd.github.VERSION.raw' },
cache: 'no-store', cache: 'no-store',
}).then((result) => { }).then((result) => {
@ -153,30 +153,39 @@ export default class API {
.then(changeTree => this.commit(options.commitMessage, changeTree)) .then(changeTree => this.commit(options.commitMessage, changeTree))
.then((response) => { .then((response) => {
if (options.mode && options.mode === BRANCH) { if (options.mode && options.mode === BRANCH) {
const newBranch = options.collectionName ? `cms/${options.collectionName}-${entry.slug}` : `cms/${entry.slug}`; const branchKey = options.collectionName ? `${options.collectionName}-${entry.slug}` : entry.slug;
return this.createBranch(newBranch, response.sha); return this.createBranch(`cms/${branchKey}`, response.sha)
.then(this.storeMetadata(branchKey, { status: 'draft' }));
} else { } else {
return this.patchBranch(this.branch, response.sha); return this.patchBranch(this.branch, response.sha);
} }
}); });
} }
getBranch() { createRef(type, name, sha) {
return this.request(`${this.repoURL}/branches/${this.branch}`); return this.request(`${this.repoURL}/git/refs`, {
method: 'POST',
body: JSON.stringify({ ref: `refs/${type}/${name}`, sha }),
});
} }
createBranch(branchName, sha) { createBranch(branchName, sha) {
return this.request(`${this.repoURL}/git/refs`, { return this.createRef('heads', branchName, sha);
method: 'POST', }
body: JSON.stringify({ ref: `refs/heads/${branchName}`, sha }),
patchRef(type, name, sha) {
return this.request(`${this.repoURL}/git/refs/${type}/${name}`, {
method: 'PATCH',
body: JSON.stringify({ sha })
}); });
} }
patchBranch(branchName, sha) { patchBranch(branchName, sha) {
return this.request(`${this.repoURL}/git/refs/heads/${branchName}`, { return this.patchRef('heads', branchName, sha);
method: 'PATCH', }
body: JSON.stringify({ sha })
}); getBranch() {
return this.request(`${this.repoURL}/branches/${this.branch}`);
} }
getTree(sha) { getTree(sha) {