Skip to content

Commit

Permalink
[bug]: url encode environment name in `github_actions_environment_sec…
Browse files Browse the repository at this point in the history
…ret` and `github_actions_environment_variable` data sources and resources
  • Loading branch information
nickfloyd authored Sep 27, 2023
2 parents 2cd8a94 + 926bafb commit ca8ae1b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
8 changes: 5 additions & 3 deletions github/data_source_github_actions_environment_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"fmt"
"net/url"

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

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

env := d.Get("environment").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)

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

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

d.SetId(buildTwoPartID(repoName, env))
d.SetId(buildTwoPartID(repoName, envName))
d.Set("secrets", all_secrets)

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccGithubActionsEnvironmentSecretsDataSource(t *testing.T) {
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "test_environment_name"
environment = "environment/test"
}
resource "github_actions_environment_secret" "test" {
Expand Down
8 changes: 5 additions & 3 deletions github/data_source_github_actions_environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"fmt"
"net/url"

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

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

env := d.Get("environment").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)

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

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

d.SetId(buildTwoPartID(repoName, env))
d.SetId(buildTwoPartID(repoName, envName))
d.Set("variables", all_variables)

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccGithubActionsEnvironmentVariablesDataSource(t *testing.T) {
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "test_environment_name"
environment = "environment/test"
}
resource "github_actions_environment_variable" "variable" {
Expand Down
10 changes: 7 additions & 3 deletions github/resource_github_actions_environment_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"log"
"net/http"
"net/url"

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

repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)
secretName := d.Get("secret_name").(string)
plaintextValue := d.Get("plaintext_value").(string)
var encryptedValue string
Expand All @@ -84,7 +86,7 @@ func resourceGithubActionsEnvironmentSecretCreateOrUpdate(d *schema.ResourceData
return err
}

keyId, publicKey, err := getEnvironmentPublicKeyDetails(repo.GetID(), envName, meta)
keyId, publicKey, err := getEnvironmentPublicKeyDetails(repo.GetID(), escapedEnvName, meta)
if err != nil {
return err
}
Expand Down Expand Up @@ -124,13 +126,14 @@ func resourceGithubActionsEnvironmentSecretRead(d *schema.ResourceData, meta int
if err != nil {
return err
}
escapedEnvName := url.PathEscape(envName)

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

secret, _, err := client.Actions.GetEnvSecret(ctx, int(repo.GetID()), envName, secretName)
secret, _, err := client.Actions.GetEnvSecret(ctx, int(repo.GetID()), escapedEnvName, secretName)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotFound {
Expand Down Expand Up @@ -181,12 +184,13 @@ func resourceGithubActionsEnvironmentSecretDelete(d *schema.ResourceData, meta i
if err != nil {
return err
}
escapedEnvName := url.PathEscape(envName)
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return err
}
log.Printf("[INFO] Deleting environment secret: %s", d.Id())
_, err = client.Actions.DeleteEnvSecret(ctx, int(repo.GetID()), envName, secretName)
_, err = client.Actions.DeleteEnvSecret(ctx, int(repo.GetID()), escapedEnvName, secretName)

return err
}
Expand Down
4 changes: 2 additions & 2 deletions github/resource_github_actions_environment_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) {
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "test_environment_name"
environment = "environment/test"
}
resource "github_actions_environment_secret" "plaintext_secret" {
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) {
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "test_environment_name"
environment = "environment/test"
}
resource "github_actions_environment_secret" "plaintext_secret" {
Expand Down
27 changes: 16 additions & 11 deletions github/resource_github_actions_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"
"net/http"
"net/url"

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

repoName := d.Get("repository").(string)
env := d.Get("environment").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)
name := d.Get("variable_name").(string)

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

_, err = client.Actions.CreateEnvVariable(ctx, int(repo.GetID()), env, variable)
_, err = client.Actions.CreateEnvVariable(ctx, int(repo.GetID()), escapedEnvName, variable)
if err != nil {
return err
}

d.SetId(buildThreePartID(repoName, env, name))
d.SetId(buildThreePartID(repoName, envName, name))
return resourceGithubActionsEnvironmentVariableRead(d, meta)
}

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

repoName := d.Get("repository").(string)
env := d.Get("environment").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)
name := d.Get("variable_name").(string)

variable := &github.ActionsVariable{
Expand All @@ -103,12 +106,12 @@ func resourceGithubActionsEnvironmentVariableUpdate(d *schema.ResourceData, meta
if err != nil {
return err
}
_, err = client.Actions.UpdateEnvVariable(ctx, int(repo.GetID()), env, variable)
_, err = client.Actions.UpdateEnvVariable(ctx, int(repo.GetID()), escapedEnvName, variable)
if err != nil {
return err
}

d.SetId(buildThreePartID(repoName, env, name))
d.SetId(buildThreePartID(repoName, envName, name))
return resourceGithubActionsEnvironmentVariableRead(d, meta)
}

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

repoName, env, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
repoName, envName, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
if err != nil {
return err
}
escapedEnvName := url.PathEscape(envName)

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

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

d.Set("repository", repoName)
d.Set("environment", env)
d.Set("environment", envName)
d.Set("variable_name", name)
d.Set("value", variable.Value)
d.Set("created_at", variable.CreatedAt.String())
Expand All @@ -155,17 +159,18 @@ func resourceGithubActionsEnvironmentVariableDelete(d *schema.ResourceData, meta
owner := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

repoName, env, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
repoName, envName, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
if err != nil {
return err
}
escapedEnvName := url.QueryEscape(envName)

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

_, err = client.Actions.DeleteEnvVariable(ctx, int(repo.GetID()), env, name)
_, err = client.Actions.DeleteEnvVariable(ctx, int(repo.GetID()), escapedEnvName, name)

return err
}
6 changes: 3 additions & 3 deletions github/resource_github_actions_environment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) {
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "test_environment_name"
environment = "environment/test"
}
resource "github_actions_environment_variable" "variable" {
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) {
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "test_environment_name"
environment = "environment/test"
}
resource "github_actions_environment_variable" "variable" {
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) {

t.Run("imports environment variables without error", func(t *testing.T) {
value := "my_variable_value"
envName := "test_environment_name"
envName := "environment/test"
varName := "test_variable"

config := fmt.Sprintf(`
Expand Down

0 comments on commit ca8ae1b

Please sign in to comment.