-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
driver_github.go
664 lines (589 loc) · 18.4 KB
/
driver_github.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package yolosvc
import (
"archive/zip"
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/gogo/protobuf/jsonpb"
//nolint:staticcheck
"berty.tech/yolo/v2/go/pkg/yolopb"
"go.uber.org/zap"
"github.com/google/go-github/v32/github"
"github.com/tevino/abool"
)
type GithubWorkerOpts struct {
Logger *zap.Logger
MaxBuilds int
LoopAfter time.Duration
ClearCache *abool.AtomicBool
Once bool
ReposFilter string
Token string
}
type githubRepoConfig struct {
owner string
repo string
// workflows map[int64]struct{}
}
type githubWorker struct {
logger *zap.Logger
svc *service
opts GithubWorkerOpts
repoConfigs []githubRepoConfig
}
func (worker *githubWorker) parseConfig() (bool, error) {
if worker.opts.ReposFilter == "" {
return false, nil
}
var repoConfigs []githubRepoConfig
for _, repo := range strings.Split(worker.opts.ReposFilter, ",") {
parts := strings.Split(repo, "/")
if len(parts) != 2 {
return false, fmt.Errorf("invalid repo config")
}
repoConfigs = append(repoConfigs, githubRepoConfig{
owner: parts[0],
repo: parts[1],
})
}
worker.repoConfigs = repoConfigs
return true, nil
}
// GitHubWorker goals is to manage the github update routine, it should try to support as much errors as possible by itself
func (svc *service) GitHubWorker(ctx context.Context, opts GithubWorkerOpts) error {
opts.applyDefaults()
worker := githubWorker{
svc: svc,
opts: opts,
logger: opts.Logger.Named("ghub"),
}
shouldRun, err := worker.parseConfig()
if err != nil {
svc.logger.Error("invalid config", zap.Error(err))
return fmt.Errorf("%w", err)
}
if !shouldRun {
return nil
}
// fetch GitHub base objects (the ones that don't change very often).
// this is done only once (for now).
{
batch, err := worker.fetchBaseObjects(ctx)
if err != nil {
worker.logger.Warn("fetch GitHub base", zap.Error(err))
} else {
if err := svc.saveBatch(ctx, batch); err != nil {
worker.logger.Warn("save batch", zap.Error(err))
}
}
}
// FIXME: create an helper that takes a batch and automatically detect missing entities, then fetch them, and finally, add them to the batch
// fetch recent activity in a loop
for iteration := 0; ; iteration++ {
since, err := lastBuildCreatedTime(ctx, svc.store, yolopb.Driver_GitHub)
if err != nil {
svc.logger.Warn("get last github build created time", zap.Error(err))
}
svc.logger.Debug("github: refresh", zap.Int("iteration", iteration), zap.Time("since", since))
// fetch repo activity
for _, repo := range worker.repoConfigs {
// FIXME: support "since"
batch, err := worker.fetchRepoActivity(ctx, repo, iteration, since)
if err != nil {
worker.logger.Warn("fetch", zap.Error(err))
} else {
if err := svc.saveBatch(ctx, batch); err != nil {
worker.logger.Warn("save batch", zap.Error(err))
}
}
}
// FIXME: subscribe to orgs' events
limits, _, err := svc.ghc.RateLimits(ctx)
if err != nil {
worker.logger.Warn("get rate limits", zap.Error(err))
} else {
reset := time.Until(limits.Core.Reset.Time)
remaining := limits.Core.Remaining
worker.logger.Debug("rate limits", zap.Int("remaining", remaining), zap.Duration("reset", reset))
}
// FIXME: if rate limit errors, use the RetryAfter helper
if opts.Once {
return nil
}
select {
case <-ctx.Done():
return nil
case <-time.After(opts.LoopAfter):
}
}
}
func (worker *githubWorker) fetchBaseObjects(ctx context.Context) (*yolopb.Batch, error) {
batch := yolopb.NewBatch()
// list orgs
{
before := time.Now()
orgs, _, err := worker.svc.ghc.Organizations.List(ctx, "", nil)
if err != nil {
return nil, err
}
worker.logger.Debug("github.Organizations.List", zap.Int("total", len(orgs)), zap.Duration("duration", time.Since(before)))
for _, org := range orgs {
batch.Entities = append(batch.Entities, worker.entityFromOrganization(org))
}
// FIXME: list members
}
// list repos
{
before := time.Now()
repos, _, err := worker.svc.ghc.Repositories.List(ctx, "", nil)
if err != nil {
return nil, err
}
worker.logger.Debug("github.Repositories.List", zap.Int("total", len(repos)), zap.Duration("duration", time.Since(before)))
for _, repo := range repos {
batch.Merge(worker.batchFromRepo(repo))
}
// FIXME: list last branches, commits etc for each repo
}
// configure repo configs
{
// FIXME: automatically configure repo configs based on .github/yolo.yml
/*
// fetch workflows
{
opts := &github.ListOptions{}
before := time.Now()
workflows, _, err := svc.ghc.Actions.ListWorkflows(ctx, repo.owner, repo.repo, opts)
if err != nil {
return nil, err
}
logger.Debug("github.Actions.ListWorkflows", zap.Int("total", len(workflows.Workflows)), zap.Duration("duration", time.Since(before)))
fmt.Println(u.PrettyJSON(workflows))
}
*/
}
return batch, nil
}
func getOverrideBuildpb(owner string, repo string, artifactID int64, token string) (*yolopb.MetadataOverride, error) {
override := &yolopb.MetadataOverride{}
// https://github.com/actions/upload-artifact#zipped-artifact-downloads
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s/%s/actions/artifacts/%d/zip", owner, repo, artifactID), nil)
if err != nil {
return nil, fmt.Errorf("fetch yolo.json: %w", err)
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("fetch yolo.json: %w", err)
}
defer res.Body.Close()
zipFile, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
bytesReader := bytes.NewReader(zipFile)
zipReader, err := zip.NewReader(bytesReader, int64(len(zipFile)))
if err != nil {
return nil, err
}
file := zipReader.File[0]
if file != nil && file.Name == "yolo.json" {
unzippedFileBytes, err := readZipFile(file)
if err != nil {
return nil, fmt.Errorf("fetch yolo.json: %w", err)
}
err = jsonpb.Unmarshal(bytes.NewReader(unzippedFileBytes), override)
if err != nil {
return nil, fmt.Errorf("fetch yolo.json: %w", err)
}
return override, nil
}
return nil, fmt.Errorf("fetch yolo.json: missing yolo.json inside artifact")
}
func (worker *githubWorker) fetchRepoActivity(ctx context.Context, repo githubRepoConfig, iteration int, lastFinishedBuild time.Time) (*yolopb.Batch, error) {
batch := yolopb.NewBatch()
// choose how maeny pages to aggregate
var maxPages int
switch {
case iteration == 0: // first run should be fast
maxPages = 1
case iteration == 1: // second run is here to populate a lot more
maxPages = 10
case iteration%10 == 0: // then, every 10 runs, check the 5 first pages to check if we miss some PRs in a flood
maxPages = 5
}
// fetch PRs
{
for page := 0; page < maxPages; page++ {
opts := &github.PullRequestListOptions{
State: "all",
Sort: "updated",
Direction: "desc",
ListOptions: github.ListOptions{
Page: page,
},
}
before := time.Now()
pulls, _, err := worker.svc.ghc.PullRequests.List(ctx, repo.owner, repo.repo, opts)
if err != nil {
return nil, err
}
worker.logger.Debug("github.PullRequests.List", zap.Int("total", len(pulls)), zap.Duration("duration", time.Since(before)))
for _, pull := range pulls {
batch.Merge(worker.batchFromPR(pull))
}
}
}
// fetch workflow runs
var runs []*github.WorkflowRun
{
for page := 0; page < maxPages; page++ {
opts := &github.ListWorkflowRunsOptions{
ListOptions: github.ListOptions{
Page: page,
},
}
before := time.Now()
ret, _, err := worker.svc.ghc.Actions.ListRepositoryWorkflowRuns(ctx, repo.owner, repo.repo, opts)
if err != nil {
return nil, err
}
worker.logger.Debug("github.Actions.ListRepositoryWorkflowRuns",
zap.Int("total", len(ret.WorkflowRuns)),
zap.Duration("duration", time.Since(before)),
zap.String("repo", repo.repo),
zap.Int("page", page),
)
// FIXME: parallelize?
for _, run := range ret.WorkflowRuns {
overridepb, err := worker.getOverridepb(ctx, repo, *run.ID)
if err != nil {
worker.svc.logger.Warn("parsing yolo.json", zap.Error(err))
}
// FIXME: compare updated_at before doing next calls
runs = append(runs, run)
isFork := run.GetHeadRepository().GetOwner().GetLogin() != repo.owner ||
run.GetHeadRepository().GetName() != repo.repo
// fetch PRs associated to this run
if isFork || run.GetHeadBranch() != "master" {
opts := &github.PullRequestListOptions{}
ret, _, err := worker.svc.ghc.PullRequests.ListPullRequestsWithCommit(
ctx,
run.GetHeadRepository().GetOwner().GetLogin(),
run.GetHeadRepository().GetName(),
run.GetHeadSHA(),
opts,
)
if err != nil {
return nil, err
}
batch.Merge(worker.batchFromWorkflowRun(run, ret, overridepb))
} else {
batch.Merge(worker.batchFromWorkflowRun(run, nil, overridepb))
}
}
}
}
// fetch artifacts
// FIXME: try to make a single call that gets artifacts for a repo with link to builds
{
// FIXME: parallelize?
for _, run := range runs {
if run.GetStatus() != "completed" {
continue
}
// example of how to make less requests, but from what I see, the current HTTP client caching is awesome, and even if the logs display a big amount of requests
/*
if _, found := repo.workflows[run.GetWorkflowID()]; !found {
continue
}
{
var updatedDates []time.Time
err := worker.svc.db.
Model(yolopb.Build{}).
Where(yolopb.Build{Driver: yolopb.Driver_GitHub, ID: run.GetHTMLURL()}).
Pluck("updated_at", &updatedDates).
Error
if err != nil {
return nil, err
}
if len(updatedDates) > 0 && updatedDates[0] == run.GetUpdatedAt().Time {
continue
}
}
*/
opts := &github.ListOptions{}
before := time.Now()
ret, _, err := worker.svc.ghc.Actions.ListWorkflowRunArtifacts(ctx, repo.owner, repo.repo, *run.ID, opts)
if err != nil {
return nil, err
}
worker.logger.Debug("github.Actions.ListWorkflowRunArtifacts", zap.Int("total", len(ret.Artifacts)), zap.Duration("duration", time.Since(before)))
for _, artifact := range ret.Artifacts {
batch.Merge(worker.batchFromWorkflowRunArtifact(run, artifact))
}
}
}
// FIXME: subscribe to organization events
return batch, nil
}
func (worker *githubWorker) getOverridepb(ctx context.Context, repo githubRepoConfig, runID int64) (*yolopb.MetadataOverride, error) {
// check for yolo.json
opts := &github.ListOptions{}
retArti, _, err := worker.svc.ghc.Actions.ListWorkflowRunArtifacts(ctx, repo.owner, repo.repo, runID, opts)
if err != nil {
return nil, err
}
for _, arti := range retArti.Artifacts {
if arti.GetName() == "yolo.json" {
return getOverrideBuildpb(repo.owner, repo.repo, *arti.ID, worker.opts.Token)
}
}
return nil, nil
}
func (worker *githubWorker) batchFromWorkflowRunArtifact(run *github.WorkflowRun, artifact *github.Artifact) *yolopb.Batch {
batch := yolopb.NewBatch()
id := fmt.Sprintf("gh_%d", artifact.GetID())
createdAt := artifact.GetCreatedAt().Time
newArtifact := yolopb.Artifact{
ID: id,
CreatedAt: &createdAt,
FileSize: artifact.GetSizeInBytes(),
LocalPath: artifact.GetName(),
DownloadURL: artifact.GetArchiveDownloadURL(),
HasBuildID: run.GetHTMLURL(),
Driver: yolopb.Driver_GitHub,
Kind: artifactKindByPath(artifact.GetName()),
MimeType: mimetypeByPath(artifact.GetName()),
State: yolopb.Artifact_Finished,
}
batch.Artifacts = append(batch.Artifacts, &newArtifact)
return batch
}
func (worker *githubWorker) batchFromWorkflowRun(run *github.WorkflowRun, prs []*github.PullRequest, override *yolopb.MetadataOverride) *yolopb.Batch {
batch := yolopb.NewBatch()
createdAt := run.GetCreatedAt().Time
updatedAt := run.GetUpdatedAt().Time
commitURL := run.GetHeadRepository().GetHTMLURL() + "/commit/" + run.GetHeadSHA()
newBuild := yolopb.Build{
ID: run.GetHTMLURL(),
ShortID: fmt.Sprintf("%d", run.GetID()),
CreatedAt: &createdAt,
UpdatedAt: &updatedAt,
HasRawCommitID: run.GetHeadSHA(),
HasCommitID: run.GetHeadSHA(),
RawBranch: run.GetHeadBranch(),
Branch: run.GetHeadBranch(),
Driver: yolopb.Driver_GitHub,
CommitURL: commitURL,
HasRawProjectID: run.GetRepository().GetHTMLURL(),
HasProjectID: run.GetRepository().GetHTMLURL(),
Message: run.GetHeadCommit().GetMessage(),
}
if len(prs) > 0 {
pr := prs[0] // only take the first one
newBuild.HasMergerequestID = pr.GetHTMLURL()
newBuild.HasRawMergerequestID = pr.GetHTMLURL()
}
if override != nil {
newBuild.ApplyMetadataOverride(override)
}
if run.GetConclusion() != "" {
newBuild.FinishedAt = &updatedAt // maybe we can have a more accurate date?
}
// state
{
var (
event = run.GetEvent()
status = run.GetStatus()
conclusion = run.GetConclusion()
)
switch {
case event == "schedule":
// skip
return batch
case status == "queued":
newBuild.State = yolopb.Build_Scheduled
case status == "in_progress":
newBuild.State = yolopb.Build_Running
case conclusion == "success":
newBuild.State = yolopb.Build_Passed
case conclusion == "failure":
newBuild.State = yolopb.Build_Failed
case conclusion == "cancelled": // nolint:misspell // this is how GitHub spells it
newBuild.State = yolopb.Build_Canceled
case conclusion == "skipped":
newBuild.State = yolopb.Build_Skipped
case conclusion == "timed_out":
newBuild.State = yolopb.Build_Timedout
// case conclusion == "action_required":
// case conclusion == "neutral":
// case conclusion == "state":
default:
newBuild.State = yolopb.Build_UnknownState
worker.logger.Error(
"unsupported event, status, or conclusion",
zap.String("event", event),
zap.String("status", status),
zap.String("conclusion", conclusion),
zap.String("url", run.GetURL()),
)
}
}
guessMissingBuildInfo(&newBuild)
batch.Builds = append(batch.Builds, &newBuild)
return batch
}
func (worker *githubWorker) batchFromPR(pr *github.PullRequest) *yolopb.Batch {
batch := yolopb.NewBatch()
mr := worker.mrFromPR(pr)
batch.MergeRequests = append(batch.MergeRequests, mr)
if mr.HasAuthor != nil {
batch.Entities = append(batch.Entities, mr.HasAuthor)
mr.HasAuthor = nil
}
batch.Entities = append(batch.Entities, mr.HasAssignees...)
batch.Entities = append(batch.Entities, mr.HasReviewers...)
return batch
}
func (worker *githubWorker) mrFromPR(pr *github.PullRequest) *yolopb.MergeRequest {
createdAt := pr.GetCreatedAt()
updatedAt := pr.GetUpdatedAt()
mergedAt := pr.GetMergedAt()
commitURL := pr.GetBase().GetRepo().GetHTMLURL() + "/commit/" + pr.GetBase().GetSHA()
branchURL := ""
mr := yolopb.MergeRequest{
ID: pr.GetHTMLURL(),
ShortID: fmt.Sprintf("%d", pr.GetNumber()),
CreatedAt: &createdAt,
UpdatedAt: &updatedAt,
Title: pr.GetTitle(),
Message: pr.GetBody(),
Driver: yolopb.Driver_GitHub,
Branch: pr.GetHead().GetLabel(),
CommitURL: commitURL,
BranchURL: branchURL,
IsWIP: pr.GetDraft(),
HasAssignees: []*yolopb.Entity{},
HasReviewers: []*yolopb.Entity{},
HasProjectID: pr.GetBase().GetRepo().GetHTMLURL(),
HasCommitID: pr.GetHead().GetSHA(),
// FIXME: labels
// FIXME: reviews
// FIXME: isFromMember vs isFromExternalContributor
}
switch state := *pr.State; state {
case "open":
mr.State = yolopb.MergeRequest_Opened
case "closed":
mr.State = yolopb.MergeRequest_Closed
default:
worker.logger.Warn("unknown PR state", zap.String("state", state))
}
if !mergedAt.IsZero() {
mr.State = yolopb.MergeRequest_Merged
mr.MergedAt = &mergedAt
}
if !mr.IsWIP && strings.HasPrefix(strings.ToLower(mr.Title), "wip") {
mr.IsWIP = true
}
if !mr.IsWIP && strings.HasPrefix(strings.ToLower(mr.Title), "draft") {
mr.IsWIP = true
}
// FIXME: also check if a label is "WIP" or "Work in progress"
if user := pr.GetUser(); user != nil {
entity := worker.entityFromUser(user)
mr.HasAuthor = entity
mr.HasAuthorID = entity.ID
}
for _, user := range pr.Assignees {
entity := worker.entityFromUser(user)
mr.HasAssignees = append(mr.HasAssignees, entity)
}
for _, user := range pr.RequestedReviewers {
entity := worker.entityFromUser(user)
mr.HasReviewers = append(mr.HasReviewers, entity)
}
return &mr
}
func (worker *githubWorker) batchFromRepo(repo *github.Repository) *yolopb.Batch {
batch := yolopb.NewBatch()
project := worker.projectFromRepo(repo)
batch.Projects = append(batch.Projects, project)
if project.HasOwner != nil {
batch.Entities = append(batch.Entities, project.HasOwner)
project.HasOwner = nil
}
return batch
}
func (worker *githubWorker) projectFromRepo(repo *github.Repository) *yolopb.Project {
createdAt := repo.GetCreatedAt().Time
updatedAt := repo.GetUpdatedAt().Time
project := yolopb.Project{
ID: repo.GetHTMLURL(),
Name: repo.GetName(),
CreatedAt: &createdAt,
UpdatedAt: &updatedAt,
Driver: yolopb.Driver_GitHub,
Description: repo.GetDescription(),
// FIXME: more fields
}
if user := repo.GetOwner(); user != nil {
entity := worker.entityFromUser(user)
project.HasOwner = entity
project.HasOwnerID = entity.ID
}
return &project
}
func (worker *githubWorker) entityFromUser(user *github.User) *yolopb.Entity {
entity := yolopb.Entity{
ID: user.GetHTMLURL(),
Name: user.GetLogin(),
AvatarURL: user.GetAvatarURL(),
Driver: yolopb.Driver_GitHub,
}
switch kind := user.GetType(); kind {
case "User":
entity.Kind = yolopb.Entity_User
case "Organization":
entity.Kind = yolopb.Entity_Organization
case "Bot":
entity.Kind = yolopb.Entity_Bot
default:
worker.logger.Warn("unknown owner type", zap.String("kind", kind))
}
return &entity
}
func (worker *githubWorker) entityFromOrganization(org *github.Organization) *yolopb.Entity {
id := org.GetHTMLURL()
if id == "" {
id = "https://github.com/" + org.GetLogin()
}
return &yolopb.Entity{
ID: id,
Name: org.GetLogin(),
AvatarURL: org.GetAvatarURL(),
Description: org.GetDescription(),
Driver: yolopb.Driver_GitHub,
Kind: yolopb.Entity_Organization,
}
}
func (opts *GithubWorkerOpts) applyDefaults() {
if opts.Logger == nil {
opts.Logger = zap.NewNop()
}
if opts.MaxBuilds == 0 {
opts.MaxBuilds = 100
}
if opts.LoopAfter == 0 {
opts.LoopAfter = time.Second * 30
}
if opts.ClearCache == nil {
opts.ClearCache = abool.New()
}
}