2016-09-04 19:55:14 +02:00
|
|
|
import semaphore from 'semaphore';
|
2016-09-06 13:04:17 -03:00
|
|
|
import { createEntry } from '../../valueObjects/Entry';
|
2016-06-05 01:52:18 -07:00
|
|
|
import AuthenticationPage from './AuthenticationPage';
|
2016-08-30 19:06:20 -03:00
|
|
|
import API from './API';
|
2016-06-05 01:52:18 -07:00
|
|
|
|
2016-09-04 19:55:14 +02:00
|
|
|
const MAX_CONCURRENT_DOWNLOADS = 10;
|
|
|
|
|
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']);
|
2016-09-04 14:01:28 +02:00
|
|
|
this.branch = config.getIn(['backend', 'branch']) || 'master';
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
entriesByFolder(collection) {
|
2016-10-27 13:12:18 -02:00
|
|
|
return this.api.listFiles(collection.get('folder'))
|
|
|
|
.then(this.fetchFiles);
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
2016-10-27 14:23:36 +02:00
|
|
|
entriesByFiles(collection) {
|
|
|
|
const files = collection.get('files').map(collectionFile => ({
|
|
|
|
path: collectionFile.get('file'),
|
|
|
|
label: collectionFile.get('label'),
|
|
|
|
}));
|
2016-10-27 13:12:18 -02:00
|
|
|
return this.fetchFiles(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchFiles = (files) => {
|
2016-10-21 20:42:14 -02:00
|
|
|
const sem = semaphore(MAX_CONCURRENT_DOWNLOADS);
|
|
|
|
const promises = [];
|
|
|
|
files.forEach((file) => {
|
2016-10-27 13:12:18 -02:00
|
|
|
promises.push(new Promise((resolve, reject) => (
|
|
|
|
sem.take(() => this.api.readFile(file.path, file.sha).then((data) => {
|
|
|
|
resolve({ file, data });
|
2016-10-21 20:42:14 -02:00
|
|
|
sem.leave();
|
|
|
|
}).catch((err) => {
|
|
|
|
sem.leave();
|
|
|
|
reject(err);
|
2016-10-27 13:12:18 -02:00
|
|
|
}))
|
|
|
|
)));
|
2016-10-21 20:42:14 -02:00
|
|
|
});
|
|
|
|
return Promise.all(promises);
|
2016-10-27 13:12:18 -02:00
|
|
|
};
|
2016-07-19 17:11:22 -03:00
|
|
|
|
2016-10-10 15:34:21 -03:00
|
|
|
// Fetches a single entry.
|
|
|
|
getEntry(collection, slug, path) {
|
2016-10-27 14:23:36 +02:00
|
|
|
return this.api.readFile(path).then(data => ({
|
|
|
|
file: { path },
|
|
|
|
data,
|
|
|
|
}));
|
2016-10-10 15:34:21 -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-09-06 13:04:17 -03:00
|
|
|
|
|
|
|
unpublishedEntries() {
|
2016-09-06 17:18:27 -03:00
|
|
|
return this.api.listUnpublishedBranches().then((branches) => {
|
|
|
|
const sem = semaphore(MAX_CONCURRENT_DOWNLOADS);
|
|
|
|
const promises = [];
|
|
|
|
branches.map((branch) => {
|
|
|
|
promises.push(new Promise((resolve, reject) => {
|
|
|
|
const contentKey = branch.ref.split('refs/heads/cms/').pop();
|
|
|
|
return sem.take(() => this.api.readUnpublishedBranchFile(contentKey).then((data) => {
|
2016-10-10 18:33:49 -03:00
|
|
|
if (data === null || data === undefined) {
|
|
|
|
resolve(null);
|
|
|
|
sem.leave();
|
|
|
|
} else {
|
|
|
|
const entryPath = data.metaData.objects.entry;
|
2016-10-18 12:32:39 -02:00
|
|
|
const entry = createEntry('draft', contentKey, entryPath, { raw: data.file });
|
2016-10-10 18:33:49 -03:00
|
|
|
entry.metaData = data.metaData;
|
|
|
|
resolve(entry);
|
|
|
|
sem.leave();
|
|
|
|
}
|
2016-09-06 17:18:27 -03:00
|
|
|
}).catch((err) => {
|
|
|
|
sem.leave();
|
|
|
|
reject(err);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
return Promise.all(promises);
|
|
|
|
}).then((entries) => {
|
2016-10-10 18:33:49 -03:00
|
|
|
const filteredEntries = entries.filter(entry => entry !== null);
|
2016-09-06 17:18:27 -03:00
|
|
|
return {
|
2016-10-10 18:33:49 -03:00
|
|
|
pagination: 0,
|
|
|
|
entries: filteredEntries,
|
2016-09-06 17:18:27 -03:00
|
|
|
};
|
2016-09-06 13:04:17 -03:00
|
|
|
});
|
|
|
|
}
|
2016-09-13 03:59:48 -03:00
|
|
|
|
|
|
|
unpublishedEntry(collection, slug) {
|
2016-10-10 16:10:55 -03:00
|
|
|
return this.unpublishedEntries().then(response => (
|
2016-10-26 15:50:34 -02:00
|
|
|
response.entries.filter((entry) => {
|
|
|
|
return entry.metaData && entry.slug === slug;
|
|
|
|
})[0]
|
2016-09-13 03:59:48 -03:00
|
|
|
));
|
|
|
|
}
|
2016-09-13 16:00:24 -03:00
|
|
|
|
|
|
|
updateUnpublishedEntryStatus(collection, slug, newStatus) {
|
|
|
|
return this.api.updateUnpublishedEntryStatus(collection, slug, newStatus);
|
|
|
|
}
|
2016-09-14 18:25:45 -03:00
|
|
|
|
|
|
|
publishUnpublishedEntry(collection, slug, status) {
|
|
|
|
return this.api.publishUnpublishedEntry(collection, slug, status);
|
|
|
|
}
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|