Skip to content

Commit

Permalink
refactor(azure): update Azure DevOps provider URL handling and improv…
Browse files Browse the repository at this point in the history
…e 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 <[email protected]>
  • Loading branch information
diegocaspi committed Dec 18, 2024
1 parent d70d8fc commit 0cd3ba9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
28 changes: 14 additions & 14 deletions internal/gitprovider/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/url"
"slices"
"strings"

"github.com/akuity/kargo/internal/git"
Expand All @@ -19,21 +18,22 @@ 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/<project>/_git/<repo>
// - https://<org>.visualstudio.com/<project>/_git/<repo>
//
// 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 {
u, err := url.Parse(repoURL)
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)

Check warning on line 36 in internal/gitprovider/azure/azure.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L31-L36

Added lines #L31 - L36 were not covered by tests
},
NewProvider: func(
repoURL string,
Expand All @@ -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{
Expand All @@ -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))
Expand Down Expand Up @@ -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)
}

Check warning on line 208 in internal/gitprovider/azure/azure.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L207-L208

Added lines #L207 - L208 were not covered by tests
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)
Expand Down

0 comments on commit 0cd3ba9

Please sign in to comment.