Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: the on-deployed trigger sends multiple notifications (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Matyushentsev authored and alexmt committed Jan 20, 2021
1 parent ea32243 commit 91ffcdd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/triggers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (svc *service) Run(triggerName string, vars map[string]interface{}) ([]Cond
}

if condition.OncePer != "" {
if oncePer, ok, err := unstructured.NestedFieldNoCopy(vars, strings.Split(condition.OncePer, ".")...); err == nil && !ok {
if oncePer, ok, err := unstructured.NestedFieldNoCopy(vars, strings.Split(condition.OncePer, ".")...); err == nil && ok {
conditionResult.OncePer = fmt.Sprintf("%v", oncePer)
}
}
Expand Down
43 changes: 43 additions & 0 deletions pkg/triggers/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,46 @@ func TestRun(t *testing.T) {
}}, res)
})
}

func TestRun_OncePerSet(t *testing.T) {
revision := "123"
svc, err := NewService(map[string][]Condition{
"my-trigger": {{
When: "var1 == 'abc'",
Send: []string{"my-template"},
OncePer: "revision",
}},
})

if !assert.NoError(t, err) {
return
}

conditionKey := fmt.Sprintf("%s:[0].%s", revision, hash("var1 == 'abc'"))

t.Run("Triggered", func(t *testing.T) {
res, err := svc.Run("my-trigger", map[string]interface{}{"var1": "abc", "revision": "123"})
if assert.NoError(t, err) {
return
}
assert.Equal(t, []ConditionResult{{
Key: conditionKey,
Triggered: true,
Templates: []string{"my-template"},
OncePer: revision,
}}, res)
})

t.Run("NotTriggered", func(t *testing.T) {
res, err := svc.Run("my-trigger", map[string]interface{}{"var1": "bcd"})
if assert.NoError(t, err) {
return
}
assert.Equal(t, []ConditionResult{{
Key: conditionKey,
Triggered: false,
Templates: []string{"my-template"},
OncePer: revision,
}}, res)
})
}

0 comments on commit 91ffcdd

Please sign in to comment.