Skip to content

Commit

Permalink
Merge pull request #260 from adivishy1/master
Browse files Browse the repository at this point in the history
[feat]: [CDS-73030]: Support for text based branch filtration
  • Loading branch information
rajatharanganath authored Jul 7, 2023
2 parents ad21d44 + aefd6d1 commit 3947016
Show file tree
Hide file tree
Showing 26 changed files with 603 additions and 0 deletions.
7 changes: 7 additions & 0 deletions scm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ type (
Reset int64
}

// BranchListOptions specifies optional branch search term and pagination
// parameters.
BranchListOptions struct {
SearchTerm string
PageListOptions ListOptions
}

// ListOptions specifies optional pagination
// parameters.
ListOptions struct {
Expand Down
11 changes: 11 additions & 0 deletions scm/driver/azure/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ func (s *gitService) ListBranches(ctx context.Context, repo string, _ scm.ListOp
return convertBranchList(out.Value), res, err
}

func (s *gitService) ListBranchesV2(ctx context.Context, repo string, opts scm.BranchListOptions) ([]*scm.Reference, *scm.Response, error) {
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/list?view=azure-devops-rest-6.0
if s.client.project == "" {
return nil, nil, ProjectRequiredError()
}
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/refs?api-version=6.0&filterContains=%s", s.client.owner, s.client.project, repo, opts.SearchTerm)
out := new(branchList)
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
return convertBranchList(out.Value), res, err
}

func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/commits/get-commits?view=azure-devops-rest-6.0
if s.client.project == "" {
Expand Down
26 changes: 26 additions & 0 deletions scm/driver/azure/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ func TestGitListBranches(t *testing.T) {
}
}

func TestGitListBranchesV2(t *testing.T) {
defer gock.Off()

gock.New("https:/dev.azure.com/").
Get("/ORG/PROJ/_apis/git/repositories/REPOID/").
Reply(200).
Type("application/json").
File("testdata/branchesFilter.json")

client := NewDefault("ORG", "PROJ")
got, _, err := client.Git.ListBranchesV2(context.Background(), "REPOID", scm.BranchListOptions{SearchTerm: "main"})
if err != nil {
t.Error(err)
return
}

want := []*scm.Reference{}
raw, _ := ioutil.ReadFile("testdata/branchesFilter.json.golden")
_ = json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

func TestGitCompareChanges(t *testing.T) {
defer gock.Off()

Expand Down
41 changes: 41 additions & 0 deletions scm/driver/azure/testdata/branchesFilter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"value": [
{
"name": "refs/heads/main",
"objectId": "e0aee6aa543294d62520fb906689da6710af149c",
"creator": {
"displayName": "tp",
"url": "https://spsproduks1.vssps.visualstudio.com/A93f74f38-2b8d-42d4-a5cb-74646f46666e/_apis/Identities/3ff4a20f-306e-677e-8a01-57f35e71f109",
"_links": {
"avatar": {
"href": "https://dev.azure.com/tphoney/_apis/GraphProfile/MemberAvatars/msa.M2ZmNGEyMGYtMzA2ZS03NzdlLThhMDEtNTdmMzVlNzFmMTA5"
}
},
"id": "3ff4a20f-306e-677e-8a01-57f35e71f109",
"uniqueName": "tp@harness.io",
"imageUrl": "https://dev.azure.com/tphoney/_api/_common/identityImage?id=3ff4a20f-306e-677e-8a01-57f35e71f109",
"descriptor": "msa.M2ZmNGEyMGYtMzA2ZS03NzdlLThhMDEtNTdmMzVlNzFmMTA5"
},
"url": "https://dev.azure.com/tphoney/d350c9c0-7749-4ff8-a78f-f9c1f0e56729/_apis/git/repositories/fde2d21f-13b9-4864-a995-83329045289a/refs?filter=heads%2Fmain"
},
{
"name": "refs/heads/main-patch",
"objectId": "01768d964c03e97260af0bd8cd9e5cd1f9ac6356",
"creator": {
"displayName": "tp",
"url": "https://spsproduks1.vssps.visualstudio.com/A93f74f38-2b8d-42d4-a5cb-74646f46666e/_apis/Identities/3ff4a20f-306e-677e-8a01-57f35e71f109",
"_links": {
"avatar": {
"href": "https://dev.azure.com/tphoney/_apis/GraphProfile/MemberAvatars/msa.M2ZmNGEyMGYtMzA2ZS03NzdlLThhMDEtNTdmMzVlNzFmMTA5"
}
},
"id": "3ff4a20f-306e-677e-8a01-57f35e71f109",
"uniqueName": "tp@harness.io",
"imageUrl": "https://dev.azure.com/tphoney/_api/_common/identityImage?id=3ff4a20f-306e-677e-8a01-57f35e71f109",
"descriptor": "msa.M2ZmNGEyMGYtMzA2ZS03NzdlLThhMDEtNTdmMzVlNzFmMTA5"
},
"url": "https://dev.azure.com/tphoney/d350c9c0-7749-4ff8-a78f-f9c1f0e56729/_apis/git/repositories/fde2d21f-13b9-4864-a995-83329045289a/refs?filter=heads%2Fpr_branch"
}
],
"count": 2
}
12 changes: 12 additions & 0 deletions scm/driver/azure/testdata/branchesFilter.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"Name": "main",
"Path": "refs/heads/main",
"Sha": "e0aee6aa543294d62520fb906689da6710af149c"
},
{
"Name": "main-patch",
"Path": "refs/heads/main-patch",
"Sha": "01768d964c03e97260af0bd8cd9e5cd1f9ac6356"
}
]
7 changes: 7 additions & 0 deletions scm/driver/bitbucket/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.Lis
copyPagination(out.pagination, res)
return convertBranchList(out), res, err
}
func (s *gitService) ListBranchesV2(ctx context.Context, repo string, opts scm.BranchListOptions) ([]*scm.Reference, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/refs/branches?%s", repo, encodeBranchListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
return convertBranchList(out), res, err
}

