This repository has been archived by the owner on Jan 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
/
hook.go
132 lines (117 loc) · 3.78 KB
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package web
import (
"regexp"
"github.com/lgtmco/lgtm/cache"
"github.com/lgtmco/lgtm/model"
"github.com/lgtmco/lgtm/remote"
"github.com/lgtmco/lgtm/store"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
)
func Hook(c *gin.Context) {
hook, err := remote.GetHook(c, c.Request)
if err != nil {
log.Errorf("Error parsing hook. %s", err)
c.String(500, "Error parsing hook. %s", err)
return
}
if hook == nil {
c.String(200, "pong")
return
}
repo, err := store.GetRepoSlug(c, hook.Repo.Slug)
if err != nil {
log.Errorf("Error getting repository %s. %s", hook.Repo.Slug, err)
c.String(404, "Repository not found.")
return
}
user, err := store.GetUser(c, repo.UserID)
if err != nil {
log.Errorf("Error getting repository owner %s. %s", repo.Slug, err)
c.String(404, "Repository owner not found.")
return
}
rcfile, _ := remote.GetContents(c, user, repo, ".lgtm")
config, err := model.ParseConfig(rcfile)
if err != nil {
log.Errorf("Error parsing .lgtm file for %s. %s", repo.Slug, err)
c.String(500, "Error parsing .lgtm file. %s.", err)
return
}
// THIS IS COMPLETELY DUPLICATED IN THE API SECTION. NOT IDEAL
file, err := remote.GetContents(c, user, repo, "MAINTAINERS")
if err != nil {
log.Debugf("no MAINTAINERS file for %s. Checking for team members.", repo.Slug)
members, merr := cache.GetMembers(c, user, repo.Owner)
if merr != nil {
log.Errorf("Error getting repository %s. %s", repo.Slug, err)
log.Errorf("Error getting org members %s. %s", repo.Owner, merr)
c.String(404, "MAINTAINERS file not found. %s", err)
return
} else {
for _, member := range members {
file = append(file, member.Login...)
file = append(file, '\n')
}
}
}
maintainer, err := model.ParseMaintainer(file)
if err != nil {
log.Errorf("Error parsing MAINTAINERS file for %s. %s", repo.Slug, err)
c.String(500, "Error parsing MAINTAINERS file. %s.", err)
return
}
comments, err := remote.GetComments(c, user, repo, hook.Issue.Number)
if err != nil {
log.Errorf("Error retrieving comments for %s pr %d. %s", repo.Slug, hook.Issue.Number, err)
c.String(500, "Error retrieving comments. %s.", err)
return
}
approvers := getApprovers(config, maintainer, hook.Issue, comments)
approved := len(approvers) >= config.Approvals
err = remote.SetStatus(c, user, repo, hook.Issue.Number, len(approvers), config.Approvals)
if err != nil {
log.Errorf("Error setting status for %s pr %d. %s", repo.Slug, hook.Issue.Number, err)
c.String(500, "Error setting status. %s.", err)
return
}
log.Debugf("processed comment for %s. received %d of %d approvals", repo.Slug, len(approvers), config.Approvals)
c.IndentedJSON(200, gin.H{
"approvers": maintainer.People,
"settings": config,
"approved": approved,
"approved_by": approvers,
})
}
// getApprovers is a helper function that analyzes the list of comments
// and returns the list of approvers.
func getApprovers(config *model.Config, maintainer *model.Maintainer, issue *model.Issue, comments []*model.Comment) []*model.Person {
approverm := map[string]bool{}
approvers := []*model.Person{}
matcher, err := regexp.Compile(config.Pattern)
if err != nil {
// this should never happen
return approvers
}
for _, comment := range comments {
// cannot lgtm your own pull request
if config.SelfApprovalOff && comment.Author == issue.Author {
continue
}
// the user must be a valid maintainer of the project
person, ok := maintainer.People[comment.Author]
if !ok {
continue
}
// the same author can't approve something twice
if _, ok := approverm[comment.Author]; ok {
continue
}
// verify the comment matches the approval pattern
if matcher.MatchString(comment.Body) {
approverm[comment.Author] = true
approvers = append(approvers, person)
}
}
return approvers
}