From 11cb236ca69785a2c14757c3b342fb82fc62f3a4 Mon Sep 17 00:00:00 2001 From: Markus Blaschke Date: Sun, 31 Mar 2024 12:53:09 +0200 Subject: [PATCH] cleanup Signed-off-by: Markus Blaschke --- azure-devops-client/main.go | 21 +++++++++++---------- main.go | 4 ++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/azure-devops-client/main.go b/azure-devops-client/main.go index 8df9a6a..d832717 100644 --- a/azure-devops-client/main.go +++ b/azure-devops-client/main.go @@ -149,6 +149,17 @@ func (c *AzureDevopsClient) SetAccessToken(token string) { c.accessToken = &token } +func (c *AzureDevopsClient) UseAzAuth() error { + opts := azidentity.DefaultAzureCredentialOptions{} + cred, err := azidentity.NewDefaultAzureCredential(&opts) + if err != nil { + return err + } + + c.azcreds = cred + return nil +} + func (c *AzureDevopsClient) SupportsPatAuthentication() bool { return c.accessToken != nil && len(*c.accessToken) > 0 } @@ -181,16 +192,6 @@ func (c *AzureDevopsClient) restWithAuthentication(domain string) (*resty.Client if c.SupportsPatAuthentication() { c.restClient.SetBasicAuth("", *c.accessToken) } else { - if c.azcreds == nil { - opts := azidentity.DefaultAzureCredentialOptions{} - cred, err := azidentity.NewDefaultAzureCredential(&opts) - if err != nil { - panic(err) - } - - c.azcreds = cred - } - ctx := context.Background() opts := policy.TokenRequestOptions{ Scopes: []string{AZURE_DEVOPS_SCOPE}, diff --git a/main.go b/main.go index 98ece34..96d68cf 100644 --- a/main.go +++ b/main.go @@ -179,6 +179,10 @@ func initAzureDevOpsConnection() { AzureDevopsClient.SetOrganization(opts.AzureDevops.Organisation) if opts.AzureDevops.AccessToken != "" { AzureDevopsClient.SetAccessToken(opts.AzureDevops.AccessToken) + } else { + if err := AzureDevopsClient.UseAzAuth(); err != nil { + logger.Fatalf(err.Error()) + } } AzureDevopsClient.SetApiVersion(opts.AzureDevops.ApiVersion) AzureDevopsClient.SetConcurrency(opts.Request.ConcurrencyLimit)