Skip to content

Commit ca8ae1b

Browse files
authored
[bug]: url encode environment name in github_actions_environment_secret and github_actions_environment_variable data sources and resources
2 parents 2cd8a94 + 926bafb commit ca8ae1b

8 files changed

+40
-27
lines changed

github/data_source_github_actions_environment_secrets.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"context"
55
"fmt"
6+
"net/url"
67

78
"github.com/google/go-github/v55/github"
89

@@ -59,7 +60,8 @@ func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta
5960
owner := meta.(*Owner).name
6061
var repoName string
6162

62-
env := d.Get("environment").(string)
63+
envName := d.Get("environment").(string)
64+
escapedEnvName := url.PathEscape(envName)
6365

6466
if fullName, ok := d.GetOk("full_name"); ok {
6567
var err error
@@ -88,7 +90,7 @@ func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta
8890

8991
var all_secrets []map[string]string
9092
for {
91-
secrets, resp, err := client.Actions.ListEnvSecrets(context.TODO(), int(repo.GetID()), env, &options)
93+
secrets, resp, err := client.Actions.ListEnvSecrets(context.TODO(), int(repo.GetID()), escapedEnvName, &options)
9294
if err != nil {
9395
return err
9496
}
@@ -106,7 +108,7 @@ func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta
106108
options.Page = resp.NextPage
107109
}
108110

109-
d.SetId(buildTwoPartID(repoName, env))
111+
d.SetId(buildTwoPartID(repoName, envName))
110112
d.Set("secrets", all_secrets)
111113

112114
return nil

github/data_source_github_actions_environment_secrets_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestAccGithubActionsEnvironmentSecretsDataSource(t *testing.T) {
2121
2222
resource "github_repository_environment" "test" {
2323
repository = github_repository.test.name
24-
environment = "test_environment_name"
24+
environment = "environment/test"
2525
}
2626
2727
resource "github_actions_environment_secret" "test" {

github/data_source_github_actions_environment_variables.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"context"
55
"fmt"
6+
"net/url"
67

78
"github.com/google/go-github/v55/github"
89

@@ -63,7 +64,8 @@ func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, met
6364
owner := meta.(*Owner).name
6465
var repoName string
6566

66-
env := d.Get("environment").(string)
67+
envName := d.Get("environment").(string)
68+
escapedEnvName := url.PathEscape(envName)
6769

6870
if fullName, ok := d.GetOk("full_name"); ok {
6971
var err error
@@ -92,7 +94,7 @@ func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, met
9294

9395
var all_variables []map[string]string
9496
for {
95-
variables, resp, err := client.Actions.ListEnvVariables(context.TODO(), int(repo.GetID()), env, &options)
97+
variables, resp, err := client.Actions.ListEnvVariables(context.TODO(), int(repo.GetID()), escapedEnvName, &options)
9698
if err != nil {
9799
return err
98100
}
@@ -111,7 +113,7 @@ func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, met
111113
options.Page = resp.NextPage
112114
}
113115

114-
d.SetId(buildTwoPartID(repoName, env))
116+
d.SetId(buildTwoPartID(repoName, envName))
115117
d.Set("variables", all_variables)
116118

117119
return nil

github/data_source_github_actions_environment_variables_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestAccGithubActionsEnvironmentVariablesDataSource(t *testing.T) {
2121
2222
resource "github_repository_environment" "test" {
2323
repository = github_repository.test.name
24-
environment = "test_environment_name"
24+
environment = "environment/test"
2525
}
2626
2727
resource "github_actions_environment_variable" "variable" {

github/resource_github_actions_environment_secret.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"log"
77
"net/http"
8+
"net/url"
89

910
"github.com/google/go-github/v55/github"
1011
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -75,6 +76,7 @@ func resourceGithubActionsEnvironmentSecretCreateOrUpdate(d *schema.ResourceData
7576

7677
repoName := d.Get("repository").(string)
7778
envName := d.Get("environment").(string)
79+
escapedEnvName := url.PathEscape(envName)
7880
secretName := d.Get("secret_name").(string)
7981
plaintextValue := d.Get("plaintext_value").(string)
8082
var encryptedValue string
@@ -84,7 +86,7 @@ func resourceGithubActionsEnvironmentSecretCreateOrUpdate(d *schema.ResourceData
8486
return err
8587
}
8688

87-
keyId, publicKey, err := getEnvironmentPublicKeyDetails(repo.GetID(), envName, meta)
89+
keyId, publicKey, err := getEnvironmentPublicKeyDetails(repo.GetID(), escapedEnvName, meta)
8890
if err != nil {
8991
return err
9092
}
@@ -124,13 +126,14 @@ func resourceGithubActionsEnvironmentSecretRead(d *schema.ResourceData, meta int
124126
if err != nil {
125127
return err
126128
}
129+
escapedEnvName := url.PathEscape(envName)
127130

128131
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
129132
if err != nil {
130133
return err
131134
}
132135

133-
secret, _, err := client.Actions.GetEnvSecret(ctx, int(repo.GetID()), envName, secretName)
136+
secret, _, err := client.Actions.GetEnvSecret(ctx, int(repo.GetID()), escapedEnvName, secretName)
134137
if err != nil {
135138
if ghErr, ok := err.(*github.ErrorResponse); ok {
136139
if ghErr.Response.StatusCode == http.StatusNotFound {
@@ -181,12 +184,13 @@ func resourceGithubActionsEnvironmentSecretDelete(d *schema.ResourceData, meta i
181184
if err != nil {
182185
return err
183186
}
187+
escapedEnvName := url.PathEscape(envName)
184188
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
185189
if err != nil {
186190
return err
187191
}
188192
log.Printf("[INFO] Deleting environment secret: %s", d.Id())
189-
_, err = client.Actions.DeleteEnvSecret(ctx, int(repo.GetID()), envName, secretName)
193+
_, err = client.Actions.DeleteEnvSecret(ctx, int(repo.GetID()), escapedEnvName, secretName)
190194

191195
return err
192196
}

github/resource_github_actions_environment_secret_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) {
2626
2727
resource "github_repository_environment" "test" {
2828
repository = github_repository.test.name
29-
environment = "test_environment_name"
29+
environment = "environment/test"
3030
}
3131
3232
resource "github_actions_environment_secret" "plaintext_secret" {
@@ -122,7 +122,7 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) {
122122
123123
resource "github_repository_environment" "test" {
124124
repository = github_repository.test.name
125-
environment = "test_environment_name"
125+
environment = "environment/test"
126126
}
127127
128128
resource "github_actions_environment_secret" "plaintext_secret" {

github/resource_github_actions_environment_variable.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log"
66
"net/http"
7+
"net/url"
78

89
"github.com/google/go-github/v55/github"
910
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -63,7 +64,8 @@ func resourceGithubActionsEnvironmentVariableCreate(d *schema.ResourceData, meta
6364
ctx := context.Background()
6465

6566
repoName := d.Get("repository").(string)
66-
env := d.Get("environment").(string)
67+
envName := d.Get("environment").(string)
68+
escapedEnvName := url.PathEscape(envName)
6769
name := d.Get("variable_name").(string)
6870

6971
variable := &github.ActionsVariable{
@@ -76,12 +78,12 @@ func resourceGithubActionsEnvironmentVariableCreate(d *schema.ResourceData, meta
7678
return err
7779
}
7880

79-
_, err = client.Actions.CreateEnvVariable(ctx, int(repo.GetID()), env, variable)
81+
_, err = client.Actions.CreateEnvVariable(ctx, int(repo.GetID()), escapedEnvName, variable)
8082
if err != nil {
8183
return err
8284
}
8385

84-
d.SetId(buildThreePartID(repoName, env, name))
86+
d.SetId(buildThreePartID(repoName, envName, name))
8587
return resourceGithubActionsEnvironmentVariableRead(d, meta)
8688
}
8789

@@ -91,7 +93,8 @@ func resourceGithubActionsEnvironmentVariableUpdate(d *schema.ResourceData, meta
9193
ctx := context.Background()
9294

9395
repoName := d.Get("repository").(string)
94-
env := d.Get("environment").(string)
96+
envName := d.Get("environment").(string)
97+
escapedEnvName := url.PathEscape(envName)
9598
name := d.Get("variable_name").(string)
9699

97100
variable := &github.ActionsVariable{
@@ -103,12 +106,12 @@ func resourceGithubActionsEnvironmentVariableUpdate(d *schema.ResourceData, meta
103106
if err != nil {
104107
return err
105108
}
106-
_, err = client.Actions.UpdateEnvVariable(ctx, int(repo.GetID()), env, variable)
109+
_, err = client.Actions.UpdateEnvVariable(ctx, int(repo.GetID()), escapedEnvName, variable)
107110
if err != nil {
108111
return err
109112
}
110113

111-
d.SetId(buildThreePartID(repoName, env, name))
114+
d.SetId(buildThreePartID(repoName, envName, name))
112115
return resourceGithubActionsEnvironmentVariableRead(d, meta)
113116
}
114117

@@ -117,17 +120,18 @@ func resourceGithubActionsEnvironmentVariableRead(d *schema.ResourceData, meta i
117120
owner := meta.(*Owner).name
118121
ctx := context.Background()
119122

120-
repoName, env, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
123+
repoName, envName, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
121124
if err != nil {
122125
return err
123126
}
127+
escapedEnvName := url.PathEscape(envName)
124128

125129
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
126130
if err != nil {
127131
return err
128132
}
129133

130-
variable, _, err := client.Actions.GetEnvVariable(ctx, int(repo.GetID()), env, name)
134+
variable, _, err := client.Actions.GetEnvVariable(ctx, int(repo.GetID()), escapedEnvName, name)
131135
if err != nil {
132136
if ghErr, ok := err.(*github.ErrorResponse); ok {
133137
if ghErr.Response.StatusCode == http.StatusNotFound {
@@ -141,7 +145,7 @@ func resourceGithubActionsEnvironmentVariableRead(d *schema.ResourceData, meta i
141145
}
142146

143147
d.Set("repository", repoName)
144-
d.Set("environment", env)
148+
d.Set("environment", envName)
145149
d.Set("variable_name", name)
146150
d.Set("value", variable.Value)
147151
d.Set("created_at", variable.CreatedAt.String())
@@ -155,17 +159,18 @@ func resourceGithubActionsEnvironmentVariableDelete(d *schema.ResourceData, meta
155159
owner := meta.(*Owner).name
156160
ctx := context.WithValue(context.Background(), ctxId, d.Id())
157161

158-
repoName, env, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
162+
repoName, envName, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
159163
if err != nil {
160164
return err
161165
}
166+
escapedEnvName := url.QueryEscape(envName)
162167

163168
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
164169
if err != nil {
165170
return err
166171
}
167172

168-
_, err = client.Actions.DeleteEnvVariable(ctx, int(repo.GetID()), env, name)
173+
_, err = client.Actions.DeleteEnvVariable(ctx, int(repo.GetID()), escapedEnvName, name)
169174

170175
return err
171176
}

github/resource_github_actions_environment_variable_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) {
2525
2626
resource "github_repository_environment" "test" {
2727
repository = github_repository.test.name
28-
environment = "test_environment_name"
28+
environment = "environment/test"
2929
}
3030
3131
resource "github_actions_environment_variable" "variable" {
@@ -103,7 +103,7 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) {
103103
104104
resource "github_repository_environment" "test" {
105105
repository = github_repository.test.name
106-
environment = "test_environment_name"
106+
environment = "environment/test"
107107
}
108108
109109
resource "github_actions_environment_variable" "variable" {
@@ -143,7 +143,7 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) {
143143

144144
t.Run("imports environment variables without error", func(t *testing.T) {
145145
value := "my_variable_value"
146-
envName := "test_environment_name"
146+
envName := "environment/test"
147147
varName := "test_variable"
148148

149149
config := fmt.Sprintf(`

0 commit comments

Comments
 (0)