Skip to content

Commit

Permalink
Add option to ignore squashed commit messages (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 authored and ajlake committed Jul 17, 2018
1 parent c80162a commit 71bbd06
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 20 deletions.
69 changes: 49 additions & 20 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ type Client struct {
}

type BulldozerFile struct {
MergeStrategy string `yaml:"strategy" validate:"nonzero"`
DeleteAfterMerge bool `yaml:"deleteAfterMerge" validate:"nonzero"`
Mode string `yaml:"mode" validate:"nonzero"`
UpdateStrategy UpdateStrategy `yaml:"updateStrategy"`
MergeStrategy string `yaml:"strategy" validate:"nonzero"`
DeleteAfterMerge bool `yaml:"deleteAfterMerge" validate:"nonzero"`
Mode string `yaml:"mode" validate:"nonzero"`
UpdateStrategy UpdateStrategy `yaml:"updateStrategy"`
IgnoreSquashedMessages bool `yaml:"ignoreSquashedMessages"`
}

func FromToken(c echo.Context, token string, opts ...Option) *Client {
Expand Down Expand Up @@ -292,6 +293,15 @@ func (client *Client) DeleteFlag(branch *github.PullRequestBranch) (bool, error)
return bulldozerFile.DeleteAfterMerge, nil
}

func (client *Client) IgnoreSquashedMessages(branch *github.PullRequestBranch) (bool, error) {
bulldozerFile, err := client.ConfigFile(branch.Repo, branch.GetRef())
if err != nil {
return false, err
}

return bulldozerFile.IgnoreSquashedMessages, nil
}

func (client *Client) OperationMode(branch *github.PullRequestBranch) (string, error) {
cfgFile, err := client.ConfigFile(branch.Repo, branch.GetRef())
if err != nil {
Expand Down Expand Up @@ -347,26 +357,26 @@ func (client *Client) CommitMessages(pr *github.PullRequest) ([]string, error) {
return commitMessages, nil
}

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

repo := pr.Base.Repo
owner := repo.Owner.GetLogin()
name := repo.GetName()

func (client *Client) commitMessage(pr *github.PullRequest, mergeMethod string) (string, error) {
commitMessage := ""
mergeMethod, err := client.MergeMethod(pr.Base)
if err != nil {
return errors.Wrapf(err, "cannot get merge method for %s on ref %s", repo.GetFullName(), pr.Base.GetRef())
}

if mergeMethod == SquashMethod {
messages, err := client.CommitMessages(pr)
repo := pr.Base.Repo
ignoreSquashedMessages, err := client.IgnoreSquashedMessages(pr.Base)
if err != nil {
return err
return "", errors.Wrapf(err,
"cannot get ignore squash messages flag for %s on ref %s",
repo.GetFullName(),
pr.Base.GetRef())
}
for _, message := range messages {
commitMessage = fmt.Sprintf("%s%s\n", commitMessage, message)

if !ignoreSquashedMessages {
messages, err := client.CommitMessages(pr)
if err != nil {
return "", err
}
for _, message := range messages {
commitMessage = fmt.Sprintf("%s%s\n", commitMessage, message)
}
}

var r *regexp.Regexp
Expand All @@ -383,6 +393,25 @@ func (client *Client) Merge(pr *github.PullRequest) error {
}
}

return commitMessage, nil
}

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

repo := pr.Base.Repo
owner := repo.Owner.GetLogin()
name := repo.GetName()

mergeMethod, err := client.MergeMethod(pr.Base)
if err != nil {
return errors.Wrapf(err, "cannot get merge method for %s on ref %s", repo.GetFullName(), pr.Base.GetRef())
}
commitMessage, err := client.commitMessage(pr, mergeMethod)
if err != nil {
return err
}

delete, err := client.DeleteFlag(pr.Base)
if err != nil {
return errors.Wrapf(err, "cannot get delete flag for %s on ref %s", repo.GetFullName(), pr.Base.GetRef())
Expand Down
61 changes: 61 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,64 @@ func TestCommitMessage(t *testing.T) {
"* 1st commit msg", "* 2nd commit msg", "* 3rd commit msg",
}, commitMessages)
}

func TestSquashCommitMessage(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/repos/o/r/pulls/1/commits", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `
[
{
"commit": {
"message": "1st commit msg"
}
},
{
"commit": {
"message": "2nd commit msg"
}
},
{
"commit": {
"message": "3rd commit msg"
}
}
]
`)
})

mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"allow_merge_commit": true,
"allow_squash_merge": true,
"allow_rebase_merge": true
}`)
})

mux.HandleFunc("/repos/o/r/contents/.bulldozer.yml", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"type": "file",
"encoding": "base64",
"content": "bW9kZTogd2hpdGVsaXN0CnN0cmF0ZWd5OiBzcXVhc2gKZGVsZXRlQWZ0ZXJNZXJnZTogdHJ1ZQppZ25vcmVTcXVhc2hlZE1lc3NhZ2VzOiB0cnVlCg==",
"name": ".bulldozer.yml",
"path": ".bulldozer.yml"
}`)
})

branch := &github.PullRequestBranch{
Ref: github.String("develop"),
Repo: fakeRepository("r"),
}
mergeMethod, err := client.MergeMethod(branch)
require.Nil(t, err)
assert.Equal(t, SquashMethod, mergeMethod)

pr := fakePullRequest(1)
commitMessage, err := client.commitMessage(pr, mergeMethod)
require.Nil(t, err)
require.Equal(t, "", commitMessage)
}

0 comments on commit 71bbd06

Please sign in to comment.