Skip to content

Commit

Permalink
add alternate azure git repo urls to credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Oct 21, 2024
1 parent c0e926d commit c25ad25
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
15 changes: 8 additions & 7 deletions cmd/dependabot/internal/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 64 additions & 4 deletions cmd/dependabot/internal/cmd/update_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"fmt"
"net/http"
"os"
"reflect"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c25ad25

Please sign in to comment.