Skip to content

Max per page #2703

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

Open
wants to merge 2 commits into
base: main
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
16 changes: 16 additions & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ func Provider() *schema.Provider {
},
},
},
// https://developer.github.com/guides/traversing-with-pagination/#basics-of-pagination
"max_per_page": {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITHUB_MAX_PER_PAGE", "100"),
Description: descriptions["max_per_page"],
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -305,6 +312,8 @@ func init() {
"Defaults to [500, 502, 503, 504]",
"max_retries": "Number of times to retry a request after receiving an error status code" +
"Defaults to 3",
"max_per_page": "Number of items per page for pagination" +
"Defaults to 100",
}
}

Expand Down Expand Up @@ -425,6 +434,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
log.Printf("[DEBUG] Setting retriableErrors to %v", retryableErrors)
}

_maxPerPage := d.Get("max_per_page").(int)
if _maxPerPage <= 0 {
return nil, diag.FromErr(fmt.Errorf("max_per_page must be greater than than 0"))
}
log.Printf("[DEBUG] Setting max_per_page to %d", _maxPerPage)
maxPerPage = _maxPerPage

parallelRequests := d.Get("parallel_requests").(bool)

if parallelRequests && isGithubDotCom {
Expand Down
31 changes: 31 additions & 0 deletions github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

var testAccProviders map[string]*schema.Provider
Expand Down Expand Up @@ -187,4 +188,34 @@ func TestAccProviderConfigure(t *testing.T) {

})

t.Run("can be configured with max per page", func(t *testing.T) {

config := fmt.Sprintf(`
provider "github" {
token = "%s"
owner = "%s"
max_per_page = 999
}`,
testToken, testOwnerFunc(),
)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, individual) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
ExpectNonEmptyPlan: false,
Check: func(_ *terraform.State) error {
if maxPerPage != 999 {
return fmt.Errorf("max_per_page should be set to 999, got %d", maxPerPage)
}
return nil
},
},
},
})

})

}
2 changes: 1 addition & 1 deletion github/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
var (
// https://developer.github.com/guides/traversing-with-pagination/#basics-of-pagination
maxPerPage = 100
)
Expand Down
Loading