Skip to content

Commit 98b139a

Browse files
committed
add pagination for pull requests
1 parent abd7a82 commit 98b139a

File tree

2 files changed

+75
-53
lines changed

2 files changed

+75
-53
lines changed

client/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
var (
2121
defaultBaseURL = "https://app.harness.io/"
2222

23+
// these can be moved to a level above if we want to make this a generic
24+
// client, keeping these here to ensure we don't end up returning too much info
25+
// when different tools get added.
2326
defaultPageSize = 5
2427
maxPageSize = 20
2528

client/pullrequest.go

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,65 +32,84 @@ func (p *PullRequestService) Get(ctx context.Context, scope dto.Scope, repoID st
3232
return pr, nil
3333
}
3434

35+
// setDefaultPaginationForPR sets default pagination values for PullRequestOptions
36+
// We don't use the generic functions here since it follows a different naming
37+
// (uses page and limit instead of page and size)
38+
func setDefaultPaginationForPR(opts *dto.PullRequestOptions) {
39+
if opts == nil {
40+
return
41+
}
42+
if opts.Page <= 0 {
43+
opts.Page = 1
44+
}
45+
46+
if opts.Limit <= 0 {
47+
opts.Limit = defaultPageSize
48+
} else if opts.Limit > maxPageSize {
49+
opts.Limit = maxPageSize
50+
}
51+
}
52+
3553
func (p *PullRequestService) List(ctx context.Context, scope dto.Scope, repoID string, opts *dto.PullRequestOptions) ([]*dto.PullRequest, error) {
3654
path := fmt.Sprintf(pullRequestListPath, repoID)
3755
params := make(map[string]string)
3856
addScope(scope, params)
3957

40-
// Add query parameters from options
41-
if opts != nil {
42-
if len(opts.State) > 0 {
43-
params["state"] = strings.Join(opts.State, ",")
44-
}
45-
if opts.SourceRepoRef != "" {
46-
params["source_repo_ref"] = opts.SourceRepoRef
47-
}
48-
if opts.SourceBranch != "" {
49-
params["source_branch"] = opts.SourceBranch
50-
}
51-
if opts.TargetBranch != "" {
52-
params["target_branch"] = opts.TargetBranch
53-
}
54-
if opts.Query != "" {
55-
params["query"] = opts.Query
56-
}
57-
if len(opts.CreatedBy) > 0 {
58-
createdByStrings := make([]string, len(opts.CreatedBy))
59-
for i, id := range opts.CreatedBy {
60-
createdByStrings[i] = fmt.Sprintf("%d", id)
61-
}
62-
params["created_by"] = strings.Join(createdByStrings, ",")
63-
}
64-
if opts.Order != "" {
65-
params["order"] = opts.Order
66-
}
67-
if opts.Sort != "" {
68-
params["sort"] = opts.Sort
69-
}
70-
if opts.CreatedLt > 0 {
71-
params["created_lt"] = fmt.Sprintf("%d", opts.CreatedLt)
72-
}
73-
if opts.CreatedGt > 0 {
74-
params["created_gt"] = fmt.Sprintf("%d", opts.CreatedGt)
75-
}
76-
if opts.UpdatedLt > 0 {
77-
params["updated_lt"] = fmt.Sprintf("%d", opts.UpdatedLt)
78-
}
79-
if opts.UpdatedGt > 0 {
80-
params["updated_gt"] = fmt.Sprintf("%d", opts.UpdatedGt)
81-
}
82-
if opts.Page > 0 {
83-
params["page"] = fmt.Sprintf("%d", opts.Page)
84-
}
85-
if opts.Limit > 0 {
86-
params["limit"] = fmt.Sprintf("%d", opts.Limit)
87-
}
88-
if opts.AuthorID > 0 {
89-
params["author_id"] = fmt.Sprintf("%d", opts.AuthorID)
90-
}
91-
if opts.IncludeChecks {
92-
params["include_checks"] = "true"
58+
// Handle nil options by creating default options
59+
if opts == nil {
60+
opts = &dto.PullRequestOptions{}
61+
}
62+
63+
setDefaultPaginationForPR(opts)
64+
65+
params["page"] = fmt.Sprintf("%d", opts.Page)
66+
params["limit"] = fmt.Sprintf("%d", opts.Limit)
67+
68+
if len(opts.State) > 0 {
69+
params["state"] = strings.Join(opts.State, ",")
70+
}
71+
if opts.SourceRepoRef != "" {
72+
params["source_repo_ref"] = opts.SourceRepoRef
73+
}
74+
if opts.SourceBranch != "" {
75+
params["source_branch"] = opts.SourceBranch
76+
}
77+
if opts.TargetBranch != "" {
78+
params["target_branch"] = opts.TargetBranch
79+
}
80+
if opts.Query != "" {
81+
params["query"] = opts.Query
82+
}
83+
if len(opts.CreatedBy) > 0 {
84+
createdByStrings := make([]string, len(opts.CreatedBy))
85+
for i, id := range opts.CreatedBy {
86+
createdByStrings[i] = fmt.Sprintf("%d", id)
9387
}
88+
params["created_by"] = strings.Join(createdByStrings, ",")
89+
}
90+
if opts.Order != "" {
91+
params["order"] = opts.Order
92+
}
93+
if opts.Sort != "" {
94+
params["sort"] = opts.Sort
95+
}
96+
if opts.CreatedLt > 0 {
97+
params["created_lt"] = fmt.Sprintf("%d", opts.CreatedLt)
98+
}
99+
if opts.CreatedGt > 0 {
100+
params["created_gt"] = fmt.Sprintf("%d", opts.CreatedGt)
101+
}
102+
if opts.UpdatedLt > 0 {
103+
params["updated_lt"] = fmt.Sprintf("%d", opts.UpdatedLt)
104+
}
105+
if opts.UpdatedGt > 0 {
106+
params["updated_gt"] = fmt.Sprintf("%d", opts.UpdatedGt)
107+
}
108+
if opts.AuthorID > 0 {
109+
params["author_id"] = fmt.Sprintf("%d", opts.AuthorID)
110+
}
111+
if opts.IncludeChecks {
112+
params["include_checks"] = "true"
94113
}
95114

96115
var prs []*dto.PullRequest

0 commit comments

Comments
 (0)