From 392b14fdba4039a67e5473165480dea483550ef6 Mon Sep 17 00:00:00 2001 From: Sebastian Olsson Date: Wed, 30 Nov 2022 23:45:40 +0100 Subject: [PATCH] URL encode environment name in `github_repository_environment` (#1392) * fix(github_repository_environment): URL encode environment name * test(github_repository_environment): Adjust test for URL encoded names Co-authored-by: Keegan Campbell --- github/resource_github_repository_environment.go | 13 +++++++++---- .../resource_github_repository_environment_test.go | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/github/resource_github_repository_environment.go b/github/resource_github_repository_environment.go index 07ce7f3bb5..ca7f6e5bc5 100644 --- a/github/resource_github_repository_environment.go +++ b/github/resource_github_repository_environment.go @@ -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" @@ -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 @@ -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 { @@ -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 } @@ -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 } diff --git a/github/resource_github_repository_environment_test.go b/github/resource_github_repository_environment_test.go index 38d57a1246..69f5e3ed93 100644 --- a/github/resource_github_repository_environment_test.go +++ b/github/resource_github_repository_environment_test.go @@ -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] @@ -42,7 +42,7 @@ func TestAccGithubRepositoryEnvironment(t *testing.T) { check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( "github_repository_environment.test", "environment", - "test_environment_name", + "environment/test", ), )