Skip to content

Commit 75f60bc

Browse files
authored
Allow whitespace when finding commit messages (#47)
Allow whitespace after the opening tag and before the closing tag that delimit commit messages in a PR body. Also place the regexp in multi-line mode and use `^` and `$` to match the start and end of lines instead of the literal `\r\n` string.
1 parent a0468b7 commit 75f60bc

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

github/github.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,23 +379,32 @@ func (client *Client) commitMessage(pr *github.PullRequest, mergeMethod string)
379379
}
380380
}
381381

382-
var r *regexp.Regexp
383-
if strings.Contains(pr.GetBody(), "==COMMIT_MSG==") {
384-
r = regexp.MustCompile("(?s:(==COMMIT_MSG==\r\n)(.*)(\r\n==COMMIT_MSG==))")
385-
} else if strings.Contains(pr.GetBody(), "==SQUASH_MSG==") {
386-
r = regexp.MustCompile("(?s:(==SQUASH_MSG==\r\n)(.*)(\r\n==SQUASH_MSG==))")
387-
}
388-
if r != nil {
389-
m := r.FindStringSubmatch(pr.GetBody())
390-
if len(m) == 4 {
391-
commitMessage = m[2]
392-
}
382+
if msg, ok := extractMessageOverride(pr.GetBody()); ok {
383+
commitMessage = msg
393384
}
394385
}
395386

396387
return commitMessage, nil
397388
}
398389

390+
func extractMessageOverride(body string) (msg string, found bool) {
391+
var r *regexp.Regexp
392+
if strings.Contains(body, "==COMMIT_MSG==") {
393+
r = regexp.MustCompile(`(?sm:(==COMMIT_MSG==\s*)^(.*)$(\s*==COMMIT_MSG==))`)
394+
} else if strings.Contains(body, "==SQUASH_MSG==") {
395+
r = regexp.MustCompile(`(?sm:(==SQUASH_MSG==\s*)^(.*)$(\s*==SQUASH_MSG==))`)
396+
}
397+
398+
if r != nil {
399+
m := r.FindStringSubmatch(body)
400+
if len(m) == 4 {
401+
msg = strings.TrimSpace(m[2])
402+
found = true
403+
}
404+
}
405+
return
406+
}
407+
399408
func (client *Client) Merge(pr *github.PullRequest) error {
400409
logger := client.Logger
401410

github/github_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,26 @@ func TestSquashCommitMessage(t *testing.T) {
771771
require.Nil(t, err)
772772
require.Equal(t, "", commitMessage)
773773
}
774+
775+
func TestExtractMessageOverride(t *testing.T) {
776+
_, ok := extractMessageOverride("no override here")
777+
assert.False(t, ok, "found unexpected message override")
778+
779+
_, ok = extractMessageOverride("==COMITT_MSG==\r\nUnclosed message")
780+
assert.False(t, ok, "found unexpected message override")
781+
782+
msg, ok := extractMessageOverride("==COMMIT_MSG==\r\nThe real message\r\n==COMMIT_MSG==")
783+
if assert.True(t, ok, "override was not found") {
784+
assert.Equal(t, "The real message", msg)
785+
}
786+
787+
msg, ok = extractMessageOverride("==COMMIT_MSG== \r\nThe real message\r\n ==COMMIT_MSG==")
788+
if assert.True(t, ok, "override was not found") {
789+
assert.Equal(t, "The real message", msg)
790+
}
791+
792+
msg, ok = extractMessageOverride("==SQUASH_MSG==\nThe real message\n==SQUASH_MSG==")
793+
if assert.True(t, ok, "override was not found") {
794+
assert.Equal(t, "The real message", msg)
795+
}
796+
}

0 commit comments

Comments
 (0)