Skip to content

Commit 59fd2b7

Browse files
feat: Max per page (#2703)
* Implement max_per_page provider config option (fixes #2557) * Add max_per_page test case --------- Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
1 parent cef45f7 commit 59fd2b7

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

github/provider.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ func Provider() *schema.Provider {
122122
},
123123
},
124124
},
125+
// https://developer.github.com/guides/traversing-with-pagination/#basics-of-pagination
126+
"max_per_page": {
127+
Type: schema.TypeInt,
128+
Optional: true,
129+
DefaultFunc: schema.EnvDefaultFunc("GITHUB_MAX_PER_PAGE", "100"),
130+
Description: descriptions["max_per_page"],
131+
},
125132
},
126133

127134
ResourcesMap: map[string]*schema.Resource{
@@ -322,6 +329,8 @@ func init() {
322329
"Defaults to [500, 502, 503, 504]",
323330
"max_retries": "Number of times to retry a request after receiving an error status code" +
324331
"Defaults to 3",
332+
"max_per_page": "Number of items per page for pagination" +
333+
"Defaults to 100",
325334
}
326335
}
327336

@@ -442,6 +451,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
442451
log.Printf("[DEBUG] Setting retriableErrors to %v", retryableErrors)
443452
}
444453

454+
_maxPerPage := d.Get("max_per_page").(int)
455+
if _maxPerPage <= 0 {
456+
return nil, diag.FromErr(fmt.Errorf("max_per_page must be greater than than 0"))
457+
}
458+
log.Printf("[DEBUG] Setting max_per_page to %d", _maxPerPage)
459+
maxPerPage = _maxPerPage
460+
445461
parallelRequests := d.Get("parallel_requests").(bool)
446462

447463
log.Printf("[DEBUG] Setting parallel_requests to %t", parallelRequests)

github/provider_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
910
)
1011

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

188189
})
189190

191+
t.Run("can be configured with max per page", func(t *testing.T) {
192+
193+
config := fmt.Sprintf(`
194+
provider "github" {
195+
token = "%s"
196+
owner = "%s"
197+
max_per_page = 999
198+
}`,
199+
testToken, testOwnerFunc(),
200+
)
201+
202+
resource.Test(t, resource.TestCase{
203+
PreCheck: func() { skipUnlessMode(t, individual) },
204+
Providers: testAccProviders,
205+
Steps: []resource.TestStep{
206+
{
207+
Config: config,
208+
ExpectNonEmptyPlan: false,
209+
Check: func(_ *terraform.State) error {
210+
if maxPerPage != 999 {
211+
return fmt.Errorf("max_per_page should be set to 999, got %d", maxPerPage)
212+
}
213+
return nil
214+
},
215+
},
216+
},
217+
})
218+
219+
})
220+
190221
}

github/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1919
)
2020

21-
const (
21+
var (
2222
// https://developer.github.com/guides/traversing-with-pagination/#basics-of-pagination
2323
maxPerPage = 100
2424
)

0 commit comments

Comments
 (0)