Skip to content

Commit

Permalink
Merge pull request #993 from jfrog/GH-987-fix-build-webhook-validation
Browse files Browse the repository at this point in the history
Fx build webhook validation
  • Loading branch information
alexhung authored Jun 5, 2024
2 parents 6c75daa + f64f7e0 commit 3fbfde6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -558,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).
Expand Down

0 comments on commit 3fbfde6

Please sign in to comment.