branching refactor

This commit is contained in:
Cássio Zen 2016-08-30 14:39:53 -03:00
parent c7544cc0a2
commit 672b43f153

View File

@ -3,7 +3,6 @@ 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'; import { BRANCH } from '../constants';
const API_ROOT = 'https://api.github.com'; const API_ROOT = 'https://api.github.com';
@ -49,7 +48,6 @@ 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));
@ -65,15 +63,9 @@ class API {
}); });
return Promise.all(files) return Promise.all(files)
.then(() => { .then(() => this.getBranch())
if (options.mode === BRANCH) { .then((branchData) => {
return this.createBranch(branchName); return this.updateTree(branchData.commit.sha, '/', fileTree);
} 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`, {
@ -81,10 +73,11 @@ 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/${branchName}`, { if (options.mode === BRANCH) {
method: 'PATCH', return this.createBranch(`cms/${entry.slug}`, response.sha);
body: JSON.stringify({ sha: response.sha }) } else {
}); return this.patchBranch(this.branch, response.sha);
}
}); });
} }
@ -117,24 +110,22 @@ class API {
}); });
} }
createBranch(branchName) { getBranch() {
return this.getBranch() return this.request(`${this.repoURL}/branches/${this.branch}`);
.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() { createBranch(branchName, sha) {
return this.request(`${this.repoURL}/branches/${this.branch}`) return this.request(`${this.repoURL}/git/refs`, {
.then(branchData => branchData.commit); method: 'POST',
body: JSON.stringify({ ref: `refs/heads/${branchName}`, sha }),
});
}
patchBranch(branchName, sha) {
return this.request(`${this.repoURL}/git/refs/heads/${branchName}`, {
method: 'PATCH',
body: JSON.stringify({ sha })
});
} }
getTree(sha) { getTree(sha) {