Skip to content

Commit

Permalink
Merge pull request lgtmco#9 from jonbodner/add_optional_comment_support
Browse files Browse the repository at this point in the history
Add optional comment support
  • Loading branch information
Jon Bodner authored and Jon Bodner committed May 19, 2016
2 parents 8afb5a1 + 4004589 commit 5568284
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
11 changes: 6 additions & 5 deletions model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions model/status_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Branch struct {

type PRHook struct {
Number int
Update bool
Repo *Repo
}

Expand Down
14 changes: 14 additions & 0 deletions remote/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
15 changes: 15 additions & 0 deletions web/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,28 @@ 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)
c.String(500, "Error setting status. %s", err)
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,
Expand Down

0 comments on commit 5568284

Please sign in to comment.