Skip to content

Commit e51490d

Browse files
authored
fix(internal/conventionalcommits): keep first value for repeated footer keys (#2440)
Footer with repeated keys is not supported with current implementation, this fix let the first value be kept instead of last. Clarify in doc comment about this limitation. When onboarding libraries that has generation pr with owlbot and release with Librarian, [commit](googleapis/google-cloud-python@34a7916), the first `Source-Link:` is useful. Alternative is to refactor `Footer` with type `map[string][]string`. It's probably not worth it because the keys we care for Librarian are not expected to repeat. This can be added as enhancement when needed. For #2080
1 parent 60b7d38 commit e51490d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

internal/conventionalcommits/conventional_commits.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type ConventionalCommit struct {
6262
// Scope is the scope of the change.
6363
Scope string `yaml:"-" json:"-"`
6464
// Footers contain metadata (e.g,"BREAKING CHANGE", "Reviewed-by").
65+
// Repeated footer keys not supported, only first value is kept
6566
Footers map[string]string `yaml:"-" json:"-"`
6667
// IsBreaking indicates if the commit introduces a breaking change.
6768
IsBreaking bool `yaml:"-" json:"-"`
@@ -346,6 +347,12 @@ func parseFooters(footerLines []string) (footers map[string]string, isBreaking b
346347
}
347348
// This is a new footer.
348349
key := strings.TrimSpace(footerMatches[1])
350+
if _, ok := footers[key]; ok {
351+
// Key already exists. Invalidate lastKey to prevent any subsequent
352+
// continuation lines from being appended to the wrong footer.
353+
lastKey = ""
354+
continue
355+
}
349356
value := strings.TrimSpace(footerMatches[2])
350357
footers[key] = value
351358
lastKey = key

internal/conventionalcommits/conventional_commits_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,18 @@ func TestParseFooters(t *testing.T) {
721721
"Co-authored-by": "Another Person <[email protected]>",
722722
},
723723
},
724+
{
725+
name: "repeated footer keys, keep first",
726+
footerLines: []string{
727+
"PiperOrigin-RevId: 123456",
728+
"Source-Link: first value",
729+
"Source-Link: second value",
730+
},
731+
wantFooters: map[string]string{
732+
"PiperOrigin-RevId": "123456",
733+
"Source-Link": "first value",
734+
},
735+
},
724736
{
725737
name: "multiline footer",
726738
footerLines: []string{

0 commit comments

Comments
 (0)