Skip to content

Commit

Permalink
v1.16.0 Adds new trigger conditions to pipeline (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
mical authored Sep 27, 2023
1 parent ae17479 commit 471f118
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Release v1.16.0 (2023-09-13)
* Adds new trigger conditions to pipeline

# Release v1.15.1 (2023-08-16)
* Adds timeout in api client

Expand Down
10 changes: 10 additions & 0 deletions buddy/resource/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ func (r *pipelineResource) Schema(_ context.Context, _ resource.SchemaRequest, r
buddy.PipelineTriggerConditionVarNotContains,
buddy.PipelineTriggerConditionDateTime,
buddy.PipelineTriggerConditionSuccessPipeline,
buddy.PipelineTriggerConditionTriggeringUserIsNotInGroup,
buddy.PipelineTriggerConditionTriggeringUserIsInGroup,
buddy.PipelineTriggerConditionTriggeringUserIs,
buddy.PipelineTriggerConditionTriggeringUserIsNot,
),
},
},
Expand Down Expand Up @@ -497,6 +501,12 @@ func (r *pipelineResource) Schema(_ context.Context, _ resource.SchemaRequest, r
"pipeline_name": schema.StringAttribute{
Optional: true,
},
"trigger_user": schema.StringAttribute{
Optional: true,
},
"trigger_group": schema.StringAttribute{
Optional: true,
},
},
},
},
Expand Down
102 changes: 98 additions & 4 deletions buddy/resource/test/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,10 @@ func TestAccPipeline_event(t *testing.T) {
newTcZoneId := "Europe/Amsterdam"
eventType := buddy.PipelineEventTypePush
newEventType := buddy.PipelineEventTypeCreateRef
user := util.RandEmail()
newUser := util.RandEmail()
group := util.RandString(10)
newGroup := util.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() {
acc.PreCheck(t)
Expand All @@ -821,7 +825,7 @@ func TestAccPipeline_event(t *testing.T) {
Steps: []resource.TestStep{
// create pipeline
{
Config: testAccPipelineConfigEvent(domain, projectName, name, eventType, ref, tcChangePath, tcVarKey, tcVarValue, tcHours, tcDays, tcZoneId),
Config: testAccPipelineConfigEvent(domain, projectName, name, eventType, ref, tcChangePath, tcVarKey, tcVarValue, tcHours, tcDays, tcZoneId, user, group),
Check: resource.ComposeTestCheckFunc(
testAccPipelineGet("buddy_pipeline.bar", &pipeline),
testAccProjectGet("buddy_project.proj", &project),
Expand Down Expand Up @@ -872,13 +876,29 @@ func TestAccPipeline_event(t *testing.T) {
TriggerDays: []int{tcDays},
ZoneId: tcZoneId,
},
{
TriggerCondition: buddy.PipelineTriggerConditionTriggeringUserIs,
TriggerUser: user,
},
{
TriggerCondition: buddy.PipelineTriggerConditionTriggeringUserIsNot,
TriggerUser: user,
},
{
TriggerCondition: buddy.PipelineTriggerConditionTriggeringUserIsInGroup,
TriggerGroup: group,
},
{
TriggerCondition: buddy.PipelineTriggerConditionTriggeringUserIsNotInGroup,
TriggerGroup: group,
},
},
}),
),
},
// update pipeline
{
Config: testAccPipelineConfigEvent(domain, projectName, newName, newEventType, newRef, newTcChangePath, newTcVarKey, newTcVarValue, newTcHours, newTcDays, newTcZoneId),
Config: testAccPipelineConfigEvent(domain, projectName, newName, newEventType, newRef, newTcChangePath, newTcVarKey, newTcVarValue, newTcHours, newTcDays, newTcZoneId, newUser, newGroup),
Check: resource.ComposeTestCheckFunc(
testAccPipelineGet("buddy_pipeline.bar", &pipeline),
testAccProjectGet("buddy_project.proj", &project),
Expand Down Expand Up @@ -929,6 +949,22 @@ func TestAccPipeline_event(t *testing.T) {
TriggerDays: []int{newTcDays},
ZoneId: newTcZoneId,
},
{
TriggerCondition: buddy.PipelineTriggerConditionTriggeringUserIs,
TriggerUser: newUser,
},
{
TriggerCondition: buddy.PipelineTriggerConditionTriggeringUserIsNot,
TriggerUser: newUser,
},
{
TriggerCondition: buddy.PipelineTriggerConditionTriggeringUserIsInGroup,
TriggerGroup: newGroup,
},
{
TriggerCondition: buddy.PipelineTriggerConditionTriggeringUserIsNotInGroup,
TriggerGroup: newGroup,
},
},
}),
),
Expand Down Expand Up @@ -1475,6 +1511,16 @@ func testAccPipelineAttributes(n string, pipeline *buddy.Pipeline, want *testAcc
return err
}
}
if triggerCondition.TriggerCondition == buddy.PipelineTriggerConditionTriggeringUserIs || triggerCondition.TriggerCondition == buddy.PipelineTriggerConditionTriggeringUserIsNot {
if err := util.CheckFieldEqualAndSet(fmt.Sprintf("TriggerConditions[%d].TriggerUser", i), pipeline.TriggerConditions[i].TriggerUser, triggerCondition.TriggerUser); err != nil {
return err
}
}
if triggerCondition.TriggerCondition == buddy.PipelineTriggerConditionTriggeringUserIsInGroup || triggerCondition.TriggerCondition == buddy.PipelineTriggerConditionTriggeringUserIsNotInGroup {
if err := util.CheckFieldEqualAndSet(fmt.Sprintf("TriggerConditions[%d].TriggerGroup", i), pipeline.TriggerConditions[i].TriggerGroup, triggerCondition.TriggerGroup); err != nil {
return err
}
}
if triggerCondition.TriggerCondition == buddy.PipelineTriggerConditionVarIs ||
triggerCondition.TriggerCondition == buddy.PipelineTriggerConditionVarIsNot ||
triggerCondition.TriggerCondition == buddy.PipelineTriggerConditionVarContains ||
Expand Down Expand Up @@ -1557,7 +1603,7 @@ func testAccPipelineGet(n string, pipeline *buddy.Pipeline) resource.TestCheckFu
}
}

