Skip to content

Commit ebf056b

Browse files
author
dhruv
committed
fix: [CDE-997]: Fixing ListBranchesV2 method for harness driver.
1 parent 98b7432 commit ebf056b

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
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: 5 additions & 7 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

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

0 commit comments

Comments
 (0)