branch commiting skeleton

This commit is contained in:
Cássio Zen 2016-08-29 19:32:56 -03:00
parent 704bc41b59
commit a1c01323e0
4 changed files with 37 additions and 10 deletions

View File

@ -3,7 +3,6 @@ backend:
delay: 0.1 delay: 0.1
media_folder: "assets/uploads" media_folder: "assets/uploads"
publish_mode: branch
collections: # A list of collections the CMS should be able to edit collections: # A list of collections the CMS should be able to edit
- name: "posts" # Used in routes, ie.: /admin/collections/:slug/edit - name: "posts" # Used in routes, ie.: /admin/collections/:slug/edit

View File

@ -1,8 +1,8 @@
import TestRepoBackend from './test-repo/implementation'; import TestRepoBackend from './test-repo/implementation';
import GitHubBackend from './github/implementation'; import GitHubBackend from './github/implementation';
import { resolveFormat } from '../formats/formats'; import { resolveFormat } from '../formats/formats';
import { randomStr } from '../lib/randomGenerator';
import { createEntry } from '../valueObjects/Entry'; import { createEntry } from '../valueObjects/Entry';
import { SIMPLE, BRANCH } from './constants';
class LocalStorageAuthStore { class LocalStorageAuthStore {
storageKey = 'nf-cms-user'; storageKey = 'nf-cms-user';
@ -93,12 +93,12 @@ class Backend {
} }
getPublishMode(config) { getPublishMode(config) {
const publish_modes = ['simple', 'branch']; const publish_modes = [SIMPLE, BRANCH];
const mode = config.get('publish_mode'); const mode = config.getIn(['backend', 'publish_mode']);
if (publish_modes.indexOf(mode) !== -1) { if (publish_modes.indexOf(mode) !== -1) {
return mode; return mode;
} else { } else {
return 'simple'; return SIMPLE;
} }
} }

View File

@ -0,0 +1,3 @@
// Create/edit modes
export const SIMPLE = 'simple';
export const BRANCH = 'branch';

View File

@ -3,6 +3,8 @@ import MediaProxy from '../../valueObjects/MediaProxy';
import { createEntry } from '../../valueObjects/Entry'; import { createEntry } from '../../valueObjects/Entry';
import AuthenticationPage from './AuthenticationPage'; import AuthenticationPage from './AuthenticationPage';
import { Base64 } from 'js-base64'; import { Base64 } from 'js-base64';
import { randomStr } from '../../lib/randomGenerator';
import { BRANCH } from '../constants';
const API_ROOT = 'https://api.github.com'; const API_ROOT = 'https://api.github.com';
@ -47,6 +49,7 @@ class API {
let filename, part, parts, subtree; let filename, part, parts, subtree;
const fileTree = {}; const fileTree = {};
const files = []; const files = [];
const branchName = ( options.mode === BRANCH ) ? 'cms-' + randomStr() : this.branch;
mediaFiles.concat(entry).forEach((file) => { mediaFiles.concat(entry).forEach((file) => {
if (file.uploaded) { return; } if (file.uploaded) { return; }
files.push(this.uploadBlob(file)); files.push(this.uploadBlob(file));
@ -62,9 +65,15 @@ class API {
}); });
return Promise.all(files) return Promise.all(files)
.then(() => this.getBranch()) .then(() => {
.then((branchData) => { if (options.mode === BRANCH) {
return this.updateTree(branchData.commit.sha, '/', fileTree); return this.createBranch(branchName);
} else {
return this.getBranch();
}
})
.then((BranchCommit) => {
return this.updateTree(BranchCommit.sha, '/', fileTree);
}) })
.then((changeTree) => { .then((changeTree) => {
return this.request(`${this.repoURL}/git/commits`, { return this.request(`${this.repoURL}/git/commits`, {
@ -72,7 +81,7 @@ class API {
body: JSON.stringify({ message: options.commitMessage, tree: changeTree.sha, parents: [changeTree.parentSha] }) body: JSON.stringify({ message: options.commitMessage, tree: changeTree.sha, parents: [changeTree.parentSha] })
}); });
}).then((response) => { }).then((response) => {
return this.request(`${this.repoURL}/git/refs/heads/${this.branch}`, { return this.request(`${this.repoURL}/git/refs/heads/${branchName}`, {
method: 'PATCH', method: 'PATCH',
body: JSON.stringify({ sha: response.sha }) body: JSON.stringify({ sha: response.sha })
}); });
@ -108,8 +117,24 @@ class API {
}); });
} }
createBranch(branchName) {
return this.getBranch()
.then(branchCommit => {
const branchData = {
ref: 'refs/heads/' + branchName,
sha: branchCommit.sha
};
return this.request(`${this.repoURL}/git/refs`, {
method: 'POST',
body: JSON.stringify(branchData),
});
})
.then(branchData => branchData.object);
}
getBranch() { getBranch() {
return this.request(`${this.repoURL}/branches/${this.branch}`); return this.request(`${this.repoURL}/branches/${this.branch}`)
.then(branchData => branchData.commit);
} }
getTree(sha) { getTree(sha) {