Skip to content
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

feat!: Add pagination to Repository/Organization Rules Get methods #3236

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
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 @@
// GitHub API docs: https://docs.github.com/rest/orgs/rules#get-all-organization-repository-rulesets
//
//meta:operation GET /orgs/{org}/rulesets
func (s *OrganizationsService) GetAllOrganizationRulesets(ctx context.Context, org string) ([]*Ruleset, *Response, error) {
func (s *OrganizationsService) GetAllOrganizationRulesets(ctx context.Context, org string, opts *ListOptions) ([]*Ruleset, *Response, error) {
u := fmt.Sprintf("orgs/%v/rulesets", org)

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

Check warning on line 23 in github/orgs_rules.go

View check run for this annotation

Codecov / codecov/patch

github/orgs_rules.go#L23

Added line #L23 was not covered by tests
}

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

mux.HandleFunc("/orgs/o/rulesets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"per_page": "2", "page": "2"})
fmt.Fprint(w, `[{
"id": 26110,
"name": "test ruleset",
Expand All @@ -38,7 +39,7 @@ func TestOrganizationsService_GetAllOrganizationRulesets(t *testing.T) {
})

ctx := context.Background()
rulesets, _, err := client.Organizations.GetAllOrganizationRulesets(ctx, "o")
rulesets, _, err := client.Organizations.GetAllOrganizationRulesets(ctx, "o", &ListOptions{Page: 2, PerPage: 2})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After line 63 below, please add:

	testBadOptions(t, methodName, func() (err error) {
		_, _, err = client.Organizations.GetAllOrganizationRulesets(ctx, "\n", &ListOptions{})
		return err
	})

if err != nil {
t.Errorf("Organizations.GetAllOrganizationRulesets returned error: %v", err)
}
Expand All @@ -62,7 +63,7 @@ func TestOrganizationsService_GetAllOrganizationRulesets(t *testing.T) {
const methodName = "GetAllOrganizationRulesets"

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Organizations.GetAllOrganizationRulesets(ctx, "o")
got, resp, err := client.Organizations.GetAllOrganizationRulesets(ctx, "o", nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down
14 changes: 12 additions & 2 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,14 @@
// 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) ([]*RepositoryRule, *Response, error) {
func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo, branch string, opts *ListOptions) ([]*RepositoryRule, *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

Check warning on line 439 in github/repos_rules.go

View check run for this annotation

Codecov / codecov/patch

github/repos_rules.go#L439

Added line #L439 was not covered by tests
}

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
Expand All @@ -454,9 +459,14 @@
// 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) ([]*Ruleset, *Response, error) {
func (s *RepositoriesService) GetAllRulesets(ctx context.Context, owner, repo string, includesParents bool, opts *ListOptions) ([]*Ruleset, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/rulesets?includes_parents=%v", owner, repo, includesParents)

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

Check warning on line 467 in github/repos_rules.go

View check run for this annotation

Codecov / codecov/patch

github/repos_rules.go#L467

Added line #L467 was not covered by tests
}

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

mux.HandleFunc("/repos/o/repo/rules/branches/branch", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"per_page": "2", "page": "2"})
fmt.Fprint(w, `[
{
"ruleset_id": 42069,
Expand All @@ -301,7 +302,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", &ListOptions{Page: 2, PerPage: 2})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After line 329 below, please add:

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

if err != nil {
t.Errorf("Repositories.GetRulesForBranch returned error: %v", err)
}
Expand All @@ -328,7 +329,7 @@ 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")
got, resp, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch", nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand All @@ -342,6 +343,7 @@ func TestRepositoriesService_GetRulesForBranchEmptyUpdateRule(t *testing.T) {

mux.HandleFunc("/repos/o/repo/rules/branches/branch", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"per_page": "2", "page": "2"})
fmt.Fprint(w, `[
{
"type": "update"
Expand All @@ -350,7 +352,7 @@ func TestRepositoriesService_GetRulesForBranchEmptyUpdateRule(t *testing.T) {
})

ctx := context.Background()
rules, _, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch")
rules, _, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch", &ListOptions{Page: 2, PerPage: 2})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After line 369 below, please add:

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

if err != nil {
t.Errorf("Repositories.GetRulesForBranch returned error: %v", err)
}
Expand All @@ -367,7 +369,7 @@ func TestRepositoriesService_GetRulesForBranchEmptyUpdateRule(t *testing.T) {
const methodName = "GetRulesForBranch"

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", nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand All @@ -381,6 +383,7 @@ func TestRepositoriesService_GetAllRulesets(t *testing.T) {

mux.HandleFunc("/repos/o/repo/rulesets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"per_page": "2", "page": "2"})
fmt.Fprint(w, `[
{
"id": 42,
Expand All @@ -400,7 +403,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", false, &ListOptions{Page: 2, PerPage: 2})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After line 431 below, please add:

	testBadOptions(t, methodName, func() (err error) {
		_, _, err = client.Repositories.GetRulesets(ctx, "\n", "\n", false, &ListOptions{})
		return err
	})

if err != nil {
t.Errorf("Repositories.GetAllRulesets returned error: %v", err)
}
Expand Down Expand Up @@ -428,7 +431,7 @@ 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", false)
got, resp, err := client.Repositories.GetAllRulesets(ctx, "o", "repo", false, nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down
Loading