Skip to content

Commit

Permalink
v6 (#2091)
Browse files Browse the repository at this point in the history
* Add retryable transport for errors (#1704)

* Add retryable transport for errors

In order to address the issue #210
I have added 3 new parameters to the provider

- retry_delay_ms
- max_retries
- retryable_errors

In case max_retries is > 0 (defaults to zero)
it will retry the request in case it is an error
the retryable_errors defaults to [500, 502, 503, 504]

It retries after the ms specified in retry_delay_ms (defaults to 1000)

* Update documentation with new parameters for retry

* Change default of max_retries to 3

* Fix typo in naming

* Update github/transport_test.go

* Add error check for data seek

* Consolidate the default retriable errors on a function

* Fix typo on the comments

Co-authored-by: Keegan Campbell <me@kfcampbell.com>

* Update vendor

* Fix merging conflicts

* Update documentation with new parameters for retry

* Change default of max_retries to 3

* Fix typo in naming

* Add error check for data seek

* Update github/transport_test.go

* Consolidate the default retriable errors on a function

* Fix typo on the comments

Co-authored-by: Keegan Campbell <me@kfcampbell.com>

* Don't run go mod tidy on release (#1788)

* Don't run go mod tidy on release

* Be more specific about releases

* Fix lint with APIMeta deprecation

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>

* fix: remove repository topic from state if it doesnt exist in GitHub anymore (#1918)

* remove repository topic if they cannot be found in GitHub anymore

* Fix build error by bumping package version in offending file

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>

* Bump version to v6 (#2106)

* Upgrade to Terraform Plugin SDK v2 (#1780)

* go mod tidy -go=1.16 && go mod tidy -go=1.17

* Run go mod vendor

* Attempt v2 upgrade

* Plugin compiling

* Fix some provider test errors

* Fix test compilation error

* ValidateFunc --> ValidateDiagFunc

* Fix casing

* Sprinkle toDiagFunc everywhere

* More fixes for validation functions

* State --> StateContext

* %s --> %v when printing diags

* ConfigureFunc --> ConfigureContextFunc

* Checking results of d.Set, round one

* Continue checking d.Set results

* Check results of d.Set, round three

* Checking d.Set results, round four

* d.Set round five

* In tests, export GITHUB_TEST_ORGANIZATION

* Remove unnecessary MaxItems on computed value

* Go build now works

* Resolve linting errors

* Apply diag.FromErr twice more

* Pass key names into toDiagFunc helper

* Construct cty.Path from strings

* Tests now working

* Update terraform-plugin-sdk version

* Remove commented attribute setting in resource_github_team.go

* Fix restrict pushes on github_branch_protection. Fix branch protection tests (#2045)

* Update restrict pushes. Fix branch protection tests

Change blocks_creations default to true. Fix broken build.

* add state migration

* rename push_restrictions to push_allowances

* correct state migration issue

* add docs clarification

* update migration func args

* fix test args

* cleanup tests

* Pass context.Background() in test

* fix timestamp fields

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>

* Set group_id correctly (#2133)

* Run go get -u github.com/golangci/golangci-lint

* Separate github_team_members import from github_team as create_default_maintainers is not defined for members resource (#2126)

Co-authored-by: Keegan Campbell <me@kfcampbell.com>

* Add missing variable definition for test case

---------

Co-authored-by: Daniel França <github.t6297kgphp.dv@koderama.com>
Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
Co-authored-by: Felix Luthman <34520175+felixlut@users.noreply.github.com>
Co-authored-by: georgekaz <1391828+georgekaz@users.noreply.github.com>
Co-authored-by: Rich Young <richjyoung@users.noreply.github.com>
  • Loading branch information
6 people committed Feb 16, 2024
1 parent 2d31318 commit 16872b7
Show file tree
Hide file tree
Showing 4,216 changed files with 232,773 additions and 467,694 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -8,6 +8,8 @@ on:
jobs:
ci:
runs-on: ubuntu-latest
env:
GITHUB_TEST_ORGANIZATION: 'kfcampbell-terraform-provider'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Expand Up @@ -21,5 +21,5 @@ linters:

linters-settings:
errcheck:
ignore: github.com/hashicorp/terraform-plugin-sdk/helper/schema:ForceNew|Set,fmt:.*,io:Close
ignore: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema:ForceNew|Set,fmt:.*,io:Close

20 changes: 14 additions & 6 deletions github/config.go
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/google/go-github/v57/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
Expand All @@ -21,6 +21,9 @@ type Config struct {
Insecure bool
WriteDelay time.Duration
ReadDelay time.Duration
RetryDelay time.Duration
RetryableErrors map[int]bool
MaxRetries int
ParallelRequests bool
}

Expand All @@ -33,16 +36,20 @@ type Owner struct {
IsOrganization bool
}

func RateLimitedHTTPClient(client *http.Client, writeDelay time.Duration, readDelay time.Duration, parallelRequests bool) *http.Client {
func RateLimitedHTTPClient(client *http.Client, writeDelay time.Duration, readDelay time.Duration, retryDelay time.Duration, parallelRequests bool, retryableErrors map[int]bool, maxRetries int) *http.Client {

client.Transport = NewEtagTransport(client.Transport)
client.Transport = NewRateLimitTransport(client.Transport, WithWriteDelay(writeDelay), WithReadDelay(readDelay), WithParallelRequests(parallelRequests))
client.Transport = logging.NewTransport("GitHub", client.Transport)
client.Transport = logging.NewSubsystemLoggingHTTPTransport("GitHub", client.Transport)
client.Transport = newPreviewHeaderInjectorTransport(map[string]string{
// TODO: remove when Stone Crop preview is moved to general availability in the GraphQL API
"Accept": "application/vnd.github.stone-crop-preview+json",
}, client.Transport)

if maxRetries > 0 {
client.Transport = NewRetryTransport(client.Transport, WithRetryDelay(retryDelay), WithRetryableErrors(retryableErrors), WithMaxRetries(maxRetries))
}

return client
}

Expand All @@ -54,7 +61,7 @@ func (c *Config) AuthenticatedHTTPClient() *http.Client {
)
client := oauth2.NewClient(ctx, ts)

return RateLimitedHTTPClient(client, c.WriteDelay, c.ReadDelay, c.ParallelRequests)
return RateLimitedHTTPClient(client, c.WriteDelay, c.ReadDelay, c.RetryDelay, c.ParallelRequests, c.RetryableErrors, c.MaxRetries)
}

func (c *Config) Anonymous() bool {
Expand All @@ -63,7 +70,7 @@ func (c *Config) Anonymous() bool {

func (c *Config) AnonymousHTTPClient() *http.Client {
client := &http.Client{Transport: &http.Transport{}}
return RateLimitedHTTPClient(client, c.WriteDelay, c.ReadDelay, c.ParallelRequests)
return RateLimitedHTTPClient(client, c.WriteDelay, c.ReadDelay, c.RetryDelay, c.ParallelRequests, c.RetryableErrors, c.MaxRetries)
}

func (c *Config) NewGraphQLClient(client *http.Client) (*githubv4.Client, error) {
Expand Down Expand Up @@ -130,7 +137,7 @@ func (c *Config) ConfigureOwner(owner *Owner) (*Owner, error) {
}

// Meta returns the meta parameter that is passed into subsequent resources
// https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/schema#ConfigureFunc
// https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema#ConfigureFunc
func (c *Config) Meta() (interface{}, error) {

var client *http.Client
Expand All @@ -153,6 +160,7 @@ func (c *Config) Meta() (interface{}, error) {
var owner Owner
owner.v4client = v4client
owner.v3client = v3client
owner.StopContext = context.Background()

_, err = c.ConfigureOwner(&owner)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions github/config_test.go
Expand Up @@ -58,6 +58,31 @@ func TestAccConfigMeta(t *testing.T) {

})

t.Run("returns a v3 REST API client with max retries", func(t *testing.T) {

config := Config{
Token: testToken,
BaseURL: "https://api.github.com/",
RetryableErrors: map[int]bool{
500: true,
502: true,
},
MaxRetries: 3,
}
meta, err := config.Meta()
if err != nil {
t.Fatalf("failed to return meta without error: %s", err.Error())
}

ctx := context.Background()
client := meta.(*Owner).v3client
_, _, err = client.Meta.Get(ctx)
if err != nil {
t.Fatalf("failed to validate returned client without error: %s", err.Error())
}

})

t.Run("returns a v4 GraphQL API client to manage individual resources", func(t *testing.T) {

config := Config{
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_actions_environment_secrets.go
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/google/go-github/v57/github"

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

func dataSourceGithubActionsEnvironmentSecrets() *schema.Resource {
Expand Down
4 changes: 2 additions & 2 deletions github/data_source_github_actions_environment_secrets_test.go
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccGithubActionsEnvironmentSecretsDataSource(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_actions_environment_variables.go
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/google/go-github/v57/github"

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

func dataSourceGithubActionsEnvironmentVariables() *schema.Resource {
Expand Down
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"testing"

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

func TestAccGithubActionsEnvironmentVariablesDataSource(t *testing.T) {
Expand Down
@@ -1,7 +1,7 @@
package github

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

func dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate() *schema.Resource {
Expand Down Expand Up @@ -38,7 +38,10 @@ func dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateRea
}

d.SetId(orgName)
d.Set("include_claim_keys", template.IncludeClaimKeys)
err = d.Set("include_claim_keys", template.IncludeClaimKeys)
if err != nil {
return err
}

return nil
}
Expand Up @@ -3,7 +3,7 @@ package github
import (
"testing"

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

func TestAccGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateDataSource(t *testing.T) {
Expand Down
12 changes: 9 additions & 3 deletions github/data_source_github_actions_organization_public_key.go
Expand Up @@ -3,7 +3,7 @@ package github
import (
"context"

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

func dataSourceGithubActionsOrganizationPublicKey() *schema.Resource {
Expand Down Expand Up @@ -40,8 +40,14 @@ func dataSourceGithubActionsOrganizationPublicKeyRead(d *schema.ResourceData, me
}

d.SetId(publicKey.GetKeyID())
d.Set("key_id", publicKey.GetKeyID())
d.Set("key", publicKey.GetKey())
err = d.Set("key_id", publicKey.GetKeyID())
if err != nil {
return err
}
err = d.Set("key", publicKey.GetKey())
if err != nil {
return err
}

return nil
}
Expand Up @@ -3,7 +3,7 @@ package github
import (
"testing"

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

func TestAccGithubActionsOrganizationPublicKeyDataSource(t *testing.T) {
Expand Down
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

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

func dataSourceGithubActionsOrganizationRegistrationToken() *schema.Resource {
Expand Down Expand Up @@ -36,8 +36,14 @@ func dataSourceGithubActionsOrganizationRegistrationTokenRead(d *schema.Resource
}

d.SetId(owner)
d.Set("token", token.Token)
d.Set("expires_at", token.ExpiresAt.Unix())
err = d.Set("token", token.Token)
if err != nil {
return err
}
err = d.Set("expires_at", token.ExpiresAt.Unix())
if err != nil {
return err
}

return nil
}
Expand Up @@ -3,7 +3,7 @@ package github
import (
"testing"

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

func TestAccGithubActionsOrganizationRegistrationTokenDataSource(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions github/data_source_github_actions_organization_secrets.go
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/google/go-github/v57/github"

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

func dataSourceGithubActionsOrganizationSecrets() *schema.Resource {
Expand Down Expand Up @@ -72,7 +72,10 @@ func dataSourceGithubActionsOrganizationSecretsRead(d *schema.ResourceData, meta
}

d.SetId(owner)
d.Set("secrets", all_secrets)
err := d.Set("secrets", all_secrets)
if err != nil {
return err
}

return nil
}
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"testing"

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

func TestAccGithubActionsOrganizationSecretsDataSource(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions github/data_source_github_actions_organization_variables.go
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/google/go-github/v57/github"

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

func dataSourceGithubActionsOrganizationVariables() *schema.Resource {
Expand Down Expand Up @@ -76,7 +76,10 @@ func dataSourceGithubActionsOrganizationVariablesRead(d *schema.ResourceData, me
}

d.SetId(owner)
d.Set("variables", all_variables)
err := d.Set("variables", all_variables)
if err != nil {
return err
}

return nil
}
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"testing"

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

func TestAccGithubActionsOrganizationVariablesDataSource(t *testing.T) {
Expand Down
12 changes: 9 additions & 3 deletions github/data_source_github_actions_public_key.go
Expand Up @@ -3,7 +3,7 @@ package github
import (
"context"

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

func dataSourceGithubActionsPublicKey() *schema.Resource {
Expand Down Expand Up @@ -40,8 +40,14 @@ func dataSourceGithubActionsPublicKeyRead(d *schema.ResourceData, meta interface
}

d.SetId(publicKey.GetKeyID())
d.Set("key_id", publicKey.GetKeyID())
d.Set("key", publicKey.GetKey())
err = d.Set("key_id", publicKey.GetKeyID())
if err != nil {
return err
}
err = d.Set("key", publicKey.GetKey())
if err != nil {
return err
}

return nil
}
4 changes: 2 additions & 2 deletions github/data_source_github_actions_public_key_test.go
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccGithubActionsPublicKeyDataSource(t *testing.T) {
Expand Down
12 changes: 9 additions & 3 deletions github/data_source_github_actions_registration_token.go
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

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

func dataSourceGithubActionsRegistrationToken() *schema.Resource {
Expand Down Expand Up @@ -42,8 +42,14 @@ func dataSourceGithubActionsRegistrationTokenRead(d *schema.ResourceData, meta i
}

d.SetId(fmt.Sprintf("%s/%s", owner, repoName))
d.Set("token", token.Token)
d.Set("expires_at", token.ExpiresAt.Unix())
err = d.Set("token", token.Token)
if err != nil {
return err
}
err = d.Set("expires_at", token.ExpiresAt.Unix())
if err != nil {
return err
}

return nil
}

0 comments on commit 16872b7

Please sign in to comment.