Skip to content

Commit 0d57260

Browse files
Jon BodnerJon Bodner
authored andcommitted
Merge pull request lgtmco#3 from jonbodner/pr_register_status_check
Pr register status check
2 parents 5372a1e + 5d8d2aa commit 0d57260

File tree

9 files changed

+91
-4
lines changed

9 files changed

+91
-4
lines changed

approval/approval.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"regexp"
66
"strings"
77
"fmt"
8+
log "github.com/Sirupsen/logrus"
89
)
910

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

2426
func Lookup(name string) (Func, error) {
27+
log.Debug("approvalMap has",approvalMap)
28+
log.Debug("looking for '%s'\n",name)
2529
f, ok := approvalMap[strings.ToLower(name)]
2630
if !ok {
2731
return nil, fmt.Errorf("Unknown Approval Algorithm %s", name)
@@ -66,4 +70,4 @@ func Simple(config *model.Config, maintainer *model.Maintainer, issue *model.Iss
6670
p(maintainer, comment)
6771
}
6872
}
69-
}
73+
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/gin-gonic/contrib/ginrus"
1212
"github.com/ianschenck/envflag"
1313
_ "github.com/joho/godotenv/autoload"
14+
15+
_ "github.com/lgtmco/lgtm/approval/org"
1416
)
1517

1618
var (

model/status_hook.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ type Branch struct {
1515
BranchStatus string
1616
Mergeable bool
1717
}
18+
19+
type PRHook struct {
20+
Number int
21+
Repo *Repo
22+
}

remote/github/github.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,41 @@ func (g *Github) GetStatusHook(r *http.Request) (*model.StatusHook, error) {
379379
return hook, nil
380380
}
381381

382+
func (g *Github) GetPRHook(r *http.Request) (*model.PRHook, error) {
383+
384+
// only process comment hooks
385+
if r.Header.Get("X-Github-Event") != "pull_request" {
386+
return nil, nil
387+
}
388+
389+
data := prHook{}
390+
err := json.NewDecoder(r.Body).Decode(&data)
391+
if err != nil {
392+
return nil, err
393+
}
394+
395+
log.Debug(data)
396+
397+
if data.Action != "opened" {
398+
return nil, nil
399+
}
400+
401+
hook := new(model.PRHook)
402+
403+
hook.Number = data.Number
404+
hook.Repo = new(model.Repo)
405+
hook.Repo.Owner = data.Repository.Owner.Login
406+
hook.Repo.Name = data.Repository.Name
407+
hook.Repo.Slug = data.Repository.FullName
408+
409+
log.Debug(*hook)
410+
411+
return hook, nil
412+
}
413+
382414
func (g *Github) GetPullRequestsForCommit(u *model.User, r *model.Repo, sha *string) ([]model.PullRequest, error) {
383415
client := setupClient(g.API, u.Token)
384-
fmt.Println("sha == ", sha, *sha)
416+
log.Debug("sha == ", sha, *sha)
385417
issues, _, err := client.Search.Issues(fmt.Sprintf("%s&type=pr", *sha), &github.SearchOptions {
386418
TextMatch: false,
387419
})
@@ -405,6 +437,10 @@ func (g *Github) GetPullRequestsForCommit(u *model.User, r *model.Repo, sha *str
405437
return nil, err
406438
}
407439

440+
log.Debug("current issue ==", v)
441+
log.Debug("current pr ==", *pr)
442+
log.Debug("combined status ==",*status)
443+
408444
out[k] = model.PullRequest{
409445
Issue: model.Issue{
410446
Number: *v.Number,

remote/github/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ type statusHook struct {
5656
Repository Repository `json:"repository"`
5757
}
5858

59+
type prHook struct {
60+
Action string `json:"action"`
61+
Number int `json:"number"`
62+
63+
Repository Repository `json:"repository"`
64+
}
65+
5966
type Repository struct {
6067
Name string `json:"name"`
6168
FullName string `json:"full_name"`

remote/github/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func DeleteHook(client *github.Client, owner, name, url string) error {
6464
func CreateHook(client *github.Client, owner, name, url string) (*github.Hook, error) {
6565
var hook = new(github.Hook)
6666
hook.Name = github.String("web")
67-
hook.Events = []string{"issue_comment", "status"}
67+
hook.Events = []string{"issue_comment", "status", "pull_request"}
6868
hook.Config = map[string]interface{}{}
6969
hook.Config["url"] = url
7070
hook.Config["content_type"] = "json"

remote/remote.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type Remote interface {
5353
// GetStatusHook gets the status hook from the http Request.
5454
GetStatusHook(r *http.Request) (*model.StatusHook, error)
5555

56+
// GetPRHook gets the pull request hook from the http Request.
57+
GetPRHook(r *http.Request) (*model.PRHook, error)
58+
5659
// GetBranchStatus returns overall status for the named branch from the remote system
5760
GetBranchStatus(*model.User, *model.Repo, string) (*model.BranchStatus, error)
5861

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

146+
// GetPRHook gets the pull request hook from the http Request.
147+
func GetPRHook(c context.Context, r *http.Request) (*model.PRHook, error) {
148+
return FromContext(c).GetPRHook(r)
149+
}
150+
143151
// GetBranchStatus gets the overal status for a branch from the remote repository.
144152
func GetBranchStatus(c context.Context, u *model.User, r *model.Repo, branch string) (*model.BranchStatus, error) {
145153
return FromContext(c).GetBranchStatus(u, r, branch)

web/hook.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,34 @@ func Hook(c *gin.Context) {
3030
processStatusHook(c, statusHook)
3131
}
3232

33-
if hook == nil && statusHook == nil {
33+
prHook, err := remote.GetPRHook(c, c.Request)
34+
if prHook != nil {
35+
processPRHook(c, prHook)
36+
}
37+
38+
if err != nil {
39+
log.Errorf("Error parsing pull request hook. %s", err)
40+
c.String(500, "Error parsing pull request hook. %s", err)
41+
return
42+
}
43+
if hook == nil && statusHook == nil && prHook == nil {
3444
c.String(200, "pong")
3545
return
3646
}
3747
}
3848

49+
func processPRHook(c *gin.Context, prHook *model.PRHook) {
50+
repo, user, err := getRepoAndUser(c, prHook.Repo.Slug)
51+
if err != nil {
52+
return
53+
}
54+
remote.SetStatus(c, user, repo, prHook.Number, false)
55+
c.IndentedJSON(200, gin.H{
56+
"number": prHook.Number,
57+
"approved": false,
58+
})
59+
}
60+
3961
func getRepoAndUser(c *gin.Context, slug string) (*model.Repo, *model.User, error){
4062
repo, err := store.GetRepoSlug(c, slug)
4163
if err != nil {

web/status_hook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func processStatusHook(c *gin.Context, hook *model.StatusHook) {
3535

3636
merged := map[string]StatusResponse{}
3737

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

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

7778
}
7879
if err != nil {
80+
log.Warnf("Unable to generate a version tag: %s",err.Error())
7981
continue
8082
}
83+
log.Debugf("Tagging merge from PR with tag: %s", *verStr)
8184
err = remote.Tag(c, user, repo, verStr, sha)
8285
if err != nil {
8386
log.Warnf("Unable to tag branch %s: %s", v.Title, err)

0 commit comments

Comments
 (0)