Skip to content

Commit

Permalink
Merge pull request lgtmco#3 from jonbodner/pr_register_status_check
Browse files Browse the repository at this point in the history
Pr register status check
  • Loading branch information
Jon Bodner authored and Jon Bodner committed May 13, 2016
2 parents 5372a1e + 5d8d2aa commit 0d57260
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 4 deletions.
6 changes: 5 additions & 1 deletion approval/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strings"
"fmt"
log "github.com/Sirupsen/logrus"
)

// Func takes in the information needed to figure out which approvers were in the PR comments
Expand All @@ -18,10 +19,13 @@ func Register(name string, f Func) error {
return fmt.Errorf("Approval Algorithm %s is already registered.", name)
}
approvalMap[strings.ToLower(name)] = f
log.Debug("added to approvalMap:",name,f)
return nil
}

func Lookup(name string) (Func, error) {
log.Debug("approvalMap has",approvalMap)
log.Debug("looking for '%s'\n",name)
f, ok := approvalMap[strings.ToLower(name)]
if !ok {
return nil, fmt.Errorf("Unknown Approval Algorithm %s", name)
Expand Down Expand Up @@ -66,4 +70,4 @@ func Simple(config *model.Config, maintainer *model.Maintainer, issue *model.Iss
p(maintainer, comment)
}
}
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/gin-gonic/contrib/ginrus"
"github.com/ianschenck/envflag"
_ "github.com/joho/godotenv/autoload"

_ "github.com/lgtmco/lgtm/approval/org"
)

var (
Expand Down
5 changes: 5 additions & 0 deletions model/status_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ type Branch struct {
BranchStatus string
Mergeable bool
}

type PRHook struct {
Number int
Repo *Repo
}
38 changes: 37 additions & 1 deletion remote/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,41 @@ func (g *Github) GetStatusHook(r *http.Request) (*model.StatusHook, error) {
return hook, nil
}

func (g *Github) GetPRHook(r *http.Request) (*model.PRHook, error) {

// only process comment hooks
if r.Header.Get("X-Github-Event") != "pull_request" {
return nil, nil
}

data := prHook{}
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
return nil, err
}

log.Debug(data)

if data.Action != "opened" {
return nil, nil
}

hook := new(model.PRHook)

hook.Number = data.Number
hook.Repo = new(model.Repo)
hook.Repo.Owner = data.Repository.Owner.Login
hook.Repo.Name = data.Repository.Name
hook.Repo.Slug = data.Repository.FullName

log.Debug(*hook)

return hook, nil
}

func (g *Github) GetPullRequestsForCommit(u *model.User, r *model.Repo, sha *string) ([]model.PullRequest, error) {
client := setupClient(g.API, u.Token)
fmt.Println("sha == ", sha, *sha)
log.Debug("sha == ", sha, *sha)
issues, _, err := client.Search.Issues(fmt.Sprintf("%s&type=pr", *sha), &github.SearchOptions {
TextMatch: false,
})
Expand All @@ -405,6 +437,10 @@ func (g *Github) GetPullRequestsForCommit(u *model.User, r *model.Repo, sha *str
return nil, err
}

log.Debug("current issue ==", v)
log.Debug("current pr ==", *pr)
log.Debug("combined status ==",*status)

out[k] = model.PullRequest{
Issue: model.Issue{
Number: *v.Number,
Expand Down
7 changes: 7 additions & 0 deletions remote/github/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ type statusHook struct {
Repository Repository `json:"repository"`
}

type prHook struct {
Action string `json:"action"`
Number int `json:"number"`

Repository Repository `json:"repository"`
}

type Repository struct {
Name string `json:"name"`
FullName string `json:"full_name"`
Expand Down
2 changes: 1 addition & 1 deletion remote/github/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func DeleteHook(client *github.Client, owner, name, url string) error {
func CreateHook(client *github.Client, owner, name, url string) (*github.Hook, error) {
var hook = new(github.Hook)
hook.Name = github.String("web")
hook.Events = []string{"issue_comment", "status"}
hook.Events = []string{"issue_comment", "status", "pull_request"}
hook.Config = map[string]interface{}{}
hook.Config["url"] = url
hook.Config["content_type"] = "json"
Expand Down
8 changes: 8 additions & 0 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type Remote interface {
// GetStatusHook gets the status hook from the http Request.
GetStatusHook(r *http.Request) (*model.StatusHook, error)

// GetPRHook gets the pull request hook from the http Request.
GetPRHook(r *http.Request) (*model.PRHook, error)

// GetBranchStatus returns overall status for the named branch from the remote system
GetBranchStatus(*model.User, *model.Repo, string) (*model.BranchStatus, error)

Expand Down Expand Up @@ -140,6 +143,11 @@ func GetStatusHook(c context.Context, r *http.Request) (*model.StatusHook, error
return FromContext(c).GetStatusHook(r)
}

// GetPRHook gets the pull request hook from the http Request.
func GetPRHook(c context.Context, r *http.Request) (*model.PRHook, error) {
return FromContext(c).GetPRHook(r)
}

// GetBranchStatus gets the overal status for a branch from the remote repository.
func GetBranchStatus(c context.Context, u *model.User, r *model.Repo, branch string) (*model.BranchStatus, error) {
return FromContext(c).GetBranchStatus(u, r, branch)
Expand Down
24 changes: 23 additions & 1 deletion web/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,34 @@ func Hook(c *gin.Context) {
processStatusHook(c, statusHook)
}

if hook == nil && statusHook == nil {
prHook, err := remote.GetPRHook(c, c.Request)
if prHook != nil {
processPRHook(c, prHook)
}

if err != nil {
log.Errorf("Error parsing pull request hook. %s", err)
c.String(500, "Error parsing pull request hook. %s", err)
return
}
if hook == nil && statusHook == nil && prHook == nil {
c.String(200, "pong")
return
}
}

func processPRHook(c *gin.Context, prHook *model.PRHook) {
repo, user, err := getRepoAndUser(c, prHook.Repo.Slug)
if err != nil {
return
}
remote.SetStatus(c, user, repo, prHook.Number, false)
c.IndentedJSON(200, gin.H{
"number": prHook.Number,
"approved": false,
})
}

func getRepoAndUser(c *gin.Context, slug string) (*model.Repo, *model.User, error){
repo, err := store.GetRepoSlug(c, slug)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions web/status_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func processStatusHook(c *gin.Context, hook *model.StatusHook) {

merged := map[string]StatusResponse{}

log.Debug("calling getPullRequestsForCommit for sha",hook.SHA)
pullRequests, err := remote.GetPullRequestsForCommit(c, user, hook.Repo, &hook.SHA)
log.Debugf("sha for commit is %s, pull requests are: %s", hook.SHA, pullRequests)

Expand Down Expand Up @@ -76,8 +77,10 @@ func processStatusHook(c *gin.Context, hook *model.StatusHook) {

}
if err != nil {
log.Warnf("Unable to generate a version tag: %s",err.Error())
continue
}
log.Debugf("Tagging merge from PR with tag: %s", *verStr)
err = remote.Tag(c, user, repo, verStr, sha)
if err != nil {
log.Warnf("Unable to tag branch %s: %s", v.Title, err)
Expand Down

0 comments on commit 0d57260

Please sign in to comment.