Skip to content
This repository has been archived by the owner on Jan 26, 2018. It is now read-only.

Send status on PR open #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pipeline:

docker:
repo: lgtm/lgtm
tag: [latest, 1.0.0]
tag: [latest, 1.1.0]
when:
branch: master
event: push
Expand Down
55 changes: 55 additions & 0 deletions remote/github/comment_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package github

import "github.com/lgtmco/lgtm/model"

// commentHook represents a subset of the issue_comment payload.
type commentHook struct {
Issue struct {
Link string `json:"html_url"`
Number int `json:"number"`
User struct {
Login string `json:"login"`
} `json:"user"`

PullRequest struct {
Link string `json:"html_url"`
} `json:"pull_request"`
} `json:"issue"`

Comment struct {
Body string `json:"body"`
User struct {
Login string `json:"login"`
} `json:"user"`
} `json:"comment"`

Repository struct {
Name string `json:"name"`
FullName string `json:"full_name"`
Desc string `json:"description"`
Private bool `json:"private"`
Owner struct {
Login string `json:"login"`
Type string `json:"type"`
Avatar string `json:"avatar_url"`
} `json:"owner"`
} `json:"repository"`
}

func (ch *commentHook) toHook() *model.Hook {
return &model.Hook{
Issue: &model.Issue{
Number: ch.Issue.Number,
Author: ch.Issue.User.Login,
},
Repo: &model.Repo{
Owner: ch.Repository.Owner.Login,
Name: ch.Repository.Name,
Slug: ch.Repository.FullName,
},
Comment: &model.Comment{
Body: ch.Comment.Body,
Author: ch.Comment.User.Login,
},
}
}
41 changes: 22 additions & 19 deletions remote/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,33 +301,36 @@ func (g *Github) SetStatus(u *model.User, r *model.Repo, num, granted, required
}

func (g *Github) GetHook(r *http.Request) (*model.Hook, error) {

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

func getCommentHook(r *http.Request) (*model.Hook, error) {
data := commentHook{}
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
return nil, err
}

if len(data.Issue.PullRequest.Link) == 0 {
return nil, nil
}
return data.toHook(), nil
}

hook := new(model.Hook)
hook.Issue = new(model.Issue)
hook.Issue.Number = data.Issue.Number
hook.Issue.Author = data.Issue.User.Login
hook.Repo = new(model.Repo)
hook.Repo.Owner = data.Repository.Owner.Login
hook.Repo.Name = data.Repository.Name
hook.Repo.Slug = data.Repository.FullName
hook.Comment = new(model.Comment)
hook.Comment.Body = data.Comment.Body
hook.Comment.Author = data.Comment.User.Login

return hook, nil
func getPRHook(r *http.Request) (*model.Hook, error) {
data := pullRequestHook{}
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
return nil, err
}
if !data.analysable() {
return nil, nil
}
return data.toHook(), nil
}
42 changes: 42 additions & 0 deletions remote/github/pull_request_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package github

import "github.com/lgtmco/lgtm/model"

// pullRequestHook represents a subset of the pull_request payload.
type pullRequestHook struct {
Action string `json:"action"`
Number int `json:"number"`

PullRequest struct {
User struct {
Login string `json:"login"`
} `json:"user"`
} `json:"pull_request"`

Repository struct {
Name string `json:"name"`
FullName string `json:"full_name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
} `json:"repository"`
}

func (prh *pullRequestHook) toHook() *model.Hook {
return &model.Hook{
Issue: &model.Issue{
Number: prh.Number,
Author: prh.PullRequest.User.Login,
},
Repo: &model.Repo{
Owner: prh.Repository.Owner.Login,
Name: prh.Repository.Name,
Slug: prh.Repository.FullName,
},
}
}

func (prh *pullRequestHook) analysable() bool {
return prh.Action == "opened" ||
prh.Action == "synchronize"
}
34 changes: 0 additions & 34 deletions remote/github/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,3 @@ type Branch struct {
} `json:"required_status_checks"`
} `json:"protection"`
}

// commentHook represents a subset of the issue_comment payload.
type commentHook struct {
Issue struct {
Link string `json:"html_url"`
Number int `json:"number"`
User struct {
Login string `json:"login"`
} `json:"user"`

PullRequest struct {
Link string `json:"html_url"`
} `json:"pull_request"`
} `json:"issue"`

Comment struct {
Body string `json:"body"`
User struct {
Login string `json:"login"`
} `json:"user"`
} `json:"comment"`

Repository struct {
Name string `json:"name"`
FullName string `json:"full_name"`
Desc string `json:"description"`
Private bool `json:"private"`
Owner struct {
Login string `json:"login"`
Type string `json:"type"`
Avatar string `json:"avatar_url"`
} `json:"owner"`
} `json:"repository"`
}
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"}
hook.Events = []string{"issue_comment", "pull_request"}
hook.Config = map[string]interface{}{}
hook.Config["url"] = url
hook.Config["content_type"] = "json"
Expand Down