Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions scm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type (
// parameters.
BranchListOptions struct {
SearchTerm string
IncludeCommit bool
PageListOptions ListOptions
}

Expand Down
19 changes: 15 additions & 4 deletions scm/driver/harness/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,32 @@ func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Refer
}

func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
return s.listBranches(ctx, repo, scm.BranchListOptions{
PageListOptions: opts,
})
}

func (s *gitService) listBranches(ctx context.Context, repo string, opts scm.BranchListOptions) ([]*scm.Reference, *scm.Response, error) {
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
repoID, queryParams, err := getRepoAndQueryParams(harnessURI)
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("api/v1/repos/%s/branches?%s&%s", repoID, encodeListOptions(opts), queryParams)

queryParams = fmt.Sprintf("%s&include_commit=%t", queryParams, opts.IncludeCommit)

if opts.SearchTerm != "" {
queryParams = fmt.Sprintf("%s&query=%s", queryParams, opts.SearchTerm)
}

path := fmt.Sprintf("api/v1/repos/%s/branches?%s&%s", repoID, encodeListOptions(opts.PageListOptions), queryParams)
out := []*branch{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertBranchList(out), res, err
}

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

func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
Expand Down
20 changes: 12 additions & 8 deletions scm/driver/harness/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,15 @@ func (s *repositoryService) list(ctx context.Context, opts scm.RepoListOptions)
queryParams = fmt.Sprintf("%s&query=%s", queryParams, opts.RepoSearchTerm.RepoName)
}

sortKey := defaultSortKey
if opts.ListOptions.SortKey != "" {
sortKey = opts.ListOptions.SortKey
if opts.ListOptions.SortKey == "" {
opts.ListOptions.SortKey = defaultSortKey
}

order := defaultOrder
if opts.ListOptions.Order != "" {
order = opts.ListOptions.Order
if opts.ListOptions.Order == "" {
opts.ListOptions.Order = defaultOrder
}

path := fmt.Sprintf("api/v1/repos?sort=%s&order=%s&%s&%s", sortKey, order, encodeListOptions(opts.ListOptions), queryParams)
path := fmt.Sprintf("api/v1/repos?%s&%s", encodeListOptions(opts.ListOptions), queryParams)
out := []*repository{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertRepositoryList(out), res, err
Expand All @@ -98,7 +96,13 @@ func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts scm
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("api/v1/repos/%s/webhooks?sort=display_name&order=asc&%s&%s", repoId, encodeListOptions(opts), queryParams)
if opts.SortKey == "" {
opts.SortKey = "display_name"
}
if opts.Order == "" {
opts.Order = "asc"
}
path := fmt.Sprintf("api/v1/repos/%s/webhooks?%s&%s", repoId, encodeListOptions(opts), queryParams)
out := []*hook{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertHookList(out), res, err
Expand Down
6 changes: 6 additions & 0 deletions scm/driver/harness/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func encodeListOptions(opts scm.ListOptions) string {
if opts.Size != 0 {
params.Set("limit", strconv.Itoa(opts.Size))
}
if opts.SortKey != "" {
params.Set("sort", opts.SortKey)
}
if opts.Order != "" {
params.Set("order", opts.Order)
}
return params.Encode()
}

Expand Down
8 changes: 5 additions & 3 deletions scm/driver/harness/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (

func Test_encodeListOptions(t *testing.T) {
opts := scm.ListOptions{
Page: 10,
Size: 30,
Page: 10,
Size: 30,
SortKey: "date",
Order: "asc",
}
want := "limit=30&page=10"
want := "limit=30&order=asc&page=10&sort=date"
got := encodeListOptions(opts)
if got != want {
t.Errorf("Want encoded list options %q, got %q", want, got)
Expand Down