Skip to content

feat!: Add support for pagination options in rules API methods #3562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
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
8 changes: 8 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion github/orgs_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ import (
// GitHub API docs: https://docs.github.com/rest/orgs/rules#get-all-organization-repository-rulesets
//
//meta:operation GET /orgs/{org}/rulesets
func (s *OrganizationsService) GetAllRepositoryRulesets(ctx context.Context, org string) ([]*RepositoryRuleset, *Response, error) {
func (s *OrganizationsService) GetAllRepositoryRulesets(ctx context.Context, org string, opts *ListOptions) ([]*RepositoryRuleset, *Response, error) {
u := fmt.Sprintf("orgs/%v/rulesets", org)

u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
Expand Down
47 changes: 45 additions & 2 deletions github/orgs_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestOrganizationsService_GetAllRepositoryRulesets(t *testing.T) {
})

ctx := context.Background()
rulesets, _, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o")
rulesets, _, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o", nil)
if err != nil {
t.Errorf("Organizations.GetAllRepositoryRulesets returned error: %v", err)
}
Expand All @@ -60,9 +60,52 @@ func TestOrganizationsService_GetAllRepositoryRulesets(t *testing.T) {
}

const methodName = "GetAllRepositoryRulesets"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o", nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestOrganizationsService_GetAllRepositoryRulesets_ListOptions(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/orgs/o/rulesets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
"per_page": "35",
})
fmt.Fprint(w, `[{
"id": 21
}]`)
})

opts := &ListOptions{Page: 2, PerPage: 35}
ctx := context.Background()
rulesets, _, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o", opts)
if err != nil {
t.Errorf("Organizations.GetAllRepositoryRulesets returned error: %v", err)
}

want := []*RepositoryRuleset{{
ID: Ptr(int64(21)),
}}
if !cmp.Equal(rulesets, want) {
t.Errorf("Organizations.GetAllRepositoryRulesets returned %+v, want %+v", rulesets, want)
}

const methodName = "GetAllRepositoryRulesets"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Organizations.GetAllRepositoryRulesets(ctx, "\n", opts)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o")
got, resp, err := client.Organizations.GetAllRepositoryRulesets(ctx, "o", opts)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down
27 changes: 23 additions & 4 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ type rulesetClearBypassActors struct {
// GitHub API docs: https://docs.github.com/rest/repos/rules#get-rules-for-a-branch
//
//meta:operation GET /repos/{owner}/{repo}/rules/branches/{branch}
func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo, branch string) (*BranchRules, *Response, error) {
func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo, branch string, opts *ListOptions) (*BranchRules, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/rules/branches/%v", owner, repo, branch)

u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
Expand All @@ -55,14 +60,28 @@ func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo
return rules, resp, nil
}

// RepositoryListRulesetsOptions specifies optional parameters to the
// RepositoriesService.GetAllRulesets method.
type RepositoryListRulesetsOptions struct {
// IncludesParents indicates whether to include rulesets configured at the organization or enterprise level that apply to the repository.
IncludesParents *bool `url:"includes_parents,omitempty"`
ListOptions
}

// GetAllRulesets gets all the repository rulesets for the specified repository.
// If includesParents is true, rulesets configured at the organization or enterprise level that apply to the repository will be returned.
// By default, this endpoint will include rulesets configured at the organization or enterprise level that apply to the repository.
// To exclude those rulesets, set the `RepositoryListRulesetsOptions.IncludesParents` parameter to `false`.
//
// GitHub API docs: https://docs.github.com/rest/repos/rules#get-all-repository-rulesets
//
//meta:operation GET /repos/{owner}/{repo}/rulesets
func (s *RepositoriesService) GetAllRulesets(ctx context.Context, owner, repo string, includesParents bool) ([]*RepositoryRuleset, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/rulesets?includes_parents=%v", owner, repo, includesParents)
func (s *RepositoriesService) GetAllRulesets(ctx context.Context, owner, repo string, opts *RepositoryListRulesetsOptions) ([]*RepositoryRuleset, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/rulesets", owner, repo)

u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
Expand Down
109 changes: 105 additions & 4 deletions github/repos_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
})

ctx := context.Background()
rules, _, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch")
rules, _, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch", nil)
if err != nil {
t.Errorf("Repositories.GetRulesForBranch returned error: %v", err)
}
Expand All @@ -55,9 +55,56 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
}

const methodName = "GetRulesForBranch"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch", nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestRepositoriesService_GetRulesForBranch_ListOptions(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/repos/o/repo/rules/branches/branch", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
"per_page": "35",
})
fmt.Fprint(w, `[
{
"ruleset_id": 42069,
"type": "creation"
}
]`)
})

opts := &ListOptions{Page: 2, PerPage: 35}
ctx := context.Background()
rules, _, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch", opts)
if err != nil {
t.Errorf("Repositories.GetRulesForBranch returned error: %v", err)
}

want := &BranchRules{
Creation: []*BranchRuleMetadata{{RulesetID: 42069}},
}

if !cmp.Equal(rules, want) {
t.Errorf("Repositories.GetRulesForBranch returned %+v, want %+v", rules, want)
}

const methodName = "GetRulesForBranch"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.GetRulesForBranch(ctx, "\n", "\n", "\n", opts)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch")
got, resp, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch", opts)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down Expand Up @@ -94,7 +141,7 @@ func TestRepositoriesService_GetAllRulesets(t *testing.T) {
})

ctx := context.Background()
ruleSet, _, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", false)
ruleSet, _, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", nil)
if err != nil {
t.Errorf("Repositories.GetAllRulesets returned error: %v", err)
}
Expand Down Expand Up @@ -124,9 +171,63 @@ func TestRepositoriesService_GetAllRulesets(t *testing.T) {
}

const methodName = "GetAllRulesets"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestRepositoriesService_GetAllRulesets_ListOptions(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/repos/o/repo/rulesets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"includes_parents": "false",
"page": "2",
"per_page": "35",
})
fmt.Fprint(w, `[
{
"id": 42
}
]`)
})

opts := &RepositoryListRulesetsOptions{
IncludesParents: Ptr(false),
ListOptions: ListOptions{
Page: 2,
PerPage: 35,
},
}
ctx := context.Background()
ruleSet, _, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", opts)
if err != nil {
t.Errorf("Repositories.GetAllRulesets returned error: %v", err)
}

want := []*RepositoryRuleset{
{
ID: Ptr(int64(42)),
},
}
if !cmp.Equal(ruleSet, want) {
t.Errorf("Repositories.GetAllRulesets returned %+v, want %+v", ruleSet, want)
}

const methodName = "GetAllRulesets"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.GetAllRulesets(ctx, "\n", "\n", opts)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", false)
got, resp, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", opts)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down
Loading