diff --git a/.golangci.yml b/.golangci.yml index 900dce093b..447195bd05 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -66,6 +66,21 @@ formatters: replacement: any - pattern: a[b:len(a)] replacement: a[b:] + - pattern: skipUnlessMode(t, enterprise) + replacement: skipUnlessEnterprise(t) + - pattern: parseTwoPartID(a, b, c) + replacement: parseID2(a) + - pattern: parseThreePartID(a, b, c, d) + replacement: parseID3(a) + - pattern: wrapErrors([]error{fmt.Errorf(a)}) + replacement: diag.Errorf(a) + - pattern: wrapErrors([]error{fmt.Errorf(a, b)}) + replacement: diag.Errorf(a, b) + - pattern: wrapErrors([]error{err}) + replacement: diag.FromErr(err) + - pattern: diag.FromErr(fmt.Errorf(a)) + replacement: diag.Errorf(a) + gofumpt: module-path: github.com/integrations/terraform-provider-github extra-rules: true diff --git a/github/acc_test.go b/github/acc_test.go index eb7b5644ef..80906378a8 100644 --- a/github/acc_test.go +++ b/github/acc_test.go @@ -318,7 +318,7 @@ func skipUnlessHasAppInstallations(t *testing.T) { t.Fatalf("failed to get test meta: %s", err) } - installations, _, err := meta.v3client.Organizations.ListInstallations(context.Background(), meta.name, nil) + installations, _, err := meta.v3client.Organizations.ListInstallations(t.Context(), meta.name, nil) if err != nil { t.Fatalf("failed to list app installations: %s", err) } diff --git a/github/config_test.go b/github/config_test.go index 1d88e231bc..68b7a944b6 100644 --- a/github/config_test.go +++ b/github/config_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "net/http" "net/http/httptest" "testing" @@ -171,7 +170,7 @@ func TestAccConfigMeta(t *testing.T) { t.Fatalf("failed to return meta without error: %s", err.Error()) } - ctx := context.Background() + ctx := t.Context() client := meta.(*Owner).v3client _, _, err = client.Meta.Get(ctx) if err != nil { @@ -191,7 +190,7 @@ func TestAccConfigMeta(t *testing.T) { t.Fatalf("failed to return meta without error: %s", err.Error()) } - ctx := context.Background() + ctx := t.Context() client := meta.(*Owner).v3client _, _, err = client.Meta.Get(ctx) if err != nil { @@ -216,7 +215,7 @@ func TestAccConfigMeta(t *testing.T) { t.Fatalf("failed to return meta without error: %s", err.Error()) } - ctx := context.Background() + ctx := t.Context() client := meta.(*Owner).v3client _, _, err = client.Meta.Get(ctx) if err != nil { @@ -242,7 +241,7 @@ func TestAccConfigMeta(t *testing.T) { GitHubServicesSha githubv4.String } } - err = client.Query(context.Background(), &query, nil) + err = client.Query(t.Context(), &query, nil) if err != nil { t.Fatalf("failed to validate returned client without error: %s", err.Error()) } @@ -261,7 +260,7 @@ func TestAccConfigMeta(t *testing.T) { t.Fatalf("failed to return meta without error: %s", err.Error()) } - ctx := context.Background() + ctx := t.Context() client := meta.(*Owner).v3client _, _, err = client.Organizations.Get(ctx, testAccConf.owner) if err != nil { @@ -292,7 +291,7 @@ func TestAccConfigMeta(t *testing.T) { variables := map[string]any{ "login": githubv4.String(testAccConf.owner), } - err = client.Query(context.Background(), &query, variables) + err = client.Query(t.Context(), &query, variables) if err != nil { t.Fatalf("failed to validate returned client without error: %s", err.Error()) } diff --git a/github/data_source_github_enterprise_test.go b/github/data_source_github_enterprise_test.go index 7db4b65000..ee87fcbe45 100644 --- a/github/data_source_github_enterprise_test.go +++ b/github/data_source_github_enterprise_test.go @@ -24,7 +24,7 @@ func TestAccGithubEnterpriseDataSource(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/data_source_github_organization_external_identities_test.go b/github/data_source_github_organization_external_identities_test.go index b071ad0655..8048c8293d 100644 --- a/github/data_source_github_organization_external_identities_test.go +++ b/github/data_source_github_organization_external_identities_test.go @@ -17,7 +17,7 @@ func TestAccGithubOrganizationExternalIdentities(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/data_source_github_organization_repository_role_test.go b/github/data_source_github_organization_repository_role_test.go index 3ab91f7aab..d95ed94f80 100644 --- a/github/data_source_github_organization_repository_role_test.go +++ b/github/data_source_github_organization_repository_role_test.go @@ -32,7 +32,7 @@ func TestAccGithubOrganizationRepositoryRoleDataSource(t *testing.T) { `, roleName) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/data_source_github_organization_repository_roles_test.go b/github/data_source_github_organization_repository_roles_test.go index 466722a068..ab35614633 100644 --- a/github/data_source_github_organization_repository_roles_test.go +++ b/github/data_source_github_organization_repository_roles_test.go @@ -45,7 +45,7 @@ func TestAccDataSourceGithubOrganizationRepositoryRoles(t *testing.T) { ` resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/data_source_github_organization_team_sync_groups_test.go b/github/data_source_github_organization_team_sync_groups_test.go index a0779c84fb..2a825e7b8d 100644 --- a/github/data_source_github_organization_team_sync_groups_test.go +++ b/github/data_source_github_organization_team_sync_groups_test.go @@ -8,7 +8,7 @@ import ( func TestAccGithubOrganizationTeamSyncGroupsDataSource_existing(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/data_source_github_user_external_identity_test.go b/github/data_source_github_user_external_identity_test.go index 5fc32740dd..868d7d3ff1 100644 --- a/github/data_source_github_user_external_identity_test.go +++ b/github/data_source_github_user_external_identity_test.go @@ -18,7 +18,7 @@ func TestAccGithubUserExternalIdentity(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/provider.go b/github/provider.go index 1baf0263ad..451abe6911 100644 --- a/github/provider.go +++ b/github/provider.go @@ -2,7 +2,6 @@ package github import ( "context" - "fmt" "log" "net/url" "os" @@ -388,13 +387,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { if v, ok := appAuthAttr["id"].(string); ok && v != "" { appID = v } else { - return nil, wrapErrors([]error{fmt.Errorf("app_auth.id must be set and contain a non-empty value")}) + return nil, diag.Errorf("app_auth.id must be set and contain a non-empty value") } if v, ok := appAuthAttr["installation_id"].(string); ok && v != "" { appInstallationID = v } else { - return nil, wrapErrors([]error{fmt.Errorf("app_auth.installation_id must be set and contain a non-empty value")}) + return nil, diag.Errorf("app_auth.installation_id must be set and contain a non-empty value") } if v, ok := appAuthAttr["pem_file"].(string); ok && v != "" { @@ -407,7 +406,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { // actual new line character before decoding. appPemFile = strings.ReplaceAll(v, `\n`, "\n") } else { - return nil, wrapErrors([]error{fmt.Errorf("app_auth.pem_file must be set and contain a non-empty value")}) + return nil, diag.Errorf("app_auth.pem_file must be set and contain a non-empty value") } apiPath := "" @@ -417,7 +416,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { appToken, err := GenerateOAuthTokenFromApp(baseURL.JoinPath(apiPath), appID, appInstallationID, appPemFile) if err != nil { - return nil, wrapErrors([]error{err}) + return nil, diag.FromErr(err) } token = appToken @@ -430,25 +429,25 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { writeDelay := d.Get("write_delay_ms").(int) if writeDelay <= 0 { - return nil, wrapErrors([]error{fmt.Errorf("write_delay_ms must be greater than 0ms")}) + return nil, diag.Errorf("write_delay_ms must be greater than 0ms") } log.Printf("[INFO] Setting write_delay_ms to %d", writeDelay) readDelay := d.Get("read_delay_ms").(int) if readDelay < 0 { - return nil, wrapErrors([]error{fmt.Errorf("read_delay_ms must be greater than or equal to 0ms")}) + return nil, diag.Errorf("read_delay_ms must be greater than or equal to 0ms") } log.Printf("[DEBUG] Setting read_delay_ms to %d", readDelay) retryDelay := d.Get("read_delay_ms").(int) if retryDelay < 0 { - return nil, diag.FromErr(fmt.Errorf("retry_delay_ms must be greater than or equal to 0ms")) + return nil, diag.Errorf("retry_delay_ms must be greater than or equal to 0ms") } log.Printf("[DEBUG] Setting retry_delay_ms to %d", retryDelay) maxRetries := d.Get("max_retries").(int) if maxRetries < 0 { - return nil, diag.FromErr(fmt.Errorf("max_retries must be greater than or equal to 0")) + return nil, diag.Errorf("max_retries must be greater than or equal to 0") } log.Printf("[DEBUG] Setting max_retries to %d", maxRetries) retryableErrors := make(map[int]bool) @@ -467,7 +466,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { _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")) + return nil, diag.Errorf("max_per_page must be greater than than 0") } log.Printf("[DEBUG] Setting max_per_page to %d", _maxPerPage) maxPerPage = _maxPerPage @@ -492,7 +491,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { meta, err := config.Meta() if err != nil { - return nil, wrapErrors([]error{err}) + return nil, diag.FromErr(err) } return meta, nil diff --git a/github/provider_test.go b/github/provider_test.go index bb9ab01400..7beca9ab05 100644 --- a/github/provider_test.go +++ b/github/provider_test.go @@ -167,7 +167,7 @@ data "github_ip_ranges" "test" {} resource.Test(t, resource.TestCase{ PreCheck: func() { - skipUnlessMode(t, enterprise) + skipUnlessEnterprise(t) if testAccConf.baseURL.Host != "api.github.com" { t.Skip("Skipping as test mode is not GHES") } diff --git a/github/resource_github_actions_environment_secret_migration_test.go b/github/resource_github_actions_environment_secret_migration_test.go index 3242431633..8a2d989648 100644 --- a/github/resource_github_actions_environment_secret_migration_test.go +++ b/github/resource_github_actions_environment_secret_migration_test.go @@ -40,7 +40,7 @@ package github // t.Run(d.testName, func(t *testing.T) { // t.Parallel() -// got, err := resourceGithubActionsEnvironmentSecretStateUpgradeV0(context.Background(), d.rawState, nil) +// got, err := resourceGithubActionsEnvironmentSecretStateUpgradeV0(t.Context(), d.rawState, nil) // if (err != nil) != d.shouldError { // t.Fatalf("unexpected error state") // } diff --git a/github/resource_github_actions_environment_secret_test.go b/github/resource_github_actions_environment_secret_test.go index 6af6b9e9c8..69a2e64b58 100644 --- a/github/resource_github_actions_environment_secret_test.go +++ b/github/resource_github_actions_environment_secret_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "encoding/base64" "fmt" "net/url" @@ -342,7 +341,7 @@ resource "github_actions_environment_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() escapedEnvName := url.PathEscape(envName) @@ -438,7 +437,7 @@ resource "github_actions_environment_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() escapedEnvName := url.PathEscape(envName) diff --git a/github/resource_github_actions_environment_variable_migration_test.go b/github/resource_github_actions_environment_variable_migration_test.go index 94bc5646b4..69326e6016 100644 --- a/github/resource_github_actions_environment_variable_migration_test.go +++ b/github/resource_github_actions_environment_variable_migration_test.go @@ -40,7 +40,7 @@ package github // t.Run(d.testName, func(t *testing.T) { // t.Parallel() -// got, err := resourceGithubActionsEnvironmentVariableStateUpgradeV0(context.Background(), d.rawState, nil) +// got, err := resourceGithubActionsEnvironmentVariableStateUpgradeV0(t.Context(), d.rawState, nil) // if (err != nil) != d.shouldError { // t.Fatalf("unexpected error state") // } diff --git a/github/resource_github_actions_environment_variable_test.go b/github/resource_github_actions_environment_variable_test.go index 488b90dc52..1acbe3cefa 100644 --- a/github/resource_github_actions_environment_variable_test.go +++ b/github/resource_github_actions_environment_variable_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "fmt" "net/url" "regexp" @@ -434,7 +433,7 @@ resource "github_actions_environment_variable" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() _, err = client.Actions.CreateEnvVariable(ctx, owner, repoName, url.PathEscape(envName), &github.ActionsVariable{ Name: varName, @@ -453,7 +452,7 @@ resource "github_actions_environment_variable" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() _, err = client.Actions.DeleteEnvVariable(ctx, owner, repoName, url.PathEscape(envName), varName) return err diff --git a/github/resource_github_actions_organization_secret_migration_test.go b/github/resource_github_actions_organization_secret_migration_test.go index 76069ddda0..0d7d61076d 100644 --- a/github/resource_github_actions_organization_secret_migration_test.go +++ b/github/resource_github_actions_organization_secret_migration_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "reflect" "testing" ) @@ -62,7 +61,7 @@ func Test_resourceGithubActionsOrganizationSecretStateUpgradeV0(t *testing.T) { t.Run(d.testName, func(t *testing.T) { t.Parallel() - got, err := resourceGithubActionsOrganizationSecretStateUpgradeV0(context.Background(), d.rawState, nil) + got, err := resourceGithubActionsOrganizationSecretStateUpgradeV0(t.Context(), d.rawState, nil) if (err != nil) != d.shouldError { t.Fatalf("unexpected error state") } diff --git a/github/resource_github_actions_organization_secret_test.go b/github/resource_github_actions_organization_secret_test.go index c69f27740a..60ac29cc57 100644 --- a/github/resource_github_actions_organization_secret_test.go +++ b/github/resource_github_actions_organization_secret_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "encoding/base64" "fmt" "regexp" @@ -455,7 +454,7 @@ resource "github_actions_organization_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() keyID, _, err := getOrganizationPublicKeyDetails(ctx, meta) if err != nil { @@ -533,7 +532,7 @@ resource "github_actions_organization_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() keyID, _, err := getOrganizationPublicKeyDetails(ctx, meta) if err != nil { diff --git a/github/resource_github_actions_organization_variable_test.go b/github/resource_github_actions_organization_variable_test.go index 41b3b33801..9bf3036174 100644 --- a/github/resource_github_actions_organization_variable_test.go +++ b/github/resource_github_actions_organization_variable_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "fmt" "regexp" "testing" @@ -356,7 +355,7 @@ resource "github_actions_organization_variable" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() _, err = client.Actions.CreateOrgVariable(ctx, owner, &github.ActionsVariable{ Name: varName, @@ -376,7 +375,7 @@ resource "github_actions_organization_variable" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() _, err = client.Actions.DeleteOrgVariable(ctx, owner, varName) return err diff --git a/github/resource_github_actions_secret_migration_test.go b/github/resource_github_actions_secret_migration_test.go index dfe589c851..89b05205ab 100644 --- a/github/resource_github_actions_secret_migration_test.go +++ b/github/resource_github_actions_secret_migration_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "reflect" "testing" ) @@ -62,7 +61,7 @@ func Test_resourceGithubActionsSecretStateUpgradeV0(t *testing.T) { t.Run(d.testName, func(t *testing.T) { t.Parallel() - got, err := resourceGithubActionsSecretStateUpgradeV0(context.Background(), d.rawState, nil) + got, err := resourceGithubActionsSecretStateUpgradeV0(t.Context(), d.rawState, nil) if (err != nil) != d.shouldError { t.Fatalf("unexpected error state") } @@ -111,7 +110,7 @@ func Test_resourceGithubActionsSecretStateUpgradeV0(t *testing.T) { // t.Run(d.testName, func(t *testing.T) { // t.Parallel() -// got, err := resourceGithubActionsSecretStateUpgradeV1(context.Background(), d.rawState, nil) +// got, err := resourceGithubActionsSecretStateUpgradeV1(t.Context(), d.rawState, nil) // if (err != nil) != d.shouldError { // t.Fatalf("unexpected error state") // } diff --git a/github/resource_github_actions_secret_test.go b/github/resource_github_actions_secret_test.go index edd7f49929..6f9fcc3ba4 100644 --- a/github/resource_github_actions_secret_test.go +++ b/github/resource_github_actions_secret_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "encoding/base64" "fmt" "testing" @@ -251,7 +250,7 @@ resource "github_actions_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() keyID, _, err := getPublicKeyDetails(ctx, meta, repoName) if err != nil { @@ -332,7 +331,7 @@ resource "github_actions_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() keyID, _, err := getPublicKeyDetails(ctx, meta, repoName) if err != nil { diff --git a/github/resource_github_actions_variable_migration_test.go b/github/resource_github_actions_variable_migration_test.go index 3130357d69..e3a4e9b896 100644 --- a/github/resource_github_actions_variable_migration_test.go +++ b/github/resource_github_actions_variable_migration_test.go @@ -38,7 +38,7 @@ package github // t.Run(d.testName, func(t *testing.T) { // t.Parallel() -// got, err := resourceGithubActionsVariableStateUpgradeV0(context.Background(), d.rawState, nil) +// got, err := resourceGithubActionsVariableStateUpgradeV0(t.Context(), d.rawState, nil) // if (err != nil) != d.shouldError { // t.Fatalf("unexpected error state") // } diff --git a/github/resource_github_actions_variable_test.go b/github/resource_github_actions_variable_test.go index 7f5b9ea0c0..91ebd94f47 100644 --- a/github/resource_github_actions_variable_test.go +++ b/github/resource_github_actions_variable_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "fmt" "regexp" "testing" @@ -318,7 +317,7 @@ resource "github_actions_variable" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() _, err = client.Actions.CreateRepoVariable(ctx, owner, repoName, &github.ActionsVariable{ Name: varName, @@ -337,7 +336,7 @@ resource "github_actions_variable" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() _, err = client.Actions.DeleteRepoVariable(ctx, owner, repoName, varName) return err diff --git a/github/resource_github_app_installation_repository.go b/github/resource_github_app_installation_repository.go index 92a8cb6f44..7a4549dce6 100644 --- a/github/resource_github_app_installation_repository.go +++ b/github/resource_github_app_installation_repository.go @@ -67,7 +67,7 @@ func resourceGithubAppInstallationRepositoryCreate(d *schema.ResourceData, meta func resourceGithubAppInstallationRepositoryRead(d *schema.ResourceData, meta any) error { client := meta.(*Owner).v3client - installationIDString, repoName, err := parseTwoPartID(d.Id(), "installation_id", "repository") + installationIDString, repoName, err := parseID2(d.Id()) if err != nil { return err } diff --git a/github/resource_github_branch.go b/github/resource_github_branch.go index 8b33ce5417..08bd4bee0a 100644 --- a/github/resource_github_branch.go +++ b/github/resource_github_branch.go @@ -122,7 +122,7 @@ func resourceGithubBranchRead(d *schema.ResourceData, meta any) error { client := meta.(*Owner).v3client orgName := meta.(*Owner).name - repoName, branchName, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, branchName, err := parseID2(d.Id()) if err != nil { return err } @@ -171,7 +171,7 @@ func resourceGithubBranchDelete(d *schema.ResourceData, meta any) error { client := meta.(*Owner).v3client orgName := meta.(*Owner).name - repoName, branchName, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, branchName, err := parseID2(d.Id()) if err != nil { return err } @@ -194,7 +194,7 @@ func resourceGithubBranchUpdate(d *schema.ResourceData, meta any) error { ctx := context.WithValue(context.Background(), ctxId, d.Id()) client := meta.(*Owner).v3client orgName := meta.(*Owner).name - repoName, oldBranchName, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, oldBranchName, err := parseID2(d.Id()) if err != nil { return err } @@ -210,14 +210,14 @@ func resourceGithubBranchUpdate(d *schema.ResourceData, meta any) error { } func resourceGithubBranchImport(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { - repoName, branchName, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, branchName, err := parseID2(d.Id()) if err != nil { return nil, err } sourceBranch := "main" if strings.Contains(branchName, ":") { - branchName, sourceBranch, err = parseTwoPartID(branchName, "branch", "source_branch") + branchName, sourceBranch, err = parseID2(branchName) if err != nil { return nil, err } diff --git a/github/resource_github_branch_protection.go b/github/resource_github_branch_protection.go index 7faa34320f..79398152df 100644 --- a/github/resource_github_branch_protection.go +++ b/github/resource_github_branch_protection.go @@ -461,7 +461,7 @@ func resourceGithubBranchProtectionDelete(d *schema.ResourceData, meta any) erro } func resourceGithubBranchProtectionImport(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { - repoName, pattern, err := parseTwoPartID(d.Id(), "repository", "pattern") + repoName, pattern, err := parseID2(d.Id()) if err != nil { return nil, err } diff --git a/github/resource_github_branch_protection_migration_test.go b/github/resource_github_branch_protection_migration_test.go new file mode 100644 index 0000000000..1462b08c68 --- /dev/null +++ b/github/resource_github_branch_protection_migration_test.go @@ -0,0 +1,35 @@ +package github + +import ( + "reflect" + "testing" +) + +func testGithubBranchProtectionStateDataV1() map[string]any { + return map[string]any{ + "blocks_creations": true, + "push_restrictions": [...]string{"/example-user"}, + } +} + +func testGithubBranchProtectionStateDataV2() map[string]any { + restrictions := []any{map[string]any{ + "blocks_creations": true, + "push_allowances": [...]string{"/example-user"}, + }} + return map[string]any{ + "restrict_pushes": restrictions, + } +} + +func Test_resourceGithubBranchProtectionStateUpgradeV1(t *testing.T) { + expected := testGithubBranchProtectionStateDataV2() + actual, err := resourceGithubBranchProtectionUpgradeV1(t.Context(), testGithubBranchProtectionStateDataV1(), nil) + if err != nil { + t.Fatalf("error migrating state: %s", err) + } + + if !reflect.DeepEqual(expected, actual) { + t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual) + } +} diff --git a/github/resource_github_branch_protection_test.go b/github/resource_github_branch_protection_test.go index 6fa000258d..45b120a329 100644 --- a/github/resource_github_branch_protection_test.go +++ b/github/resource_github_branch_protection_test.go @@ -1,9 +1,7 @@ package github import ( - "context" "fmt" - "reflect" "regexp" "testing" @@ -749,32 +747,3 @@ func importBranchProtectionByRepoID(repoLogicalName, pattern string) resource.Im return fmt.Sprintf("%s:%s", repoID, pattern), nil } } - -func testGithubBranchProtectionStateDataV1() map[string]any { - return map[string]any{ - "blocks_creations": true, - "push_restrictions": [...]string{"/example-user"}, - } -} - -func testGithubBranchProtectionStateDataV2() map[string]any { - restrictions := []any{map[string]any{ - "blocks_creations": true, - "push_allowances": [...]string{"/example-user"}, - }} - return map[string]any{ - "restrict_pushes": restrictions, - } -} - -func TestAccGithubBranchProtectionV4StateUpgradeV1(t *testing.T) { - expected := testGithubBranchProtectionStateDataV2() - actual, err := resourceGithubBranchProtectionUpgradeV1(context.Background(), testGithubBranchProtectionStateDataV1(), nil) - if err != nil { - t.Fatalf("error migrating state: %s", err) - } - - if !reflect.DeepEqual(expected, actual) { - t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual) - } -} diff --git a/github/resource_github_branch_protection_v3.go b/github/resource_github_branch_protection_v3.go index 411ae6534e..8b35359019 100644 --- a/github/resource_github_branch_protection_v3.go +++ b/github/resource_github_branch_protection_v3.go @@ -268,7 +268,7 @@ func resourceGithubBranchProtectionV3Read(d *schema.ResourceData, meta any) erro client := meta.(*Owner).v3client - repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, branch, err := parseID2(d.Id()) if err != nil { return err } @@ -345,7 +345,7 @@ func resourceGithubBranchProtectionV3Update(d *schema.ResourceData, meta any) er } client := meta.(*Owner).v3client - repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, branch, err := parseID2(d.Id()) if err != nil { return err } @@ -399,7 +399,7 @@ func resourceGithubBranchProtectionV3Delete(d *schema.ResourceData, meta any) er } client := meta.(*Owner).v3client - repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, branch, err := parseID2(d.Id()) if err != nil { return err } diff --git a/github/resource_github_branch_protection_v3_utils.go b/github/resource_github_branch_protection_v3_utils.go index 45cb269316..f7d26c9c6c 100644 --- a/github/resource_github_branch_protection_v3_utils.go +++ b/github/resource_github_branch_protection_v3_utils.go @@ -81,7 +81,7 @@ func flattenAndSetRequiredStatusChecks(d *schema.ResourceData, protection *githu func requireSignedCommitsRead(d *schema.ResourceData, meta any) error { client := meta.(*Owner).v3client - repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, branch, err := parseID2(d.Id()) if err != nil { return err } @@ -107,7 +107,7 @@ func requireSignedCommitsUpdate(d *schema.ResourceData, meta any) (err error) { requiredSignedCommit := d.Get("require_signed_commits").(bool) client := meta.(*Owner).v3client - repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch") + repoName, branch, err := parseID2(d.Id()) if err != nil { return err } diff --git a/github/resource_github_codespaces_secret.go b/github/resource_github_codespaces_secret.go index 3b42ced1cc..613228301d 100644 --- a/github/resource_github_codespaces_secret.go +++ b/github/resource_github_codespaces_secret.go @@ -112,7 +112,7 @@ func resourceGithubCodespacesSecretRead(d *schema.ResourceData, meta any) error owner := meta.(*Owner).name ctx := context.Background() - repoName, secretName, err := parseTwoPartID(d.Id(), "repository", "secret_name") + repoName, secretName, err := parseID2(d.Id()) if err != nil { return err } @@ -173,7 +173,7 @@ func resourceGithubCodespacesSecretDelete(d *schema.ResourceData, meta any) erro orgName := meta.(*Owner).name ctx := context.WithValue(context.Background(), ctxId, d.Id()) - repoName, secretName, err := parseTwoPartID(d.Id(), "repository", "secret_name") + repoName, secretName, err := parseID2(d.Id()) if err != nil { return err } @@ -196,7 +196,7 @@ func resourceGithubCodespacesSecretImport(d *schema.ResourceData, meta any) ([]* d.SetId(buildTwoPartID(parts[0], parts[1])) - repoName, secretName, err := parseTwoPartID(d.Id(), "repository", "secret_name") + repoName, secretName, err := parseID2(d.Id()) if err != nil { return nil, err } diff --git a/github/resource_github_dependabot_organization_secret_test.go b/github/resource_github_dependabot_organization_secret_test.go index 1a1871bcb0..96834a5573 100644 --- a/github/resource_github_dependabot_organization_secret_test.go +++ b/github/resource_github_dependabot_organization_secret_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "encoding/base64" "fmt" "regexp" @@ -455,7 +454,7 @@ resource "github_dependabot_organization_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() keyID, _, err := getOrganizationPublicKeyDetails(ctx, meta) if err != nil { @@ -533,7 +532,7 @@ resource "github_dependabot_organization_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() keyID, _, err := getOrganizationPublicKeyDetails(ctx, meta) if err != nil { diff --git a/github/resource_github_dependabot_secret_migration_test.go b/github/resource_github_dependabot_secret_migration_test.go index f5b6255373..5b9970999f 100644 --- a/github/resource_github_dependabot_secret_migration_test.go +++ b/github/resource_github_dependabot_secret_migration_test.go @@ -38,7 +38,7 @@ package github // t.Run(d.testName, func(t *testing.T) { // t.Parallel() -// got, err := resourceGithubDependabotSecretStateUpgradeV0(context.Background(), d.rawState, nil) +// got, err := resourceGithubDependabotSecretStateUpgradeV0(t.Context(), d.rawState, nil) // if (err != nil) != d.shouldError { // t.Fatalf("unexpected error state") // } diff --git a/github/resource_github_dependabot_secret_test.go b/github/resource_github_dependabot_secret_test.go index 97a364fea8..99b9743192 100644 --- a/github/resource_github_dependabot_secret_test.go +++ b/github/resource_github_dependabot_secret_test.go @@ -1,7 +1,6 @@ package github import ( - "context" "encoding/base64" "fmt" "testing" @@ -252,7 +251,7 @@ resource "github_dependabot_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() keyID, _, err := getDependabotPublicKeyDetails(ctx, meta, repoName) if err != nil { @@ -333,7 +332,7 @@ resource "github_dependabot_secret" "test" { } client := meta.v3client owner := meta.name - ctx := context.Background() + ctx := t.Context() keyID, _, err := getDependabotPublicKeyDetails(ctx, meta, repoName) if err != nil { diff --git a/github/resource_github_enterprise_actions_permissions_test.go b/github/resource_github_enterprise_actions_permissions_test.go index 4511865293..4faad82a04 100644 --- a/github/resource_github_enterprise_actions_permissions_test.go +++ b/github/resource_github_enterprise_actions_permissions_test.go @@ -31,7 +31,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -101,7 +101,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -149,7 +149,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -221,7 +221,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/resource_github_enterprise_actions_runner_group_test.go b/github/resource_github_enterprise_actions_runner_group_test.go index fec43eb55d..6eb6416cbc 100644 --- a/github/resource_github_enterprise_actions_runner_group_test.go +++ b/github/resource_github_enterprise_actions_runner_group_test.go @@ -44,7 +44,7 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -96,7 +96,7 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -129,7 +129,7 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -177,7 +177,7 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/resource_github_enterprise_organization_test.go b/github/resource_github_enterprise_organization_test.go index 3978c6cf85..ffd726ed1e 100644 --- a/github/resource_github_enterprise_organization_test.go +++ b/github/resource_github_enterprise_organization_test.go @@ -128,7 +128,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { } resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -169,7 +169,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { `, testAccConf.enterpriseSlug, orgName) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -249,7 +249,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { } resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -380,7 +380,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { } resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -440,7 +440,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { check := resource.ComposeTestCheckFunc() resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -484,7 +484,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { check := resource.ComposeTestCheckFunc() resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -528,7 +528,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { check := resource.ComposeTestCheckFunc() resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/resource_github_issue.go b/github/resource_github_issue.go index a7be018498..38a2465c7e 100644 --- a/github/resource_github_issue.go +++ b/github/resource_github_issue.go @@ -133,7 +133,7 @@ func resourceGithubIssueCreateOrUpdate(d *schema.ResourceData, meta any) error { func resourceGithubIssueRead(d *schema.ResourceData, meta any) error { client := meta.(*Owner).v3client - repoName, idNumber, err := parseTwoPartID(d.Id(), "repository", "issue_number") + repoName, idNumber, err := parseID2(d.Id()) if err != nil { return err } diff --git a/github/resource_github_issue_label.go b/github/resource_github_issue_label.go index 0604a8ff4d..7430b4813f 100644 --- a/github/resource_github_issue_label.go +++ b/github/resource_github_issue_label.go @@ -93,7 +93,7 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta any) er originalName = name } else { var err error - _, originalName, err = parseTwoPartID(d.Id(), "repository", "name") + _, originalName, err = parseID2(d.Id()) if err != nil { return err } @@ -115,7 +115,7 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta any) er originalName = name } else { var err error - _, originalName, err = parseTwoPartID(d.Id(), "repository", "name") + _, originalName, err = parseID2(d.Id()) if err != nil { return err } @@ -145,7 +145,7 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta any) er func resourceGithubIssueLabelRead(d *schema.ResourceData, meta any) error { client := meta.(*Owner).v3client - repoName, name, err := parseTwoPartID(d.Id(), "repository", "name") + repoName, name, err := parseID2(d.Id()) if err != nil { return err } diff --git a/github/resource_github_membership.go b/github/resource_github_membership.go index edf8e923d7..b23238bacb 100644 --- a/github/resource_github_membership.go +++ b/github/resource_github_membership.go @@ -95,7 +95,7 @@ func resourceGithubMembershipRead(ctx context.Context, d *schema.ResourceData, m client := meta.(*Owner).v3client orgName := meta.(*Owner).name - _, username, err := parseTwoPartID(d.Id(), "organization", "username") + _, username, err := parseID2(d.Id()) if err != nil { return diag.FromErr(err) } diff --git a/github/resource_github_membership_test.go b/github/resource_github_membership_test.go index 7464142692..eb907b3314 100644 --- a/github/resource_github_membership_test.go +++ b/github/resource_github_membership_test.go @@ -119,7 +119,7 @@ func testAccCheckGithubMembershipDestroy(s *terraform.State) error { continue } - orgName, username, err := parseTwoPartID(rs.Primary.ID, "organization", "username") + orgName, username, err := parseID2(rs.Primary.ID) if err != nil { return err } @@ -170,7 +170,7 @@ func testAccCheckGithubMembershipExists(ctx context.Context, n string, membershi } conn := meta.v3client - orgName, username, err := parseTwoPartID(rs.Primary.ID, "organization", "username") + orgName, username, err := parseID2(rs.Primary.ID) if err != nil { return err } @@ -201,7 +201,7 @@ func testAccCheckGithubMembershipRoleState(ctx context.Context, n string, member } conn := meta.v3client - orgName, username, err := parseTwoPartID(rs.Primary.ID, "organization", "username") + orgName, username, err := parseID2(rs.Primary.ID) if err != nil { return err } diff --git a/github/resource_github_organization_project_test.go b/github/resource_github_organization_project_test.go index afe7271138..153f0af8f6 100644 --- a/github/resource_github_organization_project_test.go +++ b/github/resource_github_organization_project_test.go @@ -68,7 +68,7 @@ package github // return err // } -// project, res, err := conn.Projects.GetProject(context.Background(), projectID) +// project, res, err := conn.Projects.GetProject(t.Context(), projectID) // if err == nil { // if project != nil && // project.GetID() == projectID { @@ -101,7 +101,7 @@ package github // } // conn := meta.v3client -// gotProject, _, err := conn.Projects.GetProject(context.Background(), projectID) +// gotProject, _, err := conn.Projects.GetProject(t.Context(), projectID) // if err != nil { // return err // } diff --git a/github/resource_github_organization_repository_role_test.go b/github/resource_github_organization_repository_role_test.go index 22e3bc1ad9..7d4d647155 100644 --- a/github/resource_github_organization_repository_role_test.go +++ b/github/resource_github_organization_repository_role_test.go @@ -30,7 +30,7 @@ func TestAccGithubOrganizationRepositoryRole(t *testing.T) { `, name, description, baseRole, permission0, permission1) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -67,7 +67,7 @@ func TestAccGithubOrganizationRepositoryRole(t *testing.T) { `, name, permission0) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/resource_github_organization_role_team.go b/github/resource_github_organization_role_team.go index f834309fce..589506e10e 100644 --- a/github/resource_github_organization_role_team.go +++ b/github/resource_github_organization_role_team.go @@ -70,7 +70,7 @@ func resourceGithubOrganizationRoleTeamRead(ctx context.Context, d *schema.Resou client := meta.(*Owner).v3client orgName := meta.(*Owner).name - roleIdString, teamSlug, err := parseTwoPartID(d.Id(), "role_id", "team_slug") + roleIdString, teamSlug, err := parseID2(d.Id()) if err != nil { return diag.FromErr(err) } diff --git a/github/resource_github_organization_role_team_assignment.go b/github/resource_github_organization_role_team_assignment.go index 9b45a49b48..ae227ead2a 100644 --- a/github/resource_github_organization_role_team_assignment.go +++ b/github/resource_github_organization_role_team_assignment.go @@ -74,7 +74,7 @@ func resourceGithubOrganizationRoleTeamAssignmentRead(d *schema.ResourceData, me ctx := context.Background() orgName := meta.(*Owner).name - teamSlug, roleIDString, err := parseTwoPartID(d.Id(), "team_slug", "role_id") + teamSlug, roleIDString, err := parseID2(d.Id()) if err != nil { return err } @@ -135,7 +135,7 @@ func resourceGithubOrganizationRoleTeamAssignmentDelete(d *schema.ResourceData, orgName := meta.(*Owner).name ctx := context.Background() - teamSlug, roleIDString, err := parseTwoPartID(d.Id(), "team_slug", "role_id") + teamSlug, roleIDString, err := parseID2(d.Id()) if err != nil { return err } diff --git a/github/resource_github_organization_role_test.go b/github/resource_github_organization_role_test.go index efc3c28b98..fe72cae646 100644 --- a/github/resource_github_organization_role_test.go +++ b/github/resource_github_organization_role_test.go @@ -20,7 +20,7 @@ func TestAccGithubOrganizationRole(t *testing.T) { `, name) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -52,7 +52,7 @@ func TestAccGithubOrganizationRole(t *testing.T) { `, name, baseRole) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -86,7 +86,7 @@ func TestAccGithubOrganizationRole(t *testing.T) { `, name, baseRole, permission0) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -122,7 +122,7 @@ func TestAccGithubOrganizationRole(t *testing.T) { `, name, description, baseRole, permission0) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -162,7 +162,7 @@ func TestAccGithubOrganizationRole(t *testing.T) { `, name, description, baseRole, permission0, permission1) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/resource_github_organization_role_user.go b/github/resource_github_organization_role_user.go index cc6cefdd55..f784e2d7ed 100644 --- a/github/resource_github_organization_role_user.go +++ b/github/resource_github_organization_role_user.go @@ -70,7 +70,7 @@ func resourceGithubOrganizationRoleUserRead(ctx context.Context, d *schema.Resou client := meta.(*Owner).v3client orgName := meta.(*Owner).name - roleIdString, login, err := parseTwoPartID(d.Id(), "role_id", "login") + roleIdString, login, err := parseID2(d.Id()) if err != nil { return diag.FromErr(err) } diff --git a/github/resource_github_project_column_test.go b/github/resource_github_project_column_test.go index 08b4daa375..f2179e6831 100644 --- a/github/resource_github_project_column_test.go +++ b/github/resource_github_project_column_test.go @@ -69,7 +69,7 @@ package github // return err // } -// column, res, err := conn.Projects.GetProjectColumn(context.Background(), columnID) +// column, res, err := conn.Projects.GetProjectColumn(t.Context(), columnID) // if err == nil { // if column != nil && // column.GetID() == columnID { @@ -101,7 +101,7 @@ package github // } // conn := meta.v3client -// gotColumn, _, err := conn.Projects.GetProjectColumn(context.Background(), columnID) +// gotColumn, _, err := conn.Projects.GetProjectColumn(t.Context(), columnID) // if err != nil { // return err // } diff --git a/github/resource_github_release.go b/github/resource_github_release.go index 9f91ccc9f8..49c12731d3 100644 --- a/github/resource_github_release.go +++ b/github/resource_github_release.go @@ -252,7 +252,7 @@ func resourceGithubReleaseDelete(ctx context.Context, d *schema.ResourceData, me } func resourceGithubReleaseImport(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { - repoName, releaseIDStr, err := parseTwoPartID(d.Id(), "repository", "release") + repoName, releaseIDStr, err := parseID2(d.Id()) if err != nil { return []*schema.ResourceData{d}, err } diff --git a/github/resource_github_repository_collaborator.go b/github/resource_github_repository_collaborator.go index 41ca685ec8..1195e7fbec 100644 --- a/github/resource_github_repository_collaborator.go +++ b/github/resource_github_repository_collaborator.go @@ -96,7 +96,7 @@ func resourceGithubRepositoryCollaboratorCreate(d *schema.ResourceData, meta any func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta any) error { client := meta.(*Owner).v3client - repoName, username, err := parseTwoPartID(d.Id(), "repository", "username") + repoName, username, err := parseID2(d.Id()) owner, repoNameWithoutOwner := parseRepoName(repoName, meta.(*Owner).name) if err != nil { return err diff --git a/github/resource_github_repository_custom_property.go b/github/resource_github_repository_custom_property.go index 22a56a15c2..777278231e 100644 --- a/github/resource_github_repository_custom_property.go +++ b/github/resource_github_repository_custom_property.go @@ -90,7 +90,7 @@ func resourceGithubRepositoryCustomPropertyRead(d *schema.ResourceData, meta any client := meta.(*Owner).v3client ctx := context.Background() - owner, repoName, propertyName, err := parseThreePartID(d.Id(), "owner", "repoName", "propertyName") + owner, repoName, propertyName, err := parseID3(d.Id()) if err != nil { return err } @@ -117,7 +117,7 @@ func resourceGithubRepositoryCustomPropertyDelete(d *schema.ResourceData, meta a client := meta.(*Owner).v3client ctx := context.Background() - owner, repoName, propertyName, err := parseThreePartID(d.Id(), "owner", "repoName", "propertyName") + owner, repoName, propertyName, err := parseID3(d.Id()) if err != nil { return err } diff --git a/github/resource_github_repository_deploy_key.go b/github/resource_github_repository_deploy_key.go index 9896e55da3..1e59fce5bd 100644 --- a/github/resource_github_repository_deploy_key.go +++ b/github/resource_github_repository_deploy_key.go @@ -88,7 +88,7 @@ func resourceGithubRepositoryDeployKeyRead(d *schema.ResourceData, meta any) err client := meta.(*Owner).v3client owner := meta.(*Owner).name - repoName, idString, err := parseTwoPartID(d.Id(), "repository", "ID") + repoName, idString, err := parseID2(d.Id()) if err != nil { return err } @@ -142,7 +142,7 @@ func resourceGithubRepositoryDeployKeyDelete(d *schema.ResourceData, meta any) e client := meta.(*Owner).v3client owner := meta.(*Owner).name - repoName, idString, err := parseTwoPartID(d.Id(), "repository", "ID") + repoName, idString, err := parseID2(d.Id()) if err != nil { return err } diff --git a/github/resource_github_repository_deploy_key_test.go b/github/resource_github_repository_deploy_key_test.go index 973725836f..fe138bb539 100644 --- a/github/resource_github_repository_deploy_key_test.go +++ b/github/resource_github_repository_deploy_key_test.go @@ -98,7 +98,7 @@ func testAccCheckGithubRepositoryDeployKeyDestroy(s *terraform.State) error { } owner := meta.name - repoName, idString, err := parseTwoPartID(rs.Primary.ID, "repository", "ID") + repoName, idString, err := parseID2(rs.Primary.ID) if err != nil { return err } @@ -136,7 +136,7 @@ func testAccCheckGithubRepositoryDeployKeyExists(ctx context.Context, n string) } conn := meta.v3client owner := meta.name - repoName, idString, err := parseTwoPartID(rs.Primary.ID, "repository", "ID") + repoName, idString, err := parseID2(rs.Primary.ID) if err != nil { return err } diff --git a/github/resource_github_repository_deployment_branch_policy.go b/github/resource_github_repository_deployment_branch_policy.go index 24ab03a751..a2a5be9457 100644 --- a/github/resource_github_repository_deployment_branch_policy.go +++ b/github/resource_github_repository_deployment_branch_policy.go @@ -168,7 +168,7 @@ func resourceGithubRepositoryDeploymentBranchPolicyDelete(d *schema.ResourceData } func resourceGithubRepositoryDeploymentBranchPolicyImport(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { - repoName, environmentName, id, err := parseThreePartID(d.Id(), "repository", "environment_name", "id") + repoName, environmentName, id, err := parseID3(d.Id()) if err != nil { return nil, err } diff --git a/github/resource_github_repository_file_migration_test.go b/github/resource_github_repository_file_migration_test.go index 94da05976f..b306d9b74f 100644 --- a/github/resource_github_repository_file_migration_test.go +++ b/github/resource_github_repository_file_migration_test.go @@ -142,7 +142,7 @@ package github // client.BaseURL = u // meta.v3client = client -// got, err := resourceGithubRepositoryFileStateUpgradeV0(context.Background(), d.rawState, meta) +// got, err := resourceGithubRepositoryFileStateUpgradeV0(t.Context(), d.rawState, meta) // if (err != nil) != d.shouldError { // t.Fatalf("unexpected error state: got error %v, shouldError %v", err, d.shouldError) // } diff --git a/github/resource_github_repository_pull_request.go b/github/resource_github_repository_pull_request.go index 88027ef0ac..b869ce13dd 100644 --- a/github/resource_github_repository_pull_request.go +++ b/github/resource_github_repository_pull_request.go @@ -328,7 +328,7 @@ func resourceGithubRepositoryPullRequestDelete(d *schema.ResourceData, meta any) func parsePullRequestID(d *schema.ResourceData) (owner, repository string, number int, err error) { var strNumber string - if owner, repository, strNumber, err = parseThreePartID(d.Id(), "owner", "base_repository", "number"); err != nil { + if owner, repository, strNumber, err = parseID3(d.Id()); err != nil { return owner, repository, number, err } diff --git a/github/resource_github_repository_ruleset.go b/github/resource_github_repository_ruleset.go index fb2549296b..eac2607adb 100644 --- a/github/resource_github_repository_ruleset.go +++ b/github/resource_github_repository_ruleset.go @@ -849,7 +849,7 @@ func resourceGithubRepositoryRulesetDelete(ctx context.Context, d *schema.Resour } func resourceGithubRepositoryRulesetImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { - repoName, rulesetIDStr, err := parseTwoPartID(d.Id(), "repository", "ruleset") + repoName, rulesetIDStr, err := parseID2(d.Id()) if err != nil { return []*schema.ResourceData{d}, err } diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index cc6df3c64d..72318dd273 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -1066,7 +1066,7 @@ resource "github_repository" "test" { `, testRepoName) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -1177,7 +1177,7 @@ resource "github_repository" "test" { } resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -1246,7 +1246,7 @@ resource "github_repository" "test" { `, testRepoName, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise); skipIfEMUEnterprise(t) }, + PreCheck: func() { skipUnlessEnterprise(t); skipIfEMUEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { diff --git a/github/resource_github_team_membership.go b/github/resource_github_team_membership.go index f81112c253..de7a4dec93 100644 --- a/github/resource_github_team_membership.go +++ b/github/resource_github_team_membership.go @@ -21,7 +21,7 @@ func resourceGithubTeamMembership() *schema.Resource { Importer: &schema.ResourceImporter{ StateContext: func(ctx context.Context, d *schema.ResourceData, m any) ([]*schema.ResourceData, error) { meta := m.(*Owner) - teamIdString, username, err := parseTwoPartID(d.Id(), "team_id", "username") + teamIdString, username, err := parseID2(d.Id()) if err != nil { return nil, err } @@ -101,7 +101,7 @@ func resourceGithubTeamMembershipRead(ctx context.Context, d *schema.ResourceDat client := meta.v3client orgId := meta.id - teamIdString, username, err := parseTwoPartID(d.Id(), "team_id", "username") + teamIdString, username, err := parseID2(d.Id()) if err != nil { return diag.FromErr(err) } diff --git a/github/resource_github_team_membership_test.go b/github/resource_github_team_membership_test.go index 643de342d9..40b034a591 100644 --- a/github/resource_github_team_membership_test.go +++ b/github/resource_github_team_membership_test.go @@ -97,7 +97,7 @@ func testAccCheckGithubTeamMembershipDestroy(s *terraform.State) error { continue } - teamIdString, username, err := parseTwoPartID(rs.Primary.ID, "team_id", "username") + teamIdString, username, err := parseID2(rs.Primary.ID) if err != nil { return err } @@ -139,7 +139,7 @@ func testAccCheckGithubTeamMembershipExists(ctx context.Context, n string, membe } conn := meta.v3client orgId := meta.id - teamIdString, username, err := parseTwoPartID(rs.Primary.ID, "team_id", "username") + teamIdString, username, err := parseID2(rs.Primary.ID) if err != nil { return err } @@ -175,7 +175,7 @@ func testAccCheckGithubTeamMembershipRoleState(ctx context.Context, n, expected } conn := meta.v3client orgId := meta.id - teamIdString, username, err := parseTwoPartID(rs.Primary.ID, "team_id", "username") + teamIdString, username, err := parseID2(rs.Primary.ID) if err != nil { return err } diff --git a/github/resource_github_team_repository.go b/github/resource_github_team_repository.go index 9b4bf8100c..3508226836 100644 --- a/github/resource_github_team_repository.go +++ b/github/resource_github_team_repository.go @@ -22,7 +22,7 @@ func resourceGithubTeamRepository() *schema.Resource { Importer: &schema.ResourceImporter{ StateContext: func(ctx context.Context, d *schema.ResourceData, m any) ([]*schema.ResourceData, error) { meta := m.(*Owner) - teamIdString, username, err := parseTwoPartID(d.Id(), "team_id", "username") + teamIdString, username, err := parseID2(d.Id()) if err != nil { return nil, err } @@ -114,7 +114,7 @@ func resourceGithubTeamRepositoryRead(ctx context.Context, d *schema.ResourceDat return diag.FromErr(err) } - teamIdString, repoName, err := parseTwoPartID(d.Id(), "team_id", "repository") + teamIdString, repoName, err := parseID2(d.Id()) if err != nil { return diag.FromErr(err) } @@ -173,7 +173,7 @@ func resourceGithubTeamRepositoryUpdate(ctx context.Context, d *schema.ResourceD client := meta.(*Owner).v3client orgId := meta.(*Owner).id - teamIdString, repoName, err := parseTwoPartID(d.Id(), "team_id", "repository") + teamIdString, repoName, err := parseID2(d.Id()) if err != nil { return diag.FromErr(err) } @@ -211,7 +211,7 @@ func resourceGithubTeamRepositoryDelete(ctx context.Context, d *schema.ResourceD client := meta.(*Owner).v3client orgId := meta.(*Owner).id - teamIdString, repoName, err := parseTwoPartID(d.Id(), "team_id", "repository") + teamIdString, repoName, err := parseID2(d.Id()) if err != nil { return diag.FromErr(err) } diff --git a/github/resource_github_team_sync_group_mapping_test.go b/github/resource_github_team_sync_group_mapping_test.go index 2f865d343e..1b83a20cc4 100644 --- a/github/resource_github_team_sync_group_mapping_test.go +++ b/github/resource_github_team_sync_group_mapping_test.go @@ -19,7 +19,7 @@ func TestAccGithubTeamSyncGroupMapping_basic(t *testing.T) { rn := "github_team_sync_group_mapping.test_mapping" resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, Steps: []resource.TestStep{ @@ -51,7 +51,7 @@ func TestAccGithubTeamSyncGroupMapping_basic(t *testing.T) { rn := "github_team_sync_group_mapping.test_mapping" resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, Steps: []resource.TestStep{ @@ -73,7 +73,7 @@ func TestAccGithubTeamSyncGroupMapping_basic(t *testing.T) { rn := "github_team_sync_group_mapping.test_mapping" resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, Steps: []resource.TestStep{ @@ -117,7 +117,7 @@ func TestAccGithubTeamSyncGroupMapping_basic(t *testing.T) { rn := "github_team_sync_group_mapping.test_mapping" resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, Steps: []resource.TestStep{ diff --git a/github/transport_test.go b/github/transport_test.go index c4176ca67a..8b1aaba15c 100644 --- a/github/transport_test.go +++ b/github/transport_test.go @@ -36,7 +36,7 @@ func TestEtagTransport(t *testing.T) { u, _ := url.Parse(ts.URL + "/") client.BaseURL = u - ctx := context.WithValue(context.Background(), ctxEtag, "something") + ctx := context.WithValue(t.Context(), ctxEtag, "something") r, _, err := client.Repositories.Get(ctx, "test", "blah") if err != nil { t.Fatal(err) @@ -149,7 +149,7 @@ func TestRateLimitTransport_abuseLimit_get(t *testing.T) { u, _ := url.Parse(ts.URL + "/") client.BaseURL = u - ctx := context.WithValue(context.Background(), ctxId, t.Name()) + ctx := context.WithValue(t.Context(), ctxId, t.Name()) r, _, err := client.Repositories.Get(ctx, "test", "blah") if err != nil { t.Fatal(err) @@ -230,7 +230,7 @@ func TestRateLimitTransport_abuseLimit_post(t *testing.T) { u, _ := url.Parse(ts.URL + "/") client.BaseURL = u - ctx := context.WithValue(context.Background(), ctxId, t.Name()) + ctx := context.WithValue(t.Context(), ctxId, t.Name()) r, _, err := client.Repositories.Create(ctx, "tada", &github.Repository{ Name: new("radek-example-48"), Description: new(""), @@ -290,7 +290,7 @@ func TestRateLimitTransport_abuseLimit_post_error(t *testing.T) { u, _ := url.Parse(ts.URL + "/") client.BaseURL = u - ctx := context.WithValue(context.Background(), ctxId, t.Name()) + ctx := context.WithValue(t.Context(), ctxId, t.Name()) _, _, err := client.Repositories.Create(ctx, "tada", &github.Repository{ Name: new("radek-example-48"), Description: new(""), @@ -423,7 +423,7 @@ func TestRetryTransport_retry_post_error(t *testing.T) { u, _ := url.Parse(ts.URL + "/") client.BaseURL = u - ctx := context.WithValue(context.Background(), ctxId, t.Name()) + ctx := context.WithValue(t.Context(), ctxId, t.Name()) _, _, err := client.Repositories.Create(ctx, "tada", &github.Repository{ Name: new("radek-example-48"), Description: new(""), @@ -486,7 +486,7 @@ func TestRetryTransport_retry_post_success(t *testing.T) { u, _ := url.Parse(ts.URL + "/") client.BaseURL = u - ctx := context.WithValue(context.Background(), ctxId, t.Name()) + ctx := context.WithValue(t.Context(), ctxId, t.Name()) _, _, err := client.Repositories.Create(ctx, "tada", &github.Repository{ Name: new("radek-example-48"), Description: new(""), diff --git a/github/util.go b/github/util.go index c62e7b6277..7930b6d8f4 100644 --- a/github/util.go +++ b/github/util.go @@ -110,60 +110,23 @@ func caseInsensitive() schema.SchemaDiffSuppressFunc { } } -// wrapErrors is provided to easily turn errors into diag.Diagnostics -// until we go through the provider and replace error usage. -func wrapErrors(errs []error) diag.Diagnostics { - var diags diag.Diagnostics - - for _, err := range errs { - diags = append(diags, diag.Diagnostic{ - Severity: diag.Error, - Summary: "Error", - Detail: err.Error(), - }) - } - - return diags -} - func validateValueFunc(values []string) schema.SchemaValidateDiagFunc { return func(v any, k cty.Path) diag.Diagnostics { - errs := make([]error, 0) value := v.(string) valid := slices.Contains(values, value) if !valid { - errs = append(errs, fmt.Errorf("%s is an invalid value for argument %s", value, k)) + return diag.Errorf("%s is an invalid value for argument %s", value, k) } - return wrapErrors(errs) + return nil } } -// return the pieces of id `left:right` as left, right. -func parseTwoPartID(id, left, right string) (string, string, error) { - parts := strings.SplitN(id, ":", 2) - if len(parts) != 2 { - return "", "", fmt.Errorf("unexpected ID format (%q); expected %s:%s", id, left, right) - } - - return parts[0], parts[1], nil -} - // format the strings into an id `a:b`. func buildTwoPartID(a, b string) string { return fmt.Sprintf("%s:%s", a, b) } -// return the pieces of id `left:center:right` as left, center, right. -func parseThreePartID(id, left, center, right string) (string, string, string, error) { - parts := strings.SplitN(id, ":", 3) - if len(parts) != 3 { - return "", "", "", fmt.Errorf("unexpected ID format (%q). Expected %s:%s:%s", id, left, center, right) - } - - return parts[0], parts[1], parts[2], nil -} - // format the strings into an id `a:b:c`. func buildThreePartID(a, b, c string) string { return fmt.Sprintf("%s:%s:%s", a, b, c) @@ -217,21 +180,31 @@ func (e *unconvertibleIdError) Error() string { var secretNameRegexp = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") func validateSecretNameFunc(v any, path cty.Path) diag.Diagnostics { - errs := make([]error, 0) + var diags diag.Diagnostics name, ok := v.(string) if !ok { - return wrapErrors([]error{fmt.Errorf("expected type of %s to be string", path)}) + return diag.Errorf("expected type of %s to be string", path) } if !secretNameRegexp.MatchString(name) { - errs = append(errs, errors.New("secret names can only contain alphanumeric characters or underscores and must not start with a number")) + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Detail: "secret names can only contain alphanumeric characters or underscores and must not start with a number", + Summary: "Error", + AttributePath: path, + }) } if strings.HasPrefix(strings.ToUpper(name), "GITHUB_") { - errs = append(errs, errors.New("secret names must not start with the GITHUB_ prefix")) + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Detail: "secret names must not start with the GITHUB_ prefix", + Summary: "Error", + AttributePath: path, + }) } - return wrapErrors(errs) + return diags } // deleteResourceOn404AndSwallow304OtherwiseReturnError will log and delete resource if error is 404 which indicates resource (or any of its ancestors) diff --git a/github/util_test.go b/github/util_test.go index 6442a70ff2..83c8a66653 100644 --- a/github/util_test.go +++ b/github/util_test.go @@ -497,29 +497,6 @@ func TestGithubUtilRole_validation(t *testing.T) { } } -func TestGithubUtilTwoPartID(t *testing.T) { - partOne, partTwo := "foo", "bar" - - id := buildTwoPartID(partOne, partTwo) - - if id != "foo:bar" { - t.Fatalf("Expected two part id to be foo:bar, actual: %s", id) - } - - parsedPartOne, parsedPartTwo, err := parseTwoPartID(id, "left", "right") - if err != nil { - t.Fatal(err) - } - - if parsedPartOne != "foo" { - t.Fatalf("Expected parsed part one foo, actual: %s", parsedPartOne) - } - - if parsedPartTwo != "bar" { - t.Fatalf("Expected parsed part two bar, actual: %s", parsedPartTwo) - } -} - func flipUsernameCase(username string) string { oc := []rune(username) @@ -585,3 +562,36 @@ func TestGithubUtilValidateSecretName(t *testing.T) { } } } + +func TestGithubUtilValidateSecretName_invalid_input(t *testing.T) { + cases := []struct { + Name any + Error bool + }{ + { + Name: 1, + Error: true, + }, + { + Name: []string{"v"}, + Error: true, + }, + { + Name: map[string]string{"_valid_underscore_": "valid_underscore"}, + Error: true, + }, + } + + for _, tc := range cases { + name := tc.Name + diags := validateSecretNameFunc(name, cty.Path{cty.GetAttrStep{Name: ""}}) + + if tc.Error != (len(diags) != 0) { + if tc.Error { + t.Fatalf("expected error, got none (%s)", tc.Name) + } else { + t.Fatalf("unexpected error(s): %v (%s)", diags, tc.Name) + } + } + } +}