Skip to content

Commit 19ab9b0

Browse files
authored
fix: use empty line as a title-body separator (#2345)
GitHub will insert an empty line between commit title and body, we use this as an indicator of separating title and message. Line breaks are not reliable because GitHub will wrap long lines with line breaks during squash and merge. Created release pull request: #2346 Fixes #2234
1 parent 13962d7 commit 19ab9b0

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

internal/conventionalcommits/conventional_commits.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ func parseSimpleCommit(commitPart commitPart, commit *gitrepo.Commit, libraryID
210210
var commits []*ConventionalCommit
211211
// Hold the subjects of each commit.
212212
var subjects [][]string
213+
// Hold the body of each commit.
214+
var body [][]string
215+
// Whether it has seen an empty line.
216+
var foundSeparator bool
213217
// If the body lines have multiple headers, separate them into different conventional commit, all associated with
214218
// the same commit sha.
215219
for _, bodyLine := range bodyLines {
@@ -221,12 +225,26 @@ func parseSimpleCommit(commitPart commitPart, commit *gitrepo.Commit, libraryID
221225
continue
222226
}
223227

224-
// This might be a multi-line header, append the line to the subject of the last commit.
225-
subjects[len(subjects)-1] = append(subjects[len(subjects)-1], strings.TrimSpace(bodyLine))
228+
bodyLine = strings.TrimSpace(bodyLine)
229+
if bodyLine == "" {
230+
foundSeparator = true
231+
continue
232+
}
233+
234+
if foundSeparator {
235+
// Since we have seen a separator, the rest of the lines are body lines of the commit.
236+
body[len(body)-1] = append(body[len(body)-1], bodyLine)
237+
} else {
238+
// We haven't seen a separator, this line is the continuation of the title.
239+
subjects[len(subjects)-1] = append(subjects[len(subjects)-1], bodyLine)
240+
}
241+
226242
continue
227243
}
228244

229245
subjects = append(subjects, []string{})
246+
body = append(body, []string{})
247+
foundSeparator = false
230248
// If there is an association for the commit (i.e. the commit has '[LIBRARY_ID]' in the
231249
// description), then use that libraryID. Otherwise, use the libraryID passed as the default.
232250
headerLibraryID := header.extractLibraryID()
@@ -248,17 +266,10 @@ func parseSimpleCommit(commitPart commitPart, commit *gitrepo.Commit, libraryID
248266
})
249267
}
250268

