diff --git a/github/provider.go b/github/provider.go index 8f44c9509..71cc00ec3 100644 --- a/github/provider.go +++ b/github/provider.go @@ -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{ @@ -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", } } @@ -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 { diff --git a/github/provider_test.go b/github/provider_test.go index ef925a901..79a2e51bf 100644 --- a/github/provider_test.go +++ b/github/provider_test.go @@ -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 @@ -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 + }, + }, + }, + }) + + }) + } diff --git a/github/util.go b/github/util.go index 321a201fb..86f4759f1 100644 --- a/github/util.go +++ b/github/util.go @@ -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 )