feat: allow custom fields inside git commit message (#1105)

Co-authored-by: Felix Kakuschke <felix.kakuschke@germanedge.com>
This commit is contained in:
Felix Kakuschke 2024-04-25 21:15:39 +02:00 committed by GitHub
parent dd53157897
commit a5a2f340ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 5 deletions

View File

@ -1118,6 +1118,7 @@ export class Backend<EF extends BaseField = UnknownField, BC extends BackendClas
path,
authorLogin: user.login,
authorName: user.name,
data: entryDraft.entry.data,
},
user.useOpenAuthoring,
);

View File

@ -9,6 +9,7 @@ import { isEmpty } from './util/string.util';
import {
addFileTemplateFields,
compileStringTemplate,
getExplicitFieldReplacement,
keyToPathArray,
parseDateFromEntry,
} from './widgets/stringTemplate';
@ -41,16 +42,17 @@ type Options<EF extends BaseField> = {
collection?: CollectionWithDefaults<EF>;
authorLogin?: string;
authorName?: string;
data?: EntryData;
};
export function commitMessageFormatter<EF extends BaseField>(
type: keyof typeof commitMessageTemplates,
config: ConfigWithDefaults<EF>,
{ slug, path, collection, authorLogin, authorName }: Options<EF>,
{ slug, path, collection, authorLogin, authorName, data }: Options<EF>,
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<EF extends BaseField>(
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.`,
);

View File

@ -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;
}

View File

@ -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