Skip to content

Commit

Permalink
Fix commit message modes after go-github changes (#193)
Browse files Browse the repository at this point in the history
Passing an empty string as the commit message now selects the default
behavior for the merge mode. Take advantage of this for summarization,
but pass a non-empty whitespace string in the empty body case.
  • Loading branch information
bluekeyes committed Jul 31, 2020
1 parent 88d5aed commit 5be348a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
38 changes: 17 additions & 21 deletions bulldozer/merge.go
Expand Up @@ -311,7 +311,16 @@ func isValidMergeMethod(input MergeMethod) bool {
}

func calculateCommitMessage(ctx context.Context, pullCtx pull.Context, option SquashOptions) (string, error) {
commitMessage := ""
// As of go-github v30, using the empty string as the commit message
// selects the default GitHub behavior for the merge mode. To actually
// clear the message, we use a non-empty string that GitHub will still
// interpret as empty.
const (
defaultMessage = ""
emptyMessage = " "
)

commitMessage := defaultMessage
switch option.Body {
case PullRequestBody:
if option.MessageDelimiter != "" {
Expand All @@ -322,19 +331,19 @@ func calculateCommitMessage(ctx context.Context, pullCtx pull.Context, option Sq
return "", errors.Wrap(err, "failed to compile message delimiter regex")
}

if m := matcher.FindStringSubmatch(pullCtx.Body()); len(m) == 4 {
if m := matcher.FindStringSubmatch(pullCtx.Body()); len(m) == 4 && m[2] != "" {
commitMessage = m[2]
} else {
commitMessage = emptyMessage
}
} else {
commitMessage = pullCtx.Body()
}
case SummarizeCommits:
summarizedMessages, err := summarizeCommitMessages(ctx, pullCtx)
if err != nil {
return "", errors.Wrap(err, "failed to summarize pull request commit messages")
}
commitMessage = summarizedMessages
case EmptyBody:
commitMessage = emptyMessage
case SummarizeCommits:
// Summarizing commits is the default behavior for squash merges
commitMessage = defaultMessage
}

return commitMessage, nil
Expand All @@ -360,16 +369,3 @@ func calculateCommitTitle(ctx context.Context, pullCtx pull.Context, option Squa
}
return title, nil
}

func summarizeCommitMessages(ctx context.Context, pullCtx pull.Context) (string, error) {
commits, err := pullCtx.Commits(ctx)
if err != nil {
return "", err
}

var builder strings.Builder
for _, c := range commits {
fmt.Fprintf(&builder, "* %s\n", c.Message)
}
return builder.String(), nil
}
6 changes: 3 additions & 3 deletions bulldozer/merge_test.go
Expand Up @@ -117,12 +117,12 @@ func TestCalculateCommitMessage(t *testing.T) {
"emptyBody": {
PullContext: defaultPullContext,
Strategy: EmptyBody,
Output: "",
Output: " ",
},
"summarizeCommits": {
PullContext: defaultPullContext,
Strategy: SummarizeCommits,
Output: "* The first commit message!\n* The second commit message!\n* The third commit message!\n",
Output: "",
},
"pullRequestBody": {
PullContext: defaultPullContext,
Expand All @@ -141,7 +141,7 @@ func TestCalculateCommitMessage(t *testing.T) {
PullContext: defaultPullContext,
Strategy: PullRequestBody,
Delimiter: "~~",
Output: "",
Output: " ",
},
}

Expand Down

0 comments on commit 5be348a

Please sign in to comment.