Skip to content

feat: Add manual_creation argument to aws_codebuild_webhook resource #40155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/40155.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_codebuild_webhook: Add `manual_creation` argument
```
18 changes: 14 additions & 4 deletions internal/service/codebuild/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func resourceWebhook() *schema.Resource {
},
ConflictsWith: []string{"branch_filter"},
},
"manual_creation": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"payload_url": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -123,7 +128,7 @@ func resourceWebhookCreate(ctx context.Context, d *schema.ResourceData, meta any
conn := meta.(*conns.AWSClient).CodeBuildClient(ctx)

projectName := d.Get("project_name").(string)
input := &codebuild.CreateWebhookInput{
input := codebuild.CreateWebhookInput{
ProjectName: aws.String(projectName),
}

Expand All @@ -139,11 +144,15 @@ func resourceWebhookCreate(ctx context.Context, d *schema.ResourceData, meta any
input.FilterGroups = expandWebhookFilterGroups(v.(*schema.Set).List())
}

if v, ok := d.GetOk("manual_creation"); ok {
input.ManualCreation = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("scope_configuration"); ok && len(v.([]any)) > 0 {
input.ScopeConfiguration = expandScopeConfiguration(v.([]any))
}

output, err := conn.CreateWebhook(ctx, input)
output, err := conn.CreateWebhook(ctx, &input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating CodeBuild Webhook (%s): %s", projectName, err)
Expand Down Expand Up @@ -177,6 +186,7 @@ func resourceWebhookRead(ctx context.Context, d *schema.ResourceData, meta any)
if err := d.Set("filter_group", flattenWebhookFilterGroups(webhook.FilterGroups)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting filter_group: %s", err)
}
d.Set("manual_creation", d.Get("manual_creation")) // Create-only.
d.Set("payload_url", webhook.PayloadUrl)
d.Set("project_name", d.Id())
if err := d.Set("scope_configuration", flattenScopeConfiguration(webhook.ScopeConfiguration)); err != nil {
Expand All @@ -192,7 +202,7 @@ func resourceWebhookUpdate(ctx context.Context, d *schema.ResourceData, meta any
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).CodeBuildClient(ctx)

input := &codebuild.UpdateWebhookInput{
input := codebuild.UpdateWebhookInput{
ProjectName: aws.String(d.Id()),
}

Expand All @@ -210,7 +220,7 @@ func resourceWebhookUpdate(ctx context.Context, d *schema.ResourceData, meta any
input.BranchFilter = aws.String(d.Get("branch_filter").(string))
}

_, err := conn.UpdateWebhook(ctx, input)
_, err := conn.UpdateWebhook(ctx, &input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating CodeBuild Webhook (%s): %s", d.Id(), err)
Expand Down
95 changes: 95 additions & 0 deletions internal/service/codebuild/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -46,6 +47,7 @@ func TestAccCodeBuildWebhook_bitbucket(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckWebhookExists(ctx, resourceName, &webhook),
resource.TestCheckResourceAttr(resourceName, "branch_filter", ""),
resource.TestCheckResourceAttr(resourceName, "manual_creation", acctest.CtFalse),
resource.TestCheckResourceAttr(resourceName, "project_name", rName),
resource.TestMatchResourceAttr(resourceName, "payload_url", regexache.MustCompile(`^https://`)),
resource.TestCheckResourceAttr(resourceName, "scope_configuration.#", "0"),
Expand Down Expand Up @@ -84,6 +86,7 @@ func TestAccCodeBuildWebhook_gitHub(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckWebhookExists(ctx, resourceName, &webhook),
resource.TestCheckResourceAttr(resourceName, "branch_filter", ""),
resource.TestCheckResourceAttr(resourceName, "manual_creation", acctest.CtFalse),
resource.TestCheckResourceAttr(resourceName, "project_name", rName),
resource.TestMatchResourceAttr(resourceName, "payload_url", regexache.MustCompile(`^https://`)),
resource.TestCheckResourceAttr(resourceName, "scope_configuration.#", "0"),
Expand Down Expand Up @@ -122,6 +125,7 @@ func TestAccCodeBuildWebhook_gitHubEnterprise(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckWebhookExists(ctx, resourceName, &webhook),
resource.TestCheckResourceAttr(resourceName, "branch_filter", "dev"),
resource.TestCheckResourceAttr(resourceName, "manual_creation", acctest.CtFalse),
resource.TestCheckResourceAttr(resourceName, "project_name", rName),
resource.TestMatchResourceAttr(resourceName, "payload_url", regexache.MustCompile(`^https://`)),
resource.TestCheckResourceAttr(resourceName, "scope_configuration.#", "0"),
Expand Down Expand Up @@ -389,6 +393,88 @@ func TestAccCodeBuildWebhook_Disappears_project(t *testing.T) {
})
}

