feat: allow custom fields inside git commit message (#1105)
Co-authored-by: Felix Kakuschke <felix.kakuschke@germanedge.com>
This commit is contained in:
parent
dd53157897
commit
a5a2f340ec
@ -1118,6 +1118,7 @@ export class Backend<EF extends BaseField = UnknownField, BC extends BackendClas
|
|||||||
path,
|
path,
|
||||||
authorLogin: user.login,
|
authorLogin: user.login,
|
||||||
authorName: user.name,
|
authorName: user.name,
|
||||||
|
data: entryDraft.entry.data,
|
||||||
},
|
},
|
||||||
user.useOpenAuthoring,
|
user.useOpenAuthoring,
|
||||||
);
|
);
|
||||||
|
@ -9,6 +9,7 @@ import { isEmpty } from './util/string.util';
|
|||||||
import {
|
import {
|
||||||
addFileTemplateFields,
|
addFileTemplateFields,
|
||||||
compileStringTemplate,
|
compileStringTemplate,
|
||||||
|
getExplicitFieldReplacement,
|
||||||
keyToPathArray,
|
keyToPathArray,
|
||||||
parseDateFromEntry,
|
parseDateFromEntry,
|
||||||
} from './widgets/stringTemplate';
|
} from './widgets/stringTemplate';
|
||||||
@ -41,16 +42,17 @@ type Options<EF extends BaseField> = {
|
|||||||
collection?: CollectionWithDefaults<EF>;
|
collection?: CollectionWithDefaults<EF>;
|
||||||
authorLogin?: string;
|
authorLogin?: string;
|
||||||
authorName?: string;
|
authorName?: string;
|
||||||
|
data?: EntryData;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function commitMessageFormatter<EF extends BaseField>(
|
export function commitMessageFormatter<EF extends BaseField>(
|
||||||
type: keyof typeof commitMessageTemplates,
|
type: keyof typeof commitMessageTemplates,
|
||||||
config: ConfigWithDefaults<EF>,
|
config: ConfigWithDefaults<EF>,
|
||||||
{ slug, path, collection, authorLogin, authorName }: Options<EF>,
|
{ slug, path, collection, authorLogin, authorName, data }: Options<EF>,
|
||||||
isOpenAuthoring?: boolean,
|
isOpenAuthoring?: boolean,
|
||||||
) {
|
) {
|
||||||
const templates = { ...commitMessageTemplates, ...(config.backend.commit_messages || {}) };
|
const templates = { ...commitMessageTemplates, ...(config.backend.commit_messages || {}) };
|
||||||
|
let explicitReplacement;
|
||||||
const commitMessage = templates[type].replace(variableRegex, (_, variable) => {
|
const commitMessage = templates[type].replace(variableRegex, (_, variable) => {
|
||||||
switch (variable) {
|
switch (variable) {
|
||||||
case 'slug':
|
case 'slug':
|
||||||
@ -64,6 +66,10 @@ export function commitMessageFormatter<EF extends BaseField>(
|
|||||||
case 'author-name':
|
case 'author-name':
|
||||||
return authorName || '';
|
return authorName || '';
|
||||||
default:
|
default:
|
||||||
|
explicitReplacement = getExplicitFieldReplacement(variable, data);
|
||||||
|
if (explicitReplacement) {
|
||||||
|
return explicitReplacement;
|
||||||
|
}
|
||||||
console.warn(
|
console.warn(
|
||||||
`[StaticCMS] Ignoring unknown variable “${variable}” in commit message template.`,
|
`[StaticCMS] Ignoring unknown variable “${variable}” in commit message template.`,
|
||||||
);
|
);
|
||||||
|
@ -187,7 +187,7 @@ export function expandPath({
|
|||||||
|
|
||||||
// Allow `fields.` prefix in placeholder to override built in replacements
|
// Allow `fields.` prefix in placeholder to override built in replacements
|
||||||
// like "slug" and "year" with values from fields of the same name.
|
// like "slug" and "year" with values from fields of the same name.
|
||||||
function getExplicitFieldReplacement(key: string, data: ObjectValue | undefined | null) {
|
export function getExplicitFieldReplacement(key: string, data: ObjectValue | undefined | null) {
|
||||||
if (!key.startsWith(FIELD_PREFIX)) {
|
if (!key.startsWith(FIELD_PREFIX)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -68,8 +68,8 @@ Static CMS generates the following commit types:
|
|||||||
|
|
||||||
| Commit type | When is it triggered? | Available template tags |
|
| Commit type | When is it triggered? | Available template tags |
|
||||||
| ------------- | ---------------------------- | ----------------------------------------------------------- |
|
| ------------- | ---------------------------- | ----------------------------------------------------------- |
|
||||||
| `create` | A new entry is created | `slug`, `path`, `collection`, `author-login`, `author-name` |
|
| `create` | A new entry is created | `slug`, `path`, `collection`, `author-login`, `author-name`, `fields` |
|
||||||
| `update` | An existing entry is changed | `slug`, `path`, `collection`, `author-login`, `author-name` |
|
| `update` | An existing entry is changed | `slug`, `path`, `collection`, `author-login`, `author-name`, `fields` |
|
||||||
| `delete` | An existing entry is deleted | `slug`, `path`, `collection`, `author-login`, `author-name` |
|
| `delete` | An existing entry is deleted | `slug`, `path`, `collection`, `author-login`, `author-name` |
|
||||||
| `uploadMedia` | A media file is uploaded | `path`, `author-login`, `author-name` |
|
| `uploadMedia` | A media file is uploaded | `path`, `author-login`, `author-name` |
|
||||||
| `deleteMedia` | A media file is deleted | `path`, `author-login`, `author-name` |
|
| `deleteMedia` | A media file is deleted | `path`, `author-login`, `author-name` |
|
||||||
@ -83,6 +83,7 @@ Template tags produce the following output:
|
|||||||
- `{{path}}`: full path to the changed file
|
- `{{path}}`: full path to the changed file
|
||||||
- `{{author-login}}`: login/username of the author
|
- `{{author-login}}`: login/username of the author
|
||||||
- `{{author-name}}`: full name of the author (might be empty based on the user's profile)
|
- `{{author-name}}`: full name of the author (might be empty based on the user's profile)
|
||||||
|
- `{{fields.[FIELD_NAME]}}`: A custom fields value
|
||||||
|
|
||||||
## Publish Mode
|
## Publish Mode
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user