Skip to content

Commit e87ae8a

Browse files
authored
feat: use BEGIN_COMMIT and END_COMMIT (#2681)
Change the commit marker from `BEGIN_COMMIT_OVERRIDE` and `END_COMMIT_OVERRIDE` to `BEGIN_COMMIT` and `END_COMMIT`. This is a backward compatible change and we can remove the deprecated field after these fields don't appear in the generation PRs. Fixes #2072
1 parent 4968d63 commit e87ae8a

File tree

7 files changed

+125
-46
lines changed

7 files changed

+125
-46
lines changed

internal/gitrepo/conventional_commits.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@ import (
2525
)
2626

2727
const (
28+
// beginCommit marks the start of the commit message block. It is used in conjunction with endCommit.
29+
beginCommit = "BEGIN_COMMIT"
30+
// beginCommitOverride marks the start of the commit message block. It is used in conjunction with endCommitOverride.
31+
// Deprecated: use beginCommit instead.
2832
beginCommitOverride = "BEGIN_COMMIT_OVERRIDE"
29-
endCommitOverride = "END_COMMIT_OVERRIDE"
30-
beginNestedCommit = "BEGIN_NESTED_COMMIT"
31-
endNestedCommit = "END_NESTED_COMMIT"
32-
breakingChangeKey = "BREAKING CHANGE"
33-
sourceLinkKey = "Source-Link"
33+
// endCommit marks the end of the commit message block.
34+
endCommit = "END_COMMIT"
35+
// endCommitOverride marks the end of the commit message block.
36+
// Deprecated: use endCommit instead.
37+
endCommitOverride = "END_COMMIT_OVERRIDE"
38+
beginNestedCommit = "BEGIN_NESTED_COMMIT"
39+
endNestedCommit = "END_NESTED_COMMIT"
40+
breakingChangeKey = "BREAKING CHANGE"
41+
sourceLinkKey = "Source-Link"
3442
)
3543

3644
var (
@@ -113,9 +121,9 @@ func (c *ConventionalCommit) MarshalJSON() ([]byte, error) {
113121

114122
// ParseCommits parses a commit message into a slice of ConventionalCommit structs.
115123
//
116-
// It supports an override block wrapped in BEGIN_COMMIT_OVERRIDE and
117-
// END_COMMIT_OVERRIDE. If found, this block takes precedence, and only its
118-
// content will be parsed.
124+
// It supports a top-level commit wrapped in BEGIN_COMMIT and END_COMMIT (BEGIN_COMMIT_OVERRIDE and
125+
// END_COMMIT_OVERRIDE are also supported for backward compatibility).
126+
// If found, this block takes precedence, and only its content will be parsed.
119127
//
120128
// The message can also contain multiple nested commits, each wrapped in
121129
// BEGIN_NESTED_COMMIT and END_NESTED_COMMIT markers.
@@ -128,7 +136,7 @@ func ParseCommits(commit *Commit, libraryID string) ([]*ConventionalCommit, erro
128136
if strings.TrimSpace(message) == "" {
129137
return nil, ErrEmptyCommitMessage
130138
}
131-
message = extractCommitMessageOverride(message)
139+
message = extractBeginCommitMessage(message)
132140

133141
var commits []*ConventionalCommit
134142

@@ -147,16 +155,29 @@ func ParseCommits(commit *Commit, libraryID string) ([]*ConventionalCommit, erro
147155
return commits, nil
148156
}
149157

150-
func extractCommitMessageOverride(message string) string {
151-
beginIndex := strings.Index(message, beginCommitOverride)
158+
func extractBeginCommitMessage(message string) string {
159+
// Search the deprecated marker first because beginCommit is the prefix
160+
// of beginCommitOverride.
161+
// TODO: remove usage when we drop support for `BEGIN_COMMIT_OVERRIDE` and `END_COMMIT_OVERRIDE`.
162+
// see https://github.com/googleapis/librarian/issues/2684
163+
beginMarker := beginCommitOverride
164+
endMarker := endCommitOverride
165+
beginIndex := strings.Index(message, beginMarker)
166+
if beginIndex == -1 {
167+
beginMarker = beginCommit
168+
endMarker = endCommit
169+
beginIndex = strings.Index(message, beginMarker)
170+
}
152171
if beginIndex == -1 {
153172
return message
154173
}
155-
afterBegin := message[beginIndex+len(beginCommitOverride):]
156-
endIndex := strings.Index(afterBegin, endCommitOverride)
174+
175+
afterBegin := message[beginIndex+len(beginMarker):]
176+
endIndex := strings.Index(afterBegin, endMarker)
157177
if endIndex == -1 {
158178
return message
159179
}
180+
160181
return strings.TrimSpace(afterBegin[:endIndex])
161182
}
162183

internal/gitrepo/conventional_commits_test.go

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,16 @@ func TestParseCommits(t *testing.T) {
200200
},
201201
},
202202
{
203-
name: "commit_override",
203+
name: "begin_commit",
204204
message: `feat: original message
205205
206-
BEGIN_COMMIT_OVERRIDE
206+
BEGIN_COMMIT
207207
fix(override): this is the override message
208208
209209
This is the body of the override.
210210
211211
Reviewed-by: Jane Doe
212-
END_COMMIT_OVERRIDE`,
212+
END_COMMIT`,
213213
want: []*ConventionalCommit{
214214
{
215215
Type: "fix",
@@ -304,7 +304,65 @@ END_NESTED_COMMIT
304304
},
305305
},
306306
{
307-
name: "commit_override_with_nested_commits",
307+
name: "begin_commit_with_nested_commits",
308+
message: `feat: API regeneration main commit
309+
310+
This pull request is generated with proto changes between
311+
... ...
312+
313+
Librarian Version: {librarian_version}
314+
Language Image: {language_image_name_and_digest}
315+
316+
BEGIN_COMMIT
317+
BEGIN_NESTED_COMMIT
318+
feat: [abc] nested commit 1
319+
320+
body of nested commit 1
321+
...
322+
323+
PiperOrigin-RevId: 123456
324+
325+
Source-Link: fake-link
326+
END_NESTED_COMMIT
327+
BEGIN_NESTED_COMMIT
328+
feat: [abc] nested commit 2
329+
330+
body of nested commit 2
331+
...
332+
333+
PiperOrigin-RevId: 654321
334+
335+
Source-Link: fake-link
336+
END_NESTED_COMMIT
337+
END_COMMIT
338+
`,
339+
want: []*ConventionalCommit{
340+
{
341+
Type: "feat",
342+
Subject: "[abc] nested commit 1",
343+
Body: "body of nested commit 1\n...",
344+
LibraryID: "abc",
345+
IsNested: true,
346+
Footers: map[string]string{"PiperOrigin-RevId": "123456", "Source-Link": "fake-link"},
347+
CommitHash: sha.String(),
348+
When: now,
349+
},
350+
{
351+
Type: "feat",
352+
Subject: "[abc] nested commit 2",
353+
IsNested: true,
354+
Body: "body of nested commit 2\n...",
355+
LibraryID: "abc",
356+
Footers: map[string]string{"PiperOrigin-RevId": "654321", "Source-Link": "fake-link"},
357+
CommitHash: sha.String(),
358+
When: now,
359+
},
360+
},
361+
},
362+
{
363+
// This test verifies that the deprecated mark, BEGIN_COMMIT_OVERRIDE and END_COMMIT_OVERRIDE
364+
// can be used to separate nested commits.
365+
name: "begin_commit_override_with_nested_commits",
308366
message: `feat: API regeneration main commit
309367
310368
This pull request is generated with proto changes between
@@ -360,18 +418,18 @@ END_COMMIT_OVERRIDE
360418
},
361419
},
362420
{
363-
name: "nest_commit_outside_of_override_ignored",
421+
name: "nest_commit_outside_of_begin_commit_ignored",
364422
message: `feat: original message
365423
366424
BEGIN_NESTED_COMMIT
367425
ignored line
368-
BEGIN_COMMIT_OVERRIDE
426+
BEGIN_COMMIT
369427
fix(override): this is the override message
370428
371429
This is the body of the override.
372430
373431
Reviewed-by: Jane Doe
374-
END_COMMIT_OVERRIDE
432+
END_COMMIT
375433
END_NESTED_COMMIT`,
376434
want: []*ConventionalCommit{
377435
{
@@ -397,7 +455,7 @@ This pull request is generated with proto changes between
397455
[googleapis/googleapis@b738e78](https://github.com/googleapis/googleapis/commit/b738e78ed63effb7d199ed2d61c9e03291b6077f)
398456
(inclusive).
399457
400-
BEGIN_COMMIT_OVERRIDE
458+
BEGIN_COMMIT
401459
BEGIN_NESTED_COMMIT
402460
feat: [texttospeech] Support promptable voices by specifying a model name and a prompt
403461
feat: [texttospeech] Add enum value M4A to enum AudioEncoding
@@ -407,7 +465,7 @@ PiperOrigin-RevId: 799242210
407465
408466
Source-Link: [googleapis/googleapis@b738e78](https://github.com/googleapis/googleapis/commit/b738e78ed63effb7d199ed2d61c9e03291b6077f)
409467
END_NESTED_COMMIT
410-
END_COMMIT_OVERRIDE`,
468+
END_COMMIT`,
411469
want: []*ConventionalCommit{
412470
{
413471
Type: "feat",
@@ -458,7 +516,7 @@ This pull request is generated with proto changes between
458516
[googleapis/googleapis@b738e78](https://github.com/googleapis/googleapis/commit/b738e78ed63effb7d199ed2d61c9e03291b6077f)
459517
(inclusive).
460518
461-
BEGIN_COMMIT_OVERRIDE
519+
BEGIN_COMMIT
462520
BEGIN_NESTED_COMMIT
463521
feat: [texttospeech] Support promptable voices by specifying a model
464522
name and a prompt
@@ -467,7 +525,7 @@ docs: [texttospeech] A comment for method 'StreamingSynthesize' in
467525
service 'TextToSpeech' is changed
468526
469527
END_NESTED_COMMIT
470-
END_COMMIT_OVERRIDE`,
528+
END_COMMIT`,
471529
want: []*ConventionalCommit{
472530
{
473531
Type: "feat",
@@ -511,7 +569,7 @@ This pull request is generated with proto changes between
511569
[googleapis/googleapis@36533b0](googleapis/googleapis@36533b0)
512570
(inclusive).
513571
514-
BEGIN_COMMIT_OVERRIDE
572+
BEGIN_COMMIT
515573
BEGIN_NESTED_COMMIT
516574
docs: [google-cloud-video-live-stream] Update requirements of resource ID fields to be more clear
517575
@@ -521,7 +579,7 @@ BEGIN_NESTED_COMMIT
521579
feat: [google-cloud-eventarc] add new fields to Eventarc resources
522580
523581
END_NESTED_COMMIT
524-
END_COMMIT_OVERRIDE`,
582+
END_COMMIT`,
525583
want: []*ConventionalCommit{
526584
{
527585
Type: "docs",

internal/librarian/commit_version_analyzer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
229229
},
230230
{
231231
path: "foo/something.txt",
232-
message: `BEGIN_COMMIT_OVERRIDE
232+
message: `BEGIN_COMMIT
233233
234234
BEGIN_NESTED_COMMIT
235235
fix: a bug1 fix
@@ -261,7 +261,7 @@ Library-IDs: foo, bar
261261
Source-link: [googleapis/googleapis@fedcba09](https://github.com/googleapis/googleapis/commit/fedcba09)
262262
END_NESTED_COMMIT
263263
264-
END_COMMIT_OVERRIDE`,
264+
END_COMMIT`,
265265
},
266266
}
267267

internal/librarian/generate_pull_request_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestFormatGenerationPRBody(t *testing.T) {
116116
"another-library": "abcdefg",
117117
},
118118
failedLibraries: []string{},
119-
want: fmt.Sprintf(`BEGIN_COMMIT_OVERRIDE
119+
want: fmt.Sprintf(`BEGIN_COMMIT
120120
121121
BEGIN_NESTED_COMMIT
122122
fix: a bug fix
@@ -127,7 +127,7 @@ Library-IDs: one-library
127127
Source-link: [googleapis/googleapis@fedcba09](https://github.com/googleapis/googleapis/commit/fedcba09)
128128
END_NESTED_COMMIT
129129
130-
END_COMMIT_OVERRIDE
130+
END_COMMIT
131131
132132
This pull request is generated with proto changes between
133133
[googleapis/googleapis@abcdef00](https://github.com/googleapis/googleapis/commit/abcdef0000000000000000000000000000000000)
@@ -208,7 +208,7 @@ Language Image: %s`,
208208
"another-library": "abcdefg",
209209
},
210210
failedLibraries: []string{},
211-
want: fmt.Sprintf(`BEGIN_COMMIT_OVERRIDE
211+
want: fmt.Sprintf(`BEGIN_COMMIT
212212
213213
BEGIN_NESTED_COMMIT
214214
fix: a bug fix
@@ -219,7 +219,7 @@ Library-IDs: one-library,another-library
219219
Source-link: [googleapis/googleapis@fedcba09](https://github.com/googleapis/googleapis/commit/fedcba09)
220220
END_NESTED_COMMIT
221221
222-
END_COMMIT_OVERRIDE
222+
END_COMMIT
223223
224224
This pull request is generated with proto changes between
225225
[googleapis/googleapis@abcdef00](https://github.com/googleapis/googleapis/commit/abcdef0000000000000000000000000000000000)
@@ -297,7 +297,7 @@ Language Image: %s`,
297297
"failed-library-a",
298298
"failed-library-b",
299299
},
300-
want: fmt.Sprintf(`BEGIN_COMMIT_OVERRIDE
300+
want: fmt.Sprintf(`BEGIN_COMMIT
301301
302302
BEGIN_NESTED_COMMIT
303303
fix: a bug fix
@@ -308,7 +308,7 @@ Library-IDs: one-library
308308
Source-link: [googleapis/googleapis@fedcba09](https://github.com/googleapis/googleapis/commit/fedcba09)
309309
END_NESTED_COMMIT
310310
311-
END_COMMIT_OVERRIDE
311+
END_COMMIT
312312
313313
This pull request is generated with proto changes between
314314
[googleapis/googleapis@abcdef00](https://github.com/googleapis/googleapis/commit/abcdef0000000000000000000000000000000000)
@@ -381,7 +381,7 @@ Language Image: %s
381381
"one-library": "1234567890",
382382
},
383383
failedLibraries: []string{},
384-
want: fmt.Sprintf(`BEGIN_COMMIT_OVERRIDE
384+
want: fmt.Sprintf(`BEGIN_COMMIT
385385
386386
BEGIN_NESTED_COMMIT
387387
fix: a bug fix
@@ -401,7 +401,7 @@ Library-IDs: one-library
401401
Source-link: [googleapis/googleapis@12345678](https://github.com/googleapis/googleapis/commit/12345678)
402402
END_NESTED_COMMIT
403403
404-
END_COMMIT_OVERRIDE
404+
END_COMMIT
405405
406406
This pull request is generated with proto changes between
407407
[googleapis/googleapis@12345678](https://github.com/googleapis/googleapis/commit/1234567890000000000000000000000000000000)
@@ -600,14 +600,14 @@ func TestFormatOnboardPRBody(t *testing.T) {
600600
},
601601
api: "path/to",
602602
library: "one-library",
603-
want: fmt.Sprintf(`BEGIN_COMMIT_OVERRIDE
603+
want: fmt.Sprintf(`BEGIN_COMMIT
604604
605605
feat: onboard a new library
606606
607607
PiperOrigin-RevId: 98765
608608
Library-IDs: one-library
609609
610-
END_COMMIT_OVERRIDE
610+
END_COMMIT
611611
612612
Librarian Version: %s
613613
Language Image: %s`,

internal/librarian/release_notes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Language Image: {{.ImageVersion}}
105105

106106
genBodyTemplate = template.Must(template.New("genBody").Funcs(template.FuncMap{
107107
"shortSHA": shortSHA,
108-
}).Parse(`BEGIN_COMMIT_OVERRIDE
108+
}).Parse(`BEGIN_COMMIT
109109
{{ range .Commits }}
110110
BEGIN_NESTED_COMMIT
111111
{{.Type}}: {{.Subject}}
@@ -116,7 +116,7 @@ Library-IDs: {{index .Footers "Library-IDs"}}
116116
Source-link: [googleapis/googleapis@{{shortSHA .CommitHash}}](https://github.com/googleapis/googleapis/commit/{{shortSHA .CommitHash}})
117117
END_NESTED_COMMIT
118118
{{ end }}
119-
END_COMMIT_OVERRIDE
119+
END_COMMIT
120120
121121
This pull request is generated with proto changes between
122122
[googleapis/googleapis@{{shortSHA .StartSHA}}](https://github.com/googleapis/googleapis/commit/{{.StartSHA}})
@@ -136,14 +136,14 @@ Language Image: {{.ImageVersion}}
136136
{{- end }}
137137
`))
138138

139-
onboardingBodyTemplate = template.Must(template.New("onboardingBody").Parse(`BEGIN_COMMIT_OVERRIDE
139+
onboardingBodyTemplate = template.Must(template.New("onboardingBody").Parse(`BEGIN_COMMIT
140140
141141
feat: onboard a new library
142142
143143
PiperOrigin-RevId: {{.PiperID}}
144144
Library-IDs: {{.LibraryID}}
145145
146-
END_COMMIT_OVERRIDE
146+
END_COMMIT
147147
148148
Librarian Version: {{.LibrarianVersion}}
149149
Language Image: {{.ImageVersion}}

0 commit comments

Comments
 (0)