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

feat(gitprovider): add Azure DevOps support and update provider confi… #3128

Merged
merged 24 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ccc05b5
feat(gitprovider): add Azure DevOps support and update provider confi…
diegocaspi Dec 12, 2024
d5f698e
fix(auth): auto attach `offline_access` only if idp supports (#3117)
Marvin9 Dec 11, 2024
cfcf3cf
fix(auth): token renew failure (#3122)
Marvin9 Dec 11, 2024
4561dcf
chore(chart): start using Kubernetes' builtin gRPC probe (#3041)
hiddeco Dec 11, 2024
6b5091c
docs: manual rolebindings for global namespaces (#2934)
fykaa Dec 12, 2024
34cf92a
chore(deps): bump nanoid from 3.3.6 to 3.3.8 in /docs/plugins/gtag (#…
dependabot[bot] Dec 12, 2024
7ab935e
chore(deps): bump nanoid from 3.3.7 to 3.3.8 in /docs (#3132)
dependabot[bot] Dec 12, 2024
da2c137
chore(deps/tools): bump golang.org/x/crypto from 0.30.0 to 0.31.0 in …
dependabot[bot] Dec 12, 2024
3a3b1db
docs: add a resources page for Kargo related talks (#2935)
fykaa Dec 13, 2024
eb7fe22
chore(deps): bump nanoid from 3.3.7 to 3.3.8 in /ui (#3133)
dependabot[bot] Dec 13, 2024
b77cfdf
chore(deps): bump golang.org/x/crypto from 0.30.0 to 0.31.0 (#3131)
dependabot[bot] Dec 13, 2024
3f8d114
docs: Kargo role matrix (#3135)
34fathombelow Dec 13, 2024
e07ab3a
chore(deps): bump the go-minor group with 12 updates (#3146)
dependabot[bot] Dec 16, 2024
874a541
chore(deps): bump curlimages/curl from 8.11.0 to 8.11.1 (#3140)
dependabot[bot] Dec 16, 2024
53552a1
chore(deps): bump actions/setup-go from 5.1.0 to 5.2.0 (#3144)
dependabot[bot] Dec 16, 2024
5092ae3
chore(deps/tools): bump k8s.io/code-generator from 0.31.3 to 0.32.0 i…
dependabot[bot] Dec 16, 2024
b7a58f2
chore(deps): bump the js-minor group in /ui with 26 updates (#3142)
dependabot[bot] Dec 16, 2024
2c5890c
chore(deps): bump the js-patch group across 1 directory with 18 updat…
dependabot[bot] Dec 16, 2024
e8d6f54
fix(controller): git-push step: pull --rebase before push (#3119)
krancour Dec 17, 2024
e4c3daa
feat(ui): PR deep links in Promotion steps and in Promotion lists (#3…
Marvin9 Dec 17, 2024
8e3af16
chore(deps): update go.mod and go.sum to include azure-devops-go-api
diegocaspi Dec 18, 2024
d70d8fc
chore: merge branch 'main' into ado-promotion-steps-support
diegocaspi Dec 18, 2024
0cd3ba9
refactor(azure): update Azure DevOps provider URL handling and improv…
diegocaspi Dec 18, 2024
0a0cba8
chore: ci errors solved
diegocaspi Dec 18, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/jferrl/go-githubauth v1.1.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/klauspost/compress v1.17.11
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0
github.com/oklog/ulid/v2 v2.1.0
github.com/otiai10/copy v1.14.0
github.com/patrickmn/go-cache v2.1.0+incompatible
Expand Down Expand Up @@ -197,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 @@ -347,6 +348,8 @@ github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxU
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0 h1:mmJCWLe63QvybxhW1iBmQWEaCKdc4SKgALfTNZ+OphU=
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0/go.mod h1:mDunUZ1IUJdJIRHvFb+LPBUtxe3AYB5MI6BMXNg8194=
github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM=
github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk=
github.com/migueleliasweb/go-github-mock v1.0.1 h1:amLEECVny28RCD1ElALUpQxrAimamznkg9rN2O7t934=
Expand Down Expand Up @@ -486,8 +489,6 @@ 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=
Expand Down
1 change: 1 addition & 0 deletions internal/directives/git_pr_opener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/akuity/kargo/internal/credentials"
"github.com/akuity/kargo/internal/gitprovider"

_ "github.com/akuity/kargo/internal/gitprovider/azure" // Azure provider registration
_ "github.com/akuity/kargo/internal/gitprovider/github" // GitHub provider registration
_ "github.com/akuity/kargo/internal/gitprovider/gitlab" // GitLab provider registration
)
Expand Down
4 changes: 2 additions & 2 deletions internal/directives/schemas/git-open-pr-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
},
"provider": {
"type": "string",
"description": "The name of the Git provider to use. Currently only 'github' and 'gitlab' are supported. Kargo will try to infer the provider if it is not explicitly specified.",
"enum": ["github", "gitlab"]
"description": "The name of the Git provider to use. Currently only 'github', 'gitlab' and 'azure' are supported. Kargo will try to infer the provider if it is not explicitly specified.",
"enum": ["github", "gitlab", "azure"]
},
"repoURL": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions internal/directives/schemas/git-wait-for-pr-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
},
"provider": {
"type": "string",
"description": "The name of the Git provider to use. Currently only 'github' and 'gitlab' are supported. Kargo will try to infer the provider if it is not explicitly specified.",
"enum": ["github", "gitlab"]
"description": "The name of the Git provider to use. Currently only 'github', 'gitlab' and 'azure' are supported. Kargo will try to infer the provider if it is not explicitly specified.",
"enum": ["github", "gitlab", "azure"]
},
"prNumber": {
"type": "number",
Expand Down
234 changes: 234 additions & 0 deletions internal/gitprovider/azure/azure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package azure

import (
"context"
"fmt"
"net/url"
"strings"

"github.com/akuity/kargo/internal/git"
"github.com/akuity/kargo/internal/gitprovider"
"k8s.io/utils/ptr"

"github.com/microsoft/azure-devops-go-api/azuredevops/v7"
adogit "github.com/microsoft/azure-devops-go-api/azuredevops/v7/git"
)

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>
//
// We support both forms.
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 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
krancour marked this conversation as resolved.
Show resolved Hide resolved
},
NewProvider: func(
repoURL string,
opts *gitprovider.Options,
) (gitprovider.Interface, error) {
return NewProvider(repoURL, opts)
},

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L41-L43

Added lines #L41 - L43 were not covered by tests
}

func init() {
gitprovider.Register(ProviderName, registration)
}

type provider struct {
org string
project string
repo string
connection *azuredevops.Connection
}

// NewProvider returns an Azure DevOps-based implementation of gitprovider.Interface.
func NewProvider(
repoURL string,
opts *gitprovider.Options,
) (gitprovider.Interface, error) {
if opts == nil || opts.Token == "" {
return nil, fmt.Errorf("token is required for Azure DevOps provider")
}
org, project, repo, err := parseRepoURL(repoURL)
if err != nil {
return nil, err
}
organizationUrl := fmt.Sprintf("https://%s/%s", modernHostSuffix, org)
connection := azuredevops.NewPatConnection(organizationUrl, opts.Token)

return &provider{
org: org,
project: project,
repo: repo,
connection: connection,
}, nil

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L61-L77

Added lines #L61 - L77 were not covered by tests
}

// CreatePullRequest implements gitprovider.Interface.
func (p *provider) CreatePullRequest(
ctx context.Context,
opts *gitprovider.CreatePullRequestOpts,
) (*gitprovider.PullRequest, error) {
gitClient, err := adogit.NewClient(ctx, p.connection)
if err != nil {
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, 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))
targetRefName := ptr.To(fmt.Sprintf("refs/heads/%s", opts.Base))
adoPR, err := gitClient.CreatePullRequest(ctx, adogit.CreatePullRequestArgs{
Project: &p.project,
RepositoryId: repoID,
GitPullRequestToCreate: &adogit.GitPullRequest{
Title: &opts.Title,
Description: &opts.Description,
SourceRefName: sourceRefName,
TargetRefName: targetRefName,
},
})
if err != nil {
return nil, fmt.Errorf("error creating pull request from %q to %q: %w", opts.Head, opts.Base, err)
}
pr, err := convertADOPullRequest(adoPR)
if err != nil {
return nil, fmt.Errorf("error converting pull request %d: %w", adoPR.PullRequestId, err)
}
return pr, nil

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L84-L116

Added lines #L84 - L116 were not covered by tests
}

// GetPullRequest implements gitprovider.Interface.
func (p *provider) GetPullRequest(
ctx context.Context,
id int64,
) (*gitprovider.PullRequest, error) {
gitClient, err := adogit.NewClient(ctx, p.connection)
if err != nil {
return nil, err
}
adoPR, err := gitClient.GetPullRequest(ctx, adogit.GetPullRequestArgs{
Project: &p.project,
RepositoryId: &p.repo,
PullRequestId: ptr.To(int(id)),
})
if err != nil {
return nil, err
}
pr, err := convertADOPullRequest(adoPR)
if err != nil {
return nil, fmt.Errorf("error converting pull request %d: %w", id, err)
}
return pr, nil

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L123-L140

Added lines #L123 - L140 were not covered by tests
}

// ListPullRequests implements gitprovider.Interface.
func (p *provider) ListPullRequests(
ctx context.Context,
opts *gitprovider.ListPullRequestOptions,
) ([]gitprovider.PullRequest, error) {
gitClient, err := adogit.NewClient(ctx, p.connection)
if err != nil {
return nil, err
}
adoPRs, err := gitClient.GetPullRequests(ctx, adogit.GetPullRequestsArgs{
Project: &p.project,
RepositoryId: &p.repo,
SearchCriteria: &adogit.GitPullRequestSearchCriteria{
Status: ptr.To(mapADOPrState(opts.State)),
SourceRefName: ptr.To(opts.HeadBranch),
TargetRefName: ptr.To(opts.BaseBranch),
},
})
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L147-L163

Added lines #L147 - L163 were not covered by tests

pts := []gitprovider.PullRequest{}
for _, adoPR := range *adoPRs {
pr, err := convertADOPullRequest(&adoPR)
if err != nil {
return nil, fmt.Errorf("error converting pull request %d: %w", adoPR.PullRequestId, err)
}
pts = append(pts, *pr)

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L165-L171

Added lines #L165 - L171 were not covered by tests
}
return pts, nil

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L173

Added line #L173 was not covered by tests
}

// mapADOPrState maps a gitprovider.PullRequestState to an adogit.PullRequestStatus.
func mapADOPrState(state gitprovider.PullRequestState) adogit.PullRequestStatus {
switch state {
case gitprovider.PullRequestStateOpen:
return adogit.PullRequestStatusValues.Active
case gitprovider.PullRequestStateClosed:
return adogit.PullRequestStatusValues.Completed

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L177-L182

Added lines #L177 - L182 were not covered by tests
}
return adogit.PullRequestStatusValues.All

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L184

Added line #L184 was not covered by tests
}

// convertADOPullRequest converts an adogit.GitPullRequest to a gitprovider.PullRequest.
func convertADOPullRequest(pr *adogit.GitPullRequest) (*gitprovider.PullRequest, error) {
if pr.LastMergeSourceCommit == nil {
return nil, fmt.Errorf("no last merge source commit found for pull request %d", ptr.Deref(pr.PullRequestId, 0))
}
mergeCommit := ptr.Deref(pr.LastMergeCommit, adogit.GitCommitRef{})
return &gitprovider.PullRequest{
Number: int64(ptr.Deref(pr.PullRequestId, 0)),
URL: ptr.Deref(pr.Url, ""),
Open: ptr.Deref(pr.Status, "notSet") == "active",
Merged: ptr.Deref(pr.Status, "notSet") == "completed",
MergeCommitSHA: ptr.Deref(mergeCommit.CommitId, ""),
Object: pr,
HeadSHA: ptr.Deref(pr.LastMergeSourceCommit.CommitId, ""),
}, nil

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

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L188-L201

Added lines #L188 - L201 were not covered by tests
}

func parseRepoURL(repoURL string) (string, string, string, error) {
u, err := url.Parse(git.NormalizeURL(repoURL))
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 == modernHostSuffix {
return parseModernRepoURL(u)
} else if strings.HasSuffix(u.Host, legacyHostSuffix) {
return parseLegacyRepoURL(u)
}
return "", "", "", fmt.Errorf("unsupported host %q", u.Host)
}

// parseModernRepoURL parses a modern Azure DevOps repository URL. example: https://dev.azure.com/org/project/_git/repo
func parseModernRepoURL(u *url.URL) (string, string, string, error) {
parts := strings.Split(u.Path, "/")
if len(parts) != 5 {
return "", "", "", fmt.Errorf("could not extract repository organization, project, and name from URL %q", u)
}
return parts[1], parts[2], parts[4], nil
}

// parseLegacyRepoURL parses a legacy Azure DevOps repository URL. example: https://org.visualstudio.com/project/_git/repo
func parseLegacyRepoURL(u *url.URL) (string, string, string, error) {
organization := strings.TrimSuffix(u.Host, ".visualstudio.com")
parts := strings.Split(u.Path, "/")
if len(parts) != 4 {
return "", "", "", fmt.Errorf("could not extract repository organization, project, and name from URL %q", u)
}
return organization, parts[1], parts[3], nil
}
Loading
Loading