@@ -31,7 +31,6 @@ import (
31
31
utilerrors "k8s.io/apimachinery/pkg/util/errors"
32
32
33
33
"k8s.io/test-infra/prow/config/secret"
34
- "k8s.io/test-infra/prow/git"
35
34
gitv2 "k8s.io/test-infra/prow/git/v2"
36
35
"k8s.io/test-infra/prow/github"
37
36
)
@@ -337,57 +336,34 @@ func (o *GitHubOptions) GitHubClientWithAccessToken(token string) (github.Client
337
336
// TODO(chaodaiG): move this logic to somewhere more appropriate instead of in
338
337
// github.go.
339
338
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 ,
360
343
}
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
370
346
}
371
347
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 )
376
351
if err != nil {
377
- client . Clean ( )
352
+ return nil , fmt . Errorf ( "failed to get git authentication: %w" , err )
378
353
}
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.
380
358
381
- user , generator , err := o . getGitAuthentication ( dryRun )
359
+ gitClientFactory , err := gitv2 . NewClientFactory ( opts . Apply )
382
360
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 )
384
362
}
385
- client .SetCredentials (user , generator )
386
-
387
- return client , nil
363
+ return gitClientFactory , nil
388
364
}
389
365
390
- func (o * GitHubOptions ) getGitAuthentication (dryRun bool ) (string , git. GitTokenGenerator , error ) {
366
+ func (o * GitHubOptions ) getGitHubAuthentication (dryRun bool ) (string , gitv2. TokenGetter , error ) {
391
367
// the client must have been created at least once for us to have generators
392
368
if o .userGenerator == nil {
393
369
if _ , err := o .GitHubClient (dryRun ); err != nil {
@@ -399,7 +375,7 @@ func (o *GitHubOptions) getGitAuthentication(dryRun bool) (string, git.GitTokenG
399
375
if err != nil {
400
376
return "" , nil , fmt .Errorf ("error getting bot name: %w" , err )
401
377
}
402
- return login , git . GitTokenGenerator (o .tokenGenerator ), nil
378
+ return login , gitv2 . TokenGetter (o .tokenGenerator ), nil
403
379
}
404
380
405
381
func (o * GitHubOptions ) appPrivateKeyGenerator () (func () * rsa.PrivateKey , error ) {
0 commit comments