-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediator.go
164 lines (130 loc) · 4.22 KB
/
mediator.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright (C) 2017 The BlameWarrior Authors.
This file is a part of BlameWarrior service.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package hooks
import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"time"
bw "github.com/blamewarrior/hooks/blamewarrior"
"github.com/blamewarrior/hooks/blamewarrior/collaborators"
"github.com/blamewarrior/hooks/blamewarrior/tokens"
"github.com/blamewarrior/hooks/blamewarrior/web"
gh "github.com/blamewarrior/hooks/github"
)
var SendingError = fmt.Errorf("sending error")
type Mediator interface {
Mediate(event string, payload []byte) (err error)
}
type MediatorService struct {
ConsumerBaseURL string
c *http.Client
payloads Payloads
webClient web.Client
collaboratorsClient collaborators.Client
tokenClient tokens.Client
reviewersClient gh.Reviewers
}
func NewMediatorService(
payloads Payloads, webClient web.Client,
collaboratorsClient collaborators.Client, reviewers gh.Reviewers) *MediatorService {
return &MediatorService{
payloads: payloads,
webClient: webClient,
collaboratorsClient: collaboratorsClient,
reviewersClient: reviewers,
}
}
func (service *MediatorService) Mediate(event string, payload []byte) (err error) {
switch event {
case "pull_request":
err = service.handlePullRequestPayload(payload)
case "member":
err = service.handleMemberPayload(payload)
}
if err != nil {
if err = service.payloads.Save(string(payload)); err != nil {
return err
}
return err
}
return nil
}
func (service *MediatorService) handleMemberPayload(payload []byte) (err error) {
ghMemberHook := new(gh.GithubMemberHook)
if err = json.Unmarshal(payload, &ghMemberHook); err != nil {
return err
}
memberRepositoryName := ghMemberHook.Repository.FullName
member := ghMemberHook.Member
switch ghMemberHook.Action {
case "added":
err = service.collaboratorsClient.AddCollaborator(memberRepositoryName, &member)
case "edited":
err = service.collaboratorsClient.EditCollaborator(memberRepositoryName, &member)
case "deleted":
err = service.collaboratorsClient.DeleteCollaborator(memberRepositoryName, ghMemberHook.Member.Login)
}
return err
}
func (service *MediatorService) handlePullRequestPayload(payload []byte) (err error) {
ghPullRequestHook := new(gh.GithubPullRequestHook)
if err = json.Unmarshal(payload, &ghPullRequestHook); err != nil {
return err
}
hookRepositoryName := ghPullRequestHook.Repository.FullName
pullRequest := bw.NewPullRequestFromGithubHook(ghPullRequestHook)
if len(pullRequest.Reviewers) == 0 {
listCollaborators, err := service.collaboratorsClient.ListCollaborator(hookRepositoryName)
reviewer := service.pickCollaboratorFrom(listCollaborators)
reviewers := []gh.Collaborator{*reviewer}
pullRequest.Reviewers = reviewers
if err = service.reviewersClient.RequestReviewers(gh.Context{},
hookRepositoryName,
pullRequest.Number,
reviewers,
); err != nil {
return err
}
}
if pullRequest.State != "open" {
comments, err := service.reviewersClient.ReviewComments(gh.Context{},
hookRepositoryName,
pullRequest.Number,
)
if err != nil {
return err
}
pullRequest.ReviewComments = comments
}
err = service.webClient.ProcessPullRequest(pullRequest)
if err != nil {
return err
}
return nil
}
func (service *MediatorService) pickCollaboratorFrom(collaborators []gh.Collaborator) *gh.Collaborator {
admins := make([]gh.Collaborator, 0)
for _, collaborator := range collaborators {
if collaborator.Admin {
admins = append(admins, collaborator)
}
}
rand.Seed(time.Now().Unix())
n := rand.Int() % len(admins)
return &admins[n]
return nil
}