Skip to content

Commit

Permalink
feat: Provide time.Parse and time.Now while evaluating trigger condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsjip committed Aug 21, 2022
1 parent 69fd80c commit 585cead
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion utils/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func NewAPIFactorySettings() api.Settings {
ConfigMapName: NotificationConfigMap,
InitGetVars: func(cfg *api.Config, configMap *corev1.ConfigMap, secret *corev1.Secret) (api.GetVars, error) {
return func(obj map[string]interface{}, dest services.Destination) map[string]interface{} {
return map[string]interface{}{"rollout": obj}
return map[string]interface{}{"rollout": obj, "time": timeExprs}
}, nil
},
}
Expand Down Expand Up @@ -353,3 +353,20 @@ func translateReasonToTrigger(reason string) string {
trigger = matchAllCap.ReplaceAllString(trigger, "${1}-${2}")
return "on-" + strings.ToLower(trigger)
}

var timeExprs = map[string]interface{}{
"Parse": parse,
"Now": now,
}

func parse(timestamp string) time.Time {
res, err := time.Parse(time.RFC3339, timestamp)
if err != nil {
panic(err)
}
return res
}

func now() time.Time {
return time.Now()
}
2 changes: 1 addition & 1 deletion utils/record/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func TestNewAPIFactorySettings(t *testing.T) {
rollout := map[string]interface{}{"name": "hello"}
vars := getVars(rollout, services.Destination{})

assert.Equal(t, map[string]interface{}{"rollout": rollout}, vars)
assert.Equal(t, map[string]interface{}{"rollout": rollout, "time": timeExprs}, vars)
}

func TestWorkloadRefObjectMap(t *testing.T) {
Expand Down

0 comments on commit 585cead

Please sign in to comment.