fix(backends): fix commit message handling (#1568)

This commit is contained in:
Caleb 2018-08-07 09:49:53 -06:00 committed by Shawn Erquhart
parent 4782c472bf
commit f7e7120db5
4 changed files with 46 additions and 15 deletions

View File

@ -1,4 +1,4 @@
import { flow, has } from "lodash";
import { flow, get } from "lodash";
import {
localForage,
unsentRequest,
@ -118,10 +118,19 @@ export default class API {
return this.processFiles(entries);
};
uploadBlob = async item => {
const contentBase64 = await (has(item, 'toBase64') ? item.toBase64() : Promise.resolve(item.raw));
uploadBlob = async (item, { commitMessage, branch = this.branch } = {}) => {
const contentBlob = get(item, 'fileObj', new Blob([item.raw]));
const formData = new FormData();
formData.append(item.path, contentBase64);
// Third param is filename header, in case path is `message`, `branch`, etc.
formData.append(item.path, contentBlob, basename(item.path));
formData.append('branch', branch);
if (commitMessage) {
formData.append("message", commitMessage);
}
if (this.commitAuthor) {
const { name, email } = this.commitAuthor;
formData.append("author", `${name} <${email}>`);
}
return flow([
unsentRequest.withMethod("POST"),
@ -131,17 +140,21 @@ export default class API {
])(`${ this.repoURL }/src`);
};
persistFiles = (files, { commitMessage, newEntry }) => Promise.all(
files.filter(({ uploaded }) => !uploaded).map(this.uploadBlob)
persistFiles = (files, { commitMessage }) => Promise.all(
files.filter(({ uploaded }) => !uploaded).map(file => this.uploadBlob(file, { commitMessage }))
);
deleteFile = (path, message, options={}) => {
const branch = options.branch || this.branch;
deleteFile = (path, message, { branch = this.branch } = {}) => {
const body = new FormData();
body.append('files', path);
if (message && message !== "") {
body.append('branch', branch);
if (message) {
body.append("message", message);
}
if (this.commitAuthor) {
const { name, email } = this.commitAuthor;
body.append("author", `${name} <${email}>`);
}
return flow([
unsentRequest.withMethod("POST"),
unsentRequest.withBody(body),

View File

@ -316,6 +316,12 @@ export default class API {
.then(resp => {
const { sha } = resp.tree.find(file => file.path === filename);
const opts = { method: 'DELETE', params: { sha, message, branch } };
if (this.commitAuthor) {
opts.params.author = {
...this.commitAuthor,
date: new Date().toISOString(),
};
}
return this.request(fileURL, opts);
});
}

View File

@ -189,18 +189,23 @@ export default class API {
const file_path = item.path.replace(/^\//, "");
const action = (updateFile ? "update" : "create");
const encoding = "base64";
const { name: author_name, email: author_email } = pick(author || {}, ["name", "email"]);
const body = JSON.stringify({
const commitParams = {
branch,
commit_message: commitMessage,
actions: [{ action, file_path, content, encoding }],
});
};
if (author) {
const { name, email } = author;
commitParams.author_name = name;
commitParams.author_email = email;
}
await this.request({
url: `${ this.repoURL }/repository/commits`,
method: "POST",
headers: { "Content-Type": "application/json" },
body,
body: JSON.stringify(commitParams),
});
return { ...item, uploaded: true };
@ -211,9 +216,16 @@ export default class API {
deleteFile = (path, commit_message, options = {}) => {
const branch = options.branch || this.branch;
const commitParams = { commit_message, branch };
if (this.commitAuthor) {
const { name, email } = this.commitAuthor;
commitParams.author_name = name;
commitParams.author_email = email;
}
return flow([
unsentRequest.withMethod("DELETE"),
unsentRequest.withParams({ commit_message, branch }),
// TODO: only send author params if they are defined.
unsentRequest.withParams(commitParams),
this.request,
])(`${ this.repoURL }/repository/files/${ encodeURIComponent(path) }`);
};

View File

@ -133,7 +133,7 @@ export default class TestRepo {
return Promise.resolve();
}
persistEntry({ path, raw, slug }, mediaFiles = [], options = {}) {
persistEntry({ path, raw, slug }, mediaFiles, options = {}) {
if (options.useWorkflow) {
const unpubStore = window.repoFilesUnpublished;
const existingEntryIndex = unpubStore.findIndex(e => e.file.path === path);