diff --git a/src/actions/mediaLibrary.js b/src/actions/mediaLibrary.js index 8f4c129b573b..64acfdfd116b 100644 --- a/src/actions/mediaLibrary.js +++ b/src/actions/mediaLibrary.js @@ -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 })); @@ -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)); }) diff --git a/src/backends/backend.js b/src/backends/backend.js index e06430c03f29..461c52f83cc9 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -1,4 +1,5 @@ import { attempt, isError } from 'lodash'; +import { Map } from 'immutable'; import { resolveFormat } from "Formats/formats"; import { selectIntegration } from 'Reducers/integrations'; import { @@ -93,6 +94,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; @@ -304,8 +331,7 @@ in the ${collection.get('name')} collection.\ }; } - 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"); @@ -322,9 +348,9 @@ in the ${collection.get('name')} collection.\ .then(() => entryObj.slug); } - persistMedia(file) { + persistMedia(config, file) { const options = { - commitMessage: `Upload ${file.path}`, + commitMessage: commitMessageFormatter('uploadMedia', config, { path: file.path }), }; return this.implementation.persistMedia(file, options); } @@ -336,12 +362,12 @@ in the ${collection.get('name')} collection.\ 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); } diff --git a/website/site/content/docs/beta-features.md b/website/site/content/docs/beta-features.md index 292d0025bbe4..b0cb86af1633 100644 --- a/website/site/content/docs/beta-features.md +++ b/website/site/content/docs/beta-features.md @@ -91,3 +91,37 @@ To enable this feature, you can set the following option in your `config.yml`: backend: squash_merges: true ``` +## Commit Message Templates +You can customize the templates used by Netlify CMS to generate commit messages by setting the `commit_messages` option under `backend` in your `config.yml`. + +Template tags wrapped in curly braces will be expanded to include information about the file changed by the commit. For example, `{{path}}` will include the full path to the file changed. + +Setting up your `config.yml` to recreate the default values would look like this: + +```yaml +backend: + commit_messages: + create: Create {{collection}} “{{slug}}” + update: Update {{collection}} “{{slug}}” + delete: Delete {{collection}} “{{slug}}” + uploadMedia: Upload “{{path}}” + deleteMedia: Delete “{{path}}” +``` + +Netlify CMS generates the following commit types: + +Commit type | When is it triggered? | Available template tags +--------------|------------------------------|----------------------------- +`create` | A new entry is created | `slug`, `path`, `collection` +`update` | An existing entry is changed | `slug`, `path`, `collection` +`delete` | An exising entry is deleted | `slug`, `path`, `collection` +`uploadMedia` | A media file is uploaded | `path` +`deleteMedia` | A media file is deleted | `path` + +Template tags produce the following output: + +- `{{slug}}`: the url-safe filename of the entry changed + +- `{{collection}}`: the name of the collection containing the entry changed + +- `{{path}}`: the full path to the file changed