Skip to content

Commit

Permalink
URL encode environment name in github_repository_environment (#1392)
Browse files Browse the repository at this point in the history
* fix(github_repository_environment): URL encode environment name

* test(github_repository_environment): Adjust test for URL encoded names

Co-authored-by: Keegan Campbell <[email protected]>
  • Loading branch information
Sebastian Olsson and kfcampbell authored Nov 30, 2022
1 parent c26ab0e commit eefa7ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions github/resource_github_repository_environment.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/v48/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -81,11 +82,12 @@ func resourceGithubRepositoryEnvironmentCreate(d *schema.ResourceData, meta inte
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.QueryEscape(envName)
updateData := createUpdateEnvironmentData(d, meta)

ctx := context.Background()

_, _, err := client.Repositories.CreateUpdateEnvironment(ctx, owner, repoName, envName, &updateData)
_, _, err := client.Repositories.CreateUpdateEnvironment(ctx, owner, repoName, escapedEnvName, &updateData)

if err != nil {
return err
Expand All @@ -101,13 +103,14 @@ func resourceGithubRepositoryEnvironmentRead(d *schema.ResourceData, meta interf

owner := meta.(*Owner).name
repoName, envName, err := parseTwoPartID(d.Id(), "repository", "environment")
escapedEnvName := url.QueryEscape(envName)
if err != nil {
return err
}

ctx := context.WithValue(context.Background(), ctxId, d.Id())

env, _, err := client.Repositories.GetEnvironment(ctx, owner, repoName, envName)
env, _, err := client.Repositories.GetEnvironment(ctx, owner, repoName, escapedEnvName)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotFound {
Expand Down Expand Up @@ -170,11 +173,12 @@ func resourceGithubRepositoryEnvironmentUpdate(d *schema.ResourceData, meta inte
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.QueryEscape(envName)
updateData := createUpdateEnvironmentData(d, meta)

ctx := context.Background()

resultKey, _, err := client.Repositories.CreateUpdateEnvironment(ctx, owner, repoName, envName, &updateData)
resultKey, _, err := client.Repositories.CreateUpdateEnvironment(ctx, owner, repoName, escapedEnvName, &updateData)
if err != nil {
return err
}
Expand All @@ -189,13 +193,14 @@ func resourceGithubRepositoryEnvironmentDelete(d *schema.ResourceData, meta inte

owner := meta.(*Owner).name
repoName, envName, err := parseTwoPartID(d.Id(), "repository", "environment")
escapedEnvName := url.QueryEscape(envName)
if err != nil {
return err
}

ctx := context.WithValue(context.Background(), ctxId, d.Id())

_, err = client.Repositories.DeleteEnvironment(ctx, owner, repoName, envName)
_, err = client.Repositories.DeleteEnvironment(ctx, owner, repoName, escapedEnvName)
return err
}

Expand Down
4 changes: 2 additions & 2 deletions github/resource_github_repository_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAccGithubRepositoryEnvironment(t *testing.T) {
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "test_environment_name"
environment = "environment/test"
wait_timer = 10000
reviewers {
users = [data.github_user.current.id]
Expand All @@ -42,7 +42,7 @@ func TestAccGithubRepositoryEnvironment(t *testing.T) {
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository_environment.test", "environment",
"test_environment_name",
"environment/test",
),
)

Expand Down

0 comments on commit eefa7ca

Please sign in to comment.