Skip to content

Commit e23b660

Browse files
authored
Merge pull request #30924 from cjwagner/unify-git
Unify v1 and v2 git clients.
2 parents 96a2e85 + abd1e23 commit e23b660

File tree

21 files changed

+38
-1233
lines changed

21 files changed

+38
-1233
lines changed

prow/cmd/config-bootstrapper/main_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ var (
3939
defaultBranch = localgit.DefaultBranch("")
4040
)
4141

42-
func TestRun(t *testing.T) {
43-
testRun(localgit.New, t)
44-
}
45-
4642
func TestRunV2(t *testing.T) {
4743
testRun(localgit.NewV2, t)
4844
}

prow/config/inrepoconfig_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,6 @@ postsubmits: [{"name": "oli", "spec": {"containers": [{}]}}]`),
625625
}
626626
}
627627

628-
func TestDefaultProwYAMLGetter_RejectsJustOrg(t *testing.T) {
629-
testDefaultProwYAMLGetter_RejectsJustOrg(localgit.New, t)
630-
}
631-
632628
func TestDefaultProwYAMLGetter_RejectsJustOrgV2(t *testing.T) {
633629
testDefaultProwYAMLGetter_RejectsJustOrg(localgit.NewV2, t)
634630
}

prow/external-plugins/cherrypicker/server_test.go

-25
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,6 @@ func makeFakeRepoWithCommit(clients localgit.Clients, t *testing.T) (*localgit.L
263263
return lg, c
264264
}
265265

266-
func TestCherryPickIC(t *testing.T) {
267-
t.Parallel()
268-
testCherryPickIC(localgit.New, t)
269-
}
270-
271266
func TestCherryPickICV2(t *testing.T) {
272267
t.Parallel()
273268
testCherryPickIC(localgit.NewV2, t)
@@ -346,11 +341,6 @@ func testCherryPickIC(clients localgit.Clients, t *testing.T) {
346341
}
347342
}
348343

349-
func TestCherryPickPR(t *testing.T) {
350-
t.Parallel()
351-
testCherryPickPR(localgit.New, t)
352-
}
353-
354344
func TestCherryPickPRV2(t *testing.T) {
355345
t.Parallel()
356346
testCherryPickPR(localgit.NewV2, t)
@@ -501,11 +491,6 @@ func testCherryPickPR(clients localgit.Clients, t *testing.T) {
501491
}
502492
}
503493

504-
func TestCherryPickOfCherryPickPR(t *testing.T) {
505-
t.Parallel()
506-
testCherryPickOfCherryPickPR(localgit.New, t)
507-
}
508-
509494
func TestCherryPickOfCherryPickPRV2(t *testing.T) {
510495
t.Parallel()
511496
testCherryPickOfCherryPickPR(localgit.NewV2, t)
@@ -630,11 +615,6 @@ func testCherryPickOfCherryPickPR(clients localgit.Clients, t *testing.T) {
630615
}
631616
}
632617

633-
func TestCherryPickPRWithLabels(t *testing.T) {
634-
t.Parallel()
635-
testCherryPickPRWithLabels(localgit.New, t)
636-
}
637-
638618
func TestCherryPickPRWithLabelsV2(t *testing.T) {
639619
t.Parallel()
640620
testCherryPickPRWithLabels(localgit.NewV2, t)
@@ -913,11 +893,6 @@ func TestCherryPickCreateIssue(t *testing.T) {
913893
}
914894
}
915895

916-
func TestCherryPickPRAssignments(t *testing.T) {
917-
t.Parallel()
918-
testCherryPickPRAssignments(localgit.New, t)
919-
}
920-
921896
func TestCherryPickPRAssignmentsV2(t *testing.T) {
922897
t.Parallel()
923898
testCherryPickPRAssignments(localgit.NewV2, t)

prow/flagutil/git.go

-99
This file was deleted.

prow/flagutil/github.go

+19-43
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
utilerrors "k8s.io/apimachinery/pkg/util/errors"
3232

3333
"k8s.io/test-infra/prow/config/secret"
34-
"k8s.io/test-infra/prow/git"
3534
gitv2 "k8s.io/test-infra/prow/git/v2"
3635
"k8s.io/test-infra/prow/github"
3736
)
@@ -337,57 +336,34 @@ func (o *GitHubOptions) GitHubClientWithAccessToken(token string) (github.Client
337336
// TODO(chaodaiG): move this logic to somewhere more appropriate instead of in
338337
// github.go.
339338
func (o *GitHubOptions) GitClientFactory(cookieFilePath string, cacheDir *string, dryRun, persistCache bool) (gitv2.ClientFactory, error) {
340-
var gitClientFactory gitv2.ClientFactory
341-
if cookieFilePath != "" && o.TokenPath == "" && o.AppPrivateKeyPath == "" {
342-
opts := gitv2.ClientFactoryOpts{
343-
CookieFilePath: cookieFilePath,
344-
Persist: &persistCache,
345-
}
346-
if cacheDir != nil && *cacheDir != "" {
347-
opts.CacheDirBase = cacheDir
348-
}
349-
var err error
350-
gitClientFactory, err = gitv2.NewClientFactory(opts.Apply)
351-
if err != nil {
352-
return nil, fmt.Errorf("failed to create git client from cookieFile: %v\n(cookieFile is only for Gerrit)", err)
353-
}
354-
} else {
355-
gitClient, err := o.GitClient(dryRun)
356-
if err != nil {
357-
return nil, fmt.Errorf("Error getting git client: %w", err)
358-
}
359-
gitClientFactory = gitv2.ClientFactoryFrom(gitClient)
339+
opts := gitv2.ClientFactoryOpts{
340+
CookieFilePath: cookieFilePath,
341+
Host: o.Host,
342+
Persist: &persistCache,
360343
}
361-
362-
return gitClientFactory, nil
363-
}
364-
365-
// GitClient returns a Git client.
366-
func (o *GitHubOptions) GitClient(dryRun bool) (client *git.Client, err error) {
367-
client, err = git.NewClientWithHost(o.Host)
368-
if err != nil {
369-
return nil, err
344+
if cacheDir != nil && *cacheDir != "" {
345+
opts.CacheDirBase = cacheDir
370346
}
371347

372-
// We must capture the value of client here to prevent issues related
373-
// to the use of named return values when an error is encountered.
374-
// Without this, we risk a nil pointer dereference.
375-
defer func(client *git.Client) {
348+
if cookieFilePath == "" && (o.TokenPath != "" || o.AppPrivateKeyPath != "") {
349+
// Make a client with auth suitable for GitHub
350+
user, generator, err := o.getGitHubAuthentication(dryRun)
376351
if err != nil {
377-
client.Clean()
352+
return nil, fmt.Errorf("failed to get git authentication: %w", err)
378353
}
379-
}(client)
354+
opts.Username = func() (string, error) { return user, nil }
355+
opts.Token = generator
356+
}
357+
// If the client is for Gerrit we're already set with the cookie filepath.
380358

381-
user, generator, err := o.getGitAuthentication(dryRun)
359+
gitClientFactory, err := gitv2.NewClientFactory(opts.Apply)
382360
if err != nil {
383-
return nil, fmt.Errorf("failed to get git authentication: %w", err)
361+
return nil, fmt.Errorf("failed to create git client factory: %w", err)
384362
}
385-
client.SetCredentials(user, generator)
386-
387-
return client, nil
363+
return gitClientFactory, nil
388364
}
389365

390-
func (o *GitHubOptions) getGitAuthentication(dryRun bool) (string, git.GitTokenGenerator, error) {
366+
func (o *GitHubOptions) getGitHubAuthentication(dryRun bool) (string, gitv2.TokenGetter, error) {
391367
// the client must have been created at least once for us to have generators
392368
if o.userGenerator == nil {
393369
if _, err := o.GitHubClient(dryRun); err != nil {
@@ -399,7 +375,7 @@ func (o *GitHubOptions) getGitAuthentication(dryRun bool) (string, git.GitTokenG
399375
if err != nil {
400376
return "", nil, fmt.Errorf("error getting bot name: %w", err)
401377
}
402-
return login, git.GitTokenGenerator(o.tokenGenerator), nil
378+
return login, gitv2.TokenGetter(o.tokenGenerator), nil
403379
}
404380

405381
func (o *GitHubOptions) appPrivateKeyGenerator() (func() *rsa.PrivateKey, error) {

0 commit comments

Comments
 (0)