Skip to content

Commit 00811c5

Browse files
authored
Fix commit message delimiter parsing (#132)
Also add a test for commit message calculation to avoid similar errors.
1 parent 452e334 commit 00811c5

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

bulldozer/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func calculateCommitMessage(ctx context.Context, pullCtx pull.Context, option Sq
266266
return "", errors.Wrap(err, "failed to compile message delimiter regex")
267267
}
268268

269-
if m := matcher.FindStringSubmatch(commitMessage); len(m) == 4 {
269+
if m := matcher.FindStringSubmatch(pullCtx.Body()); len(m) == 4 {
270270
commitMessage = m[2]
271271
}
272272
} else {

bulldozer/merge_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,65 @@ func TestCalculateCommitTitle(t *testing.T) {
9797
}
9898
}
9999

100+
func TestCalculateCommitMessage(t *testing.T) {
101+
defaultPullContext := &pulltest.MockPullContext{
102+
TitleValue: "This is the PR title!",
103+
BodyValue: "This is the PR body!",
104+
CommitsValue: []*pull.Commit{
105+
{SHA: "f6374a30ec7a3f2dbf35b40ac984b64358ccd246", Message: "The first commit message!"},
106+
{SHA: "89aec3244253260261351047f0bf6d9b7626c4f6", Message: "The second commit message!"},
107+
{SHA: "9907911cde43652c51808f79047c98f0d48ae58f", Message: "The third commit message!"},
108+
},
109+
}
110+
111+
tests := map[string]struct {
112+
PullContext pull.Context
113+
Strategy MessageStrategy
114+
Delimiter string
115+
Output string
116+
}{
117+
"emptyBody": {
118+
PullContext: defaultPullContext,
119+
Strategy: EmptyBody,
120+
Output: "",
121+
},
122+
"summarizeCommits": {
123+
PullContext: defaultPullContext,
124+
Strategy: SummarizeCommits,
125+
Output: "* The first commit message!\n* The second commit message!\n* The third commit message!\n",
126+
},
127+
"pullRequestBody": {
128+
PullContext: defaultPullContext,
129+
Strategy: PullRequestBody,
130+
Output: "This is the PR body!",
131+
},
132+
"pullRequestBodyDelimiter": {
133+
PullContext: &pulltest.MockPullContext{
134+
BodyValue: "Prefix text...\n~~\nThe delimited body\n~~\nSuffix text...",
135+
},
136+
Strategy: PullRequestBody,
137+
Delimiter: "~~",
138+
Output: "The delimited body",
139+
},
140+
"pullRequestBodyMissingDelimiter": {
141+
PullContext: defaultPullContext,
142+
Strategy: PullRequestBody,
143+
Delimiter: "~~",
144+
Output: "",
145+
},
146+
}
147+
148+
ctx := context.Background()
149+
150+
for name, test := range tests {
151+
t.Run(name, func(t *testing.T) {
152+
output, err := calculateCommitMessage(ctx, test.PullContext, SquashOptions{Body: test.Strategy, MessageDelimiter: test.Delimiter})
153+
require.NoError(t, err)
154+
assert.Equal(t, test.Output, output, "calculated body is incorrect")
155+
})
156+
}
157+
}
158+
100159
func TestPushRestrictionMerger(t *testing.T) {
101160
normal := &MockMerger{}
102161
restricted := &MockMerger{}

0 commit comments

Comments
 (0)