func TestAccCodeBuildWebhook_manualCreation(t *testing.T) {
ctx := acctest.Context(t)
var webhook types.Webhook
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_codebuild_webhook.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheck(ctx, t)
testAccPreCheckSourceCredentialsForServerType(ctx, t, types.ServerTypeGithub)
},
ErrorCheck: acctest.ErrorCheck(t, names.CodeBuildServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckWebhookDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccWebhookConfig_manualCreation(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckWebhookExists(ctx, resourceName, &webhook),
resource.TestCheckResourceAttr(resourceName, "manual_creation", acctest.CtTrue),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"manual_creation", "secret"},
},
},
})
}

func TestAccCodeBuildWebhook_upgradeV5_94_1(t *testing.T) {
ctx := acctest.Context(t)
var webhook types.Webhook
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_codebuild_webhook.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheck(ctx, t)
testAccPreCheckSourceCredentialsForServerType(ctx, t, types.ServerTypeGithub)
},
ErrorCheck: acctest.ErrorCheck(t, names.CodeBuildServiceID),
CheckDestroy: testAccCheckWebhookDestroy(ctx),
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
"aws": {
Source: "hashicorp/aws",
VersionConstraint: "5.94.1",
},
},
Config: testAccWebhookConfig_gitHub(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckWebhookExists(ctx, resourceName, &webhook),
),
},
{
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Config: testAccWebhookConfig_gitHub(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckWebhookExists(ctx, resourceName, &webhook),
),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
PostApplyPreRefresh: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
PostApplyPostRefresh: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
},
})
}

func testAccCheckWebhookFilter(webhook *types.Webhook, expectedFilters [][]types.WebhookFilter) resource.TestCheckFunc {
return func(s *terraform.State) error {
got, want := webhook.FilterGroups, expectedFilters
Expand Down Expand Up @@ -569,3 +655,12 @@ resource "aws_codebuild_webhook" "test" {
}
`, rName))
}

func testAccWebhookConfig_manualCreation(rName string) string {
return acctest.ConfigCompose(testAccProjectConfig_basic(rName), `
resource "aws_codebuild_webhook" "test" {
project_name = aws_codebuild_project.test.name
manual_creation = true
}
`)
}
1 change: 1 addition & 0 deletions website/docs/r/codebuild_webhook.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ This resource supports the following arguments:

* `project_name` - (Required) The name of the build project.
* `build_type` - (Optional) The type of build this webhook will trigger. Valid values for this parameter are: `BUILD`, `BUILD_BATCH`.
* `manual_creation` - (Optional) If true, CodeBuild doesn't create a webhook in GitHub and instead returns `payload_url` and `secret` values for the webhook. The `payload_url` and `secret` values in the output can be used to manually create a webhook within GitHub.
* `branch_filter` - (Optional) A regular expression used to determine which branches get built. Default is all branches are built. We recommend using `filter_group` over `branch_filter`.
* `filter_group` - (Optional) Information about the webhook's trigger. Filter group blocks are documented below.
* `scope_configuration` - (Optional) Scope configuration for global or organization webhooks. Scope configuration blocks are documented below.
Expand Down
Loading