forked from jenkins-x/test-infra
/
tide.go
1181 lines (1090 loc) · 35.1 KB
/
tide.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package tide contains a controller for managing a tide pool of PRs. The
// controller will automatically retest PRs in the pool and merge them if they
// pass tests.
package tide
import (
"context"
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
"sync"
"time"
"github.com/shurcooL/githubv4"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/git"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/kube"
"k8s.io/test-infra/prow/pjutil"
"k8s.io/test-infra/prow/tide/blockers"
)
type kubeClient interface {
ListProwJobs(string) ([]kube.ProwJob, error)
CreateProwJob(kube.ProwJob) (kube.ProwJob, error)
}
type githubClient interface {
CreateStatus(string, string, string, github.Status) error
GetCombinedStatus(org, repo, ref string) (*github.CombinedStatus, error)
GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error)
GetRef(string, string, string) (string, error)
Merge(string, string, int, github.MergeDetails) error
Query(context.Context, interface{}, map[string]interface{}) error
}
type contextChecker interface {
// IsOptional tells whether a context is optional.
IsOptional(string) bool
// MissingRequiredContexts tells if required contexts are missing from the list of contexts provided.
MissingRequiredContexts([]string) []string
}
// Controller knows how to sync PRs and PJs.
type Controller struct {
logger *logrus.Entry
ca *config.Agent
ghc githubClient
kc kubeClient
gc *git.Client
sc *statusController
m sync.Mutex
pools []Pool
// changedFiles caches the names of files changed by PRs.
// Cache entries expire if they are not used during a sync loop.
changedFiles *changedFilesAgent
}
// Action represents what actions the controller can take. It will take
// exactly one action each sync.
type Action string
// Constants for various actions the controller might take
const (
Wait Action = "WAIT"
Trigger = "TRIGGER"
TriggerBatch = "TRIGGER_BATCH"
Merge = "MERGE"
MergeBatch = "MERGE_BATCH"
PoolBlocked = "BLOCKED"
)
// Pool represents information about a tide pool. There is one for every
// org/repo/branch combination that has PRs in the pool.
type Pool struct {
Org string
Repo string
Branch string
// PRs with passing tests, pending tests, and missing or failed tests.
// Note that these results are rolled up. If all tests for a PR are passing
// except for one pending, it will be in PendingPRs.
SuccessPRs []PullRequest
PendingPRs []PullRequest
MissingPRs []PullRequest
// Empty if there is no pending batch.
BatchPending []PullRequest
// Which action did we last take, and to what target(s), if any.
Action Action
Target []PullRequest
Blockers []blockers.Blocker
}
// NewController makes a Controller out of the given clients.
func NewController(ghcSync, ghcStatus *github.Client, kc *kube.Client, ca *config.Agent, gc *git.Client, logger *logrus.Entry) *Controller {
if logger == nil {
logger = logrus.NewEntry(logrus.StandardLogger())
}
sc := &statusController{
logger: logger.WithField("controller", "status-update"),
ghc: ghcStatus,
ca: ca,
newPoolPending: make(chan bool, 1),
shutDown: make(chan bool),
}
go sc.run()
return &Controller{
logger: logger.WithField("controller", "sync"),
ghc: ghcSync,
kc: kc,
ca: ca,
gc: gc,
sc: sc,
changedFiles: &changedFilesAgent{
ghc: ghcSync,
nextChangeCache: make(map[changeCacheKey][]string),
},
}
}
// Shutdown signals the statusController to stop working and waits for it to
// finish its last update loop before terminating.
// Controller.Sync() should not be used after this function is called.
func (c *Controller) Shutdown() {
c.sc.shutdown()
}
func prKey(pr *PullRequest) string {
return fmt.Sprintf("%s#%d", string(pr.Repository.NameWithOwner), int(pr.Number))
}
// org/repo#number -> pr
func byRepoAndNumber(prs []PullRequest) map[string]PullRequest {
m := make(map[string]PullRequest)
for _, pr := range prs {
key := prKey(&pr)
m[key] = pr
}
return m
}
// newExpectedContext creates a Context with Expected state.
func newExpectedContext(c string) Context {
return Context{
Context: githubv4.String(c),
State: githubv4.StatusStateExpected,
Description: githubv4.String(""),
}
}
// contextsToStrings converts a list Context to a list of string
func contextsToStrings(contexts []Context) []string {
var names []string
for _, c := range contexts {
names = append(names, string(c.Context))
}
return names
}
// Sync runs one sync iteration.
func (c *Controller) Sync() error {
defer c.changedFiles.prune()
ctx := context.Background()
c.logger.Debug("Building tide pool.")
prs := make(map[string]PullRequest)
for _, q := range c.ca.Config().Tide.Queries {
results, err := search(ctx, c.ghc, c.logger, q.Query())
if err != nil {
return err
}
for _, pr := range results {
prs[prKey(&pr)] = pr
}
}
var pjs []kube.ProwJob
var blocks blockers.Blockers
var err error
if len(prs) > 0 {
pjs, err = c.kc.ListProwJobs(kube.EmptySelector)
if err != nil {
return err
}
if label := c.ca.Config().Tide.BlockerLabel; label != "" {
c.logger.Debugf("Searching for blocking issues (label %q).", label)
orgs, repos := c.ca.Config().Tide.Queries.OrgsAndRepos()
blocks, err = blockers.FindAll(c.ghc, c.logger, label, orgs, repos)
if err != nil {
return err
}
}
}
// Partition PRs into subpools and filter out non-pool PRs.
rawPools, err := c.dividePool(prs, pjs)
if err != nil {
return err
}
filteredPools := c.filterSubpools(rawPools)
// Notify statusController about the new pool.
c.sc.Lock()
c.sc.poolPRs = poolPRMap(filteredPools)
select {
case c.sc.newPoolPending <- true:
default:
}
c.sc.Unlock()
// Load the subpools into a channel for use as a work queue.
sps := make(chan subpool, len(filteredPools))
for _, sp := range filteredPools {
sps <- *sp
}
close(sps)
goroutines := c.ca.Config().Tide.MaxGoroutines
if goroutines > len(sps) {
goroutines = len(sps)
}
wg := &sync.WaitGroup{}
wg.Add(goroutines)
c.logger.Debugf("Firing up %d goroutines", goroutines)
poolChan := make(chan Pool, len(sps))
for i := 0; i < goroutines; i++ {
go func() {
defer wg.Done()
for sp := range sps {
spBlocks := blocks.GetApplicable(sp.org, sp.repo, sp.branch)
if pool, err := c.syncSubpool(sp, spBlocks); err != nil {
sp.log.WithError(err).Errorf("Error syncing subpool.")
} else {
poolChan <- pool
}
}
}()
}
wg.Wait()
close(poolChan)
pools := make([]Pool, 0, len(sps))
for pool := range poolChan {
pools = append(pools, pool)
}
sortPools(pools)
c.m.Lock()
defer c.m.Unlock()
c.pools = pools
return nil
}
func (c *Controller) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.m.Lock()
defer c.m.Unlock()
b, err := json.Marshal(c.pools)
if err != nil {
c.logger.WithError(err).Error("Encoding JSON.")
b = []byte("[]")
}
if _, err = w.Write(b); err != nil {
c.logger.WithError(err).Error("Writing JSON response.")
}
}
// filterSubpools filters non-pool PRs out of the initially identified subpools,
// deleting any pools that become empty.
// See filterSubpool for filtering details.
func (c *Controller) filterSubpools(raw map[string]*subpool) map[string]*subpool {
filtered := make(map[string]*subpool)
for key, sp := range raw {
var err error
// TODO: move initialization of 'presubmitContexts' 'cc' and 'sha' to an 'initPool' func.
sp.presubmitContexts, err = c.presubmitsByPull(sp)
if err != nil {
sp.log.WithError(err).Error("Determining required presubmit prowjobs.")
continue
}
sp.cc, err = c.ca.Config().GetTideContextPolicy(sp.org, sp.repo, sp.branch)
if err != nil {
sp.log.WithError(err).Error("Setting up context checker.")
continue
}
if spFiltered := filterSubpool(c.ghc, sp); spFiltered != nil {
sp.log.WithField("key", key).WithField("pool", spFiltered).Debug("filtered sub-pool")
filtered[key] = spFiltered
} else {
sp.log.WithField("key", key).WithField("pool", spFiltered).Debug("filtering sub-pool removed all PRs")
}
}
return filtered
}
// filterSubpool filters PRs from an initially identified subpool, returning the
// filtered subpool.
// If the subpool becomes empty 'nil' is returned to indicate that the subpool
// should be deleted.
func filterSubpool(ghc githubClient, sp *subpool) *subpool {
var toKeep []PullRequest
for _, pr := range sp.prs {
if !filterPR(ghc, sp, &pr) {
toKeep = append(toKeep, pr)
}
}
if len(toKeep) == 0 {
return nil
}
sp.prs = toKeep
return sp
}
// filterPR indicates if a PR should be filtered out of the subpool.
// Specifically we filter out PRs that:
// - Have known merge conflicts.
// - Have failing or missing status contexts.
// - Have pending required status contexts that are not associated with a
// ProwJob. (This ensures that the 'tide' context indicates that the pending
// status is preventing merge. Required ProwJob statuses are allowed to be
// 'pending' because this prevents kicking PRs from the pool when Tide is
// retesting them.)
func filterPR(ghc githubClient, sp *subpool, pr *PullRequest) bool {
log := sp.log.WithFields(pr.logFields())
// Skip PRs that are known to be unmergeable.
if pr.Mergeable == githubv4.MergeableStateConflicting {
log.Debug("filtering out PR as it is unmergeable")
return true
}
// Filter out PRs with unsuccessful contexts unless the only unsuccessful
// contexts are pending required prowjobs.
contexts, err := headContexts(log, ghc, pr)
if err != nil {
log.WithError(err).Error("Getting head contexts.")
return true
}
pjContexts := sp.presubmitContexts[int(pr.Number)]
for _, ctx := range unsuccessfulContexts(contexts, sp.cc, log) {
if ctx.State != githubv4.StatusStatePending || !pjContexts.Has(string(ctx.Context)) {
log.WithField("context", ctx.Context).Debug("filtering out PR as unsuccessful context is not a pending Prow-controlled context")
return true
}
}
return false
}
// poolPRMap collects all subpool PRs into a map containing all pooled PRs.
func poolPRMap(subpoolMap map[string]*subpool) map[string]PullRequest {
prs := make(map[string]PullRequest)
for _, sp := range subpoolMap {
for _, pr := range sp.prs {
prs[prKey(&pr)] = pr
}
}
return prs
}
type simpleState string
const (
noneState simpleState = "none"
pendingState simpleState = "pending"
successState simpleState = "success"
)
func toSimpleState(s kube.ProwJobState) simpleState {
if s == kube.TriggeredState || s == kube.PendingState {
return pendingState
} else if s == kube.SuccessState {
return successState
}
return noneState
}
// isPassingTests returns whether or not all contexts set on the PR except for
// the tide pool context are passing.
func isPassingTests(log *logrus.Entry, ghc githubClient, pr PullRequest, cc contextChecker) bool {
log = log.WithFields(pr.logFields())
contexts, err := headContexts(log, ghc, &pr)
if err != nil {
log.WithError(err).Error("Getting head commit status contexts.")
// If we can't get the status of the commit, assume that it is failing.
return false
}
unsuccessful := unsuccessfulContexts(contexts, cc, log)
return len(unsuccessful) == 0
}
// unsuccessfulContexts determines which contexts from the list that we care about are
// failed. For instance, we do not care about our own context.
// If the branchProtection is set to only check for required checks, we will skip
// all non-required tests. If required tests are missing from the list, they will be
// added to the list of failed contexts.
func unsuccessfulContexts(contexts []Context, cc contextChecker, log *logrus.Entry) []Context {
var failed []Context
for _, ctx := range contexts {
if string(ctx.Context) == statusContext {
continue
}
if cc.IsOptional(string(ctx.Context)) {
continue
}
if ctx.State != githubv4.StatusStateSuccess {
failed = append(failed, ctx)
}
}
for _, c := range cc.MissingRequiredContexts(contextsToStrings(contexts)) {
failed = append(failed, newExpectedContext(c))
}
log.Debugf("from %d total contexts (%v) found %d failing contexts: %v", len(contexts), contextsToStrings(contexts), len(failed), contextsToStrings(failed))
return failed
}
func pickSmallestPassingNumber(log *logrus.Entry, ghc githubClient, prs []PullRequest, cc contextChecker) (bool, PullRequest) {
smallestNumber := -1
var smallestPR PullRequest
for _, pr := range prs {
if smallestNumber != -1 && int(pr.Number) >= smallestNumber {
continue
}
if len(pr.Commits.Nodes) < 1 {
continue
}
if !isPassingTests(log, ghc, pr, cc) {
continue
}
smallestNumber = int(pr.Number)
smallestPR = pr
}
return smallestNumber > -1, smallestPR
}
// accumulateBatch returns a list of PRs that can be merged after passing batch
// testing, if any exist. It also returns a list of PRs currently being batch
// tested.
func accumulateBatch(presubmits map[int]sets.String, prs []PullRequest, pjs []kube.ProwJob, log *logrus.Entry) ([]PullRequest, []PullRequest) {
log.Debug("accumulating PRs for batch testing")
if len(presubmits) == 0 {
log.Debug("no presubmits configured, no batch can be triggered")
return nil, nil
}
prNums := make(map[int]PullRequest)
for _, pr := range prs {
prNums[int(pr.Number)] = pr
}
type accState struct {
prs []PullRequest
jobStates map[string]simpleState
// Are the pull requests in the ref still acceptable? That is, do they
// still point to the heads of the PRs?
validPulls bool
}
states := make(map[string]*accState)
for _, pj := range pjs {
if pj.Spec.Type != kube.BatchJob {
continue
}
// If any batch job is pending, return now.
if toSimpleState(pj.Status.State) == pendingState {
var pending []PullRequest
var pendingNums []int
for _, pull := range pj.Spec.Refs.Pulls {
pending = append(pending, prNums[pull.Number])
pendingNums = append(pendingNums, pull.Number)
}
log.Debugf("no new batch necessary, current batch pending: %v", pendingNums)
return nil, pending
}
// Otherwise, accumulate results.
ref := pj.Spec.Refs.String()
if _, ok := states[ref]; !ok {
states[ref] = &accState{
jobStates: make(map[string]simpleState),
validPulls: true,
}
for _, pull := range pj.Spec.Refs.Pulls {
if pr, ok := prNums[pull.Number]; ok && string(pr.HeadRefOID) == pull.SHA {
states[ref].prs = append(states[ref].prs, pr)
} else if !ok {
states[ref].validPulls = false
log.WithField("batch", ref).WithFields(pr.logFields()).Debug("batch invalid, PR left pool")
break
} else {
states[ref].validPulls = false
log.WithField("batch", ref).WithFields(pr.logFields()).Debug("batch invalid, PR HEAD changed")
break
}
}
}
if !states[ref].validPulls {
// The batch contains a PR ref that has changed. Skip it.
continue
}
context := pj.Spec.Context
if s, ok := states[ref].jobStates[context]; !ok || s == noneState {
states[ref].jobStates[context] = toSimpleState(pj.Status.State)
}
}
for ref, state := range states {
if !state.validPulls {
continue
}
requiredPresubmits := sets.NewString()
for _, pr := range state.prs {
requiredPresubmits = requiredPresubmits.Union(presubmits[int(pr.Number)])
}
passesAll := true
for _, p := range requiredPresubmits.List() {
if s, ok := state.jobStates[p]; !ok || s != successState {
passesAll = false
log.WithField("batch", ref).Debug("batch invalid, required presubmit %s not passing", p)
break
}
}
if !passesAll {
continue
}
return state.prs, nil
}
return nil, nil
}
// accumulate returns the supplied PRs sorted into three buckets based on their
// accumulated state across the presubmits.
func accumulate(presubmits map[int]sets.String, prs []PullRequest, pjs []kube.ProwJob, log *logrus.Entry) (successes, pendings, nones []PullRequest) {
for _, pr := range prs {
// Accumulate the best result for each job.
psStates := make(map[string]simpleState)
for _, pj := range pjs {
if pj.Spec.Type != kube.PresubmitJob {
continue
}
if pj.Spec.Refs.Pulls[0].Number != int(pr.Number) {
continue
}
if pj.Spec.Refs.Pulls[0].SHA != string(pr.HeadRefOID) {
continue
}
name := pj.Spec.Context
oldState := psStates[name]
newState := toSimpleState(pj.Status.State)
if oldState == noneState || oldState == "" {
psStates[name] = newState
} else if oldState == pendingState && newState == successState {
psStates[name] = successState
}
}
// The overall result is the worst of the best.
overallState := successState
for _, ps := range presubmits[int(pr.Number)].List() {
if s, ok := psStates[ps]; !ok {
overallState = noneState
log.WithFields(pr.logFields()).Debugf("missing presubmit %s", ps)
break
} else if s == noneState {
overallState = noneState
log.WithFields(pr.logFields()).Debugf("presubmit %s not passing", ps)
break
} else if s == pendingState {
log.WithFields(pr.logFields()).Debugf("presubmit %s pending", ps)
overallState = pendingState
}
}
if overallState == successState {
successes = append(successes, pr)
} else if overallState == pendingState {
pendings = append(pendings, pr)
} else {
nones = append(nones, pr)
}
}
return
}
func prNumbers(prs []PullRequest) []int {
var nums []int
for _, pr := range prs {
nums = append(nums, int(pr.Number))
}
return nums
}
func (c *Controller) pickBatch(sp subpool, cc contextChecker) ([]PullRequest, error) {
// we must choose the oldest PRs for the batch
sort.Slice(sp.prs, func(i, j int) bool { return sp.prs[i].Number < sp.prs[j].Number })
var candidates []PullRequest
for _, pr := range sp.prs {
if isPassingTests(sp.log, c.ghc, pr, cc) {
candidates = append(candidates, pr)
}
}
if len(candidates) == 0 {
sp.log.Debugf("of %d possible PRs, none were passing tests, no batch will be created", len(sp.prs))
return nil, nil
}
sp.log.Debugf("of %d possible PRs, %d are passing tests", len(sp.prs), len(candidates))
r, err := c.gc.Clone(sp.org + "/" + sp.repo)
if err != nil {
return nil, err
}
defer r.Clean()
if err := r.Config("user.name", "prow"); err != nil {
return nil, err
}
if err := r.Config("user.email", "prow@localhost"); err != nil {
return nil, err
}
if err := r.Config("commit.gpgsign", "false"); err != nil {
sp.log.Warningf("Cannot set gpgsign=false in gitconfig: %v", err)
}
if err := r.Checkout(sp.sha); err != nil {
return nil, err
}
var res []PullRequest
for _, pr := range candidates {
if ok, err := r.Merge(string(pr.HeadRefOID)); err != nil {
// we failed to abort the merge and our git client is
// in a bad state; it must be cleaned before we try again
return nil, err
} else if ok {
res = append(res, pr)
// TODO: Make this configurable per subpool.
if len(res) == 5 {
break
}
}
}
return res, nil
}
func (c *Controller) mergePRs(sp subpool, prs []PullRequest) error {
maxRetries := 3
for i, pr := range prs {
backoff := time.Second * 4
log := sp.log.WithFields(pr.logFields())
for retry := 0; retry < maxRetries; retry++ {
if err := c.ghc.Merge(sp.org, sp.repo, int(pr.Number), github.MergeDetails{
SHA: string(pr.HeadRefOID),
MergeMethod: string(c.ca.Config().Tide.MergeMethod(sp.org, sp.repo)),
}); err != nil {
if _, ok := err.(github.ModifiedHeadError); ok {
// This is a possible source of incorrect behavior. If someone
// modifies their PR as we try to merge it in a batch then we
// end up in an untested state. This is unlikely to cause any
// real problems.
log.WithError(err).Warning("Merge failed: PR was modified.")
break
} else if _, ok = err.(github.UnmergablePRBaseChangedError); ok {
// Github complained that the base branch was modified. This is a
// strange error because the API doesn't even allow the request to
// specify the base branch sha, only the head sha.
// We suspect that github is complaining because we are making the
// merge requests too rapidly and it cannot recompute mergability
// in time. https://github.com/kubernetes/test-infra/issues/5171
// We handle this by sleeping for a few seconds before trying to
// merge again.
log.WithError(err).Warning("Merge failed: Base branch was modified.")
if retry+1 < maxRetries {
time.Sleep(backoff)
backoff *= 2
}
} else if _, ok = err.(github.UnauthorizedToPushError); ok {
// Github let us know that the token used cannot push to the branch.
// Even if the robot is set up to have write access to the repo, an
// overzealous branch protection setting will not allow the robot to
// push to a specific branch.
log.WithError(err).Error("Merge failed: Branch needs to be configured to allow this robot to push.")
break
} else if _, ok = err.(github.UnmergablePRError); ok {
log.WithError(err).Error("Merge failed: PR is unmergable. How did it pass tests?!")
break
} else {
log.WithError(err).Error("Merge failed.")
return err
}
} else {
log.Info("Merged.")
// If we have more PRs to merge, sleep to give Github time to recalculate
// mergeability.
if i+1 < len(prs) {
time.Sleep(time.Second * 3)
}
break
}
}
}
return nil
}
func (c *Controller) trigger(sp subpool, presubmitContexts map[int]sets.String, prs []PullRequest) error {
requiredContexts := sets.NewString()
for _, pr := range prs {
requiredContexts = requiredContexts.Union(presubmitContexts[int(pr.Number)])
}
// TODO(cjwagner): DRY this out when generalizing triggering code (and code to determine required and to-run jobs).
for _, ps := range c.ca.Config().Presubmits[sp.org+"/"+sp.repo] {
if ps.SkipReport || !ps.RunsAgainstBranch(sp.branch) || !requiredContexts.Has(ps.Context) {
continue
}
refs := kube.Refs{
Org: sp.org,
Repo: sp.repo,
BaseRef: sp.branch,
BaseSHA: sp.sha,
}
for _, pr := range prs {
refs.Pulls = append(
refs.Pulls,
kube.Pull{
Number: int(pr.Number),
Author: string(pr.Author.Login),
SHA: string(pr.HeadRefOID),
},
)
}
var spec kube.ProwJobSpec
if len(prs) == 1 {
spec = pjutil.PresubmitSpec(ps, refs)
} else {
spec = pjutil.BatchSpec(ps, refs)
}
pj := pjutil.NewProwJob(spec, ps.Labels)
if _, err := c.kc.CreateProwJob(pj); err != nil {
return err
}
}
return nil
}
func (c *Controller) takeAction(sp subpool, batchPending, successes, pendings, nones, batchMerges []PullRequest) (Action, []PullRequest, error) {
// Merge the batch!
if len(batchMerges) > 0 {
return MergeBatch, batchMerges, c.mergePRs(sp, batchMerges)
}
// Do not merge PRs while waiting for a batch to complete. We don't want to
// invalidate the old batch result.
if len(successes) > 0 && len(batchPending) == 0 {
if ok, pr := pickSmallestPassingNumber(sp.log, c.ghc, successes, sp.cc); ok {
return Merge, []PullRequest{pr}, c.mergePRs(sp, []PullRequest{pr})
}
}
// If no presubmits are configured, just wait.
if len(sp.presubmitContexts) == 0 {
return Wait, nil, nil
}
// If we have no serial jobs pending or successful, trigger one.
if len(nones) > 0 && len(pendings) == 0 && len(successes) == 0 {
if ok, pr := pickSmallestPassingNumber(sp.log, c.ghc, nones, sp.cc); ok {
return Trigger, []PullRequest{pr}, c.trigger(sp, sp.presubmitContexts, []PullRequest{pr})
}
}
// If we have no batch, trigger one.
if len(sp.prs) > 1 && len(batchPending) == 0 {
batch, err := c.pickBatch(sp, sp.cc)
if err != nil {
return Wait, nil, err
}
if len(batch) > 1 {
return TriggerBatch, batch, c.trigger(sp, sp.presubmitContexts, batch)
}
}
return Wait, nil, nil
}
// changedFilesAgent queries and caches the names of files changed by PRs.
// Cache entries expire if they are not used during a sync loop.
type changedFilesAgent struct {
ghc githubClient
changeCache map[changeCacheKey][]string
// nextChangeCache caches file change info that is relevant this sync for use next sync.
// This becomes the new changeCache when prune() is called at the end of each sync.
nextChangeCache map[changeCacheKey][]string
sync.RWMutex
}
type changeCacheKey struct {
org, repo string
number int
sha string
}
// prChanges gets the files changed by the PR, either from the cache or by
// querying GitHub.
func (c *changedFilesAgent) prChanges(pr *PullRequest) ([]string, error) {
cacheKey := changeCacheKey{
org: string(pr.Repository.Owner.Login),
repo: string(pr.Repository.Name),
number: int(pr.Number),
sha: string(pr.HeadRefOID),
}
c.RLock()
changedFiles, ok := c.changeCache[cacheKey]
if ok {
c.RUnlock()
c.Lock()
c.nextChangeCache[cacheKey] = changedFiles
c.Unlock()
return changedFiles, nil
}
if changedFiles, ok = c.nextChangeCache[cacheKey]; ok {
c.RUnlock()
return changedFiles, nil
}
c.RUnlock()
// We need to query the changes from GitHub.
changes, err := c.ghc.GetPullRequestChanges(
string(pr.Repository.Owner.Login),
string(pr.Repository.Name),
int(pr.Number),
)
if err != nil {
return nil, fmt.Errorf("error getting PR changes for #%d: %v", int(pr.Number), err)
}
changedFiles = make([]string, 0, len(changes))
for _, change := range changes {
changedFiles = append(changedFiles, change.Filename)
}
c.Lock()
c.nextChangeCache[cacheKey] = changedFiles
c.Unlock()
return changedFiles, nil
}
// prune removes any cached file changes that were not used since the last prune.
func (c *changedFilesAgent) prune() {
c.Lock()
defer c.Unlock()
c.changeCache = c.nextChangeCache
c.nextChangeCache = make(map[changeCacheKey][]string)
}
func (c *Controller) presubmitsByPull(sp *subpool) (map[int]sets.String, error) {
presubmits := make(map[int]sets.String, len(sp.prs))
record := func(num int, context string) {
if jobs, ok := presubmits[num]; ok {
jobs.Insert(context)
} else {
presubmits[num] = sets.NewString(context)
}
}
for _, ps := range c.ca.Config().Presubmits[sp.org+"/"+sp.repo] {
if !ps.ContextRequired() || !ps.RunsAgainstBranch(sp.branch) {
continue
}
if ps.AlwaysRun {
// Every PR requires this job.
for _, pr := range sp.prs {
record(int(pr.Number), ps.Context)
}
} else if ps.RunIfChanged != "" {
// This is a run if changed job so we need to check if each PR requires it.
for _, pr := range sp.prs {
changedFiles, err := c.changedFiles.prChanges(&pr)
if err != nil {
return nil, err
}
if ps.RunsAgainstChanges(changedFiles) {
record(int(pr.Number), ps.Context)
}
}
}
}
return presubmits, nil
}
func (c *Controller) syncSubpool(sp subpool, blocks []blockers.Blocker) (Pool, error) {
sp.log.Infof("Syncing subpool: %d PRs, %d PJs.", len(sp.prs), len(sp.pjs))
successes, pendings, nones := accumulate(sp.presubmitContexts, sp.prs, sp.pjs, sp.log)
batchMerge, batchPending := accumulateBatch(sp.presubmitContexts, sp.prs, sp.pjs, sp.log)
sp.log.WithFields(logrus.Fields{
"prs-passing": prNumbers(successes),
"prs-pending": prNumbers(pendings),
"prs-missing": prNumbers(nones),
"batch-passing": prNumbers(batchMerge),
"batch-pending": prNumbers(batchPending),
}).Info("Subpool accumulated.")
var act Action
var targets []PullRequest
var err error
if len(blocks) > 0 {
act = PoolBlocked
} else {
act, targets, err = c.takeAction(sp, batchPending, successes, pendings, nones, batchMerge)
}
sp.log.WithFields(logrus.Fields{
"action": string(act),
"targets": prNumbers(targets),
}).Info("Subpool synced.")
return Pool{
Org: sp.org,
Repo: sp.repo,
Branch: sp.branch,
SuccessPRs: successes,
PendingPRs: pendings,
MissingPRs: nones,
BatchPending: batchPending,
Action: act,
Target: targets,
Blockers: blocks,
},
err
}
func sortPools(pools []Pool) {
sort.Slice(pools, func(i, j int) bool {
if string(pools[i].Org) != string(pools[j].Org) {
return string(pools[i].Org) < string(pools[j].Org)
}
if string(pools[i].Repo) != string(pools[j].Repo) {
return string(pools[i].Repo) < string(pools[j].Repo)
}
return string(pools[i].Branch) < string(pools[j].Branch)
})
sortPRs := func(prs []PullRequest) {
sort.Slice(prs, func(i, j int) bool { return int(prs[i].Number) < int(prs[j].Number) })
}
for i := range pools {
sortPRs(pools[i].SuccessPRs)
sortPRs(pools[i].PendingPRs)
sortPRs(pools[i].MissingPRs)
sortPRs(pools[i].BatchPending)
}
}
type subpool struct {
log *logrus.Entry
org string
repo string
branch string
sha string
pjs []kube.ProwJob
prs []PullRequest
cc contextChecker
presubmitContexts map[int]sets.String
}
// dividePool splits up the list of pull requests and prow jobs into a group
// per repo and branch. It only keeps ProwJobs that match the latest branch.
func (c *Controller) dividePool(pool map[string]PullRequest, pjs []kube.ProwJob) (map[string]*subpool, error) {
sps := make(map[string]*subpool)
for _, pr := range pool {
org := string(pr.Repository.Owner.Login)
repo := string(pr.Repository.Name)
branch := string(pr.BaseRef.Name)
branchRef := string(pr.BaseRef.Prefix) + string(pr.BaseRef.Name)
fn := fmt.Sprintf("%s/%s %s", org, repo, branch)
if sps[fn] == nil {
sha, err := c.ghc.GetRef(org, repo, strings.TrimPrefix(branchRef, "refs/"))
if err != nil {
return nil, err
}
sps[fn] = &subpool{
log: c.logger.WithFields(logrus.Fields{