2016-12-23 16:59:48 -02:00
|
|
|
import LocalForage from "localforage";
|
|
|
|
import { Base64 } from "js-base64";
|
|
|
|
import _ from "lodash";
|
2017-03-20 12:28:04 -07:00
|
|
|
import { filterPromises, resolvePromiseProperties } from "../../lib/promiseHelper";
|
2017-01-10 22:23:22 -02:00
|
|
|
import AssetProxy from "../../valueObjects/AssetProxy";
|
2016-12-23 16:59:48 -02:00
|
|
|
import { SIMPLE, EDITORIAL_WORKFLOW, status } from "../../constants/publishModes";
|
2017-01-11 20:58:15 -02:00
|
|
|
import { APIError, EditorialWorkflowError } from "../../valueObjects/errors";
|
2016-08-30 19:06:20 -03:00
|
|
|
|
|
|
|
export default class API {
|
2016-12-23 16:59:48 -02:00
|
|
|
constructor(config) {
|
|
|
|
this.api_root = config.api_root || "https://api.github.com";
|
|
|
|
this.token = config.token || false;
|
|
|
|
this.branch = config.branch || "master";
|
|
|
|
this.repo = config.repo || "";
|
2016-10-10 18:33:49 -03:00
|
|
|
this.repoURL = `/repos/${ this.repo }`;
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
user() {
|
2016-12-23 16:59:48 -02:00
|
|
|
return this.request("/user");
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
|
2017-08-01 20:28:03 -07:00
|
|
|
isCollaborator(user) {
|
|
|
|
return this.request('/user/repos').then((repos) => {
|
|
|
|
let contributor = false
|
|
|
|
for (const repo of repos) {
|
2017-08-11 12:19:01 -07:00
|
|
|
if (repo.full_name.toLowerCase() === this.repo.toLowerCase() && repo.permissions.push) contributor = true;
|
2017-08-01 20:28:03 -07:00
|
|
|
}
|
|
|
|
return contributor;
|
|
|
|
}).catch((error) => {
|
|
|
|
console.error("Problem with response of /user/repos from GitHub");
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-30 19:06:20 -03:00
|
|
|
requestHeaders(headers = {}) {
|
2016-12-23 16:59:48 -02:00
|
|
|
const baseHeader = {
|
|
|
|
"Content-Type": "application/json",
|
2016-10-10 18:33:49 -03:00
|
|
|
...headers,
|
2016-08-30 19:06:20 -03:00
|
|
|
};
|
2016-12-23 16:59:48 -02:00
|
|
|
|
|
|
|
if (this.token) {
|
|
|
|
baseHeader.Authorization = `token ${ this.token }`;
|
|
|
|
return baseHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseHeader;
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
parseJsonResponse(response) {
|
|
|
|
return response.json().then((json) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
return Promise.reject(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-04 14:01:28 +02:00
|
|
|
urlFor(path, options) {
|
2017-06-12 13:01:53 -07:00
|
|
|
const cacheBuster = new Date().getTime();
|
|
|
|
const params = [`ts=${cacheBuster}`];
|
2016-09-04 14:01:28 +02:00
|
|
|
if (options.params) {
|
|
|
|
for (const key in options.params) {
|
2016-10-10 18:33:49 -03:00
|
|
|
params.push(`${ key }=${ encodeURIComponent(options.params[key]) }`);
|
2016-09-04 14:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (params.length) {
|
2016-12-23 16:59:48 -02:00
|
|
|
path += `?${ params.join("&") }`;
|
2016-09-04 14:01:28 +02:00
|
|
|
}
|
2016-12-23 16:59:48 -02:00
|
|
|
return this.api_root + path;
|
2016-09-04 14:01:28 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 19:06:20 -03:00
|
|
|
request(path, options = {}) {
|
|
|
|
const headers = this.requestHeaders(options.headers || {});
|
2016-09-04 14:01:28 +02:00
|
|
|
const url = this.urlFor(path, options);
|
2017-01-11 20:58:15 -02:00
|
|
|
let responseStatus;
|
2016-10-10 18:33:49 -03:00
|
|
|
return fetch(url, { ...options, headers }).then((response) => {
|
2017-01-11 20:58:15 -02:00
|
|
|
responseStatus = response.status;
|
2016-12-23 16:59:48 -02:00
|
|
|
const contentType = response.headers.get("Content-Type");
|
2016-09-14 18:55:42 -03:00
|
|
|
if (contentType && contentType.match(/json/)) {
|
2016-08-30 19:06:20 -03:00
|
|
|
return this.parseJsonResponse(response);
|
|
|
|
}
|
|
|
|
return response.text();
|
2017-01-11 20:58:15 -02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
throw new APIError(error.message, responseStatus, 'GitHub');
|
2016-08-30 19:06:20 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-31 16:41:29 -03:00
|
|
|
checkMetadataRef() {
|
2016-10-10 18:33:49 -03:00
|
|
|
return this.request(`${ this.repoURL }/git/refs/meta/_netlify_cms?${ Date.now() }`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
cache: "no-store",
|
2016-08-30 19:06:20 -03:00
|
|
|
})
|
2016-08-31 16:41:29 -03:00
|
|
|
.then(response => response.object)
|
2016-10-10 18:33:49 -03:00
|
|
|
.catch((error) => {
|
2016-08-31 16:41:29 -03:00
|
|
|
// Meta ref doesn't exist
|
2016-08-30 19:06:20 -03:00
|
|
|
const readme = {
|
2016-12-23 16:59:48 -02:00
|
|
|
raw: "# Netlify CMS\n\nThis tree is used by the Netlify CMS to store metadata information for specific files and branches.",
|
2016-08-30 19:06:20 -03:00
|
|
|
};
|
|
|
|
|
2016-08-31 13:30:14 -03:00
|
|
|
return this.uploadBlob(readme)
|
2016-10-10 18:33:49 -03:00
|
|
|
.then(item => this.request(`${ this.repoURL }/git/trees`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({ tree: [{ path: "README.md", mode: "100644", type: "blob", sha: item.sha }] }),
|
2016-08-30 19:06:20 -03:00
|
|
|
}))
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(tree => this.commit("First Commit", tree))
|
|
|
|
.then(response => this.createRef("meta", "_netlify_cms", response.sha))
|
2016-08-31 13:30:14 -03:00
|
|
|
.then(response => response.object);
|
2016-08-30 19:06:20 -03:00
|
|
|
});
|
2016-08-31 13:30:14 -03:00
|
|
|
}
|
2016-08-30 19:06:20 -03:00
|
|
|
|
2016-08-31 15:44:00 -03:00
|
|
|
storeMetadata(key, data) {
|
2016-08-31 17:33:12 -03:00
|
|
|
return this.checkMetadataRef()
|
2016-08-31 13:30:14 -03:00
|
|
|
.then((branchData) => {
|
|
|
|
const fileTree = {
|
2016-10-10 18:33:49 -03:00
|
|
|
[`${ key }.json`]: {
|
|
|
|
path: `${ key }.json`,
|
2016-08-31 13:30:14 -03:00
|
|
|
raw: JSON.stringify(data),
|
2016-10-10 18:33:49 -03:00
|
|
|
file: true,
|
|
|
|
},
|
2016-08-31 13:30:14 -03:00
|
|
|
};
|
2016-08-30 19:06:20 -03:00
|
|
|
|
2016-10-10 18:33:49 -03:00
|
|
|
return this.uploadBlob(fileTree[`${ key }.json`])
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(item => this.updateTree(branchData.sha, "/", fileTree))
|
2016-10-10 18:33:49 -03:00
|
|
|
.then(changeTree => this.commit(`Updating “${ key }” metadata`, changeTree))
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(response => this.patchRef("meta", "_netlify_cms", response.sha))
|
2016-09-13 16:00:24 -03:00
|
|
|
.then(() => {
|
2016-10-10 18:33:49 -03:00
|
|
|
LocalForage.setItem(`gh.meta.${ key }`, {
|
2016-09-13 16:00:24 -03:00
|
|
|
expires: Date.now() + 300000, // In 5 minutes
|
2016-10-10 18:33:49 -03:00
|
|
|
data,
|
2016-09-13 16:00:24 -03:00
|
|
|
});
|
|
|
|
});
|
2016-08-31 13:30:14 -03:00
|
|
|
});
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
|
2016-09-06 17:18:27 -03:00
|
|
|
retrieveMetadata(key) {
|
2016-10-10 18:33:49 -03:00
|
|
|
const cache = LocalForage.getItem(`gh.meta.${ key }`);
|
2016-09-13 16:00:24 -03:00
|
|
|
return cache.then((cached) => {
|
|
|
|
if (cached && cached.expires > Date.now()) { return cached.data; }
|
2016-10-21 20:42:14 -02:00
|
|
|
console.log("%c Checking for MetaData files", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line
|
2016-10-10 18:33:49 -03:00
|
|
|
return this.request(`${ this.repoURL }/contents/${ key }.json`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
params: { ref: "refs/meta/_netlify_cms" },
|
|
|
|
headers: { Accept: "application/vnd.github.VERSION.raw" },
|
|
|
|
cache: "no-store",
|
2016-09-13 16:00:24 -03:00
|
|
|
})
|
2016-10-21 20:42:14 -02:00
|
|
|
.then(response => JSON.parse(response))
|
|
|
|
.catch(error => console.log("%c %s does not have metadata", "line-height: 30px;text-align: center;font-weight: bold", key)); // eslint-disable-line
|
2016-09-13 16:00:24 -03:00
|
|
|
});
|
2016-08-31 15:44:00 -03:00
|
|
|
}
|
|
|
|
|
2016-09-06 17:18:27 -03:00
|
|
|
readFile(path, sha, branch = this.branch) {
|
2016-10-10 18:33:49 -03:00
|
|
|
const cache = sha ? LocalForage.getItem(`gh.${ sha }`) : Promise.resolve(null);
|
2016-08-30 19:06:20 -03:00
|
|
|
return cache.then((cached) => {
|
|
|
|
if (cached) { return cached; }
|
|
|
|
|
2016-10-10 18:33:49 -03:00
|
|
|
return this.request(`${ this.repoURL }/contents/${ path }`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
headers: { Accept: "application/vnd.github.VERSION.raw" },
|
2016-09-06 17:18:27 -03:00
|
|
|
params: { ref: branch },
|
2017-03-15 00:06:31 -07:00
|
|
|
cache: "no-store",
|
2016-08-30 19:06:20 -03:00
|
|
|
}).then((result) => {
|
|
|
|
if (sha) {
|
2016-10-10 18:33:49 -03:00
|
|
|
LocalForage.setItem(`gh.${ sha }`, result);
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
listFiles(path) {
|
2016-10-10 18:33:49 -03:00
|
|
|
return this.request(`${ this.repoURL }/contents/${ path }`, {
|
|
|
|
params: { ref: this.branch },
|
2017-04-14 19:19:45 +01:00
|
|
|
})
|
|
|
|
.then(files => {
|
|
|
|
if (!Array.isArray(files)) {
|
|
|
|
throw new Error(`Cannot list files, path ${path} is not a directory but a ${files.type}`);
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
})
|
|
|
|
.then(files => files.filter(file => file.type === "file"));
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
|
2016-09-06 17:18:27 -03:00
|
|
|
readUnpublishedBranchFile(contentKey) {
|
2017-03-20 12:28:04 -07:00
|
|
|
const metaDataPromise = this.retrieveMetadata(contentKey)
|
|
|
|
.then(data => (data.objects.entry.path ? data : Promise.reject(null)));
|
|
|
|
return resolvePromiseProperties({
|
|
|
|
metaData: metaDataPromise,
|
|
|
|
fileData: metaDataPromise.then(
|
|
|
|
data => this.readFile(data.objects.entry.path, null, data.branch)),
|
|
|
|
isModification: metaDataPromise.then(
|
|
|
|
data => this.isUnpublishedEntryModification(data.objects.entry.path, this.branch)),
|
2017-03-15 18:47:18 -07:00
|
|
|
})
|
2017-01-11 20:58:15 -02:00
|
|
|
.catch(() => {
|
|
|
|
throw new EditorialWorkflowError('content is not under editorial workflow', true);
|
|
|
|
});
|
2016-09-06 17:18:27 -03:00
|
|
|
}
|
|
|
|
|
2017-03-15 18:47:18 -07:00
|
|
|
isUnpublishedEntryModification(path, branch) {
|
|
|
|
return this.readFile(path, null, branch)
|
|
|
|
.then(data => true)
|
|
|
|
.catch((err) => {
|
|
|
|
if (err.message && err.message === "Not Found") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-06 17:18:27 -03:00
|
|
|
listUnpublishedBranches() {
|
2017-01-11 17:45:54 -02:00
|
|
|
console.log("%c Checking for Unpublished entries", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line
|
|
|
|
return this.request(`${ this.repoURL }/git/refs/heads/cms`)
|
2017-03-06 16:05:08 -08:00
|
|
|
.then(branches => filterPromises(branches, (branch) => {
|
|
|
|
const branchName = branch.ref.substring("/refs/heads/".length - 1);
|
|
|
|
|
|
|
|
// Get PRs with a `head` of `branchName`. Note that this is a
|
|
|
|
// substring match, so we need to check that the `head.ref` of
|
|
|
|
// at least one of the returned objects matches `branchName`.
|
2017-07-21 23:40:33 -07:00
|
|
|
return this.request(`${ this.repoURL }/pulls`, {
|
|
|
|
params: {
|
|
|
|
head: branchName,
|
|
|
|
state: 'open',
|
|
|
|
},
|
|
|
|
})
|
2017-03-06 16:05:08 -08:00
|
|
|
.then(prs => prs.some(pr => pr.head.ref === branchName));
|
|
|
|
}))
|
2017-01-11 17:45:54 -02:00
|
|
|
.catch((error) => {
|
|
|
|
console.log("%c No Unpublished entries", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line
|
|
|
|
throw error;
|
|
|
|
});
|
2016-09-06 17:18:27 -03:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:58:15 -02:00
|
|
|
composeFileTree(files) {
|
|
|
|
let filename;
|
|
|
|
let part;
|
|
|
|
let parts;
|
|
|
|
let subtree;
|
2016-08-30 19:06:20 -03:00
|
|
|
const fileTree = {};
|
2016-09-05 12:12:38 -03:00
|
|
|
|
|
|
|
files.forEach((file) => {
|
2016-08-30 19:06:20 -03:00
|
|
|
if (file.uploaded) { return; }
|
2016-12-23 16:59:48 -02:00
|
|
|
parts = file.path.split("/").filter(part => part);
|
2016-08-30 19:06:20 -03:00
|
|
|
filename = parts.pop();
|
|
|
|
subtree = fileTree;
|
|
|
|
while (part = parts.shift()) {
|
|
|
|
subtree[part] = subtree[part] || {};
|
|
|
|
subtree = subtree[part];
|
|
|
|
}
|
|
|
|
subtree[filename] = file;
|
|
|
|
file.file = true;
|
|
|
|
});
|
2017-01-11 20:58:15 -02:00
|
|
|
|
|
|
|
return fileTree;
|
|
|
|
}
|
|
|
|
|
|
|
|
persistFiles(entry, mediaFiles, options) {
|
|
|
|
const uploadPromises = [];
|
|
|
|
const files = mediaFiles.concat(entry);
|
|
|
|
|
|
|
|
files.forEach((file) => {
|
|
|
|
if (file.uploaded) { return; }
|
|
|
|
uploadPromises.push(this.uploadBlob(file));
|
|
|
|
});
|
|
|
|
|
|
|
|
const fileTree = this.composeFileTree(files);
|
|
|
|
|
2016-09-13 14:31:18 -03:00
|
|
|
return Promise.all(uploadPromises).then(() => {
|
|
|
|
if (!options.mode || (options.mode && options.mode === SIMPLE)) {
|
|
|
|
return this.getBranch()
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(branchData => this.updateTree(branchData.commit.sha, "/", fileTree))
|
2016-09-13 14:31:18 -03:00
|
|
|
.then(changeTree => this.commit(options.commitMessage, changeTree))
|
|
|
|
.then(response => this.patchBranch(this.branch, response.sha));
|
|
|
|
} else if (options.mode && options.mode === EDITORIAL_WORKFLOW) {
|
2017-01-11 20:58:15 -02:00
|
|
|
const mediaFilesList = mediaFiles.map(file => ({ path: file.path, sha: file.sha }));
|
2016-09-13 14:31:18 -03:00
|
|
|
return this.editorialWorkflowGit(fileTree, entry, mediaFilesList, options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-21 23:40:33 -07:00
|
|
|
deleteFile(path, message, options={}) {
|
|
|
|
const branch = options.branch || this.branch;
|
|
|
|
const fileURL = `${ this.repoURL }/contents/${ path }`;
|
|
|
|
// We need to request the file first to get the SHA
|
|
|
|
return this.request(fileURL)
|
|
|
|
.then(({ sha }) => this.request(fileURL, {
|
|
|
|
method: "DELETE",
|
|
|
|
params: {
|
|
|
|
sha,
|
|
|
|
message,
|
|
|
|
branch,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-09-13 14:31:18 -03:00
|
|
|
editorialWorkflowGit(fileTree, entry, filesList, options) {
|
2016-10-26 15:50:34 -02:00
|
|
|
const contentKey = entry.slug;
|
2016-10-10 18:33:49 -03:00
|
|
|
const branchName = `cms/${ contentKey }`;
|
2016-09-13 14:31:18 -03:00
|
|
|
const unpublished = options.unpublished || false;
|
|
|
|
if (!unpublished) {
|
2017-01-11 20:58:15 -02:00
|
|
|
// Open new editorial review workflow for this entry - Create new metadata and commit to new branch`
|
2016-10-26 15:50:34 -02:00
|
|
|
const contentKey = entry.slug;
|
2016-10-10 18:33:49 -03:00
|
|
|
const branchName = `cms/${ contentKey }`;
|
2016-09-14 18:25:45 -03:00
|
|
|
|
2016-09-13 14:31:18 -03:00
|
|
|
return this.getBranch()
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(branchData => this.updateTree(branchData.commit.sha, "/", fileTree))
|
2016-08-31 13:30:14 -03:00
|
|
|
.then(changeTree => this.commit(options.commitMessage, changeTree))
|
2016-09-14 18:25:45 -03:00
|
|
|
.then(commitResponse => this.createBranch(branchName, commitResponse.sha))
|
|
|
|
.then(branchResponse => this.createPR(options.commitMessage, branchName))
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(prResponse => this.user().then(user => user.name ? user.name : user.login)
|
2016-09-13 14:31:18 -03:00
|
|
|
.then(username => this.storeMetadata(contentKey, {
|
2016-12-23 16:59:48 -02:00
|
|
|
type: "PR",
|
2016-09-14 18:25:45 -03:00
|
|
|
pr: {
|
|
|
|
number: prResponse.number,
|
2016-10-10 18:33:49 -03:00
|
|
|
head: prResponse.head && prResponse.head.sha,
|
2016-09-14 18:25:45 -03:00
|
|
|
},
|
2016-09-13 14:31:18 -03:00
|
|
|
user: username,
|
|
|
|
status: status.first(),
|
|
|
|
branch: branchName,
|
|
|
|
collection: options.collectionName,
|
|
|
|
title: options.parsedData && options.parsedData.title,
|
|
|
|
description: options.parsedData && options.parsedData.description,
|
|
|
|
objects: {
|
2017-01-11 20:58:15 -02:00
|
|
|
entry: {
|
|
|
|
path: entry.path,
|
|
|
|
sha: entry.sha,
|
|
|
|
},
|
2016-10-10 18:33:49 -03:00
|
|
|
files: filesList,
|
2016-09-13 14:31:18 -03:00
|
|
|
},
|
2016-10-10 18:33:49 -03:00
|
|
|
timeStamp: new Date().toISOString(),
|
2017-01-11 20:58:15 -02:00
|
|
|
}
|
|
|
|
)));
|
2016-09-13 14:31:18 -03:00
|
|
|
} else {
|
|
|
|
// Entry is already on editorial review workflow - just update metadata and commit to existing branch
|
|
|
|
return this.getBranch(branchName)
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(branchData => this.updateTree(branchData.commit.sha, "/", fileTree))
|
2016-09-13 14:31:18 -03:00
|
|
|
.then(changeTree => this.commit(options.commitMessage, changeTree))
|
|
|
|
.then((response) => {
|
2016-10-26 15:50:34 -02:00
|
|
|
const contentKey = entry.slug;
|
2016-10-10 18:33:49 -03:00
|
|
|
const branchName = `cms/${ contentKey }`;
|
2016-12-23 16:59:48 -02:00
|
|
|
return this.user().then(user => user.name ? user.name : user.login)
|
2016-09-13 14:31:18 -03:00
|
|
|
.then(username => this.retrieveMetadata(contentKey))
|
2016-10-10 18:33:49 -03:00
|
|
|
.then((metadata) => {
|
2016-09-13 14:31:18 -03:00
|
|
|
let files = metadata.objects && metadata.objects.files || [];
|
|
|
|
files = files.concat(filesList);
|
2017-01-11 20:58:15 -02:00
|
|
|
const updatedPR = metadata.pr;
|
|
|
|
updatedPR.head = response.sha;
|
2016-09-13 14:31:18 -03:00
|
|
|
return {
|
|
|
|
...metadata,
|
2017-01-11 20:58:15 -02:00
|
|
|
pr: updatedPR,
|
2016-09-09 17:15:58 -03:00
|
|
|
title: options.parsedData && options.parsedData.title,
|
|
|
|
description: options.parsedData && options.parsedData.description,
|
2016-09-06 17:18:27 -03:00
|
|
|
objects: {
|
2017-01-11 20:58:15 -02:00
|
|
|
entry: {
|
|
|
|
path: entry.path,
|
|
|
|
sha: entry.sha,
|
|
|
|
},
|
2016-10-10 18:33:49 -03:00
|
|
|
files: _.uniq(files),
|
2016-09-09 17:15:58 -03:00
|
|
|
},
|
2016-10-10 18:33:49 -03:00
|
|
|
timeStamp: new Date().toISOString(),
|
2016-09-13 14:31:18 -03:00
|
|
|
};
|
|
|
|
})
|
|
|
|
.then(updatedMetadata => this.storeMetadata(contentKey, updatedMetadata))
|
|
|
|
.then(this.patchBranch(branchName, response.sha));
|
2016-08-30 19:06:20 -03:00
|
|
|
});
|
2016-09-13 14:31:18 -03:00
|
|
|
}
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
|
2016-09-13 16:00:24 -03:00
|
|
|
updateUnpublishedEntryStatus(collection, slug, status) {
|
2016-10-18 12:32:39 -02:00
|
|
|
const contentKey = slug;
|
2016-09-13 16:00:24 -03:00
|
|
|
return this.retrieveMetadata(contentKey)
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(metadata => ({
|
|
|
|
...metadata,
|
|
|
|
status,
|
|
|
|
}))
|
2016-09-13 16:00:24 -03:00
|
|
|
.then(updatedMetadata => this.storeMetadata(contentKey, updatedMetadata));
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:47:36 -05:00
|
|
|
deleteUnpublishedEntry(collection, slug) {
|
|
|
|
const contentKey = slug;
|
|
|
|
return this.retrieveMetadata(contentKey)
|
|
|
|
.then(metadata => this.closePR(metadata.pr, metadata.objects))
|
2017-07-21 23:40:33 -07:00
|
|
|
.then(() => this.deleteBranch(`cms/${ contentKey }`))
|
|
|
|
// If the PR doesn't exist, then this has already been deleted -
|
|
|
|
// deletion should be idempotent, so we can consider this a
|
|
|
|
// success.
|
|
|
|
.catch((err) => {
|
|
|
|
if (err.message === "Reference does not exist") {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
2017-03-11 13:47:36 -05:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:58:15 -02:00
|
|
|
publishUnpublishedEntry(collection, slug) {
|
2016-10-18 12:32:39 -02:00
|
|
|
const contentKey = slug;
|
2017-01-11 20:58:15 -02:00
|
|
|
let prNumber;
|
2016-09-14 18:25:45 -03:00
|
|
|
return this.retrieveMetadata(contentKey)
|
2017-01-11 20:58:15 -02:00
|
|
|
.then(metadata => this.mergePR(metadata.pr, metadata.objects))
|
2016-10-10 18:33:49 -03:00
|
|
|
.then(() => this.deleteBranch(`cms/${ contentKey }`));
|
2016-09-14 18:25:45 -03:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:58:15 -02:00
|
|
|
|
2016-08-31 16:41:29 -03:00
|
|
|
createRef(type, name, sha) {
|
2016-10-10 18:33:49 -03:00
|
|
|
return this.request(`${ this.repoURL }/git/refs`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
method: "POST",
|
2016-10-10 18:33:49 -03:00
|
|
|
body: JSON.stringify({ ref: `refs/${ type }/${ name }`, sha }),
|
2016-08-30 19:06:20 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-31 16:41:29 -03:00
|
|
|
patchRef(type, name, sha) {
|
2017-02-03 15:10:27 +00:00
|
|
|
return this.request(`${ this.repoURL }/git/refs/${ type }/${ encodeURIComponent(name) }`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
method: "PATCH",
|
2016-10-10 18:33:49 -03:00
|
|
|
body: JSON.stringify({ sha }),
|
2016-08-30 19:06:20 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-14 18:55:42 -03:00
|
|
|
deleteRef(type, name, sha) {
|
2017-02-03 15:10:27 +00:00
|
|
|
return this.request(`${ this.repoURL }/git/refs/${ type }/${ encodeURIComponent(name) }`, {
|
2017-07-21 23:40:33 -07:00
|
|
|
method: 'DELETE',
|
2016-09-14 18:55:42 -03:00
|
|
|
});
|
2016-08-31 16:41:29 -03:00
|
|
|
}
|
|
|
|
|
2016-09-13 14:31:18 -03:00
|
|
|
getBranch(branch = this.branch) {
|
2017-02-03 15:10:27 +00:00
|
|
|
return this.request(`${ this.repoURL }/branches/${ encodeURIComponent(branch) }`);
|
2016-08-31 16:41:29 -03:00
|
|
|
}
|
|
|
|
|
2016-09-14 18:55:42 -03:00
|
|
|
createBranch(branchName, sha) {
|
2016-12-23 16:59:48 -02:00
|
|
|
return this.createRef("heads", branchName, sha);
|
2016-09-14 18:55:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
patchBranch(branchName, sha) {
|
2016-12-23 16:59:48 -02:00
|
|
|
return this.patchRef("heads", branchName, sha);
|
2016-09-14 18:55:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteBranch(branchName) {
|
2016-12-23 16:59:48 -02:00
|
|
|
return this.deleteRef("heads", branchName);
|
2016-09-14 18:55:42 -03:00
|
|
|
}
|
|
|
|
|
2017-04-12 22:58:23 +01:00
|
|
|
createPR(title, head, base = this.branch) {
|
2016-12-23 16:59:48 -02:00
|
|
|
const body = "Automatically generated by Netlify CMS";
|
2016-10-10 18:33:49 -03:00
|
|
|
return this.request(`${ this.repoURL }/pulls`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
method: "POST",
|
2016-08-31 17:33:12 -03:00
|
|
|
body: JSON.stringify({ title, body, head, base }),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:47:36 -05:00
|
|
|
closePR(pullrequest, objects) {
|
|
|
|
const headSha = pullrequest.head;
|
|
|
|
const prNumber = pullrequest.number;
|
|
|
|
console.log("%c Deleting PR", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line
|
|
|
|
return this.request(`${ this.repoURL }/pulls/${ prNumber }`, {
|
|
|
|
method: "PATCH",
|
|
|
|
body: JSON.stringify({
|
|
|
|
state: closed,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:58:15 -02:00
|
|
|
mergePR(pullrequest, objects) {
|
|
|
|
const headSha = pullrequest.head;
|
|
|
|
const prNumber = pullrequest.number;
|
|
|
|
console.log("%c Merging PR", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line
|
|
|
|
return this.request(`${ this.repoURL }/pulls/${ prNumber }/merge`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
method: "PUT",
|
2016-09-14 18:25:45 -03:00
|
|
|
body: JSON.stringify({
|
2016-12-23 16:59:48 -02:00
|
|
|
commit_message: "Automatically generated. Merged on Netlify CMS.",
|
2016-10-10 18:33:49 -03:00
|
|
|
sha: headSha,
|
2016-09-14 18:25:45 -03:00
|
|
|
}),
|
2017-01-11 20:58:15 -02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
if (error instanceof APIError && error.status === 405) {
|
|
|
|
this.forceMergePR(pullrequest, objects);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
forceMergePR(pullrequest, objects) {
|
|
|
|
const files = objects.files.concat(objects.entry);
|
|
|
|
const fileTree = this.composeFileTree(files);
|
|
|
|
let commitMessage = "Automatically generated. Merged on Netlify CMS\n\nForce merge of:";
|
|
|
|
files.forEach((file) => {
|
|
|
|
commitMessage += `\n* "${ file.path }"`;
|
2016-09-14 18:25:45 -03:00
|
|
|
});
|
2017-01-11 20:58:15 -02:00
|
|
|
console.log("%c Automatic merge not possible - Forcing merge.", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line
|
|
|
|
return this.getBranch()
|
|
|
|
.then(branchData => this.updateTree(branchData.commit.sha, "/", fileTree))
|
|
|
|
.then(changeTree => this.commit(commitMessage, changeTree))
|
|
|
|
.then(response => this.patchBranch(this.branch, response.sha));
|
2016-09-14 18:25:45 -03:00
|
|
|
}
|
|
|
|
|
2016-08-30 19:06:20 -03:00
|
|
|
getTree(sha) {
|
2016-10-10 18:33:49 -03:00
|
|
|
return sha ? this.request(`${ this.repoURL }/git/trees/${ sha }`) : Promise.resolve({ tree: [] });
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
toBase64(str) {
|
|
|
|
return Promise.resolve(
|
|
|
|
Base64.encode(str)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadBlob(item) {
|
2017-01-10 22:23:22 -02:00
|
|
|
const content = item instanceof AssetProxy ? item.toBase64() : this.toBase64(item.raw);
|
2016-08-30 19:06:20 -03:00
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
return content.then(contentBase64 => 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;
|
|
|
|
}));
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateTree(sha, path, fileTree) {
|
|
|
|
return this.getTree(sha)
|
|
|
|
.then((tree) => {
|
2017-01-11 20:58:15 -02:00
|
|
|
let obj;
|
|
|
|
let filename;
|
|
|
|
let fileOrDir;
|
2016-10-10 18:33:49 -03:00
|
|
|
const updates = [];
|
|
|
|
const added = {};
|
2016-08-30 19:06:20 -03:00
|
|
|
|
2016-10-10 18:33:49 -03:00
|
|
|
for (let i = 0, len = tree.tree.length; i < len; i++) {
|
2016-08-30 19:06:20 -03:00
|
|
|
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 ?
|
2016-12-23 16:59:48 -02:00
|
|
|
{ path: filename, mode: "100644", type: "blob", sha: fileOrDir.sha } :
|
2016-08-30 19:06:20 -03:00
|
|
|
this.updateTree(null, filename, fileOrDir)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Promise.all(updates)
|
2016-12-23 16:59:48 -02:00
|
|
|
.then(updates => this.request(`${ this.repoURL }/git/trees`, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({ base_tree: sha, tree: updates }),
|
|
|
|
})).then(response => ({ path, mode: "040000", type: "tree", sha: response.sha, parentSha: sha }));
|
2016-08-30 19:06:20 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:30:14 -03:00
|
|
|
commit(message, changeTree) {
|
|
|
|
const tree = changeTree.sha;
|
|
|
|
const parents = changeTree.parentSha ? [changeTree.parentSha] : [];
|
2016-10-10 18:33:49 -03:00
|
|
|
return this.request(`${ this.repoURL }/git/commits`, {
|
2016-12-23 16:59:48 -02:00
|
|
|
method: "POST",
|
2016-10-10 18:33:49 -03:00
|
|
|
body: JSON.stringify({ message, tree, parents }),
|
2016-08-31 13:30:14 -03:00
|
|
|
});
|
|
|
|
}
|
2016-08-30 19:06:20 -03:00
|
|
|
}
|