Skip to content
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

[Webhook] Support operation in webhook mapping #206

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions internal/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ type (
Mappings struct {
Blueprint string `json:"blueprint,omitempty"`
Filter *string `json:"filter,omitempty"`
Operation *string `json:"operation,omitempty"`
ItemsToParse *string `json:"itemsToParse,omitempty"`
Entity *EntityProperty `json:"entity,omitempty"`
}
Expand Down
1 change: 1 addition & 0 deletions port/webhook/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type EntityModel struct {
type MappingsModel struct {
Blueprint types.String `tfsdk:"blueprint"`
Filter types.String `tfsdk:"filter"`
Operation types.String `tfsdk:"operation"`
ItemsToParse types.String `tfsdk:"items_to_parse"`
Entity *EntityModel `tfsdk:"entity"`
}
Expand Down
1 change: 1 addition & 0 deletions port/webhook/refreshWebhookState.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func refreshWebhookState(ctx context.Context, state *WebhookModel, w *cli.Webhoo
}

mapping.Filter = flex.GoStringToFramework(v.Filter)
mapping.Operation = flex.GoStringToFramework(v.Operation)
mapping.ItemsToParse = flex.GoStringToFramework(v.ItemsToParse)
mapping.Entity.Icon = flex.GoStringToFramework(v.Entity.Icon)
mapping.Entity.Title = flex.GoStringToFramework(v.Entity.Title)
Expand Down
125 changes: 125 additions & 0 deletions port/webhook/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,131 @@ func TestAccPortWebhook(t *testing.T) {
})
}

func TestAccPortWebhookWithOperation(t *testing.T) {
identifier := utils.GenID()
webhookIdentifier := utils.GenID()
var testAccActionConfigCreate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(`
resource "port_webhook" "create_pr" {
identifier = "%s"
title = "Test"
icon = "Terraform"
enabled = true
mappings = [
{
"blueprint" = port_blueprint.microservice.identifier,
"filter" = ".headers.\"X-GitHub-Event\" == \"pull_request\" && .body.pull_request.action == \"opened\"",
"items_to_parse" = ".body.pull_request",
"operation" = "create",
"entity" = {
"identifier" = ".body.pull_request.id | tostring",
"title" = ".body.pull_request.title",
"icon" = "\"Terraform\"",
"team" = "\"port\"",
"properties" = {
"author" = ".body.pull_request.user.login",
"url" = ".body.pull_request.html_url"
}
}
},
{
"blueprint" = port_blueprint.microservice.identifier,
"filter" = ".headers.\"X-GitHub-Event\" == \"pull_request\" && .body.pull_request.state == \"closed\"",
"items_to_parse" = ".body.pull_request",
"operation": "delete",
"entity" = {
"identifier" = ".body.pull_request.id | tostring",
"title" = ".body.pull_request.title",
"icon" = "\"Terraform\"",
"team" = "\"port\"",
"properties" = {
"author" = ".body.pull_request.user.login",
"url" = ".body.pull_request.html_url"
}
}
},
{
"blueprint" = port_blueprint.microservice.identifier,
"filter" = ".headers.\"X-GitHub-Event\" == \"pull_request\" && .body.pull_request.state == \"edited\"",
"items_to_parse" = ".body.pull_request",
"entity" = {
"identifier" = ".body.pull_request.id | tostring",
"title" = ".body.pull_request.title",
"icon" = "\"Terraform\"",
"team" = "\"port\"",
"properties" = {
"author" = ".body.pull_request.user.login",
"url" = ".body.pull_request.html_url"
}
}
}
]
}`, webhookIdentifier)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: acctest.ProviderConfig + testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port_webhook.create_pr", "identifier", webhookIdentifier),
resource.TestCheckResourceAttr("port_webhook.create_pr", "title", "Test"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "icon", "Terraform"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "enabled", "true"),
resource.TestCheckResourceAttrWith("port_webhook.create_pr", "url",
func(value string) error {
if value == "" {
return fmt.Errorf("value is empty")
}
return nil
}),
resource.TestCheckResourceAttrWith("port_webhook.create_pr", "webhook_key",
func(value string) error {
if value == "" {
return fmt.Errorf("value is empty")
}
return nil
}),
resource.TestCheckResourceAttr("port_webhook.create_pr", "security.signature_header_name", "X-Hub-Signature-256"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "security.signature_algorithm", "sha256"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "security.signature_prefix", "sha256="),
resource.TestCheckResourceAttr("port_webhook.create_pr", "security.request_identifier_path", ".body.repository.full_name"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.blueprint", identifier),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.filter", ".headers.\"X-GitHub-Event\" == \"pull_request\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.operation", "create"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😸

resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.items_to_parse", ".body.pull_request"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.entity.identifier", ".body.pull_request.id | tostring"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.entity.title", ".body.pull_request.title"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.entity.icon", "\"Terraform\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.entity.team", "\"port\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.entity.properties.author", ".body.pull_request.user.login"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.0.entity.properties.url", ".body.pull_request.html_url"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.blueprint", identifier),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.filter", ".headers.\"X-GitHub-Event\" == \"pull_request\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.operation", "delete"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.items_to_parse", ".body.pull_request"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.entity.identifier", ".body.pull_request.id | tostring"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.entity.title", ".body.pull_request.title"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.entity.icon", "\"Terraform\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.entity.team", "\"port\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.entity.properties.author", ".body.pull_request.user.login"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.1.entity.properties.url", ".body.pull_request.html_url"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.blueprint", identifier),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.filter", ".headers.\"X-GitHub-Event\" == \"pull_request\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.operation", ""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.items_to_parse", ".body.pull_request"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.entity.identifier", ".body.pull_request.id | tostring"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.entity.title", ".body.pull_request.title"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.entity.icon", "\"Terraform\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.entity.team", "\"port\""),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.entity.properties.author", ".body.pull_request.user.login"),
resource.TestCheckResourceAttr("port_webhook.create_pr", "mappings.2.entity.properties.url", ".body.pull_request.html_url"),
),
},
},
})
}

func TestAccPortWebhookImport(t *testing.T) {
identifier := utils.GenID()
webhookIdentifier := utils.GenID()
Expand Down
4 changes: 4 additions & 0 deletions port/webhook/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func WebhookMappingSchema() map[string]schema.Attribute {
MarkdownDescription: "The items to parser of the mapping",
Optional: true,
},
"operation": schema.StringAttribute{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MarkdownDescription: "The operation of the mapping",
Optional: true,
},
"entity": schema.SingleNestedAttribute{
MarkdownDescription: "The entity of the mapping",
Required: true,
Expand Down
4 changes: 4 additions & 0 deletions port/webhook/webhookResourceToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func webhookResourceToPortBody(ctx context.Context, state *WebhookModel) (*cli.W
filter := v.Filter.ValueString()
mapping.Filter = &filter
}
if !v.Operation.IsNull() {
operation := v.Operation.ValueString()
mapping.Operation = &operation
}

if !v.ItemsToParse.IsNull() {
ItemsToParse := v.ItemsToParse.ValueString()
Expand Down
Loading