Skip to content

Commit 5f97395

Browse files
authored
Merge pull request #27052 from chaodaiG/revert-tide-interface
Revert "Tide Controller interacts with GitHub by interfaces"
2 parents 55d672f + b12af77 commit 5f97395

File tree

9 files changed

+202
-399
lines changed

9 files changed

+202
-399
lines changed

prow/config/prow-config-documented.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,8 +1158,6 @@ tide:
11581158
# Special values:
11591159
# 0 => unlimited batch size
11601160
# -1 => batch merging disabled :(
1161-
1162-
# This is suitable for any source code provider, so keep it global.
11631161
batch_size_limit:
11641162
"": 0
11651163

@@ -1266,8 +1264,6 @@ tide:
12661264
# eligible. Continuing on an old batch allows to re-use all existing test results whereas
12671265
# starting a new one requires to start new instances of all tests.
12681266
# Use '*' as key to set this globally. Defaults to true.
1269-
1270-
# This is suitable for any source code provider, so keep it global.
12711267
prioritize_existing_batches:
12721268
"": false
12731269

prow/config/tide.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ type Tide struct {
155155
// Special values:
156156
// 0 => unlimited batch size
157157
// -1 => batch merging disabled :(
158-
//
159-
// This is suitable for any source code provider, so keep it global.
160158
BatchSizeLimitMap map[string]int `json:"batch_size_limit,omitempty"`
161159

162160
// Priority is an ordered list of sets of labels that would be prioritized before other PRs
@@ -169,8 +167,6 @@ type Tide struct {
169167
// eligible. Continuing on an old batch allows to re-use all existing test results whereas
170168
// starting a new one requires to start new instances of all tests.
171169
// Use '*' as key to set this globally. Defaults to true.
172-
//
173-
// This is suitable for any source code provider, so keep it global.
174170
PrioritizeExistingBatchesMap map[string]bool `json:"prioritize_existing_batches,omitempty"`
175171

176172
// DisplayAllQueriesInStatus controls if Tide should mention all queries in the status it

prow/tide/codereview.go

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,9 @@ package tide
1919
import (
2020
"encoding/json"
2121
"errors"
22-
"fmt"
23-
"strconv"
24-
"sync"
2522
"time"
2623

2724
"github.com/sirupsen/logrus"
28-
utilerrors "k8s.io/apimachinery/pkg/util/errors"
29-
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
30-
"k8s.io/test-infra/prow/config"
31-
"k8s.io/test-infra/prow/git/types"
32-
"k8s.io/test-infra/prow/git/v2"
33-
"k8s.io/test-infra/prow/tide/blockers"
3425
)
3526

3627
// CodeReviewForDeck contains superset of data from CodeReviewCommon, it's meant
@@ -170,140 +161,3 @@ func CodeReviewCommonFromPullRequest(pr *PullRequest) *CodeReviewCommon {
170161

171162
return crc
172163
}
173-
174-
// provider is the interface implemented by each source code
175-
// providers, such as GitHub and Gerrit.
176-
type provider interface {
177-
Query() (map[string]CodeReviewCommon, error)
178-
blockers() (blockers.Blockers, error)
179-
isAllowedToMerge(crc *CodeReviewCommon) (string, error)
180-
GetRef(org, repo, ref string) (string, error)
181-
// headContexts returns Contexts from all presubmit requirements.
182-
// Tide needs to know whether a PR passed all tests or not, this includes
183-
// prow jobs, but also any external tests that are required by GitHub branch
184-
// protection, for example GH actions. For GitHub these are all reflected on
185-
// status contexts, and more importantly each prowjob is a context. For
186-
// Gerrit we can transform every prow jobs into a context, and mark it
187-
// optional if the prowjob doesn't vote on label that's required for
188-
// merging. And also transform any other label that is not voted by prow
189-
// into a context.
190-
headContexts(pr *CodeReviewCommon) ([]Context, error)
191-
mergePRs(sp subpool, prs []CodeReviewCommon, dontUpdateStatus *threadSafePRSet) error
192-
GetTideContextPolicy(gitClient git.ClientFactory, org, repo, branch string, baseSHAGetter config.RefGetter, headSHA string) (contextChecker, error)
193-
refsForJob(sp subpool, prs []CodeReviewCommon) prowapi.Refs
194-
prMergeMethod(crc *CodeReviewCommon) (types.PullRequestMergeType, error)
195-
}
196-
197-
// GitHubProvider implements provider, used by tide Controller for
198-
// interacting directly with GitHub.
199-
//
200-
// Tide Controller should only use GitHubProvider for communicating with GitHub.
201-
type GitHubProvider struct {
202-
cfg config.Getter
203-
ghc githubClient
204-
usesGitHubAppsAuth bool
205-
206-
*mergeChecker
207-
logger *logrus.Entry
208-
}
209-
210-
func (gi *GitHubProvider) blockers() (blockers.Blockers, error) {
211-
label := gi.cfg().Tide.BlockerLabel
212-
if label == "" {
213-
return blockers.Blockers{}, nil
214-
}
215-
216-
gi.logger.WithField("blocker_label", label).Debug("Searching for blocker issues")
217-
orgExcepts, repos := gi.cfg().Tide.Queries.OrgExceptionsAndRepos()
218-
orgs := make([]string, 0, len(orgExcepts))
219-
for org := range orgExcepts {
220-
orgs = append(orgs, org)
221-
}
222-
orgRepoQuery := orgRepoQueryStrings(orgs, repos.UnsortedList(), orgExcepts)
223-
return blockers.FindAll(gi.ghc, gi.logger, label, orgRepoQuery, gi.usesGitHubAppsAuth)
224-
}
225-
226-
// Query gets all open PRs based on tide configuration.
227-
func (gi *GitHubProvider) Query() (map[string]CodeReviewCommon, error) {
228-
lock := sync.Mutex{}
229-
wg := sync.WaitGroup{}
230-
prs := make(map[string]CodeReviewCommon)
231-
var errs []error
232-
for i, query := range gi.cfg().Tide.Queries {
233-
234-
// Use org-sharded queries only when GitHub apps auth is in use
235-
var queries map[string]string
236-
if gi.usesGitHubAppsAuth {
237-
queries = query.OrgQueries()
238-
} else {
239-
queries = map[string]string{"": query.Query()}
240-
}
241-
242-
for org, q := range queries {
243-
org, q, i := org, q, i
244-
wg.Add(1)
245-
go func() {
246-
defer wg.Done()
247-
results, err := gi.search(gi.ghc.QueryWithGitHubAppsSupport, gi.logger, q, time.Time{}, time.Now(), org)
248-
249-
resultString := "success"
250-
if err != nil {
251-
resultString = "error"
252-
}
253-
tideMetrics.queryResults.WithLabelValues(strconv.Itoa(i), org, resultString).Inc()
254-
255-
lock.Lock()
256-
defer lock.Unlock()
257-
if err != nil && len(results) == 0 {
258-
gi.logger.WithField("query", q).WithError(err).Warn("Failed to execute query.")
259-
errs = append(errs, fmt.Errorf("query %d, err: %w", i, err))
260-
return
261-
}
262-
if err != nil {
263-
gi.logger.WithError(err).WithField("query", q).Warning("found partial results")
264-
}
265-
266-
for _, pr := range results {
267-
crc := CodeReviewCommonFromPullRequest(&pr)
268-
prs[prKey(crc)] = *crc
269-
}
270-
}()
271-
}
272-
}
273-
wg.Wait()
274-
275-
return prs, utilerrors.NewAggregate(errs)
276-
}
277-
278-
func (gi *GitHubProvider) GetRef(org, repo, ref string) (string, error) {
279-
return gi.ghc.GetRef(org, repo, ref)
280-
}
281-
282-
func (gi *GitHubProvider) GetTideContextPolicy(gitClient git.ClientFactory, org, repo, branch string, baseSHAGetter config.RefGetter, headSHA string) (contextChecker, error) {
283-
return gi.cfg().GetTideContextPolicy(gitClient, org, repo, branch, baseSHAGetter, headSHA)
284-
}
285-
286-
func (gi *GitHubProvider) refsForJob(sp subpool, prs []CodeReviewCommon) prowapi.Refs {
287-
refs := prowapi.Refs{
288-
Org: sp.org,
289-
Repo: sp.repo,
290-
BaseRef: sp.branch,
291-
BaseSHA: sp.sha,
292-
}
293-
for _, pr := range prs {
294-
refs.Pulls = append(
295-
refs.Pulls,
296-
prowapi.Pull{
297-
Number: pr.Number,
298-
Title: pr.Title,
299-
Author: string(pr.AuthorLogin),
300-
SHA: pr.HeadRefOID,
301-
},
302-
)
303-
}
304-
return refs
305-
}
306-
307-
func (gi *GitHubProvider) prMergeMethod(crc *CodeReviewCommon) (types.PullRequestMergeType, error) {
308-
return gi.mergeChecker.prMergeMethod(gi.cfg().Tide, crc)
309-
}

prow/tide/search.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func floor(t time.Time) time.Time {
4040
return t
4141
}
4242

43-
func (gi *GitHubProvider) search(query querier, log *logrus.Entry, q string, start, end time.Time, org string) ([]PullRequest, error) {
43+
func search(query querier, log *logrus.Entry, q string, start, end time.Time, org string) ([]CodeReviewCommon, error) {
4444
start = floor(start)
4545
end = floor(end)
4646
log = log.WithFields(logrus.Fields{
@@ -56,7 +56,7 @@ func (gi *GitHubProvider) search(query querier, log *logrus.Entry, q string, sta
5656
}
5757

5858
var totalCost, remaining int
59-
var ret []PullRequest
59+
var ret []CodeReviewCommon
6060
var sq searchQuery
6161
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
6262
defer cancel()
@@ -71,7 +71,7 @@ func (gi *GitHubProvider) search(query querier, log *logrus.Entry, q string, sta
7171
totalCost += int(sq.RateLimit.Cost)
7272
remaining = int(sq.RateLimit.Remaining)
7373
for _, n := range sq.Search.Nodes {
74-
ret = append(ret, n.PullRequest)
74+
ret = append(ret, *CodeReviewCommonFromPullRequest(&n.PullRequest))
7575
}
7676
if !sq.Search.PageInfo.HasNextPage {
7777
break

prow/tide/search_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ func TestSearch(t *testing.T) {
136136

137137
for _, tc := range cases {
138138
t.Run(tc.name, func(t *testing.T) {
139-
client := &GitHubProvider{}
140139
var i int
141140
querier := func(_ context.Context, result interface{}, actual map[string]interface{}, _ string) error {
142141
expected := map[string]interface{}{
@@ -156,7 +155,7 @@ func TestSearch(t *testing.T) {
156155
*ret = sq
157156
return nil
158157
}
159-
prs, err := client.search(querier, logrus.WithField("test", tc.name), q, tc.start, tc.end, "")
158+
prs, err := search(querier, logrus.WithField("test", tc.name), q, tc.start, tc.end, "")
160159
switch {
161160
case err != nil:
162161
if !tc.err {
@@ -165,8 +164,12 @@ func TestSearch(t *testing.T) {
165164
case tc.err:
166165
t.Errorf("failed to receive expected error")
167166
}
168-
169-
if !reflect.DeepEqual(tc.expected, prs) {
167+
// Always check prs because we might return some results on error
168+
var expectedCrcs []CodeReviewCommon
169+
for _, pr := range tc.expected {
170+
expectedCrcs = append(expectedCrcs, *CodeReviewCommonFromPullRequest(&pr))
171+
}
172+
if !reflect.DeepEqual(expectedCrcs, prs) {
170173
t.Errorf("prs do not match:\n%s", diff.ObjectReflectDiff(tc.expected, prs))
171174
}
172175
})

prow/tide/status.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ type storedState struct {
6262
PreviousQuery string
6363
}
6464

65-
// statusController is a goroutine runs in the background
6665
type statusController struct {
6766
pjClient ctrlruntimeclient.Client
6867
logger *logrus.Entry
6968
config config.Getter
70-
ghProvider *GitHubProvider
7169
ghc githubClient
7270
gc git.ClientFactory
7371
usesGitHubAppsAuth bool
7472

73+
mergeChecker *mergeChecker
74+
7575
// shutDown is used to signal to the main controller that the statusController
7676
// has completed processing after newPoolPending is closed.
7777
shutDown chan bool
@@ -278,7 +278,7 @@ func (sc *statusController) expectedStatus(log *logrus.Entry, queryMap *config.Q
278278

279279
repo := config.OrgRepo{Org: crc.Org, Repo: crc.Repo}
280280

281-
if reason, err := sc.ghProvider.isAllowedToMerge(crc); err != nil {
281+
if reason, err := sc.mergeChecker.isAllowed(crc); err != nil {
282282
return "", "", fmt.Errorf("error checking if merge is allowed: %w", err)
283283
} else if reason != "" {
284284
log.WithField("reason", reason).Debug("The PR is not mergeable")
@@ -414,7 +414,7 @@ func (sc *statusController) setStatuses(all []CodeReviewCommon, pool map[string]
414414
process := func(pr *CodeReviewCommon) {
415415
processed.Insert(prKey(pr))
416416
log := sc.logger.WithFields(pr.logFields())
417-
contexts, err := sc.ghProvider.headContexts(pr)
417+
contexts, err := headContexts(log, sc.ghc, pr)
418418
if err != nil {
419419
log.WithError(err).Error("Getting head commit status contexts, skipping...")
420420
return
@@ -657,7 +657,7 @@ func (sc *statusController) search() []CodeReviewCommon {
657657
}
658658
sc.storedStateLock.Unlock()
659659

660-
result, err := sc.ghProvider.search(sc.ghc.QueryWithGitHubAppsSupport, sc.logger, query, latestPR.Time, now, org)
660+
result, err := search(sc.ghc.QueryWithGitHubAppsSupport, sc.logger, query, latestPR.Time, now, org)
661661
log.WithField("duration", time.Since(now).String()).WithField("result_count", len(result)).Debug("Searched for open PRs.")
662662

663663
func() {
@@ -669,7 +669,7 @@ func (sc *statusController) search() []CodeReviewCommon {
669669
log.Debug("no new results")
670670
return
671671
}
672-
latest := result[len(result)-1].UpdatedAt
672+
latest := result[len(result)-1].UpdatedAtTime
673673
if latest.IsZero() {
674674
log.Debug("latest PR has zero time")
675675
return
@@ -684,10 +684,7 @@ func (sc *statusController) search() []CodeReviewCommon {
684684
lock.Lock()
685685
defer lock.Unlock()
686686

687-
for _, pr := range result {
688-
pr := pr
689-
prs = append(prs, *CodeReviewCommonFromPullRequest(&pr))
690-
}
687+
prs = append(prs, result...)
691688
errs = append(errs, err)
692689
}()
693690

prow/tide/status_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,14 +1222,11 @@ func TestSetStatusRespectsRequiredContexts(t *testing.T) {
12221222
ca.Set(&config.Config{})
12231223

12241224
sc := &statusController{
1225-
logger: log,
1226-
ghc: fghc,
1227-
config: ca.Config,
1228-
pjClient: fakectrlruntimeclient.NewFakeClient(),
1229-
ghProvider: &GitHubProvider{
1230-
ghc: fghc,
1231-
mergeChecker: newMergeChecker(ca.Config, fghc),
1232-
},
1225+
logger: log,
1226+
ghc: fghc,
1227+
config: ca.Config,
1228+
pjClient: fakectrlruntimeclient.NewFakeClient(),
1229+
mergeChecker: newMergeChecker(ca.Config, fghc),
12331230
statusUpdate: &statusUpdate{
12341231
dontUpdateStatus: &threadSafePRSet{},
12351232
newPoolPending: make(chan bool),

0 commit comments

Comments
 (0)