Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit a5a2f34

Browse files
authoredApr 25, 2024··
feat: allow custom fields inside git commit message (#1105)
Co-authored-by: Felix Kakuschke <[email protected]>
1 parent dd53157 commit a5a2f34

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed
 

‎packages/core/src/backend.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ export class Backend<EF extends BaseField = UnknownField, BC extends BackendClas
11181118
path,
11191119
authorLogin: user.login,
11201120
authorName: user.name,
1121+
data: entryDraft.entry.data,
11211122
},
11221123
user.useOpenAuthoring,
11231124
);

‎packages/core/src/lib/formatters.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { isEmpty } from './util/string.util';
99
import {
1010
addFileTemplateFields,
1111
compileStringTemplate,
12+
getExplicitFieldReplacement,
1213
keyToPathArray,
1314
parseDateFromEntry,
1415
} from './widgets/stringTemplate';
@@ -41,16 +42,17 @@ type Options<EF extends BaseField> = {
4142
collection?: CollectionWithDefaults<EF>;
4243
authorLogin?: string;
4344
authorName?: string;
45+
data?: EntryData;
4446
};
4547

4648
export function commitMessageFormatter<EF extends BaseField>(
4749
type: keyof typeof commitMessageTemplates,
4850
config: ConfigWithDefaults<EF>,
49-
{ slug, path, collection, authorLogin, authorName }: Options<EF>,
51+
{ slug, path, collection, authorLogin, authorName, data }: Options<EF>,
5052
isOpenAuthoring?: boolean,
5153
) {
5254
const templates = { ...commitMessageTemplates, ...(config.backend.commit_messages || {}) };
53-
55+
let explicitReplacement;
5456
const commitMessage = templates[type].replace(variableRegex, (_, variable) => {
5557
switch (variable) {
5658
case 'slug':
@@ -64,6 +66,10 @@ export function commitMessageFormatter<EF extends BaseField>(
6466
case 'author-name':
6567
return authorName || '';
6668
default:
69+
explicitReplacement = getExplicitFieldReplacement(variable, data);
70+
if (explicitReplacement) {
71+
return explicitReplacement;
72+
}
6773
console.warn(
6874
`[StaticCMS] Ignoring unknown variable “${variable}” in commit message template.`,
6975
);

‎packages/core/src/lib/widgets/stringTemplate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function expandPath({
187187

188188
// Allow `fields.` prefix in placeholder to override built in replacements
189189
// like "slug" and "year" with values from fields of the same name.
190-
function getExplicitFieldReplacement(key: string, data: ObjectValue | undefined | null) {
190+
export function getExplicitFieldReplacement(key: string, data: ObjectValue | undefined | null) {
191191
if (!key.startsWith(FIELD_PREFIX)) {
192192
return;
193193
}

‎packages/docs/content/docs/configuration-options.mdx

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ Static CMS generates the following commit types:
6868

6969
| Commit type | When is it triggered? | Available template tags |
7070
| ------------- | ---------------------------- | ----------------------------------------------------------- |
71-
| `create` | A new entry is created | `slug`, `path`, `collection`, `author-login`, `author-name` |
72-
| `update` | An existing entry is changed | `slug`, `path`, `collection`, `author-login`, `author-name` |
71+
| `create` | A new entry is created | `slug`, `path`, `collection`, `author-login`, `author-name`, `fields` |
72+
| `update` | An existing entry is changed | `slug`, `path`, `collection`, `author-login`, `author-name`, `fields` |
7373
| `delete` | An existing entry is deleted | `slug`, `path`, `collection`, `author-login`, `author-name` |
7474
| `uploadMedia` | A media file is uploaded | `path`, `author-login`, `author-name` |
7575
| `deleteMedia` | A media file is deleted | `path`, `author-login`, `author-name` |
@@ -83,6 +83,7 @@ Template tags produce the following output:
8383
- `{{path}}`: full path to the changed file
8484
- `{{author-login}}`: login/username of the author
8585
- `{{author-name}}`: full name of the author (might be empty based on the user's profile)
86+
- `{{fields.[FIELD_NAME]}}`: A custom fields value
8687

8788
## Publish Mode
8889

0 commit comments

Comments
 (0)
This repository has been archived.