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

v6 #2091

Merged
merged 19 commits into from
Feb 16, 2024
Merged

v6 #2091

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
Loading
Loading