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

add alternate azure git repo urls to credentials for submodules #372

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment is incorrect; the next section is not skipped and the CredentialsMetadata is correctly updated. The test has been modified to verify this.

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
Copy link
Contributor Author

@brettfo brettfo Oct 21, 2024

Choose a reason for hiding this comment

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

This behavior is unchanged, but was not explicitly tested.

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",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the new URL that's added to the credentials.

}

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{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what the unnecessary deleted code did, just adding explicit verification here.

"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