From f7e7120db50a295349e429eefcad8313a720c752 Mon Sep 17 00:00:00 2001 From: Caleb Date: Tue, 7 Aug 2018 09:49:53 -0600 Subject: [PATCH] fix(backends): fix commit message handling (#1568) --- .../netlify-cms-backend-bitbucket/src/API.js | 31 +++++++++++++------ .../netlify-cms-backend-github/src/API.js | 6 ++++ .../netlify-cms-backend-gitlab/src/API.js | 22 ++++++++++--- .../src/implementation.js | 2 +- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/packages/netlify-cms-backend-bitbucket/src/API.js b/packages/netlify-cms-backend-bitbucket/src/API.js index e0f75db2..a039ef6a 100644 --- a/packages/netlify-cms-backend-bitbucket/src/API.js +++ b/packages/netlify-cms-backend-bitbucket/src/API.js @@ -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), diff --git a/packages/netlify-cms-backend-github/src/API.js b/packages/netlify-cms-backend-github/src/API.js index f5daac01..dd95bcdd 100644 --- a/packages/netlify-cms-backend-github/src/API.js +++ b/packages/netlify-cms-backend-github/src/API.js @@ -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); }); } diff --git a/packages/netlify-cms-backend-gitlab/src/API.js b/packages/netlify-cms-backend-gitlab/src/API.js index 39e8f17d..1f7bb231 100644 --- a/packages/netlify-cms-backend-gitlab/src/API.js +++ b/packages/netlify-cms-backend-gitlab/src/API.js @@ -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) }`); }; diff --git a/packages/netlify-cms-backend-test/src/implementation.js b/packages/netlify-cms-backend-test/src/implementation.js index f3c9d1c3..543ce837 100644 --- a/packages/netlify-cms-backend-test/src/implementation.js +++ b/packages/netlify-cms-backend-test/src/implementation.js @@ -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);