From 0cd3ba969bcf896c7e5ac8d1ad54fd4e6fdec9e2 Mon Sep 17 00:00:00 2001 From: Diego Caspi Date: Wed, 18 Dec 2024 21:13:28 +0100 Subject: [PATCH] refactor(azure): update Azure DevOps provider URL handling and improve error messages - Removed dependency on slices package and replaced it with string comparisons for host suffixes. - Updated error messages for better clarity. - Refactored URL parsing logic to clearly distinguish between modern and legacy Azure DevOps URLs. - Removed unused dependency from go.mod and updated go.sum accordingly. Signed-off-by: Diego Caspi --- go.mod | 1 - go.sum | 5 +++-- internal/gitprovider/azure/azure.go | 28 ++++++++++++++-------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 3b897768f..af4b52c71 100644 --- a/go.mod +++ b/go.mod @@ -198,7 +198,6 @@ require ( github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/vbatts/tar-split v0.11.3 // indirect - github.com/xanzy/go-gitlab v0.115.0 github.com/xlab/treeprint v1.2.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect go.opentelemetry.io/otel v1.31.0 // indirect diff --git a/go.sum b/go.sum index 9815a13b9..ed6591e5b 100644 --- a/go.sum +++ b/go.sum @@ -248,6 +248,7 @@ github.com/google/s2a-go v0.1.8 h1:zZDs9gcbt9ZPLV0ndSyQk6Kacx2g/X+SKYovpnz3SMM= github.com/google/s2a-go v0.1.8/go.mod h1:6iNWHTpQ+nfNRN5E00MSdfDwVesa8hhS32PhPO8deJA= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/enterprise-certificate-proxy v0.3.4 h1:XYIDZApgAnrN1c855gTgghdIA6Stxb52D5RnLI1SLyw= @@ -488,10 +489,10 @@ github.com/vbatts/tar-split v0.11.3 h1:hLFqsOLQ1SsppQNTMpkpPXClLDfC2A3Zgy9OUU+RV github.com/vbatts/tar-split v0.11.3/go.mod h1:9QlHN18E+fEH7RdG+QAJJcuya3rqT7eXSTY7wGrAokY= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= -github.com/xanzy/go-gitlab v0.115.0 h1:6DmtItNcVe+At/liXSgfE/DZNZrGfalQmBRmOcJjOn8= -github.com/xanzy/go-gitlab v0.115.0/go.mod h1:5XCDtM7AM6WMKmfDdOiEpyRWUqui2iS9ILfvCZ2gJ5M= +github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= diff --git a/internal/gitprovider/azure/azure.go b/internal/gitprovider/azure/azure.go index d7e3748be..22d1f1410 100644 --- a/internal/gitprovider/azure/azure.go +++ b/internal/gitprovider/azure/azure.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/url" - "slices" "strings" "github.com/akuity/kargo/internal/git" @@ -19,11 +18,14 @@ const ProviderName = "azure" // Azure DevOps URLs can be of two different forms: // -// - https://dev.azure.com/org/project/_git/repo -// - https://org.visualstudio.com/project/_git/repo +// - https://dev.azure.com/org//_git/ +// - https://.visualstudio.com//_git/ // // We support both forms. -var providerSuffixes = []string{"dev.azure.com", "visualstudio.com"} +const ( + legacyHostSuffix = "visualstudio.com" + modernHostSuffix = "dev.azure.com" +) var registration = gitprovider.Registration{ Predicate: func(repoURL string) bool { @@ -31,9 +33,7 @@ var registration = gitprovider.Registration{ if err != nil { return false } - return slices.ContainsFunc(providerSuffixes, func(suffix string) bool { - return strings.HasSuffix(u.Host, suffix) - }) + return u.Host == modernHostSuffix || strings.HasSuffix(u.Host, legacyHostSuffix) }, NewProvider: func( repoURL string, @@ -60,13 +60,13 @@ func NewProvider( opts *gitprovider.Options, ) (gitprovider.Interface, error) { if opts == nil || opts.Token == "" { - return nil, fmt.Errorf("options are required for Azure DevOps provider") + return nil, fmt.Errorf("token is required for Azure DevOps provider") } org, project, repo, err := parseRepoURL(repoURL) if err != nil { - return nil, fmt.Errorf("error creating Azure DevOps provider: %w", err) + return nil, err } - organizationUrl := fmt.Sprintf("https://dev.azure.com/%s", org) + organizationUrl := fmt.Sprintf("https://%s/%s", modernHostSuffix, org) connection := azuredevops.NewPatConnection(organizationUrl, opts.Token) return &provider{ @@ -84,14 +84,14 @@ func (p *provider) CreatePullRequest( ) (*gitprovider.PullRequest, error) { gitClient, err := adogit.NewClient(ctx, p.connection) if err != nil { - return nil, err + return nil, fmt.Errorf("error creating Azure DevOps client: %w", err) } repository, err := gitClient.GetRepository(ctx, adogit.GetRepositoryArgs{ Project: &p.project, RepositoryId: &p.repo, }) if err != nil { - return nil, err + return nil, fmt.Errorf("error getting repository %q: %w", p.repo, err) } repoID := ptr.To(repository.Id.String()) sourceRefName := ptr.To(fmt.Sprintf("refs/heads/%s", opts.Head)) @@ -206,9 +206,9 @@ func parseRepoURL(repoURL string) (string, string, string, error) { if err != nil { return "", "", "", fmt.Errorf("error parsing Azure DevOps repository URL %q: %w", repoURL, err) } - if u.Host == "dev.azure.com" { + if u.Host == modernHostSuffix { return parseModernRepoURL(u) - } else if strings.HasSuffix(u.Host, ".visualstudio.com") { + } else if strings.HasSuffix(u.Host, legacyHostSuffix) { return parseLegacyRepoURL(u) } return "", "", "", fmt.Errorf("unsupported host %q", u.Host)