Skip to content

Commit 15eef26

Browse files
Merge pull request #342 from drone/CDE-997
fix: [CDE-997]: Fixing ListBranchesV2 method for harness driver.
2 parents 98b7432 + bac4f9e commit 15eef26

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

scm/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type (
7272
// parameters.
7373
BranchListOptions struct {
7474
SearchTerm string
75+
IncludeCommit bool
7576
PageListOptions ListOptions
7677
}
7778

scm/driver/harness/git.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,32 @@ func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Refer
6161
}
6262

6363
func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
64+
return s.listBranches(ctx, repo, scm.BranchListOptions{
65+
PageListOptions: opts,
66+
})
67+
}
68+
69+
func (s *gitService) listBranches(ctx context.Context, repo string, opts scm.BranchListOptions) ([]*scm.Reference, *scm.Response, error) {
6470
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
6571
repoID, queryParams, err := getRepoAndQueryParams(harnessURI)
6672
if err != nil {
6773
return nil, nil, err
6874
}
69-
path := fmt.Sprintf("api/v1/repos/%s/branches?%s&%s", repoID, encodeListOptions(opts), queryParams)
75+
76+
queryParams = fmt.Sprintf("%s&include_commit=%t", queryParams, opts.IncludeCommit)
77+
78+
if opts.SearchTerm != "" {
79+
queryParams = fmt.Sprintf("%s&query=%s", queryParams, opts.SearchTerm)
80+
}
81+
82+
path := fmt.Sprintf("api/v1/repos/%s/branches?%s&%s", repoID, encodeListOptions(opts.PageListOptions), queryParams)
7083
out := []*branch{}
7184
res, err := s.client.do(ctx, "GET", path, nil, &out)
7285
return convertBranchList(out), res, err
7386
}
7487

7588
func (s *gitService) ListBranchesV2(ctx context.Context, repo string, opts scm.BranchListOptions) ([]*scm.Reference, *scm.Response, error) {
76-
// Harness doesnt provide support listing based on searchTerm
77-
// Hence calling the ListBranches
78-
return s.ListBranches(ctx, repo, opts.PageListOptions)
89+
return s.listBranches(ctx, repo, opts)
7990
}
8091

8192
func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {

scm/driver/harness/repo.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,15 @@ func (s *repositoryService) list(ctx context.Context, opts scm.RepoListOptions)
6767
queryParams = fmt.Sprintf("%s&query=%s", queryParams, opts.RepoSearchTerm.RepoName)
6868
}
6969

70-
sortKey := defaultSortKey
71-
if opts.ListOptions.SortKey != "" {
72-
sortKey = opts.ListOptions.SortKey
70+
if opts.ListOptions.SortKey == "" {
71+
opts.ListOptions.SortKey = defaultSortKey
7372
}
7473

75-
order := defaultOrder
76-
if opts.ListOptions.Order != "" {
77-
order = opts.ListOptions.Order
74+
if opts.ListOptions.Order == "" {
75+
opts.ListOptions.Order = defaultOrder
7876
}
7977

80-
path := fmt.Sprintf("api/v1/repos?sort=%s&order=%s&%s&%s", sortKey, order, encodeListOptions(opts.ListOptions), queryParams)
78+
path := fmt.Sprintf("api/v1/repos?%s&%s", encodeListOptions(opts.ListOptions), queryParams)
8179
out := []*repository{}
8280
res, err := s.client.do(ctx, "GET", path, nil, &out)
8381
return convertRepositoryList(out), res, err
@@ -98,7 +96,13 @@ func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts scm
9896
if err != nil {
9997
return nil, nil, err
10098
}
101-
path := fmt.Sprintf("api/v1/repos/%s/webhooks?sort=display_name&order=asc&%s&%s", repoId, encodeListOptions(opts), queryParams)
99+
if opts.SortKey == "" {
100+
opts.SortKey = "display_name"
101+
}
102+
if opts.Order == "" {
103+
opts.Order = "asc"
104+
}
105+
path := fmt.Sprintf("api/v1/repos/%s/webhooks?%s&%s", repoId, encodeListOptions(opts), queryParams)
102106
out := []*hook{}
103107
res, err := s.client.do(ctx, "GET", path, nil, &out)
104108
return convertHookList(out), res, err

scm/driver/harness/util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ func encodeListOptions(opts scm.ListOptions) string {
7777
if opts.Size != 0 {
7878
params.Set("limit", strconv.Itoa(opts.Size))
7979
}
80+
if opts.SortKey != "" {
81+
params.Set("sort", opts.SortKey)
82+
}
83+
if opts.Order != "" {
84+
params.Set("order", opts.Order)
85+
}
8086
return params.Encode()
8187
}
8288

scm/driver/harness/util_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212

1313
func Test_encodeListOptions(t *testing.T) {
1414
opts := scm.ListOptions{
15-
Page: 10,
16-
Size: 30,
15+
Page: 10,
16+
Size: 30,
17+
SortKey: "date",
18+
Order: "asc",
1719
}
18-
want := "limit=30&page=10"
20+
want := "limit=30&order=asc&page=10&sort=date"
1921
got := encodeListOptions(opts)
2022
if got != want {
2123
t.Errorf("Want encoded list options %q, got %q", want, got)

0 commit comments

Comments
 (0)