func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/commits/%s?%s", repo, opts.Ref, encodeCommitListOptions(opts))
Expand Down
34 changes: 34 additions & 0 deletions scm/driver/bitbucket/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,40 @@ func TestGitListBranches(t *testing.T) {
t.Run("Page", testPage(res))
}

func TestGitListBranchesV2(t *testing.T) {
defer gock.Off()

gock.New("https://api.bitbucket.org").
Get("/2.0/repositories/atlassian/stash-example-plugin/refs").
MatchParam("q", "name~\\\"mast\\\"").
MatchParam("page", "1").
MatchParam("pagelen", "30").
Reply(200).
Type("application/json").
File("testdata/branchesFilter.json")

client, _ := New("https://api.bitbucket.org")
got, res, err := client.Git.ListBranchesV2(context.Background(), "atlassian/stash-example-plugin", scm.BranchListOptions{SearchTerm: "mast", PageListOptions: struct {
URL string
Page int
Size int
}{Page: 1, Size: 30}})
if err != nil {
t.Error(err)
}

want := []*scm.Reference{}
raw, _ := ioutil.ReadFile("testdata/branchesFilter.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}

t.Run("Page", testPage(res))
}

func TestGitListTags(t *testing.T) {
defer gock.Off()

Expand Down
201 changes: 201 additions & 0 deletions scm/driver/bitbucket/testdata/branchesFilter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"pagelen": 30,
"values": [
{
"type": "branch",
"name": "master",
"links": {
"commits": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commits\/master"
},
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/refs\/branches\/master"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin\/branch\/master"
}
},
"target": {
"hash": "a6e5e7d797edf751cbd839d6bd4aef86c941eec9",
"repository": {
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin"
},
"avatar": {
"href": "https:\/\/bytebucket.org\/ravatar\/%7B7dd600e6-0d9c-4801-b967-cb4cc17359ff%7D?ts=default"
}
},
"type": "repository",
"name": "stash-example-plugin",
"full_name": "atlassian\/stash-example-plugin",
"uuid": "{7dd600e6-0d9c-4801-b967-cb4cc17359ff}"
},
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
"comments": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9\/comments"
},
"patch": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/patch\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin\/commits\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
"diff": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/diff\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
"approve": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9\/approve"
},
"statuses": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9\/statuses"
}
},
"author": {
"raw": "Adam Ahmed <aahmed@atlassian.com>",
"type": "author",
"user": {
"username": "aahmed",
"display_name": "Adam Ahmed",
"account_id": "557057:74dc5efb-ffe7-49af-b427-6abc299bb3b9",
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/users\/aahmed"
},
"html": {
"href": "https:\/\/bitbucket.org\/aahmed\/"
},
"avatar": {
"href": "https:\/\/bitbucket.org\/account\/aahmed\/avatar\/32\/"
}
},
"type": "user",
"uuid": "{3d5de233-98d4-4138-b4af-8678fbb009ad}"
}
},
"parents": [
{
"hash": "5be6855032e171280a1acb860d7265c29f40487c",
"type": "commit",
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/5be6855032e171280a1acb860d7265c29f40487c"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin\/commits\/5be6855032e171280a1acb860d7265c29f40487c"
}
}
}
],
"date": "2015-08-27T03:25:04+00:00",
"message": "Add Apache 2.0 License\n",
"type": "commit"
}
},
{
"type": "branch",
"name": "master-patch",
"links": {
"commits": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commits\/master"
},
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/refs\/branches\/master"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin\/branch\/master"
}
},
"target": {
"hash": "a6e5e7d797edf751cbd839d6bd4aef86c941eec8",
"repository": {
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin"
},
"avatar": {
"href": "https:\/\/bytebucket.org\/ravatar\/%7B7dd600e6-0d9c-4801-b967-cb4cc17359ff%7D?ts=default"
}
},
"type": "repository",
"name": "stash-example-plugin",
"full_name": "atlassian\/stash-example-plugin",
"uuid": "{7dd600e6-0d9c-4801-b967-cb4cc17359ff}"
},
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
"comments": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9\/comments"
},
"patch": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/patch\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin\/commits\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
"diff": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/diff\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
"approve": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9\/approve"
},
"statuses": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/a6e5e7d797edf751cbd839d6bd4aef86c941eec9\/statuses"
}
},
"author": {
"raw": "Adam Ahmed <aahmed@atlassian.com>",
"type": "author",
"user": {
"username": "aahmed",
"display_name": "Adam Ahmed",
"account_id": "557057:74dc5efb-ffe7-49af-b427-6abc299bb3b9",
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/users\/aahmed"
},
"html": {
"href": "https:\/\/bitbucket.org\/aahmed\/"
},
"avatar": {
"href": "https:\/\/bitbucket.org\/account\/aahmed\/avatar\/32\/"
}
},
"type": "user",
"uuid": "{3d5de233-98d4-4138-b4af-8678fbb009ad}"
}
},
"parents": [
{
"hash": "5be6855032e171280a1acb860d7265c29f40487c",
"type": "commit",
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commit\/5be6855032e171280a1acb860d7265c29f40487c"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin\/commits\/5be6855032e171280a1acb860d7265c29f40487c"
}
}
}
],
"date": "2015-08-27T03:25:04+00:00",
"message": "Add Apache 2.0 License\n",
"type": "commit"
}
}
],
"page": 1,
"next": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin\/refs\/branches?pagelen=30&page=2"
}
12 changes: 12 additions & 0 deletions scm/driver/bitbucket/testdata/branchesFilter.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"Name": "master",
"Path": "refs/heads/master",
"Sha": "a6e5e7d797edf751cbd839d6bd4aef86c941eec9"
},
{
"Name": "master-patch",
"Path": "refs/heads/master-patch",
"Sha": "a6e5e7d797edf751cbd839d6bd4aef86c941eec8"
}
]
Loading

0 comments on commit 3947016

Please sign in to comment.