Skip to content

Commit

Permalink
chore(internal): Truncate long commit body (#9278)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhshkh committed Jan 22, 2024
1 parent 267df3e commit 0c4483e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 22 deletions.
27 changes: 26 additions & 1 deletion internal/gapicgen/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"github.com/go-git/go-git/v5"
)

const (
maxTruncatedTitleLen = 25
)

// ChangeInfo represents a change and its associated metadata.
type ChangeInfo struct {
Body string
Expand All @@ -35,6 +39,15 @@ type ChangeInfo struct {
// the conventional commit footer pattern. This will allow these changes to be
// parsed into the changelog.
func FormatChanges(changes []*ChangeInfo, onlyGapicChanges bool) string {
formatted := truncateAndFormatChanges(changes, onlyGapicChanges, false)
if len(formatted) > maxChangesLen {
// Retry formatting by truncating
return truncateAndFormatChanges(changes, onlyGapicChanges, true)
}
return formatted
}

func truncateAndFormatChanges(changes []*ChangeInfo, onlyGapicChanges, truncate bool) string {
if len(changes) == 0 {
return ""
}
Expand All @@ -44,14 +57,26 @@ func FormatChanges(changes []*ChangeInfo, onlyGapicChanges bool) string {
if onlyGapicChanges {
continue
}
sb.WriteString(fmt.Sprintf("%s\n", c.Title))
title := c.Title
if truncate && len(title) > maxTruncatedTitleLen {
title = fmt.Sprintf("%v...", title[:maxTruncatedTitleLen])
}
sb.WriteString(fmt.Sprintf("%s\n", title))

// Format the commit body to conventional commit footer standards.
splitBody := strings.Split(c.Body, "\n")
for i := range splitBody {
splitBody[i] = fmt.Sprintf(" %s", splitBody[i])
}
body := strings.Join(splitBody, "\n")

if truncate {
startBody := strings.Index(body, "PiperOrigin-RevId")
if startBody != -1 {
body = fmt.Sprintf(" %s", body[startBody:])
}
}

sb.WriteString(fmt.Sprintf("%s\n\n", body))
}
// If the buffer is empty except for the "Changes:" text return an empty
Expand Down
59 changes: 39 additions & 20 deletions internal/gapicgen/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,59 @@ func TestParseConventionalCommitPkg(t *testing.T) {

func TestFormatChanges(t *testing.T) {
tests := []struct {
name string
changes []*ChangeInfo
onlyGapics bool
want string
name string
changes []*ChangeInfo
onlyGapics bool
maxChangesLen int
want string
}{
{
name: "basic",
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar"}},
want: "\nChanges:\n\nfix: foo\n bar\n\n",
name: "basic",
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar"}},
maxChangesLen: maxChangesLen,
want: "\nChanges:\n\nfix: foo\n bar\n\n",
},
{
name: "breaking change",
changes: []*ChangeInfo{{Title: "feat!: breaking change", Body: "BREAKING CHANGE: The world is breaking."}},
maxChangesLen: maxChangesLen,
want: "\nChanges:\n\nfeat!: breaking change\n BREAKING CHANGE: The world is breaking.\n\n",
},
{
name: "multi-lined body indented",
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar\nbaz"}},
maxChangesLen: maxChangesLen,
want: "\nChanges:\n\nfix: foo\n bar\n baz\n\n",
},
{
name: "breaking change",
changes: []*ChangeInfo{{Title: "feat!: breaking change", Body: "BREAKING CHANGE: The world is breaking."}},
want: "\nChanges:\n\nfeat!: breaking change\n BREAKING CHANGE: The world is breaking.\n\n",
name: "multi-lined body indented, multiple changes",
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar\nbaz"}, {Title: "fix: baz", Body: "foo\nbar"}},
maxChangesLen: maxChangesLen,
want: "\nChanges:\n\nfix: foo\n bar\n baz\n\nfix: baz\n foo\n bar\n\n",
},
{
name: "multi-lined body indented",
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar\nbaz"}},
want: "\nChanges:\n\nfix: foo\n bar\n baz\n\n",
name: "no package, filtered",
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar"}},
onlyGapics: true,
maxChangesLen: maxChangesLen,
want: "",
},
{
name: "multi-lined body indented, multiple changes",
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar\nbaz"}, {Title: "fix: baz", Body: "foo\nbar"}},
want: "\nChanges:\n\nfix: foo\n bar\n baz\n\nfix: baz\n foo\n bar\n\n",
name: "truncate long title",
changes: []*ChangeInfo{{Title: "fix: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod", Body: "tempor incididunt ut\nPiperOrigin-RevId: bar"}},
maxChangesLen: 100,
want: "\nChanges:\n\nfix: Lorem ipsum dolor si...\n PiperOrigin-RevId: bar\n\n",
},
{
name: "no package, filtered",
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar"}},
onlyGapics: true,
want: "",
name: "no truncate short title",
changes: []*ChangeInfo{{Title: "fix: Lorem ipsum dolor", Body: "tempor incididunt ut\n PiperOrigin-RevId: bar"}},
maxChangesLen: 50,
want: "\nChanges:\n\nfix: Lorem ipsum dolor\n PiperOrigin-RevId: bar\n\n",
},
}

for _, tc := range tests {
maxChangesLen = tc.maxChangesLen
t.Run(tc.name, func(t *testing.T) {
if got := FormatChanges(tc.changes, tc.onlyGapics); got != tc.want {
t.Errorf("FormatChanges() = %q, want %q", got, tc.want)
Expand Down
6 changes: 5 additions & 1 deletion internal/gapicgen/git/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ If you have been assigned to review this PR, please:
- Approve and submit this PR if you believe it's ready to ship. That will prompt
genbot to assign reviewers to the google-cloud-go PR.
`
maxCommitBodyLen = 65536
)

var conventionalCommitScopeRe = regexp.MustCompile(`.*\((.*)\): .*`)
var (
conventionalCommitScopeRe = regexp.MustCompile(`.*\((.*)\): .*`)
maxChangesLen = maxCommitBodyLen - len(genprotoCommitBody)
)

// PullRequest represents a GitHub pull request.
type PullRequest struct {
Expand Down

0 comments on commit 0c4483e

Please sign in to comment.