Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable user-configured commit message templates #1359

Merged
merged 10 commits into from
May 23, 2018
4 changes: 2 additions & 2 deletions src/actions/mediaLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function persistMedia(file, opts = {}) {
const assetProxy = await createAssetProxy(fileName, file, false, privateUpload);
dispatch(addAsset(assetProxy));
if (!integration) {
const asset = await backend.persistMedia(assetProxy);
const asset = await backend.persistMedia(state.config, assetProxy);
return dispatch(mediaPersisted(asset));
}
return dispatch(mediaPersisted(assetProxy.asset, { privateUpload }));
Expand Down Expand Up @@ -144,7 +144,7 @@ export function deleteMedia(file, opts = {}) {
});
}
dispatch(mediaDeleting());
return backend.deleteMedia(file.path)
return backend.deleteMedia(state.config, file.path)
.then(() => {
return dispatch(mediaDeleted(file));
})
Expand Down
40 changes: 33 additions & 7 deletions src/backends/backend.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { attempt, isError } from 'lodash';
import { Map } from 'immutable';
import { resolveFormat } from "Formats/formats";
import { selectIntegration } from 'Reducers/integrations';
import {
Expand Down Expand Up @@ -88,6 +89,32 @@ const slugFormatter = (template = "{{slug}}", entryData, slugConfig) => {
return sanitizeSlug(slug, slugConfig);
};

const commitMessageTemplates = Map({
create: 'Create {{collection}} “{{slug}}”',
update: 'Update {{collection}} “{{slug}}”',
delete: 'Delete {{collection}} “{{slug}}”',
uploadMedia: 'Upload “{{path}}”',
deleteMedia: 'Delete “{{path}}”'
});

const commitMessageFormatter = (type, config, { slug, path, collection }) => {
const templates = commitMessageTemplates.merge(config.getIn(['backend', 'commit_messages'], Map()));
const messageTemplate = templates.get(type);
return messageTemplate.replace(/\{\{([^\}]+)\}\}/g, (_, variable) => {
switch (variable) {
case 'slug':
return slug;
case 'path':
return path;
case 'collection':
return collection.get('label');
default:
console.warn(`Ignoring unknown variable “${ variable }” in commit message template.`);
return '';
}
});
}

class Backend {
constructor(implementation, backendName, authStore = null) {
this.implementation = implementation;
Expand Down Expand Up @@ -265,8 +292,7 @@ class Backend {
};
}

const commitMessage = `${ (newEntry ? "Create " : "Update ") +
collection.get("label") } “${ entryObj.slug }”`;
const commitMessage = commitMessageFormatter(newEntry ? 'create' : 'update', config, { collection, slug: entryObj.slug, path: entryObj.path });

const mode = config.get("publish_mode");

Expand All @@ -283,9 +309,9 @@ class Backend {
.then(() => entryObj.slug);
}

persistMedia(file) {
persistMedia(config, file) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch the argument order for persistMedia and deleteMedia so the config is the last argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will do. One note: I decided to pass config as the first argument because that is what persistEntry and deleteEntry currently do. Not sure if inconsistency between entry and media actions is a concern.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have asked why you did that - you're right, consistency first. Thanks for pointing that out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, great. I’ll work on some documentation now.

const options = {
commitMessage: `Upload ${file.path}`,
commitMessage: commitMessageFormatter('uploadMedia', config, { path: file.path }),
};
return this.implementation.persistMedia(file, options);
}
Expand All @@ -297,12 +323,12 @@ class Backend {
throw (new Error("Not allowed to delete entries in this collection"));
}

const commitMessage = `Delete ${ collection.get('label') } “${ slug }”`;
const commitMessage = commitMessageFormatter('delete', config, { collection, slug, path });
return this.implementation.deleteFile(path, commitMessage);
}

deleteMedia(path) {
const commitMessage = `Delete ${path}`;
deleteMedia(config, path) {
const commitMessage = commitMessageFormatter('deleteMedia', config, { path });
return this.implementation.deleteFile(path, commitMessage);
}

Expand Down