func testAccPipelineConfigEvent(domain string, projectName string, name string, eventType string, ref string, tcChangePath string, tcVarKey string, tcVarValue string, tcHours int, tcDays int, tcZoneId string) string {
func testAccPipelineConfigEvent(domain string, projectName string, name string, eventType string, ref string, tcChangePath string, tcVarKey string, tcVarValue string, tcHours int, tcDays int, tcZoneId string, user string, group string) string {
return fmt.Sprintf(`
resource "buddy_workspace" "foo" {
domain = "%s"
Expand All @@ -1568,6 +1614,38 @@ resource "buddy_project" "proj" {
display_name = "%s"
}
resource "buddy_member" "user" {
domain = "${buddy_workspace.foo.domain}"
email = "%s"
}
resource "buddy_group" "group" {
domain = "${buddy_workspace.foo.domain}"
name = "%s"
}
resource "buddy_permission" "perm" {
domain = "${buddy_workspace.foo.domain}"
name = "perm"
pipeline_access_level = "RUN_ONLY"
repository_access_level = "READ_ONLY"
sandbox_access_level = "DENIED"
}
resource "buddy_project_group" "group_in_project" {
domain = "${buddy_workspace.foo.domain}"
project_name = "${buddy_project.proj.name}"
group_id = "${buddy_group.group.group_id}"
permission_id = "${buddy_permission.perm.permission_id}"
}
resource "buddy_project_member" "user_in_project" {
domain = "${buddy_workspace.foo.domain}"
project_name = "${buddy_project.proj.name}"
member_id = "${buddy_member.user.member_id}"
permission_id = "${buddy_permission.perm.permission_id}"
}
resource "buddy_pipeline" "bar" {
domain = "${buddy_workspace.foo.domain}"
project_name = "${buddy_project.proj.name}"
Expand Down Expand Up @@ -1610,8 +1688,24 @@ resource "buddy_pipeline" "bar" {
days = [%d]
zone_id = "%s"
}
trigger_condition {
condition = "TRIGGERING_USER_IS"
trigger_user = "${buddy_member.user.email}"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_NOT"
trigger_user = "${buddy_member.user.email}"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_IN_GROUP"
trigger_group = "${buddy_group.group.name}"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_NOT_IN_GROUP"
trigger_group = "${buddy_group.group.name}"
}
}
`, domain, projectName, name, eventType, ref, tcChangePath, tcVarKey, tcVarValue, tcVarKey, tcVarValue, tcVarKey, tcVarValue, tcVarKey, tcVarValue, tcHours, tcDays, tcZoneId)
`, domain, projectName, user, group, name, eventType, ref, tcChangePath, tcVarKey, tcVarValue, tcVarKey, tcVarValue, tcVarKey, tcVarValue, tcVarKey, tcVarValue, tcHours, tcDays, tcZoneId)
}

func testAccPipelinePermissionsEmpty(domain string, projectName string, name string, ref string) string {
Expand Down
8 changes: 8 additions & 0 deletions buddy/util/triggerConditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type triggerConditionModel struct {
ZoneId types.String `tfsdk:"zone_id"`
ProjectName types.String `tfsdk:"project_name"`
PipelineName types.String `tfsdk:"pipeline_name"`
TriggerUser types.String `tfsdk:"trigger_user"`
TriggerGroup types.String `tfsdk:"trigger_group"`
}

func TriggerConditionsModelToApi(ctx context.Context, s *types.Set) (*[]*buddy.PipelineTriggerCondition, diag.Diagnostics) {
Expand Down Expand Up @@ -58,6 +60,12 @@ func TriggerConditionsModelToApi(ctx context.Context, s *types.Set) (*[]*buddy.P
diags.Append(d...)
tc.TriggerConditionPaths = *paths
}
if !v.TriggerGroup.IsNull() && !v.TriggerGroup.IsUnknown() {
tc.TriggerGroup = v.TriggerGroup.ValueString()
}
if !v.TriggerUser.IsNull() && !v.TriggerUser.IsUnknown() {
tc.TriggerUser = v.TriggerUser.ValueString()
}
triggerConditions[i] = tc
}
return &triggerConditions, diags
Expand Down
18 changes: 18 additions & 0 deletions docs/resources/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ resource "buddy_pipeline" "conditions" {
days = [1, 20]
zone_id = "America/Monterrey"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_NOT_IN_GROUP"
trigger_group = "devs"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_IN_GROUP"
trigger_group = "admins"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_NOT"
trigger_user = "test1@test.com"
}
trigger_condition {
condition = "TRIGGERING_USER_IS"
trigger_user = "test2@test.com"
}
}
```

Expand Down Expand Up @@ -266,6 +282,8 @@ Optional:
- `paths` (Set of String)
- `pipeline_name` (String)
- `project_name` (String)
- `trigger_group` (String)
- `trigger_user` (String)
- `variable_key` (String)
- `variable_value` (String)
- `zone_id` (String)
Expand Down
16 changes: 16 additions & 0 deletions examples/resources/buddy_pipeline/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,20 @@ resource "buddy_pipeline" "conditions" {
days = [1, 20]
zone_id = "America/Monterrey"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_NOT_IN_GROUP"
trigger_group = "devs"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_IN_GROUP"
trigger_group = "admins"
}
trigger_condition {
condition = "TRIGGERING_USER_IS_NOT"
trigger_user = "test1@test.com"
}
trigger_condition {
condition = "TRIGGERING_USER_IS"
trigger_user = "test2@test.com"
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/bflad/tfproviderlint v0.29.0
github.com/buddy/api-go-sdk v1.13.4
github.com/buddy/api-go-sdk v1.14.0
github.com/golangci/golangci-lint v1.52.2
github.com/hashicorp/terraform-plugin-docs v0.14.1
github.com/hashicorp/terraform-plugin-framework v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/breml/bidichk v0.2.4 h1:i3yedFWWQ7YzjdZJHnPo9d/xURinSq3OM+gyM43K4/8=
github.com/breml/bidichk v0.2.4/go.mod h1:7Zk0kRFt1LIZxtQdl9W9JwGAcLTTkOs+tN7wuEYGJ3s=
github.com/breml/errchkjson v0.3.1 h1:hlIeXuspTyt8Y/UmP5qy1JocGNR00KQHgfaNtRAjoxQ=
github.com/breml/errchkjson v0.3.1/go.mod h1:XroxrzKjdiutFyW3nWhw34VGg7kiMsDQox73yWCGI2U=
github.com/buddy/api-go-sdk v1.13.4 h1:Ht0A+cuGYHYEB3oYCS+6XkNdy2EBhS2WA69h6bqf8Ik=
github.com/buddy/api-go-sdk v1.13.4/go.mod h1:l3gE/GMTiq92cQGjQsBWwoTMaTPkLZqNZwLQOBAQiQw=
github.com/buddy/api-go-sdk v1.14.0 h1:r9rHHnLj/T4t2iGE2LtWsQbOSYeIF2NYP+Q1/XS2UXM=
github.com/buddy/api-go-sdk v1.14.0/go.mod h1:l3gE/GMTiq92cQGjQsBWwoTMaTPkLZqNZwLQOBAQiQw=
github.com/butuzov/ireturn v0.1.1 h1:QvrO2QF2+/Cx1WA/vETCIYBKtRjc30vesdoPUNo1EbY=
github.com/butuzov/ireturn v0.1.1/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down

0 comments on commit 471f118

Please sign in to comment.