Metadata saving
This commit is contained in:
parent
388cada3de
commit
2f635944fd
@ -11,7 +11,6 @@ export default class API {
|
|||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
this.branch = branch;
|
this.branch = branch;
|
||||||
this.repoURL = `/repos/${this.repo}`;
|
this.repoURL = `/repos/${this.repo}`;
|
||||||
this.checkMetadataBranch();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user() {
|
user() {
|
||||||
@ -48,33 +47,43 @@ export default class API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
checkMetadataBranch() {
|
checkMetadataBranch() {
|
||||||
this.request(`${this.repoURL}/contents?ref=_netlify_cms`, {
|
return this.request(`${this.repoURL}/branches/_netlify_cms?${Date.now()}`, {
|
||||||
cache: false
|
cache: 'no-store',
|
||||||
})
|
})
|
||||||
.then(result => console.log(result))
|
.then(response => response.commit)
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
//Branch doesn't exist
|
//Branch 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 branch is used by the Netlify CMS to store metadata information for specific files and branches.'
|
||||||
};
|
};
|
||||||
|
|
||||||
this.uploadBlob(readme)
|
return this.uploadBlob(readme)
|
||||||
.then(item => this.request(`${this.repoURL}/git/trees`, {
|
.then(item => this.request(`${this.repoURL}/git/trees`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
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.request(`${this.repoURL}/git/commits`, {
|
.then(tree => this.commit('First Commit', tree))
|
||||||
method: 'POST',
|
.then(response => this.createBranch('_netlify_cms', response.sha))
|
||||||
body: JSON.stringify({ message: 'First Commit', tree: tree.sha, parents: [] })
|
.then(response => response.object);
|
||||||
}))
|
|
||||||
.then(response => this.createBranch('_netlify_cms', response.sha));
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// List all branches inside /cms
|
storeMetadata(name, data) {
|
||||||
// this.request(`${this.repoURL}/git/refs/heads/cms/`).then((result) => {
|
this.checkMetadataBranch()
|
||||||
// console.log(result);
|
.then((branchData) => {
|
||||||
// });
|
const fileTree = {
|
||||||
|
[`${name}.json`]: {
|
||||||
|
path: `${name}.json`,
|
||||||
|
raw: JSON.stringify(data),
|
||||||
|
file: true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return this.uploadBlob(fileTree[`${name}.json`])
|
||||||
|
.then(item => this.updateTree(branchData.sha, '/', fileTree))
|
||||||
|
.then(changeTree => this.commit(`Updating “${name}” metadata`, changeTree))
|
||||||
|
.then(response => this.patchBranch('_netlify_cms', response.sha));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
readFile(path, sha) {
|
readFile(path, sha) {
|
||||||
@ -119,18 +128,11 @@ export default class API {
|
|||||||
subtree[filename] = file;
|
subtree[filename] = file;
|
||||||
file.file = true;
|
file.file = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all(files)
|
return Promise.all(files)
|
||||||
.then(() => this.getBranch())
|
.then(() => this.getBranch())
|
||||||
.then((branchData) => {
|
.then(branchData => this.updateTree(branchData.commit.sha, '/', fileTree))
|
||||||
return this.updateTree(branchData.commit.sha, '/', fileTree);
|
.then(changeTree => this.commit(options.commitMessage, changeTree))
|
||||||
})
|
.then((response) => {
|
||||||
.then((changeTree) => {
|
|
||||||
return this.request(`${this.repoURL}/git/commits`, {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({ message: options.commitMessage, tree: changeTree.sha, parents: [changeTree.parentSha] })
|
|
||||||
});
|
|
||||||
}).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 newBranch = options.collectionName ? `cms/${options.collectionName}-${entry.slug}` : `cms/${entry.slug}`;
|
||||||
return this.createBranch(newBranch, response.sha);
|
return this.createBranch(newBranch, response.sha);
|
||||||
@ -225,4 +227,13 @@ export default class API {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commit(message, changeTree) {
|
||||||
|
const tree = changeTree.sha;
|
||||||
|
const parents = changeTree.parentSha ? [changeTree.parentSha] : [];
|
||||||
|
return this.request(`${this.repoURL}/git/commits`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ message, tree, parents })
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user