diff --git a/model/config.go b/model/config.go index afaa4bd..b5f4181 100644 --- a/model/config.go +++ b/model/config.go @@ -11,13 +11,14 @@ type Config struct { Pattern string `json:"pattern" toml:"pattern"` Team string `json:"team" toml:"team"` SelfApprovalOff bool `json:"self_approval_off" toml:"self_approval_off"` - DoMerge bool `json:"do_merge" toml:"do_merge"` - DoVersion bool `json:"do_version" toml:"do_version"` + DoMerge bool `json:"do_merge" toml:"do_merge"` + DoVersion bool `json:"do_version" toml:"do_version"` ApprovalAlg string `json:"approval_algorithm" toml:"approval_algorithm"` VersionAlg string `json:"version_algorithm" toml:"version_algorithm"` - VersionFormat string `json:"version_format" toml:"version_format"` + VersionFormat string `json:"version_format" toml:"version_format"` + DoComment bool `json:"do_comment" toml:"do_comment"` - re *regexp.Regexp + re *regexp.Regexp } // ParseConfig parses a projects .lgtm file @@ -36,7 +37,7 @@ func ParseConfigStr(data string) (*Config, error) { c.Approvals = 2 } if len(c.Pattern) == 0 { - c.Pattern = `(?i)LGTM\s*(\S*)` + c.Pattern = `(?i)^LGTM\s*(\S*)` } if len(c.Team) == 0 { c.Team = "MAINTAINERS" diff --git a/model/status_hook.go b/model/status_hook.go index 731da59..9a2e571 100644 --- a/model/status_hook.go +++ b/model/status_hook.go @@ -18,6 +18,7 @@ type Branch struct { type PRHook struct { Number int + Update bool Repo *Repo } diff --git a/remote/github/github.go b/remote/github/github.go index 4930e18..47bccae 100644 --- a/remote/github/github.go +++ b/remote/github/github.go @@ -429,6 +429,7 @@ func (g *Github) GetPRHook(r *http.Request) (*model.PRHook, error) { hook := new(model.PRHook) hook.Number = data.Number + hook.Update = (data.Action == "synchronize") hook.Repo = new(model.Repo) hook.Repo.Owner = data.Repository.Owner.Login hook.Repo.Name = data.Repository.Name @@ -649,3 +650,16 @@ func (g *Github) Tag(u *model.User, r *model.Repo, version *string, sha *string) return err } + + +func (g *Github) WriteComment(u *model.User, r *model.Repo, num int, message string) error { + client := setupClient(g.API, u.Token) + emsg := "Message from LGTM -- " + message + _, _, err := client.Issues.CreateComment(r.Owner, r.Name, num, &github.IssueComment{ + Body: github.String(emsg), + }) + if err != nil { + return err + } + return nil +} diff --git a/remote/remote.go b/remote/remote.go index cbb7662..d564ef2 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -79,6 +79,9 @@ type Remote interface { // UpdatePRsForCommit sets the commit's status to pending for LGTM if it is already on an open Pull Request UpdatePRsForCommit(u *model.User, r *model.Repo, sha *string) (bool, error) + + // WriteComment puts a new comment from LGTM into the PR + WriteComment(u *model.User, r *model.Repo, num int, message string) error } // GetUser authenticates a user with the remote system. @@ -191,4 +194,8 @@ func GetPullRequestsForCommit(c context.Context, u *model.User, r *model.Repo, s func UpdatePRsForCommit(c context.Context, u *model.User, r *model.Repo, sha *string) (bool, error) { return FromContext(c).UpdatePRsForCommit(u, r, sha) +} + +func WriteComment(c context.Context, u *model.User, r *model.Repo, num int, message string) error { + return FromContext(c).WriteComment(u, r, num, message) } \ No newline at end of file diff --git a/web/hook.go b/web/hook.go index cbf2a6f..be54ca5 100644 --- a/web/hook.go +++ b/web/hook.go @@ -52,6 +52,7 @@ func processPRHook(c *gin.Context, prHook *model.PRHook) { if err != nil { return } + err = remote.SetStatus(c, user, repo, prHook.Number, false) if err != nil { log.Errorf("Error setting status. %s", err) @@ -59,6 +60,20 @@ func processPRHook(c *gin.Context, prHook *model.PRHook) { return } + config, _, err := getConfigAndMaintainers(c, user, repo) + if err != nil { + return + } + + if prHook.Update && config.DoComment { + err = remote.WriteComment(c, user, repo, prHook.Number, "The Pull Request has been updated. No comments before this one will count for approval.") + if err != nil { + log.Errorf("Error writing comment for status. %s", err) + c.String(500, "Error writing comment for status. %s", err) + return + } + } + c.IndentedJSON(200, gin.H{ "number": prHook.Number, "approved": false,