Skip to content

Commit daf68a9

Browse files
authored
add alternate azure git repo urls to credentials (#372)
1 parent c0e926d commit daf68a9

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

cmd/dependabot/internal/cmd/update.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,19 +332,20 @@ func processInput(input *model.Input, flags *UpdateFlags) {
332332
if hasLocalAzureToken && !isGitSourceInCreds && azureRepo != nil {
333333
log.Println("Inserting $LOCAL_AZURE_ACCESS_TOKEN into credentials")
334334
log.Printf("Inserting artifacts credentials for %s organization.", azureRepo.Org)
335+
336+
// add both `dev.azure.com` and `org.visualstudio.com` credentials
335337
input.Credentials = append(input.Credentials, model.Credential{
336338
"type": "git_source",
337339
"host": "dev.azure.com",
338340
"username": "x-access-token",
339341
"password": "$LOCAL_AZURE_ACCESS_TOKEN",
340342
})
341-
if len(input.Job.CredentialsMetadata) > 0 {
342-
// Add the metadata since the next section will be skipped.
343-
input.Job.CredentialsMetadata = append(input.Job.CredentialsMetadata, map[string]any{
344-
"type": "git_source",
345-
"host": "dev.azure.com",
346-
})
347-
}
343+
input.Credentials = append(input.Credentials, model.Credential{
344+
"type": "git_source",
345+
"host": fmt.Sprintf("%s.visualstudio.com", azureRepo.Org),
346+
"username": "x-access-token",
347+
"password": "$LOCAL_AZURE_ACCESS_TOKEN",
348+
})
348349
}
349350

350351
// Calculate the credentials-metadata as it cannot be provided by the user anymore.

cmd/dependabot/internal/cmd/update_test.go

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"net/http"
56
"os"
67
"reflect"
8+
"sort"
79
"strings"
810
"testing"
911
"time"
@@ -115,18 +117,76 @@ func Test_processInput(t *testing.T) {
115117

116118
processInput(&input, &flags)
117119

118-
if len(input.Credentials) != 4 {
119-
t.Fatal("expected credentials to be added")
120-
}
121-
// Ensure all credentials are either git_source or azure
120+
// Ensure all credentials are either git_source or nuget
122121
for _, cred := range input.Credentials {
123122
if cred["type"] != "git_source" && cred["type"] != "nuget_feed" {
124123
t.Errorf("expected credentials to be either git_source or nuget_feed, got %s", cred["type"])
125124
}
126125
}
126+
127+
// Validate NuGet feeds
128+
actualNuGetFeedStrings := []string{}
129+
for _, cred := range input.Credentials {
130+
if cred["type"] == "nuget_feed" {
131+
actualNuGetFeedStrings = append(actualNuGetFeedStrings, fmt.Sprintf("%s|%s", cred["host"], cred["password"]))
132+
}
133+
}
134+
135+
expectedNuGetFeeds := []string{
136+
"org.pkgs.visualstudio.com|$LOCAL_AZURE_ACCESS_TOKEN",
137+
"pkgs.dev.azure.com|$LOCAL_AZURE_ACCESS_TOKEN",
138+
}
139+
140+
assertStringArraysEqual(t, expectedNuGetFeeds, actualNuGetFeedStrings)
141+
142+
// Validate credentials
143+
actualCredentialStrings := []string{}
144+
for _, cred := range input.Credentials {
145+
if cred["type"] == "git_source" {
146+
actualCredentialStrings = append(actualCredentialStrings, fmt.Sprintf("%s|%s|%s", cred["host"], cred["username"], cred["password"]))
147+
}
148+
}
149+
150+
expectedGitCredentials := []string{
151+
"dev.azure.com|org|$LOCAL_AZURE_ACCESS_TOKEN",
152+
"dev.azure.com|x-access-token|$LOCAL_AZURE_ACCESS_TOKEN",
153+
"org.visualstudio.com|x-access-token|$LOCAL_AZURE_ACCESS_TOKEN",
154+
}
155+
156+
assertStringArraysEqual(t, expectedGitCredentials, actualCredentialStrings)
157+
158+
// Validate credentials metadata
159+
credentialsMetadataHosts := map[string]string{}
160+
for _, cred := range input.Job.CredentialsMetadata {
161+
if cred["type"] == "git_source" {
162+
// dedup hosts with a map
163+
credentialsMetadataHosts[fmt.Sprintf("%s", cred["host"])] = ""
164+
}
165+
}
166+
167+
actualCredentialsMetadataHosts := []string{}
168+
for host := range credentialsMetadataHosts {
169+
actualCredentialsMetadataHosts = append(actualCredentialsMetadataHosts, host)
170+
}
171+
172+
expectedGitCredentalsMetadataHosts := []string{
173+
"dev.azure.com",
174+
"org.visualstudio.com",
175+
}
176+
177+
assertStringArraysEqual(t, expectedGitCredentalsMetadataHosts, actualCredentialsMetadataHosts)
127178
})
128179
}
129180

181+
func assertStringArraysEqual(t *testing.T, expected, actual []string) {
182+
sort.Strings(expected)
183+
sort.Strings(actual)
184+
185+
if !reflect.DeepEqual(expected, actual) {
186+
t.Errorf("expected strings to be\n\t%v\n got\n\t%v", expected, actual)
187+
}
188+
}
189+
130190
func Test_extractInput(t *testing.T) {
131191
t.Run("test arguments", func(t *testing.T) {
132192
cmd := NewUpdateCommand()

0 commit comments

Comments
 (0)