Skip to content

Commit e5c21cb

Browse files
committed
Add support for pagination options in rules API methods
Updated `GetRulesForBranch`, `GetAllRulesets`, and `GetAllRepositoryRulesets` methods to accept optional pagination parameters (`ListOptions`). Enhanced test cases to validate the use of these parameters in API requests.
1 parent 04274a9 commit e5c21cb

4 files changed

Lines changed: 57 additions & 11 deletions

File tree

github/orgs_rules.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ import (
1515
// GitHub API docs: https://docs.github.com/rest/orgs/rules#get-all-organization-repository-rulesets
1616
//
1717
//meta:operation GET /orgs/{org}/rulesets
18-
func (s *OrganizationsService) GetAllRepositoryRulesets(ctx context.Context, org string) ([]*RepositoryRuleset, *Response, error) {
18+
func (s *OrganizationsService) GetAllRepositoryRulesets(ctx context.Context, org string, opts *ListOptions) ([]*RepositoryRuleset, *Response, error) {
1919
u := fmt.Sprintf("orgs/%v/rulesets", org)
2020

21+
u, err := addOptions(u, opts)
22+
if err != nil {
23+
return nil, nil, err
24+
}
25+
2126
req, err := s.client.NewRequest("GET", u, nil)
2227
if err != nil {
2328
return nil, nil, err

github/orgs_rules_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func TestOrganizationsService_GetAllRepositoryRulesets(t *testing.T) {
2020

2121
mux.HandleFunc("/orgs/o/rulesets", func(w http.ResponseWriter, r *http.Request) {
2222
testMethod(t, r, "GET")
23+
testFormValues(t, r, values{
24+
"page": "2",
25+
"per_page": "35",
26+
})
2327
fmt.Fprint(w, `[{
2428
"id": 21,
2529
"name": "test ruleset",
@@ -37,8 +41,9 @@ func TestOrganizationsService_GetAllRepositoryRulesets(t *testing.T) {
3741
}]`)
3842
})
3943

44+
opts := &ListOptions{Page: 2, PerPage: 35}
4045
ctx := context.Background()
41-
rulesets, _, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o")
46+
rulesets, _, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o", opts)
4247
if err != nil {
4348
t.Errorf("Organizations.GetAllRepositoryRulesets returned error: %v", err)
4449
}
@@ -62,7 +67,7 @@ func TestOrganizationsService_GetAllRepositoryRulesets(t *testing.T) {
6267
const methodName = "GetAllRepositoryRulesets"
6368

6469
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
65-
got, resp, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o")
70+
got, resp, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o", opts)
6671
if got != nil {
6772
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
6873
}

github/repos_rules.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ type rulesetClearBypassActors struct {
3838
// GitHub API docs: https://docs.github.com/rest/repos/rules#get-rules-for-a-branch
3939
//
4040
//meta:operation GET /repos/{owner}/{repo}/rules/branches/{branch}
41-
func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo, branch string) (*BranchRules, *Response, error) {
41+
func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo, branch string, opts *ListOptions) (*BranchRules, *Response, error) {
4242
u := fmt.Sprintf("repos/%v/%v/rules/branches/%v", owner, repo, branch)
4343

44+
u, err := addOptions(u, opts)
45+
if err != nil {
46+
return nil, nil, err
47+
}
48+
4449
req, err := s.client.NewRequest("GET", u, nil)
4550
if err != nil {
4651
return nil, nil, err
@@ -55,14 +60,28 @@ func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo
5560
return rules, resp, nil
5661
}
5762

63+
// RepositoryListRulesetsOptions specifies optional parameters to the
64+
// RepositoriesService.GetAllRulesets method.
65+
type RepositoryListRulesetsOptions struct {
66+
// IncludesParents indicates whether to include rulesets configured at the organization or enterprise level that apply to the repository.
67+
IncludesParents *bool `url:"includes_parents,omitempty"`
68+
ListOptions
69+
}
70+
5871
// GetAllRulesets gets all the repository rulesets for the specified repository.
59-
// If includesParents is true, rulesets configured at the organization or enterprise level that apply to the repository will be returned.
72+
// By default, this endpoint will include rulesets configured at the organization or enterprise level that apply to the repository.
73+
// To exclude those rulesets, set the `RepositoryListRulesetsOptions.IncludesParents` parameter to `false`.
6074
//
6175
// GitHub API docs: https://docs.github.com/rest/repos/rules#get-all-repository-rulesets
6276
//
6377
//meta:operation GET /repos/{owner}/{repo}/rulesets
64-
func (s *RepositoriesService) GetAllRulesets(ctx context.Context, owner, repo string, includesParents bool) ([]*RepositoryRuleset, *Response, error) {
65-
u := fmt.Sprintf("repos/%v/%v/rulesets?includes_parents=%v", owner, repo, includesParents)
78+
func (s *RepositoriesService) GetAllRulesets(ctx context.Context, owner, repo string, opts *RepositoryListRulesetsOptions) ([]*RepositoryRuleset, *Response, error) {
79+
u := fmt.Sprintf("repos/%v/%v/rulesets", owner, repo)
80+
81+
u, err := addOptions(u, opts)
82+
if err != nil {
83+
return nil, nil, err
84+
}
6685

6786
req, err := s.client.NewRequest("GET", u, nil)
6887
if err != nil {

github/repos_rules_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
2020

2121
mux.HandleFunc("/repos/o/repo/rules/branches/branch", func(w http.ResponseWriter, r *http.Request) {
2222
testMethod(t, r, "GET")
23+
testFormValues(t, r, values{
24+
"page": "2",
25+
"per_page": "35",
26+
})
2327
fmt.Fprint(w, `[
2428
{
2529
"ruleset_id": 42069,
@@ -39,8 +43,9 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
3943
]`)
4044
})
4145

46+
opts := &ListOptions{Page: 2, PerPage: 35}
4247
ctx := context.Background()
43-
rules, _, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch")
48+
rules, _, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch", opts)
4449
if err != nil {
4550
t.Errorf("Repositories.GetRulesForBranch returned error: %v", err)
4651
}
@@ -57,7 +62,7 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
5762
const methodName = "GetRulesForBranch"
5863

5964
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
60-
got, resp, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch")
65+
got, resp, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch", opts)
6166
if got != nil {
6267
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
6368
}
@@ -71,6 +76,11 @@ func TestRepositoriesService_GetAllRulesets(t *testing.T) {
7176

7277
mux.HandleFunc("/repos/o/repo/rulesets", func(w http.ResponseWriter, r *http.Request) {
7378
testMethod(t, r, "GET")
79+
testFormValues(t, r, values{
80+
"includes_parents": "false",
81+
"page": "2",
82+
"per_page": "35",
83+
})
7484
fmt.Fprintf(w, `[
7585
{
7686
"id": 42,
@@ -93,8 +103,15 @@ func TestRepositoriesService_GetAllRulesets(t *testing.T) {
93103
]`, referenceTimeStr)
94104
})
95105

106+
opts := &RepositoryListRulesetsOptions{
107+
IncludesParents: Ptr(false),
108+
ListOptions: ListOptions{
109+
Page: 2,
110+
PerPage: 35,
111+
},
112+
}
96113
ctx := context.Background()
97-
ruleSet, _, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", false)
114+
ruleSet, _, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", opts)
98115
if err != nil {
99116
t.Errorf("Repositories.GetAllRulesets returned error: %v", err)
100117
}
@@ -126,7 +143,7 @@ func TestRepositoriesService_GetAllRulesets(t *testing.T) {
126143
const methodName = "GetAllRulesets"
127144

128145
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
129-
got, resp, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", false)
146+
got, resp, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", opts)
130147
if got != nil {
131148
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
132149
}

0 commit comments

Comments
 (0)