From c25ad2500a47f2bfe54a0c380c032b08c1b9aa12 Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Mon, 21 Oct 2024 16:55:02 -0600 Subject: [PATCH] add alternate azure git repo urls to credentials --- cmd/dependabot/internal/cmd/update.go | 15 ++--- cmd/dependabot/internal/cmd/update_test.go | 68 ++++++++++++++++++++-- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/cmd/dependabot/internal/cmd/update.go b/cmd/dependabot/internal/cmd/update.go index 188f821..39a5734 100644 --- a/cmd/dependabot/internal/cmd/update.go +++ b/cmd/dependabot/internal/cmd/update.go @@ -332,19 +332,20 @@ func processInput(input *model.Input, flags *UpdateFlags) { if hasLocalAzureToken && !isGitSourceInCreds && azureRepo != nil { log.Println("Inserting $LOCAL_AZURE_ACCESS_TOKEN into credentials") log.Printf("Inserting artifacts credentials for %s organization.", azureRepo.Org) + + // add both `dev.azure.com` and `org.visualstudio.com` credentials input.Credentials = append(input.Credentials, model.Credential{ "type": "git_source", "host": "dev.azure.com", "username": "x-access-token", "password": "$LOCAL_AZURE_ACCESS_TOKEN", }) - if len(input.Job.CredentialsMetadata) > 0 { - // Add the metadata since the next section will be skipped. - input.Job.CredentialsMetadata = append(input.Job.CredentialsMetadata, map[string]any{ - "type": "git_source", - "host": "dev.azure.com", - }) - } + input.Credentials = append(input.Credentials, model.Credential{ + "type": "git_source", + "host": fmt.Sprintf("%s.visualstudio.com", azureRepo.Org), + "username": "x-access-token", + "password": "$LOCAL_AZURE_ACCESS_TOKEN", + }) } // Calculate the credentials-metadata as it cannot be provided by the user anymore. diff --git a/cmd/dependabot/internal/cmd/update_test.go b/cmd/dependabot/internal/cmd/update_test.go index 7ee8152..ebe998e 100644 --- a/cmd/dependabot/internal/cmd/update_test.go +++ b/cmd/dependabot/internal/cmd/update_test.go @@ -1,9 +1,11 @@ package cmd import ( + "fmt" "net/http" "os" "reflect" + "sort" "strings" "testing" "time" @@ -115,18 +117,76 @@ func Test_processInput(t *testing.T) { processInput(&input, &flags) - if len(input.Credentials) != 4 { - t.Fatal("expected credentials to be added") - } - // Ensure all credentials are either git_source or azure + // Ensure all credentials are either git_source or nuget for _, cred := range input.Credentials { if cred["type"] != "git_source" && cred["type"] != "nuget_feed" { t.Errorf("expected credentials to be either git_source or nuget_feed, got %s", cred["type"]) } } + + // Validate NuGet feeds + actualNuGetFeedStrings := []string{} + for _, cred := range input.Credentials { + if cred["type"] == "nuget_feed" { + actualNuGetFeedStrings = append(actualNuGetFeedStrings, fmt.Sprintf("%s|%s", cred["host"], cred["password"])) + } + } + + expectedNuGetFeeds := []string{ + "org.pkgs.visualstudio.com|$LOCAL_AZURE_ACCESS_TOKEN", + "pkgs.dev.azure.com|$LOCAL_AZURE_ACCESS_TOKEN", + } + + assertStringArraysEqual(t, expectedNuGetFeeds, actualNuGetFeedStrings) + + // Validate credentials + actualCredentialStrings := []string{} + for _, cred := range input.Credentials { + if cred["type"] == "git_source" { + actualCredentialStrings = append(actualCredentialStrings, fmt.Sprintf("%s|%s|%s", cred["host"], cred["username"], cred["password"])) + } + } + + expectedGitCredentials := []string{ + "dev.azure.com|org|$LOCAL_AZURE_ACCESS_TOKEN", + "dev.azure.com|x-access-token|$LOCAL_AZURE_ACCESS_TOKEN", + "org.visualstudio.com|x-access-token|$LOCAL_AZURE_ACCESS_TOKEN", + } + + assertStringArraysEqual(t, expectedGitCredentials, actualCredentialStrings) + + // Validate credentials metadata + credentialsMetadataHosts := map[string]string{} + for _, cred := range input.Job.CredentialsMetadata { + if cred["type"] == "git_source" { + // dedup hosts with a map + credentialsMetadataHosts[fmt.Sprintf("%s", cred["host"])] = "" + } + } + + actualCredentialsMetadataHosts := []string{} + for host := range credentialsMetadataHosts { + actualCredentialsMetadataHosts = append(actualCredentialsMetadataHosts, host) + } + + expectedGitCredentalsMetadataHosts := []string{ + "dev.azure.com", + "org.visualstudio.com", + } + + assertStringArraysEqual(t, expectedGitCredentalsMetadataHosts, actualCredentialsMetadataHosts) }) } +func assertStringArraysEqual(t *testing.T, expected, actual []string) { + sort.Strings(expected) + sort.Strings(actual) + + if !reflect.DeepEqual(expected, actual) { + t.Errorf("expected strings to be\n\t%v\n got\n\t%v", expected, actual) + } +} + func Test_extractInput(t *testing.T) { t.Run("test arguments", func(t *testing.T) { cmd := NewUpdateCommand()