Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue where the resty clients weren't initialized correctly #89

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions azure-devops-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ func (c *AzureDevopsClient) SupportsPatAuthentication() bool {
}

func (c *AzureDevopsClient) rest() *resty.Client {

var client, err = c.restWithAuthentication(&c.restClient, "dev.azure.com")
var client, err = c.restWithAuthentication(c.restClient, "dev.azure.com")

if err != nil {
c.logger.Fatalf("could not create a rest client: %v", err)
Expand All @@ -171,8 +170,7 @@ func (c *AzureDevopsClient) rest() *resty.Client {
}

func (c *AzureDevopsClient) restVsrm() *resty.Client {

var client, err = c.restWithAuthentication(&c.restClientVsrm, "vsrm.dev.azure.com")
var client, err = c.restWithAuthentication(c.restClientVsrm, "vsrm.dev.azure.com")

if err != nil {
c.logger.Fatalf("could not create a rest client: %v", err)
Expand All @@ -181,14 +179,13 @@ func (c *AzureDevopsClient) restVsrm() *resty.Client {
return client
}

func (c *AzureDevopsClient) restWithAuthentication(restClient **resty.Client, domain string) (*resty.Client, error) {

if *restClient == nil {
*restClient = c.restWithoutToken(domain)
func (c *AzureDevopsClient) restWithAuthentication(restClient *resty.Client, domain string) (*resty.Client, error) {
if restClient == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mblaschke I see that you removed the double pointer I added here. I added this on purpose: assigning a value to restClient here will not update c.restClient that gets passed into this function. To make this work, I added the double pointer, so that I can actually update the c.restClient field.

Therefore, the client will not get cached in the devops client object (c.restClient will always be nil), if restClient == nil statement will always evaluate to true, and the client will always get reinitialized.

restClient = c.restWithoutToken(domain)
}

if c.SupportsPatAuthentication() {
(*restClient).SetBasicAuth("", *c.accessToken)
restClient.SetBasicAuth("", *c.accessToken)
} else {
ctx := context.Background()
opts := policy.TokenRequestOptions{
Expand All @@ -199,10 +196,10 @@ func (c *AzureDevopsClient) restWithAuthentication(restClient **resty.Client, do
panic(err)
}

(*restClient).SetBasicAuth("", accessToken.Token)
restClient.SetBasicAuth("", accessToken.Token)
}

return (*restClient), nil
return restClient, nil
}

func (c *AzureDevopsClient) restWithoutToken(domain string) *resty.Client {
Expand Down