From 9f9eeef3ded7857e4ee7b46f4798e98b677af473 Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Tue, 4 Jun 2024 14:51:28 -0700 Subject: [PATCH 1/3] Add include_patterns to validation --- .../resource_artifactory_webhook_build.go | 8 ++-- .../resource_artifactory_webhook_test.go | 43 ++++++++++++++++++- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/pkg/artifactory/resource/webhook/resource_artifactory_webhook_build.go b/pkg/artifactory/resource/webhook/resource_artifactory_webhook_build.go index 6ac1f4172..542235d89 100644 --- a/pkg/artifactory/resource/webhook/resource_artifactory_webhook_build.go +++ b/pkg/artifactory/resource/webhook/resource_artifactory_webhook_build.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" utilsdk "github.com/jfrog/terraform-provider-shared/util/sdk" ) @@ -57,13 +56,12 @@ var unpackBuildCriteria = func(terraformCriteria map[string]interface{}, baseCri } var buildCriteriaValidation = func(ctx context.Context, criteria map[string]interface{}) error { - tflog.Debug(ctx, "buildCriteriaValidation") - anyBuild := criteria["any_build"].(bool) selectedBuilds := criteria["selected_builds"].(*schema.Set).List() + includePatterns := criteria["include_patterns"].(*schema.Set).List() - if !anyBuild && len(selectedBuilds) == 0 { - return fmt.Errorf("selected_builds cannot be empty when any_build is false") + if !anyBuild && (len(selectedBuilds) == 0 && len(includePatterns) == 0) { + return fmt.Errorf("selected_builds or include_patterns cannot be empty when any_build is false") } return nil diff --git a/pkg/artifactory/resource/webhook/resource_artifactory_webhook_test.go b/pkg/artifactory/resource/webhook/resource_artifactory_webhook_test.go index b62a132a3..1ea616450 100644 --- a/pkg/artifactory/resource/webhook/resource_artifactory_webhook_test.go +++ b/pkg/artifactory/resource/webhook/resource_artifactory_webhook_test.go @@ -27,7 +27,7 @@ var domainValidationErrorMessageLookup = map[string]string{ "artifact": "repo_keys cannot be empty when any_local, any_remote, and any_federated are false", "artifact_property": "repo_keys cannot be empty when any_local, any_remote, and any_federated are false", "docker": "repo_keys cannot be empty when any_local, any_remote, and any_federated are false", - "build": "selected_builds cannot be empty when any_build is false", + "build": "selected_builds or include_patterns cannot be empty when any_build is false", "release_bundle": "registered_release_bundle_names cannot be empty when any_release_bundle is false", "distribution": "registered_release_bundle_names cannot be empty when any_release_bundle is false", "artifactory_release_bundle": "registered_release_bundle_names cannot be empty when any_release_bundle is false", @@ -246,6 +246,47 @@ func TestAccWebhook_HandlerValidation_ProxyWithURL(t *testing.T) { }) } +func TestAccWebhook_BuildWithIncludePatterns(t *testing.T) { + id := testutil.RandomInt() + name := fmt.Sprintf("webhook-%d", id) + fqrn := fmt.Sprintf("artifactory_build_webhook.%s", name) + + params := map[string]interface{}{ + "webhookName": name, + } + webhookConfig := util.ExecuteTemplate("TestAccWebhookBuildPatterns", ` + resource "artifactory_build_webhook" "{{ .webhookName }}" { + key = "{{ .webhookName }}" + description = "test description" + event_types = ["uploaded"] + criteria { + any_build = false + selected_builds = [] + include_patterns = ["foo"] + } + handler { + url = "https://tempurl.org" + } + } + `, params) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ProviderFactories: acctest.ProviderFactories, + CheckDestroy: acctest.VerifyDeleted(fqrn, acctest.CheckRepo), + + Steps: []resource.TestStep{ + { + Config: webhookConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(fqrn, "criteria.0.include_patterns.#", "1"), + resource.TestCheckResourceAttr(fqrn, "criteria.0.include_patterns.0", "foo"), + ), + }, + }, + }) +} + func TestAccWebhook_AllTypes(t *testing.T) { // Can only realistically test these 3 types of webhook since creating // build, release_bundle, or distribution in test environment is almost impossible From a4929d4dbeee9733558535cec7bb2dc14a42eb43 Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Tue, 4 Jun 2024 15:03:09 -0700 Subject: [PATCH 2/3] Add test for build custom webhook --- .../resource_artifactory_webhook_test.go | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pkg/artifactory/resource/webhook/resource_artifactory_webhook_test.go b/pkg/artifactory/resource/webhook/resource_artifactory_webhook_test.go index 1ea616450..0d71a83be 100644 --- a/pkg/artifactory/resource/webhook/resource_artifactory_webhook_test.go +++ b/pkg/artifactory/resource/webhook/resource_artifactory_webhook_test.go @@ -599,6 +599,48 @@ func customWebhookTestCase(webhookType string, t *testing.T) (*testing.T, resour } } +func TestAccCustomWebhook_BuildWithIncludePatterns(t *testing.T) { + id := testutil.RandomInt() + name := fmt.Sprintf("webhook-%d", id) + fqrn := fmt.Sprintf("artifactory_build_custom_webhook.%s", name) + + params := map[string]interface{}{ + "webhookName": name, + } + webhookConfig := util.ExecuteTemplate("TestAccCustomWebhookBuildPatterns", ` + resource "artifactory_build_custom_webhook" "{{ .webhookName }}" { + key = "{{ .webhookName }}" + description = "test description" + event_types = ["uploaded"] + criteria { + any_build = false + selected_builds = [] + include_patterns = ["foo"] + } + handler { + url = "https://tempurl.org" + payload = "{ \"ref\": \"main\" , \"inputs\": { \"artifact_path\": \"test-repo/repo-path\" } }" + } + } + `, params) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ProviderFactories: acctest.ProviderFactories, + CheckDestroy: acctest.VerifyDeleted(fqrn, acctest.CheckRepo), + + Steps: []resource.TestStep{ + { + Config: webhookConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(fqrn, "criteria.0.include_patterns.#", "1"), + resource.TestCheckResourceAttr(fqrn, "criteria.0.include_patterns.0", "foo"), + ), + }, + }, + }) +} + func testCheckWebhook(id string, request *resty.Request) (*resty.Response, error) { return request. SetPathParam("webhookKey", id). From c3836799134a237a5fd8d36935e942c07a7b8ff5 Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Tue, 4 Jun 2024 15:04:30 -0700 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3df1d055e..c08902033 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 10.8.4 (June 5, 2024) + +BUG FIXES: + +* resource/artifactory_build_webhook, resource/artifactory_custom_build_webhook: Fix criteria validation to allow `include_patterns` attribute values with `any_build` attribute set to `false`. Issue: [#987](https://github.com/jfrog/terraform-provider-artifactory/issues/987) PR: [#993](https://github.com/jfrog/terraform-provider-artifactory/pull/993) + ## 10.8.3 (June 3, 2024) BUG FIXES: