Skip to content

Commit dbbf69f

Browse files
[feat]: [PL-25025]: Updated Project validation for Azure API (#179)
* [PL-25025]: Project Validation changes for Azure
1 parent 78b5e76 commit dbbf69f

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

scm/driver/azure/azure.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"bytes"
1010
"context"
1111
"encoding/json"
12+
"errors"
1213
"fmt"
1314
"net/url"
1415
"strings"
@@ -116,6 +117,10 @@ func (e *Error) Error() string {
116117
return e.Message
117118
}
118119

120+
func ProjectRequiredError() error {
121+
return errors.New("This API endpoint requires a project to be specified")
122+
}
123+
119124
func SanitizeBranchName(name string) string {
120125
if strings.Contains(name, "/") {
121126
return name

scm/driver/azure/content.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type contentService struct {
1818

1919
func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
2020
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/items/get?view=azure-devops-rest-6.0
21+
if s.client.project == "" {
22+
return nil, nil, ProjectRequiredError()
23+
}
2124
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/items?path=%s&includeContent=true&$format=json", s.client.owner, s.client.project, repo, path)
2225
endpoint += generateURIFromRef(ref)
2326
endpoint += "&api-version=6.0"
@@ -33,6 +36,9 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm
3336
}
3437

3538
func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
39+
if s.client.project == "" {
40+
return nil, ProjectRequiredError()
41+
}
3642
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/pushes?api-version=6.0", s.client.owner, s.client.project, repo)
3743
ref := refUpdate{
3844
Name: SanitizeBranchName(params.Branch),
@@ -59,6 +65,9 @@ func (s *contentService) Create(ctx context.Context, repo, path string, params *
5965
}
6066

6167
func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
68+
if s.client.project == "" {
69+
return nil, ProjectRequiredError()
70+
}
6271
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/pushes?api-version=6.0", s.client.owner, s.client.project, repo)
6372
ref := refUpdate{
6473
Name: SanitizeBranchName(params.Branch),
@@ -85,6 +94,9 @@ func (s *contentService) Update(ctx context.Context, repo, path string, params *
8594
}
8695

8796
func (s *contentService) Delete(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
97+
if s.client.project == "" {
98+
return nil, ProjectRequiredError()
99+
}
88100
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/pushes?api-version=6.0", s.client.owner, s.client.project, repo)
89101
ref := refUpdate{
90102
Name: SanitizeBranchName(params.Branch),
@@ -109,6 +121,9 @@ func (s *contentService) Delete(ctx context.Context, repo, path string, params *
109121

110122
func (s *contentService) List(ctx context.Context, repo, path, ref string, _ scm.ListOptions) ([]*scm.ContentInfo, *scm.Response, error) {
111123
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/items/list?view=azure-devops-rest-6.0
124+
if s.client.project == "" {
125+
return nil, nil, ProjectRequiredError()
126+
}
112127
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/items?path=%s&recursionLevel=Full&$format=json", s.client.owner, s.client.project, repo, path)
113128
endpoint += generateURIFromRef(ref)
114129
out := new(contentList)

scm/driver/azure/git.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type gitService struct {
1818

1919
func (s *gitService) CreateBranch(ctx context.Context, repo string, params *scm.CreateBranch) (*scm.Response, error) {
2020
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/update-refs?view=azure-devops-rest-6.0
21+
if s.client.project == "" {
22+
return nil, ProjectRequiredError()
23+
}
2124
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/refs?api-version=6.0", s.client.owner, s.client.project, repo)
2225

2326
in := make(crudBranch, 1)
@@ -28,11 +31,17 @@ func (s *gitService) CreateBranch(ctx context.Context, repo string, params *scm.
2831
}
2932

3033
func (s *gitService) FindBranch(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
34+
if s.client.project == "" {
35+
return nil, nil, ProjectRequiredError()
36+
}
3137
return nil, nil, scm.ErrNotSupported
3238
}
3339

3440
func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Commit, *scm.Response, error) {
3541
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/commits/get?view=azure-devops-rest-6.0#get-by-id
42+
if s.client.project == "" {
43+
return nil, nil, ProjectRequiredError()
44+
}
3645
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/commits/%s?api-version=6.0", s.client.owner, s.client.project, repo, ref)
3746
out := new(gitCommit)
3847
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
@@ -45,6 +54,9 @@ func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Refer
4554

4655
func (s *gitService) ListBranches(ctx context.Context, repo string, _ scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
4756
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/list?view=azure-devops-rest-6.0
57+
if s.client.project == "" {
58+
return nil, nil, ProjectRequiredError()
59+
}
4860
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/refs?api-version=6.0", s.client.owner, s.client.project, repo)
4961
out := new(branchList)
5062
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
@@ -53,6 +65,9 @@ func (s *gitService) ListBranches(ctx context.Context, repo string, _ scm.ListOp
5365

5466
func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
5567
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/commits/get-commits?view=azure-devops-rest-6.0
68+
if s.client.project == "" {
69+
return nil, nil, ProjectRequiredError()
70+
}
5671
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/commits?", s.client.owner, s.client.project, repo)
5772
if opts.Ref != "" {
5873
endpoint += fmt.Sprintf("searchCriteria.itemVersion.version=%s&", opts.Ref)
@@ -77,6 +92,9 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
7792

7893
func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
7994
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/diffs/get?view=azure-devops-rest-6.0
95+
if s.client.project == "" {
96+
return nil, nil, ProjectRequiredError()
97+
}
8098
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/diffs/commits?", s.client.owner, s.client.project, repo)
8199
// add base
82100
endpoint += fmt.Sprintf("baseVersion=%s&baseVersionType=commit&", source)

scm/driver/azure/repo.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type RepositoryService struct {
2020
// Find returns the repository by name.
2121
func (s *RepositoryService) Find(ctx context.Context, repo string) (*scm.Repository, *scm.Response, error) {
2222
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/get?view=azure-devops-rest-4.1
23+
if s.client.project == "" {
24+
return nil, nil, ProjectRequiredError()
25+
}
2326
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s?api-version=6.0", s.client.owner, s.client.project, repo)
2427

2528
out := new(repository)
@@ -55,6 +58,9 @@ func (s *RepositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*
5558
// ListHooks returns a list or repository hooks.
5659
func (s *RepositoryService) ListHooks(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Hook, *scm.Response, error) {
5760
// https://docs.microsoft.com/en-us/rest/api/azure/devops/hooks/subscriptions/list?view=azure-devops-rest-6.0
61+
if s.client.project == "" {
62+
return nil, nil, ProjectRequiredError()
63+
}
5864
endpoint := fmt.Sprintf("%s/_apis/hooks/subscriptions?api-version=6.0", s.client.owner)
5965
out := new(subscriptions)
6066
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
@@ -69,6 +75,9 @@ func (s *RepositoryService) ListStatus(ctx context.Context, repo, ref string, op
6975
// CreateHook creates a new repository webhook.
7076
func (s *RepositoryService) CreateHook(ctx context.Context, repo string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
7177
// https://docs.microsoft.com/en-us/rest/api/azure/devops/hooks/subscriptions/create?view=azure-devops-rest-6.0
78+
if s.client.project == "" {
79+
return nil, nil, ProjectRequiredError()
80+
}
7281
endpoint := fmt.Sprintf("%s/_apis/hooks/subscriptions?api-version=6.0", s.client.owner)
7382
in := new(subscription)
7483
in.Status = "enabled"
@@ -119,6 +128,9 @@ func (s *RepositoryService) UpdateHook(ctx context.Context, repo, id string, inp
119128
// DeleteHook deletes a repository webhook.
120129
func (s *RepositoryService) DeleteHook(ctx context.Context, repo, id string) (*scm.Response, error) {
121130
// https://docs.microsoft.com/en-us/rest/api/azure/devops/hooks/subscriptions/delete?view=azure-devops-rest-6.0
131+
if s.client.project == "" {
132+
return nil, ProjectRequiredError()
133+
}
122134
endpoint := fmt.Sprintf("%s/_apis/hooks/subscriptions/%s?api-version=6.0", s.client.owner, id)
123135
return s.client.do(ctx, "DELETE", endpoint, nil, nil)
124136
}

0 commit comments

Comments
 (0)