From a5a2f340ec2c5eefd26c4e299a9a85242ef6a28a Mon Sep 17 00:00:00 2001 From: Felix Kakuschke <91883988+fkakuschkeORS@users.noreply.github.com> Date: Thu, 25 Apr 2024 21:15:39 +0200 Subject: [PATCH] feat: allow custom fields inside git commit message (#1105) Co-authored-by: Felix Kakuschke --- packages/core/src/backend.ts | 1 + packages/core/src/lib/formatters.ts | 10 ++++++++-- packages/core/src/lib/widgets/stringTemplate.ts | 2 +- packages/docs/content/docs/configuration-options.mdx | 5 +++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/core/src/backend.ts b/packages/core/src/backend.ts index 3ba85272a..f1e416bf3 100644 --- a/packages/core/src/backend.ts +++ b/packages/core/src/backend.ts @@ -1118,6 +1118,7 @@ export class Backend = { collection?: CollectionWithDefaults; authorLogin?: string; authorName?: string; + data?: EntryData; }; export function commitMessageFormatter( type: keyof typeof commitMessageTemplates, config: ConfigWithDefaults, - { slug, path, collection, authorLogin, authorName }: Options, + { slug, path, collection, authorLogin, authorName, data }: Options, isOpenAuthoring?: boolean, ) { const templates = { ...commitMessageTemplates, ...(config.backend.commit_messages || {}) }; - + let explicitReplacement; const commitMessage = templates[type].replace(variableRegex, (_, variable) => { switch (variable) { case 'slug': @@ -64,6 +66,10 @@ export function commitMessageFormatter( case 'author-name': return authorName || ''; default: + explicitReplacement = getExplicitFieldReplacement(variable, data); + if (explicitReplacement) { + return explicitReplacement; + } console.warn( `[StaticCMS] Ignoring unknown variable “${variable}” in commit message template.`, ); diff --git a/packages/core/src/lib/widgets/stringTemplate.ts b/packages/core/src/lib/widgets/stringTemplate.ts index edf6733c4..322572ee7 100644 --- a/packages/core/src/lib/widgets/stringTemplate.ts +++ b/packages/core/src/lib/widgets/stringTemplate.ts @@ -187,7 +187,7 @@ export function expandPath({ // Allow `fields.` prefix in placeholder to override built in replacements // 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)) { return; } diff --git a/packages/docs/content/docs/configuration-options.mdx b/packages/docs/content/docs/configuration-options.mdx index 857bf939f..e244ab69e 100644 --- a/packages/docs/content/docs/configuration-options.mdx +++ b/packages/docs/content/docs/configuration-options.mdx @@ -68,8 +68,8 @@ Static CMS generates the following commit types: | Commit type | When is it triggered? | Available template tags | | ------------- | ---------------------------- | ----------------------------------------------------------- | -| `create` | A new entry is created | `slug`, `path`, `collection`, `author-login`, `author-name` | -| `update` | An existing entry is changed | `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`, `fields` | | `delete` | An existing entry is deleted | `slug`, `path`, `collection`, `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` | @@ -83,6 +83,7 @@ Template tags produce the following output: - `{{path}}`: full path to the changed file - `{{author-login}}`: login/username of the author - `{{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