2016-06-05 01:52:18 -07:00
|
|
|
import LocalForage from 'localforage';
|
2016-07-19 17:11:22 -03:00
|
|
|
import MediaProxy from '../../valueObjects/MediaProxy';
|
2016-08-24 21:36:44 -03:00
|
|
|
import { createEntry } from '../../valueObjects/Entry';
|
2016-06-05 01:52:18 -07:00
|
|
|
import AuthenticationPage from './AuthenticationPage';
|
2016-07-19 17:11:22 -03:00
|
|
|
import { Base64 } from 'js-base64';
|
2016-08-29 19:32:56 -03:00
|
|
|
import { randomStr } from '../../lib/randomGenerator';
|
|
|
|
import { BRANCH } from '../constants';
|
2016-06-05 01:52:18 -07:00
|
|
|
|
|
|
|
const API_ROOT = 'https://api.github.com';
|
|
|
|
|
|
|
|
class API {
|
|
|
|
constructor(token, repo, branch) {
|
|
|
|
this.token = token;
|
|
|
|
this.repo = repo;
|
|
|
|
this.branch = branch;
|
2016-07-18 16:09:35 -03:00
|
|
|
this.repoURL = `/repos/${this.repo}`;
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
user() {
|
|
|
|
return this.request('/user');
|
|
|
|
}
|
|
|
|
|
|
|
|
readFile(path, sha) {
|
|
|
|
const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null);
|
|
|
|
return cache.then((cached) => {
|
|
|
|
if (cached) { return cached; }
|
|
|
|
|
2016-07-18 16:09:35 -03:00
|
|
|
return this.request(`${this.repoURL}/contents/${path}`, {
|
|
|
|
headers: { Accept: 'application/vnd.github.VERSION.raw' },
|
2016-07-19 17:29:40 -03:00
|
|
|
body: { ref: this.branch },
|
2016-06-05 01:52:18 -07:00
|
|
|
cache: false
|
|
|
|
}).then((result) => {
|
|
|
|
if (sha) {
|
|
|
|
LocalForage.setItem(`gh.${sha}`, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
listFiles(path) {
|
2016-07-18 16:09:35 -03:00
|
|
|
return this.request(`${this.repoURL}/contents/${path}`, {
|
2016-07-19 17:29:40 -03:00
|
|
|
body: { ref: this.branch }
|
2016-06-05 01:52:18 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:09:04 -03:00
|
|
|
persistFiles(entry, mediaFiles, options) {
|
2016-07-19 17:11:22 -03:00
|
|
|
let filename, part, parts, subtree;
|
|
|
|
const fileTree = {};
|
|
|
|
const files = [];
|
2016-08-29 19:32:56 -03:00
|
|
|
const branchName = ( options.mode === BRANCH ) ? 'cms-' + randomStr() : this.branch;
|
2016-07-19 17:11:22 -03:00
|
|
|
mediaFiles.concat(entry).forEach((file) => {
|
|
|
|
if (file.uploaded) { return; }
|
|
|
|
files.push(this.uploadBlob(file));
|
|
|
|
parts = file.path.split('/').filter((part) => part);
|
|
|
|
filename = parts.pop();
|
|
|
|
subtree = fileTree;
|
|
|
|
while (part = parts.shift()) {
|
|
|
|
subtree[part] = subtree[part] || {};
|
|
|
|
subtree = subtree[part];
|
|
|
|
}
|
|
|
|
subtree[filename] = file;
|
|
|
|
file.file = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(files)
|
2016-08-29 19:32:56 -03:00
|
|
|
.then(() => {
|
|
|
|
if (options.mode === BRANCH) {
|
|
|
|
return this.createBranch(branchName);
|
|
|
|
} else {
|
|
|
|
return this.getBranch();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then((BranchCommit) => {
|
|
|
|
return this.updateTree(BranchCommit.sha, '/', fileTree);
|
2016-07-19 17:11:22 -03:00
|
|
|
})
|
|
|
|
.then((changeTree) => {
|
|
|
|
return this.request(`${this.repoURL}/git/commits`, {
|
2016-07-19 17:29:40 -03:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ message: options.commitMessage, tree: changeTree.sha, parents: [changeTree.parentSha] })
|
2016-07-19 17:11:22 -03:00
|
|
|
});
|
|
|
|
}).then((response) => {
|
2016-08-29 19:32:56 -03:00
|
|
|
return this.request(`${this.repoURL}/git/refs/heads/${branchName}`, {
|
2016-07-19 17:29:40 -03:00
|
|
|
method: 'PATCH',
|
|
|
|
body: JSON.stringify({ sha: response.sha })
|
2016-07-19 17:11:22 -03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
requestHeaders(headers = {}) {
|
|
|
|
return {
|
|
|
|
Authorization: `token ${this.token}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...headers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
parseJsonResponse(response) {
|
|
|
|
return response.json().then((json) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
return Promise.reject(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
request(path, options = {}) {
|
|
|
|
const headers = this.requestHeaders(options.headers || {});
|
2016-07-18 16:09:35 -03:00
|
|
|
return fetch(API_ROOT + path, { ...options, headers: headers }).then((response) => {
|
2016-06-05 01:52:18 -07:00
|
|
|
if (response.headers.get('Content-Type').match(/json/)) {
|
|
|
|
return this.parseJsonResponse(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.text();
|
|
|
|
});
|
|
|
|
}
|
2016-07-19 17:11:22 -03:00
|
|
|
|
2016-08-29 19:32:56 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:11:22 -03:00
|
|
|
getBranch() {
|
2016-08-29 19:32:56 -03:00
|
|
|
return this.request(`${this.repoURL}/branches/${this.branch}`)
|
|
|
|
.then(branchData => branchData.commit);
|
2016-07-19 17:11:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
getTree(sha) {
|
|
|
|
return sha ? this.request(`${this.repoURL}/git/trees/${sha}`) : Promise.resolve({ tree: [] });
|
|
|
|
}
|
|
|
|
|
|
|
|
toBase64(str) {
|
|
|
|
return Promise.resolve(
|
|
|
|
Base64.encode(str)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadBlob(item) {
|
|
|
|
const content = item instanceof MediaProxy ? item.toBase64() : this.toBase64(item.raw);
|
|
|
|
|
|
|
|
return content.then((contentBase64) => {
|
|
|
|
return this.request(`${this.repoURL}/git/blobs`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
content: contentBase64,
|
|
|
|
encoding: 'base64'
|
|
|
|
})
|
|
|
|
}).then((response) => {
|
|
|
|
item.sha = response.sha;
|
|
|
|
item.uploaded = true;
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTree(sha, path, fileTree) {
|
|
|
|
return this.getTree(sha)
|
|
|
|
.then((tree) => {
|
|
|
|
var obj, filename, fileOrDir;
|
|
|
|
var updates = [];
|
|
|
|
var added = {};
|
|
|
|
|
|
|
|
for (var i = 0, len = tree.tree.length; i < len; i++) {
|
|
|
|
obj = tree.tree[i];
|
|
|
|
if (fileOrDir = fileTree[obj.path]) {
|
|
|
|
added[obj.path] = true;
|
|
|
|
if (fileOrDir.file) {
|
|
|
|
updates.push({ path: obj.path, mode: obj.mode, type: obj.type, sha: fileOrDir.sha });
|
|
|
|
} else {
|
|
|
|
updates.push(this.updateTree(obj.sha, obj.path, fileOrDir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (filename in fileTree) {
|
|
|
|
fileOrDir = fileTree[filename];
|
|
|
|
if (added[filename]) { continue; }
|
|
|
|
updates.push(
|
|
|
|
fileOrDir.file ?
|
|
|
|
{ path: filename, mode: '100644', type: 'blob', sha: fileOrDir.sha } :
|
|
|
|
this.updateTree(null, filename, fileOrDir)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Promise.all(updates)
|
|
|
|
.then((updates) => {
|
|
|
|
return this.request(`${this.repoURL}/git/trees`, {
|
2016-07-19 17:29:40 -03:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ base_tree: sha, tree: updates })
|
2016-07-19 17:11:22 -03:00
|
|
|
});
|
|
|
|
}).then((response) => {
|
|
|
|
return { path: path, mode: '040000', type: 'tree', sha: response.sha, parentSha: sha };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class GitHub {
|
|
|
|
constructor(config) {
|
|
|
|
this.config = config;
|
|
|
|
if (config.getIn(['backend', 'repo']) == null) {
|
|
|
|
throw 'The GitHub backend needs a "repo" in the backend configuration.';
|
|
|
|
}
|
|
|
|
this.repo = config.getIn(['backend', 'repo']);
|
|
|
|
}
|
|
|
|
|
|
|
|
authComponent() {
|
|
|
|
return AuthenticationPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
setUser(user) {
|
|
|
|
this.api = new API(user.token, this.repo, this.branch || 'master');
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticate(state) {
|
|
|
|
this.api = new API(state.token, this.repo, this.branch || 'master');
|
|
|
|
return this.api.user().then((user) => {
|
|
|
|
user.token = state.token;
|
|
|
|
return user;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
entries(collection) {
|
|
|
|
return this.api.listFiles(collection.get('folder')).then((files) => (
|
|
|
|
Promise.all(files.map((file) => (
|
|
|
|
this.api.readFile(file.path, file.sha).then((data) => {
|
2016-08-24 21:36:44 -03:00
|
|
|
return createEntry(file.path, file.path.split('/').pop().replace(/\.[^\.]+$/, ''), data);
|
2016-06-05 01:52:18 -07:00
|
|
|
})
|
|
|
|
)))
|
|
|
|
)).then((entries) => ({
|
|
|
|
pagination: {},
|
|
|
|
entries
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
entry(collection, slug) {
|
|
|
|
return this.entries(collection).then((response) => (
|
|
|
|
response.entries.filter((entry) => entry.slug === slug)[0]
|
|
|
|
));
|
|
|
|
}
|
2016-07-19 17:11:22 -03:00
|
|
|
|
2016-08-29 17:09:04 -03:00
|
|
|
persistEntry(entry, mediaFiles = [], options = {}) {
|
|
|
|
return this.api.persistFiles(entry, mediaFiles, options);
|
2016-07-19 17:11:22 -03:00
|
|
|
}
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|