Skip to content

Commit

Permalink
Enable user-configured commit message templates (decaporg#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis authored and erquhart committed May 23, 2018
1 parent 41be0a4 commit 5cc1594
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
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 @@ -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;
Expand Down Expand Up @@ -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");

Expand All @@ -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);
}
Expand All @@ -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);
}

Expand Down
34 changes: 34 additions & 0 deletions website/site/content/docs/beta-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5cc1594

Please sign in to comment.