Skip to content

Commit

Permalink
Allow whitespace when finding commit messages (#47)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bluekeyes authored Aug 29, 2018
1 parent a0468b7 commit 75f60bc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
31 changes: 20 additions & 11 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,23 +379,32 @@ func (client *Client) commitMessage(pr *github.PullRequest, mergeMethod string)
}
}

var r *regexp.Regexp
if strings.Contains(pr.GetBody(), "==COMMIT_MSG==") {
r = regexp.MustCompile("(?s:(==COMMIT_MSG==\r\n)(.*)(\r\n==COMMIT_MSG==))")
} else if strings.Contains(pr.GetBody(), "==SQUASH_MSG==") {
r = regexp.MustCompile("(?s:(==SQUASH_MSG==\r\n)(.*)(\r\n==SQUASH_MSG==))")
}
if r != nil {
m := r.FindStringSubmatch(pr.GetBody())
if len(m) == 4 {
commitMessage = m[2]
}
if msg, ok := extractMessageOverride(pr.GetBody()); ok {
commitMessage = msg
}
}

return commitMessage, nil
}

func extractMessageOverride(body string) (msg string, found bool) {
var r *regexp.Regexp
if strings.Contains(body, "==COMMIT_MSG==") {
r = regexp.MustCompile(`(?sm:(==COMMIT_MSG==\s*)^(.*)$(\s*==COMMIT_MSG==))`)
} else if strings.Contains(body, "==SQUASH_MSG==") {
r = regexp.MustCompile(`(?sm:(==SQUASH_MSG==\s*)^(.*)$(\s*==SQUASH_MSG==))`)
}

if r != nil {
m := r.FindStringSubmatch(body)
if len(m) == 4 {
msg = strings.TrimSpace(m[2])
found = true
}
}
return
}

func (client *Client) Merge(pr *github.PullRequest) error {
logger := client.Logger

Expand Down
23 changes: 23 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,26 @@ func TestSquashCommitMessage(t *testing.T) {
require.Nil(t, err)
require.Equal(t, "", commitMessage)
}

func TestExtractMessageOverride(t *testing.T) {
_, ok := extractMessageOverride("no override here")
assert.False(t, ok, "found unexpected message override")

_, ok = extractMessageOverride("==COMITT_MSG==\r\nUnclosed message")
assert.False(t, ok, "found unexpected message override")

msg, ok := extractMessageOverride("==COMMIT_MSG==\r\nThe real message\r\n==COMMIT_MSG==")
if assert.True(t, ok, "override was not found") {
assert.Equal(t, "The real message", msg)
}

msg, ok = extractMessageOverride("==COMMIT_MSG== \r\nThe real message\r\n ==COMMIT_MSG==")
if assert.True(t, ok, "override was not found") {
assert.Equal(t, "The real message", msg)
}

msg, ok = extractMessageOverride("==SQUASH_MSG==\nThe real message\n==SQUASH_MSG==")
if assert.True(t, ok, "override was not found") {
assert.Equal(t, "The real message", msg)
}
}

0 comments on commit 75f60bc

Please sign in to comment.