From c36d51f09fc28127c3d10015e24103efbb98e09a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C4=9Fan=20=C3=9Cst=C3=BCngel?= Date: Tue, 24 May 2022 02:52:33 +0300 Subject: [PATCH 1/5] added WebhookURL attribute to VCSRepo struct --- workspace.go | 1 + 1 file changed, 1 insertion(+) diff --git a/workspace.go b/workspace.go index 77983afed..cd21eb457 100644 --- a/workspace.go +++ b/workspace.go @@ -182,6 +182,7 @@ type VCSRepo struct { OAuthTokenID string `jsonapi:"attr,oauth-token-id"` RepositoryHTTPURL string `jsonapi:"attr,repository-http-url"` ServiceProvider string `jsonapi:"attr,service-provider"` + WebhookURL string `jsonapi:"attr,webhook-url"` } // WorkspaceActions represents the workspace actions. From 36b26f45daab0c267cd912aa5728741c7a9a29f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C4=9Fan=20=C3=9Cst=C3=BCngel?= Date: Tue, 24 May 2022 02:53:58 +0300 Subject: [PATCH 2/5] added WebhookURL to Unmarshal tests --- registry_module_integration_test.go | 2 ++ workspace_integration_test.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/registry_module_integration_test.go b/registry_module_integration_test.go index 58cbaee25..84dfa49a7 100644 --- a/registry_module_integration_test.go +++ b/registry_module_integration_test.go @@ -746,6 +746,7 @@ func TestRegistryModule_Unmarshal(t *testing.T) { "oauth-token-id": "token", "repository-http-url": "github.com", "service-provider": "github", + "webhook-url": "https://app.terraform.io/webhooks/vcs/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", }, "version-statuses": []interface{}{ map[string]interface{}{ @@ -779,6 +780,7 @@ func TestRegistryModule_Unmarshal(t *testing.T) { assert.Equal(t, rm.VCSRepo.OAuthTokenID, "token") assert.Equal(t, rm.VCSRepo.RepositoryHTTPURL, "github.com") assert.Equal(t, rm.VCSRepo.ServiceProvider, "github") + assert.Equal(t, rm.VCSRepo.WebhookURL, "https://app.terraform.io/webhooks/vcs/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee") assert.Equal(t, rm.Status, RegistryModuleStatusPending) assert.Equal(t, rm.VersionStatuses[0].Version, "1.1.1") assert.Equal(t, rm.VersionStatuses[0].Status, RegistryModuleVersionStatusPending) diff --git a/workspace_integration_test.go b/workspace_integration_test.go index b63ff3101..03f64a504 100644 --- a/workspace_integration_test.go +++ b/workspace_integration_test.go @@ -1446,6 +1446,7 @@ func TestWorkspace_Unmarshal(t *testing.T) { "oauth-token-id": "token", "repository-http-url": "github.com", "service-provider": "github", + "webhook-url": "https://app.terraform.io/webhooks/vcs/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", }, "actions": map[string]interface{}{ "is-destroyable": true, @@ -1481,6 +1482,7 @@ func TestWorkspace_Unmarshal(t *testing.T) { assert.Equal(t, ws.VCSRepo.OAuthTokenID, "token") assert.Equal(t, ws.VCSRepo.RepositoryHTTPURL, "github.com") assert.Equal(t, ws.VCSRepo.ServiceProvider, "github") + assert.Equal(t, ws.VCSRepo.WebhookURL, "https://app.terraform.io/webhooks/vcs/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee") assert.Equal(t, ws.Actions.IsDestroyable, true) assert.Equal(t, ws.TriggerPrefixes, []string{"prefix-"}) assert.Equal(t, ws.TriggerPatterns, []string{"pattern1/**/*", "pattern2/**/submodule/*"}) From 10e6b1171b943f85ef316350edb90e00a9d3d6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C4=9Fan=20=C3=9Cst=C3=BCngel?= Date: Tue, 24 May 2022 02:58:11 +0300 Subject: [PATCH 3/5] since we cannot know what webhook url will be in advance, had to break VCSRepo assert to multiple attribute asserts --- policy_set_integration_test.go | 34 ++++++++++++++--------------- registry_module_integration_test.go | 17 +++++++-------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/policy_set_integration_test.go b/policy_set_integration_test.go index 340c1c8ae..8989be58d 100644 --- a/policy_set_integration_test.go +++ b/policy_set_integration_test.go @@ -165,15 +165,14 @@ func TestPolicySetsCreate(t *testing.T) { assert.Equal(t, ps.Description, "") assert.False(t, ps.Global) assert.Equal(t, ps.PoliciesPath, "/policy-sets/foo") - assert.Equal(t, ps.VCSRepo, &VCSRepo{ - Branch: "policies", - DisplayIdentifier: githubIdentifier, - Identifier: githubIdentifier, - OAuthTokenID: oc.ID, - IngressSubmodules: true, - RepositoryHTTPURL: fmt.Sprintf("https://github.com/%s", githubIdentifier), - ServiceProvider: string(ServiceProviderGithub), - }) + assert.Equal(t, ps.VCSRepo.Branch, "policies") + assert.Equal(t, ps.VCSRepo.DisplayIdentifier, githubIdentifier) + assert.Equal(t, ps.VCSRepo.Identifier, githubIdentifier) + assert.Equal(t, ps.VCSRepo.IngressSubmodules, true) + assert.Equal(t, ps.VCSRepo.OAuthTokenID, oc.ID) + assert.Equal(t, ps.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) + assert.Equal(t, ps.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) + assert.Regexp(t, "^https://app\.terraform\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", ps.VCSRepo.WebhookURL) }) t.Run("with vcs policy updated", func(t *testing.T) { @@ -203,15 +202,14 @@ func TestPolicySetsCreate(t *testing.T) { assert.Equal(t, ps.Description, "") assert.False(t, ps.Global) assert.Equal(t, ps.PoliciesPath, "/policy-sets/bar") - assert.Equal(t, ps.VCSRepo, &VCSRepo{ - Branch: "policies", - DisplayIdentifier: githubIdentifier, - Identifier: githubIdentifier, - OAuthTokenID: oc.ID, - IngressSubmodules: false, - RepositoryHTTPURL: fmt.Sprintf("https://github.com/%s", githubIdentifier), - ServiceProvider: string(ServiceProviderGithub), - }) + assert.Equal(t, ps.VCSRepo.Branch, "policies") + assert.Equal(t, ps.VCSRepo.DisplayIdentifier, githubIdentifier) + assert.Equal(t, ps.VCSRepo.Identifier, githubIdentifier) + assert.Equal(t, ps.VCSRepo.IngressSubmodules, false) + assert.Equal(t, ps.VCSRepo.OAuthTokenID, oc.ID) + assert.Equal(t, ps.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) + assert.Equal(t, ps.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) + assert.Regexp(t, "^https://app\.terraform\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", ps.VCSRepo.WebhookURL) }) t.Run("without a name provided", func(t *testing.T) { diff --git a/registry_module_integration_test.go b/registry_module_integration_test.go index 84dfa49a7..7975e45b5 100644 --- a/registry_module_integration_test.go +++ b/registry_module_integration_test.go @@ -265,15 +265,14 @@ func TestRegistryModulesCreateWithVCSConnection(t *testing.T) { assert.NotEmpty(t, rm.ID) assert.Equal(t, registryModuleName, rm.Name) assert.Equal(t, registryModuleProvider, rm.Provider) - assert.Equal(t, &VCSRepo{ - Branch: "", - Identifier: githubIdentifier, - OAuthTokenID: oauthTokenTest.ID, - DisplayIdentifier: githubIdentifier, - IngressSubmodules: true, - RepositoryHTTPURL: fmt.Sprintf("https://github.com/%s", githubIdentifier), - ServiceProvider: string(ServiceProviderGithub), - }, rm.VCSRepo) + assert.Equal(t, rm.VCSRepo.Branch, "") + assert.Equal(t, rm.VCSRepo.DisplayIdentifier, githubIdentifier) + assert.Equal(t, rm.VCSRepo.Identifier, githubIdentifier) + assert.Equal(t, rm.VCSRepo.IngressSubmodules, true) + assert.Equal(t, rm.VCSRepo.OAuthTokenID, oauthTokenTest.ID) + assert.Equal(t, rm.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) + assert.Equal(t, rm.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) + assert.Regexp(t, "^https://app\.terraform\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", rm.VCSRepo.WebhookURL) t.Run("permissions are properly decoded", func(t *testing.T) { assert.True(t, rm.Permissions.CanDelete) From c5a87bd12809d8b32f643cef82b9b816f3a0ac34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C4=9Fan=20=C3=9Cst=C3=BCngel?= Date: Tue, 24 May 2022 04:03:35 +0300 Subject: [PATCH 4/5] regex typo --- policy_set_integration_test.go | 4 ++-- registry_module_integration_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/policy_set_integration_test.go b/policy_set_integration_test.go index 8989be58d..e49ccf7a0 100644 --- a/policy_set_integration_test.go +++ b/policy_set_integration_test.go @@ -172,7 +172,7 @@ func TestPolicySetsCreate(t *testing.T) { assert.Equal(t, ps.VCSRepo.OAuthTokenID, oc.ID) assert.Equal(t, ps.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) assert.Equal(t, ps.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) - assert.Regexp(t, "^https://app\.terraform\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", ps.VCSRepo.WebhookURL) + assert.Regexp(t, "^https://app\\.terraform\\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", ps.VCSRepo.WebhookURL) }) t.Run("with vcs policy updated", func(t *testing.T) { @@ -209,7 +209,7 @@ func TestPolicySetsCreate(t *testing.T) { assert.Equal(t, ps.VCSRepo.OAuthTokenID, oc.ID) assert.Equal(t, ps.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) assert.Equal(t, ps.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) - assert.Regexp(t, "^https://app\.terraform\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", ps.VCSRepo.WebhookURL) + assert.Regexp(t, "^https://app\\.terraform\\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", ps.VCSRepo.WebhookURL) }) t.Run("without a name provided", func(t *testing.T) { diff --git a/registry_module_integration_test.go b/registry_module_integration_test.go index 7975e45b5..a6bcefd2f 100644 --- a/registry_module_integration_test.go +++ b/registry_module_integration_test.go @@ -272,7 +272,7 @@ func TestRegistryModulesCreateWithVCSConnection(t *testing.T) { assert.Equal(t, rm.VCSRepo.OAuthTokenID, oauthTokenTest.ID) assert.Equal(t, rm.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) assert.Equal(t, rm.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) - assert.Regexp(t, "^https://app\.terraform\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", rm.VCSRepo.WebhookURL) + assert.Regexp(t, "^https://app\\.terraform\\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", rm.VCSRepo.WebhookURL) t.Run("permissions are properly decoded", func(t *testing.T) { assert.True(t, rm.Permissions.CanDelete) From 4b777cf2f356481a8e5cfb0864bc3b59e2d3539d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C4=9Fan=20=C3=9Cst=C3=BCngel?= Date: Thu, 26 May 2022 01:57:40 +0300 Subject: [PATCH 5/5] replaced hostname in webhook url regex to address field of DefaultConfig() --- policy_set_integration_test.go | 5 +++-- registry_module_integration_test.go | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/policy_set_integration_test.go b/policy_set_integration_test.go index e49ccf7a0..2cd02cd56 100644 --- a/policy_set_integration_test.go +++ b/policy_set_integration_test.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "os" + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -172,7 +173,7 @@ func TestPolicySetsCreate(t *testing.T) { assert.Equal(t, ps.VCSRepo.OAuthTokenID, oc.ID) assert.Equal(t, ps.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) assert.Equal(t, ps.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) - assert.Regexp(t, "^https://app\\.terraform\\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", ps.VCSRepo.WebhookURL) + assert.Regexp(t, fmt.Sprintf("^%s/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", regexp.QuoteMeta(DefaultConfig().Address)), ps.VCSRepo.WebhookURL) }) t.Run("with vcs policy updated", func(t *testing.T) { @@ -209,7 +210,7 @@ func TestPolicySetsCreate(t *testing.T) { assert.Equal(t, ps.VCSRepo.OAuthTokenID, oc.ID) assert.Equal(t, ps.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) assert.Equal(t, ps.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) - assert.Regexp(t, "^https://app\\.terraform\\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", ps.VCSRepo.WebhookURL) + assert.Regexp(t, fmt.Sprintf("^%s/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", regexp.QuoteMeta(DefaultConfig().Address)), ps.VCSRepo.WebhookURL) }) t.Run("without a name provided", func(t *testing.T) { diff --git a/registry_module_integration_test.go b/registry_module_integration_test.go index a6bcefd2f..e6d7fdf8c 100644 --- a/registry_module_integration_test.go +++ b/registry_module_integration_test.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "os" + "regexp" "strings" "testing" @@ -272,7 +273,7 @@ func TestRegistryModulesCreateWithVCSConnection(t *testing.T) { assert.Equal(t, rm.VCSRepo.OAuthTokenID, oauthTokenTest.ID) assert.Equal(t, rm.VCSRepo.RepositoryHTTPURL, fmt.Sprintf("https://github.com/%s", githubIdentifier)) assert.Equal(t, rm.VCSRepo.ServiceProvider, string(ServiceProviderGithub)) - assert.Regexp(t, "^https://app\\.terraform\\.io/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", rm.VCSRepo.WebhookURL) + assert.Regexp(t, fmt.Sprintf("^%s/webhooks/vcs/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$", regexp.QuoteMeta(DefaultConfig().Address)), rm.VCSRepo.WebhookURL) t.Run("permissions are properly decoded", func(t *testing.T) { assert.True(t, rm.Permissions.CanDelete)