Skip to content

Commit d4d121f

Browse files
authored
fix: Escape environment name for id (#3079)
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent 0271796 commit d4d121f

19 files changed

+860
-884
lines changed

github/data_source_github_actions_environment_public_key.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55
"net/url"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
)
910

1011
func dataSourceGithubActionsEnvironmentPublicKey() *schema.Resource {
1112
return &schema.Resource{
12-
Read: dataSourceGithubActionsEnvironmentPublicKeyRead,
13+
ReadContext: dataSourceGithubActionsEnvironmentPublicKeyRead,
1314

1415
Schema: map[string]*schema.Schema{
1516
"repository": {
@@ -32,33 +33,29 @@ func dataSourceGithubActionsEnvironmentPublicKey() *schema.Resource {
3233
}
3334
}
3435

35-
func dataSourceGithubActionsEnvironmentPublicKeyRead(d *schema.ResourceData, meta any) error {
36-
ctx := context.Background()
36+
func dataSourceGithubActionsEnvironmentPublicKeyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
3737
client := meta.(*Owner).v3client
3838
owner := meta.(*Owner).name
3939
repository := d.Get("repository").(string)
4040

4141
envName := d.Get("environment").(string)
42-
escapedEnvName := url.PathEscape(envName)
4342

4443
repo, _, err := client.Repositories.Get(ctx, owner, repository)
4544
if err != nil {
46-
return err
45+
return diag.FromErr(err)
4746
}
4847

49-
publicKey, _, err := client.Actions.GetEnvPublicKey(ctx, int(repo.GetID()), escapedEnvName)
48+
publicKey, _, err := client.Actions.GetEnvPublicKey(ctx, int(repo.GetID()), url.PathEscape(envName))
5049
if err != nil {
51-
return err
50+
return diag.FromErr(err)
5251
}
5352

5453
d.SetId(publicKey.GetKeyID())
55-
err = d.Set("key_id", publicKey.GetKeyID())
56-
if err != nil {
57-
return err
54+
if err := d.Set("key_id", publicKey.GetKeyID()); err != nil {
55+
return diag.FromErr(err)
5856
}
59-
err = d.Set("key", publicKey.GetKey())
60-
if err != nil {
61-
return err
57+
if err := d.Set("key", publicKey.GetKey()); err != nil {
58+
return diag.FromErr(err)
6259
}
6360

6461
return nil

github/data_source_github_actions_environment_secrets.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ package github
22

33
import (
44
"context"
5-
"fmt"
65
"net/url"
76

87
"github.com/google/go-github/v81/github"
98

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1111
)
1212

1313
func dataSourceGithubActionsEnvironmentSecrets() *schema.Resource {
1414
return &schema.Resource{
15-
Read: dataSourceGithubActionsEnvironmentSecretsRead,
15+
ReadContext: dataSourceGithubActionsEnvironmentSecretsRead,
1616

1717
Schema: map[string]*schema.Schema{
1818
"full_name": {
@@ -55,20 +55,18 @@ func dataSourceGithubActionsEnvironmentSecrets() *schema.Resource {
5555
}
5656
}
5757

58-
func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta any) error {
59-
ctx := context.Background()
58+
func dataSourceGithubActionsEnvironmentSecretsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
6059
client := meta.(*Owner).v3client
6160
owner := meta.(*Owner).name
6261
var repoName string
6362

6463
envName := d.Get("environment").(string)
65-
escapedEnvName := url.PathEscape(envName)
6664

6765
if fullName, ok := d.GetOk("full_name"); ok {
6866
var err error
6967
owner, repoName, err = splitRepoFullName(fullName.(string))
7068
if err != nil {
71-
return err
69+
return diag.FromErr(err)
7270
}
7371
}
7472

@@ -77,23 +75,23 @@ func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta
7775
}
7876

7977
if repoName == "" {
80-
return fmt.Errorf("one of %q or %q has to be provided", "full_name", "name")
78+
return diag.Errorf("one of %q or %q has to be provided", "full_name", "name")
8179
}
8280

8381
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
8482
if err != nil {
85-
return err
83+
return diag.FromErr(err)
8684
}
8785

8886
options := github.ListOptions{
89-
PerPage: 100,
87+
PerPage: maxPerPage,
9088
}
9189

9290
var all_secrets []map[string]string
9391
for {
94-
secrets, resp, err := client.Actions.ListEnvSecrets(ctx, int(repo.GetID()), escapedEnvName, &options)
92+
secrets, resp, err := client.Actions.ListEnvSecrets(ctx, int(repo.GetID()), url.PathEscape(envName), &options)
9593
if err != nil {
96-
return err
94+
return diag.FromErr(err)
9795
}
9896
for _, secret := range secrets.Secrets {
9997
new_secret := map[string]string{
@@ -109,8 +107,15 @@ func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta
109107
options.Page = resp.NextPage
110108
}
111109

112-
d.SetId(buildTwoPartID(repoName, envName))
113-
_ = d.Set("secrets", all_secrets)
110+
if id, err := buildID(repoName, escapeIDPart(envName)); err != nil {
111+
return diag.FromErr(err)
112+
} else {
113+
d.SetId(id)
114+
}
115+
116+
if err := d.Set("secrets", all_secrets); err != nil {
117+
return diag.FromErr(err)
118+
}
114119

115120
return nil
116121
}

github/data_source_github_actions_environment_secrets_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ func TestAccGithubActionsEnvironmentSecretsDataSource(t *testing.T) {
1515

1616
config := fmt.Sprintf(`
1717
resource "github_repository" "test" {
18-
name = "%s"
19-
auto_init = true
18+
name = "%s"
2019
}
2120
2221
resource "github_repository_environment" "test" {
23-
repository = github_repository.test.name
24-
environment = "environment / test"
22+
repository = github_repository.test.name
23+
environment = "environment / test"
2524
}
2625
2726
resource "github_actions_environment_secret" "test" {
28-
secret_name = "secret_1"
29-
environment = github_repository_environment.test.environment
30-
repository = github_repository.test.name
27+
repository = github_repository.test.name
28+
environment = github_repository_environment.test.environment
29+
secret_name = "secret_1"
3130
plaintext_value = "foo"
3231
}
3332
`, repoName)

github/data_source_github_actions_environment_variables.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ package github
22

33
import (
44
"context"
5-
"fmt"
65
"net/url"
76

87
"github.com/google/go-github/v81/github"
98

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1111
)
1212

1313
func dataSourceGithubActionsEnvironmentVariables() *schema.Resource {
1414
return &schema.Resource{
15-
Read: dataSourceGithubActionsEnvironmentVariablesRead,
15+
ReadContext: dataSourceGithubActionsEnvironmentVariablesRead,
1616

1717
Schema: map[string]*schema.Schema{
1818
"full_name": {
@@ -59,20 +59,18 @@ func dataSourceGithubActionsEnvironmentVariables() *schema.Resource {
5959
}
6060
}
6161

62-
func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, meta any) error {
63-
ctx := context.Background()
62+
func dataSourceGithubActionsEnvironmentVariablesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
6463
client := meta.(*Owner).v3client
6564
owner := meta.(*Owner).name
6665
var repoName string
6766

6867
envName := d.Get("environment").(string)
69-
escapedEnvName := url.PathEscape(envName)
7068

7169
if fullName, ok := d.GetOk("full_name"); ok {
7270
var err error
7371
owner, repoName, err = splitRepoFullName(fullName.(string))
7472
if err != nil {
75-
return err
73+
return diag.FromErr(err)
7674
}
7775
}
7876

@@ -81,18 +79,18 @@ func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, met
8179
}
8280

8381
if repoName == "" {
84-
return fmt.Errorf("one of %q or %q has to be provided", "full_name", "name")
82+
return diag.Errorf("one of %q or %q has to be provided", "full_name", "name")
8583
}
8684

8785
options := github.ListOptions{
88-
PerPage: 100,
86+
PerPage: maxPerPage,
8987
}
9088

9189
var all_variables []map[string]string
9290
for {
93-
variables, resp, err := client.Actions.ListEnvVariables(ctx, owner, repoName, escapedEnvName, &options)
91+
variables, resp, err := client.Actions.ListEnvVariables(ctx, owner, repoName, url.PathEscape(envName), &options)
9492
if err != nil {
95-
return err
93+
return diag.FromErr(err)
9694
}
9795
for _, variable := range variables.Variables {
9896
new_variable := map[string]string{
@@ -109,8 +107,15 @@ func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, met
109107
options.Page = resp.NextPage
110108
}
111109

112-
d.SetId(buildTwoPartID(repoName, envName))
113-
_ = d.Set("variables", all_variables)
110+
if id, err := buildID(repoName, escapeIDPart(envName)); err != nil {
111+
return diag.FromErr(err)
112+
} else {
113+
d.SetId(id)
114+
}
115+
116+
if err := d.Set("variables", all_variables); err != nil {
117+
return diag.FromErr(err)
118+
}
114119

115120
return nil
116121
}

github/data_source_github_repository_environment_deployment_policies.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package github
22

33
import (
44
"context"
5-
"fmt"
5+
"net/url"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -49,9 +49,9 @@ func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(ctx context.Con
4949
client := meta.(*Owner).v3client
5050
owner := meta.(*Owner).name
5151
repoName := d.Get("repository").(string)
52-
environmentName := d.Get("environment").(string)
52+
envName := d.Get("environment").(string)
5353

54-
policies, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, owner, repoName, environmentName)
54+
policies, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, owner, repoName, url.PathEscape(envName))
5555
if err != nil {
5656
return diag.FromErr(err)
5757
}
@@ -65,9 +65,13 @@ func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(ctx context.Con
6565
results = append(results, policyMap)
6666
}
6767

68-
d.SetId(fmt.Sprintf("%s:%s", repoName, environmentName))
69-
err = d.Set("policies", results)
70-
if err != nil {
68+
if id, err := buildID(repoName, escapeIDPart(envName)); err != nil {
69+
return diag.FromErr(err)
70+
} else {
71+
d.SetId(id)
72+
}
73+
74+
if err = d.Set("policies", results); err != nil {
7175
return diag.FromErr(err)
7276
}
7377

github/data_source_github_repository_environments.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55

66
"github.com/google/go-github/v81/github"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
)
910

1011
func dataSourceGithubRepositoryEnvironments() *schema.Resource {
1112
return &schema.Resource{
12-
Read: dataSourceGithubRepositoryEnvironmentsRead,
13+
ReadContext: dataSourceGithubRepositoryEnvironmentsRead,
1314

1415
Schema: map[string]*schema.Schema{
1516
"repository": {
@@ -36,7 +37,7 @@ func dataSourceGithubRepositoryEnvironments() *schema.Resource {
3637
}
3738
}
3839

39-
func dataSourceGithubRepositoryEnvironmentsRead(d *schema.ResourceData, meta any) error {
40+
func dataSourceGithubRepositoryEnvironmentsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
4041
client := meta.(*Owner).v3client
4142
orgName := meta.(*Owner).name
4243
repoName := d.Get("repository").(string)
@@ -45,9 +46,9 @@ func dataSourceGithubRepositoryEnvironmentsRead(d *schema.ResourceData, meta any
4546

4647
for {
4748
listOptions := &github.EnvironmentListOptions{}
48-
environments, resp, err := client.Repositories.ListEnvironments(context.Background(), orgName, repoName, listOptions)
49+
environments, resp, err := client.Repositories.ListEnvironments(ctx, orgName, repoName, listOptions)
4950
if err != nil {
50-
return err
51+
return diag.FromErr(err)
5152
}
5253

5354
results = append(results, flattenEnvironments(environments)...)
@@ -60,9 +61,8 @@ func dataSourceGithubRepositoryEnvironmentsRead(d *schema.ResourceData, meta any
6061
}
6162

6263
d.SetId(repoName)
63-
err := d.Set("environments", results)
64-
if err != nil {
65-
return err
64+
if err := d.Set("environments", results); err != nil {
65+
return diag.FromErr(err)
6666
}
6767

6868
return nil

0 commit comments

Comments
 (0)