251-
if len(commits) == 1 {
252-
// If only one conventional commit is found, i.e., only one header line is
253-
// in the commit message, assign the body field.
254-
commits[0].Body = strings.TrimSpace(strings.Join(bodyLines[1:], "\n"))
255-
} else {
256-
// Otherwise, concatenate all lines as the subject of the corresponding commit.
257-
// This is a workaround when GitHub inserts line breaks in the middle of a long line after squash and merge.
258-
for i, commit := range commits {
259-
sub := fmt.Sprintf("%s %s", commit.Subject, strings.Join(subjects[i], " "))
260-
commit.Subject = strings.TrimSpace(sub)
261-
}
269+
for i, commit := range commits {
270+
sub := fmt.Sprintf("%s %s", commit.Subject, strings.Join(subjects[i], " "))
271+
commit.Subject = strings.TrimSpace(sub)
272+
commit.Body = strings.Join(body[i], "\n")
262273
}
263274

264275
return commits, nil

internal/conventionalcommits/conventional_commits_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func TestParseCommits(t *testing.T) {
121121
},
122122
{
123123
name: "commit_with_multiple_footers_for_generated_changes",
124-
message: "feat: [library-name] add new feature\nThis is the body.\n...\n\nPiperOrigin-RevId: piper_cl_number\n\nSource-Link: [googleapis/googleapis@{source_commit_hash}](https://github.com/googleapis/googleapis/commit/abcdefg1234567)",
124+
message: "feat: [library-name] add new feature\n\nThis is the body.\n...\n\nPiperOrigin-RevId: piper_cl_number\n\nSource-Link: [googleapis/googleapis@{source_commit_hash}](https://github.com/googleapis/googleapis/commit/abcdefg1234567)",
125125
want: []*ConventionalCommit{
126126
{
127127
Type: "feat",
@@ -250,8 +250,9 @@ END_COMMIT_OVERRIDE`,
250250
wantErrPhrase: "empty commit",
251251
},
252252
{
253-
name: "commit with nested commit",
253+
name: "commit_with_nested_commit",
254254
message: `feat(parser): main feature
255+
255256
main commit body
256257
257258
BEGIN_NESTED_COMMIT
@@ -305,7 +306,7 @@ END_NESTED_COMMIT
305306
{
306307
name: "commit_with_empty_nested_commit",
307308
message: `feat(parser): main feature
308-
main commit body
309+
2nd line of title
309310
310311
BEGIN_NESTED_COMMIT
311312
END_NESTED_COMMIT
@@ -314,8 +315,7 @@ END_NESTED_COMMIT
314315
{
315316
Type: "feat",
316317
Scope: "parser",
317-
Subject: "main feature",
318-
Body: "main commit body",
318+
Subject: "main feature 2nd line of title",
319319
LibraryID: "example-id",
320320
IsNested: false,
321321
Footers: map[string]string{},
@@ -338,6 +338,7 @@ Language Image: {language_image_name_and_digest}
338338
BEGIN_COMMIT_OVERRIDE
339339
BEGIN_NESTED_COMMIT
340340
feat: [abc] nested commit 1
341+
341342
body of nested commit 1
342343
...
343344
@@ -347,6 +348,7 @@ Source-Link: fake-link
347348
END_NESTED_COMMIT
348349
BEGIN_NESTED_COMMIT
349350
feat: [abc] nested commit 2
351+
350352
body of nested commit 2
351353
...
352354

internal/librarian/release_notes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ Language Image: {{.ImageVersion}}
7676
### {{.Heading}}
7777
{{ range .Commits }}
7878
{{ if index .Footers "PiperOrigin-RevId" -}}
79-
* {{.Subject}} {{.Body}} (PiperOrigin-RevId: {{index .Footers "PiperOrigin-RevId"}}) ([{{shortSHA .CommitHash}}]({{"https://github.com/"}}{{$noteSection.RepoOwner}}/{{$noteSection.RepoName}}/commit/{{shortSHA .CommitHash}}))
79+
* {{.Subject}} (PiperOrigin-RevId: {{index .Footers "PiperOrigin-RevId"}}) ([{{shortSHA .CommitHash}}]({{"https://github.com/"}}{{$noteSection.RepoOwner}}/{{$noteSection.RepoName}}/commit/{{shortSHA .CommitHash}}))
8080
{{- else -}}
81-
* {{.Subject}} {{.Body}} ([{{shortSHA .CommitHash}}]({{"https://github.com/"}}{{$noteSection.RepoOwner}}/{{$noteSection.RepoName}}/commit/{{shortSHA .CommitHash}}))
81+
* {{.Subject}} ([{{shortSHA .CommitHash}}]({{"https://github.com/"}}{{$noteSection.RepoOwner}}/{{$noteSection.RepoName}}/commit/{{shortSHA .CommitHash}}))
8282
{{- end }}
8383
{{ end }}
8484

internal/librarian/release_notes_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,11 @@ Language Image: go:1.21
529529
530530
### Features
531531
532-
* new feature ([1234567](https://github.com/owner/repo/commit/1234567))
532+
* new feature ([1234567](https://github.com/owner/repo/commit/1234567))
533533
534534
### Bug Fixes
535535
536-
* a bug fix ([fedcba0](https://github.com/owner/repo/commit/fedcba0))
536+
* a bug fix ([fedcba0](https://github.com/owner/repo/commit/fedcba0))
537537
538538
</details>`,
539539
librarianVersion, today),
@@ -579,17 +579,17 @@ Language Image: go:1.21
579579
580580
### Features
581581
582-
* new feature (PiperOrigin-RevId: 123456) ([1234567](https://github.com/owner/repo/commit/1234567))
582+
* new feature (PiperOrigin-RevId: 123456) ([1234567](https://github.com/owner/repo/commit/1234567))
583583
584584
### Bug Fixes
585585
586-
* a bug fix (PiperOrigin-RevId: 987654) ([fedcba0](https://github.com/owner/repo/commit/fedcba0))
586+
* a bug fix (PiperOrigin-RevId: 987654) ([fedcba0](https://github.com/owner/repo/commit/fedcba0))
587587
588588
</details>`,
589589
librarianVersion, today),
590590
},
591591
{
592-
name: "single library with multiple features",
592+
name: "single_library_with_multiple_features",
593593
state: &config.LibrarianState{
594594
Image: "go:1.21",
595595
Libraries: []*config.LibraryState{
@@ -623,9 +623,9 @@ Language Image: go:1.21
623623
624624
### Features
625625
626-
* new feature ([1234567](https://github.com/owner/repo/commit/1234567))
626+
* new feature ([1234567](https://github.com/owner/repo/commit/1234567))
627627
628-
* another new feature ([fedcba0](https://github.com/owner/repo/commit/fedcba0))
628+
* another new feature ([fedcba0](https://github.com/owner/repo/commit/fedcba0))
629629
630630
</details>`,
631631
librarianVersion, today),
@@ -674,7 +674,7 @@ Language Image: go:1.21
674674
675675
### Features
676676
677-
* feature for a ([1234567](https://github.com/owner/repo/commit/1234567))
677+
* feature for a ([1234567](https://github.com/owner/repo/commit/1234567))
678678
679679
</details>
680680
@@ -685,7 +685,7 @@ Language Image: go:1.21
685685
686686
### Bug Fixes
687687
688-
* fix for b ([fedcba0](https://github.com/owner/repo/commit/fedcba0))
688+
* fix for b ([fedcba0](https://github.com/owner/repo/commit/fedcba0))
689689
690690
</details>`,
691691
librarianVersion, today, today),
@@ -725,7 +725,7 @@ Language Image: go:1.21
725725
726726
### Features
727727
728-
* new feature ([1234567](https://github.com/owner/repo/commit/1234567))
728+
* new feature ([1234567](https://github.com/owner/repo/commit/1234567))
729729
730730
</details>`,
731731
librarianVersion, today),
@@ -761,7 +761,7 @@ Language Image: go:1.21
761761
762762
### Features
763763
764-
* new feature this is the body ([1234567](https://github.com/owner/repo/commit/1234567))
764+
* new feature ([1234567](https://github.com/owner/repo/commit/1234567))
765765
766766
</details>`,
767767
librarianVersion, today),

0 commit comments

Comments
 (0)