This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
/
service.go
325 lines (265 loc) · 10.3 KB
/
service.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package repohook
import (
"encoding/json"
"fmt"
"strings"
"github.com/golangci/golangci-api/internal/shared/config"
uuid "github.com/satori/go.uuid"
"github.com/golangci/golangci-api/pkg/api/policy"
"github.com/golangci/golangci-api/pkg/worker/analyze/analyzesqueue/pullanalyzesqueue"
"github.com/golangci/golangci-api/internal/api/apierrors"
"github.com/golangci/golangci-api/internal/shared/logutil"
"github.com/golangci/golangci-api/internal/shared/providers"
"github.com/golangci/golangci-api/internal/shared/providers/provider"
"github.com/golangci/golangci-api/pkg/api/models"
"github.com/golangci/golangci-api/pkg/api/request"
"github.com/golangci/golangci-api/pkg/api/workers/primaryqueue/repoanalyzes"
"github.com/golangci/golangci-api/pkg/worker/lib/github"
gh "github.com/google/go-github/github"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
type GithubWebhook struct {
request.ShortRepo
HookID string `request:",urlPart,"`
EventType string `request:"X-GitHub-Event,header,"`
DeliveryGUID string `request:"X-GitHub-Delivery,header,"`
}
func (w GithubWebhook) FillLogContext(lctx logutil.Context) {
w.ShortRepo.FillLogContext(lctx)
lctx["event_type"] = w.EventType
lctx["delivery_guid"] = w.DeliveryGUID
}
type Service interface {
//url:/v1/repos/{owner}/{name}/hooks/{hookid} method:POST
HandleGithubWebhook(rc *request.AnonymousContext, reqRepo *GithubWebhook, body request.Body) error
}
var errSkipWehbook = errors.New("skip webhook")
type BasicService struct {
ProviderFactory providers.Factory
AnalysisLauncherQueue *repoanalyzes.LauncherProducer
PullAnalyzeQueue *pullanalyzesqueue.Producer
ActiveSubPolicy *policy.ActiveSubscription
Cfg config.Config
}
//nolint:gocyclo
func (s BasicService) HandleGithubWebhook(rc *request.AnonymousContext, req *GithubWebhook, body request.Body) error {
eventType := req.EventType
if eventType == "ping" {
rc.Log.Infof("Got ping webhook")
return nil
}
if req.DeliveryGUID == "" {
req.DeliveryGUID = uuid.NewV4().String()
rc.Log.Warnf("Got no delivery guid in github webhook, generated it")
}
if s.Cfg.GetBool("SERVICE_IS_DISABLED", false) {
return nil
}
var repo models.Repo
err := models.NewRepoQuerySet(rc.DB).HookIDEq(req.HookID).One(&repo)
if err != nil {
if err == gorm.ErrRecordNotFound {
return errors.Wrapf(apierrors.ErrNotFound, "no repo for hook id %s", req.HookID)
}
return errors.Wrapf(err, "can't get repo with hook id %q", req.HookID)
}
// req's owner and name are ignored
switch eventType {
case "pull_request":
if err := s.handleGithubPullRequestWebhook(rc, &repo, req, body); err != nil {
if errors.Cause(err) == errSkipWehbook {
return nil
}
return errors.Wrapf(err, "failed to handle github %s webhook", eventType)
}
return nil
case "push":
if err := s.handleGithubPushWebhook(rc, &repo, req, body); err != nil {
if errors.Cause(err) == errSkipWehbook {
return nil
}
return errors.Wrapf(err, "failed to handle github %s webhook", eventType)
}
return nil
}
return fmt.Errorf("got unknown github webhook event type %s, body: %s", eventType, string(body))
}
//nolint:gocyclo
func (s BasicService) handleGithubPullRequestWebhook(rc *request.AnonymousContext, repo *models.Repo,
req *GithubWebhook, body request.Body) error {
skipRepos := s.Cfg.GetStringList("PULL_REQUEST_WEBHOOK_SKIP_REPOS")
for _, sr := range skipRepos {
if strings.EqualFold(sr, repo.FullName) {
rc.Log.Infof("Skip webhook for repo %s because it's in ignore list", repo.FullName)
return errSkipWehbook
}
}
var auth models.Auth
if err := models.NewAuthQuerySet(rc.DB).UserIDEq(repo.UserID).One(&auth); err != nil {
return errors.Wrapf(err, "failed to get auth for repo %d", repo.ID)
}
p, err := s.ProviderFactory.Build(&auth)
if err != nil {
return errors.Wrapf(err, "failed to build provider for auth %d", auth.ID)
}
ev, err := p.ParsePullRequestEvent(rc.Ctx, body)
if err != nil {
if errors.Cause(err) == provider.ErrUnauthorized {
rc.Log.Infof("Skip webhook for repo %s: %s", repo.FullName, err)
return errSkipWehbook
}
return errors.Wrap(err, "failed to parse pull request event")
}
rc.Log.Infof("Got repo %s github pull request #%d webhook", repo.String(), ev.PullRequestNumber)
if ev.Action != provider.Opened && ev.Action != provider.Synchronized {
rc.Log.Infof("Got github webhook with action %s, skip it", ev.Action)
return errSkipWehbook
}
setCommitStatus := func(state github.Status, desc string) error {
commitErr := p.SetCommitStatus(rc.Ctx, repo.Owner(), repo.Repo(), ev.Head.CommitSHA, &provider.CommitStatus{
State: string(state),
Description: desc,
Context: s.Cfg.GetString("APP_NAME"),
})
if commitErr != nil {
if commitErr == provider.ErrUnauthorized || commitErr == provider.ErrNotFound {
rc.Log.Infof("Can't set github commit status to '%s' for repo %s and commit %s, skipping webhook: %s",
desc, repo.FullNameWithProvider(), ev.Head.CommitSHA, commitErr)
return errSkipWehbook
}
return errors.Wrap(commitErr, "failed to set commit status")
}
return nil
}
accessToken := auth.AccessToken
if ev.Repo.IsPrivate {
if err = s.ActiveSubPolicy.CheckForProviderPullRequestEvent(rc.Ctx, p, ev); err != nil {
logger := s.getNoSubWarnLogger(rc, repo)
if errors.Cause(err) == policy.ErrNoActiveSubscription {
logger("Got PR to %s with no active subscription, skip it and set commit status: %s", repo.FullName, err)
return setCommitStatus(github.StatusError, "No active paid subscription for the private repo")
}
if errors.Cause(err) == policy.ErrNoSeatInSubscription {
logger("Got PR to %s without matched private seat, skip it and set commit status: %s", repo.FullName, err)
return setCommitStatus(github.StatusError, "Git author's email wasn't configured in GolangCI")
}
return err
}
if auth.PrivateAccessToken == "" {
rc.Log.Errorf("Got PR to %s with no user private access token", repo.FullName)
return setCommitStatus(github.StatusError, "No private repos access token")
}
accessToken = auth.PrivateAccessToken
rc.Log.Infof("Got PR webhook to the private repo %s", repo.String())
}
// TODO: create pr analysis only if could set commit status
analysis, err := s.createGithubPullRequestAnalysis(rc, ev, repo, req)
if err != nil {
return err
}
err = setCommitStatus(github.StatusPending, "Waiting in queue...")
if err != nil {
return err
}
githubCtx := github.Context{
Repo: github.Repo{
Owner: repo.Owner(),
Name: repo.Repo(),
IsPrivate: ev.Repo.IsPrivate, // TODO: sync ev.Repo with models.Repo
},
GithubAccessToken: accessToken,
PullRequestNumber: analysis.PullRequestNumber,
}
msg := pullanalyzesqueue.RunMessage{
Context: githubCtx,
UserID: repo.UserID,
AnalysisGUID: analysis.GithubDeliveryGUID,
CommitSHA: analysis.CommitSHA,
}
if err = s.PullAnalyzeQueue.Put(&msg); err != nil {
return errors.Wrap(err, "can't send pull request for analysis into queue")
}
rc.Log.Infof("Sent task to pull request analyze queue")
return nil
}
func (s BasicService) createGithubPullRequestAnalysis(rc *request.AnonymousContext, ev *provider.PullRequestEvent,
repo *models.Repo, req *GithubWebhook) (*models.PullRequestAnalysis, error) {
analysis := models.PullRequestAnalysis{
RepoID: repo.ID,
PullRequestNumber: ev.PullRequestNumber,
GithubDeliveryGUID: req.DeliveryGUID,
CommitSHA: ev.Head.CommitSHA,
Status: "sent_to_queue",
ResultJSON: []byte("{}"),
}
if err := analysis.Create(rc.DB); err != nil {
return nil, errors.Wrap(err, "can't create analysis")
}
rc.Log.Infof("Created pr analysis id=%d", analysis.ID)
return &analysis, nil
}
func (s BasicService) checkSubscription(rc *request.AnonymousContext, repo *models.Repo) error {
var auth models.Auth
if err := models.NewAuthQuerySet(rc.DB).UserIDEq(repo.UserID).One(&auth); err != nil {
return errors.Wrapf(err, "failed to get auth for repo %d", repo.ID)
}
p, err := s.ProviderFactory.Build(&auth)
if err != nil {
return errors.Wrapf(err, "failed to build provider for auth %d", auth.ID)
}
pr, err := p.GetRepoByName(rc.Ctx, repo.Owner(), repo.Repo())
if err != nil {
return errors.Wrap(err, "failed to get provider repo by name")
}
return s.ActiveSubPolicy.CheckForProviderRepo(p, pr)
}
func (s BasicService) getNoSubWarnLogger(rc *request.AnonymousContext, repo *models.Repo) logutil.Func {
ignoredRepos := s.Cfg.GetStringList("KNOWN_PRIVATE_REPOS_WO_SUB")
for _, ignoredRepo := range ignoredRepos {
if strings.EqualFold(ignoredRepo, repo.FullName) {
return rc.Log.Infof
}
}
return rc.Log.Warnf
}
func (s BasicService) isNoSubError(err error) bool {
causeErr := errors.Cause(err)
return causeErr == policy.ErrNoActiveSubscription || causeErr == policy.ErrNoSeatInSubscription
}
func (s BasicService) handleGithubPushWebhook(rc *request.AnonymousContext, repo *models.Repo, req *GithubWebhook, body request.Body) error {
var payload gh.PushEvent
if err := json.Unmarshal(body, &payload); err != nil {
return errors.Wrapf(apierrors.ErrBadRequest, "invalid payload json: %s", err)
}
if payload.GetRepo().GetDefaultBranch() == "" {
rc.Log.Errorf("Got push webhook without default branch, skip it: %+v, %+v",
payload.GetRepo(), payload)
return errSkipWehbook
}
if payload.GetRepo().GetPrivate() {
if err := s.checkSubscription(rc, repo); err != nil {
if s.isNoSubError(err) {
s.getNoSubWarnLogger(rc, repo)(
"Got push webhook to %s with no active subscription, skip it: %s",
repo.FullName, err)
return errSkipWehbook
}
// TODO: render message about inactive sub and send notification
rc.Log.Warnf("Failed to check subscription for push webhook, retry it: %s", err)
return err
}
rc.Log.Infof("Got push webhook to the private repo %s", repo.String())
}
branch := strings.TrimPrefix(payload.GetRef(), "refs/heads/")
if branch != payload.GetRepo().GetDefaultBranch() {
rc.Log.Infof("Got push webhook for branch %s, but default branch is %s, skip it",
branch, payload.GetRepo().GetDefaultBranch())
return errSkipWehbook
}
// TODO: update default branch if changed
if err := s.AnalysisLauncherQueue.Put(repo.ID, payload.GetHeadCommit().GetID(), req.DeliveryGUID); err != nil {
return errors.Wrap(err, "failed to send to analyzes queue")
}
return nil
}