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

Add new 13.6 flow migration to truncate result names and categories #1292

Merged
merged 2 commits into from
Dec 3, 2024
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
4 changes: 2 additions & 2 deletions flows/actions/set_run_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type SetRunResultAction struct {
baseAction
universalAction

Name string `json:"name" validate:"required,max=128"`
Name string `json:"name" validate:"required,max=64"`
Value string `json:"value" engine:"evaluated"`
Category string `json:"category,omitempty" engine:"localized" validate:"max=128"`
Category string `json:"category,omitempty" engine:"localized" validate:"max=36"`
}

// NewSetRunResult creates a new set run result action
Expand Down
11 changes: 0 additions & 11 deletions flows/actions/testdata/set_run_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@
},
"read_error": "field 'name' is required"
},
{
"description": "Read fails when category is too long",
"action": {
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"type": "set_run_result",
"name": "Foo",
"value": "bar",
"category": "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
},
"read_error": "field 'category' must be less than or equal to 128"
},
{
"description": "Error event and action skipped if value contains expression error",
"action": {
Expand Down
2 changes: 1 addition & 1 deletion flows/definition/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// CurrentSpecVersion is the flow spec version supported by this library
var CurrentSpecVersion = semver.MustParse("13.5.0")
var CurrentSpecVersion = semver.MustParse("13.6.0")

// IsVersionSupported checks the given version is supported
func IsVersionSupported(v *semver.Version) bool {
Expand Down
54 changes: 54 additions & 0 deletions flows/definition/migrations/13_x.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,74 @@
package migrations

import (
"strings"

"github.com/Masterminds/semver"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/stringsx"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/excellent/refactor"
)

func init() {
registerMigration(semver.MustParse("13.6.0"), Migrate13_6)
registerMigration(semver.MustParse("13.5.0"), Migrate13_5)
registerMigration(semver.MustParse("13.4.0"), Migrate13_4)
registerMigration(semver.MustParse("13.3.0"), Migrate13_3)
registerMigration(semver.MustParse("13.2.0"), Migrate13_2)
registerMigration(semver.MustParse("13.1.0"), Migrate13_1)
}

// Migrate13_6 ensures that names of results and categories respect definition limits.
//
// @version 13_6 "13.6"
func Migrate13_6(f Flow, cfg *Config) (Flow, error) {
const maxResultName = 64
const maxCategoryName = 36

truncate := func(s string, max int) string {
return strings.TrimSpace(stringsx.Truncate(s, max)) // so we don't leave trailing spaces
}

for _, node := range f.Nodes() {
for _, action := range node.Actions() {
if action.Type() == "set_run_result" {
name, _ := action["name"].(string)
category, _ := action["category"].(string)

if len(name) > maxResultName {
action["name"] = truncate(name, maxResultName)
}
if len(category) > maxCategoryName {
action["category"] = truncate(category, maxCategoryName)
}
}
}

router := node.Router()
if router != nil {
resultName, _ := router["result_name"].(string)
categories, _ := router["categories"].([]any)

if len(resultName) > maxResultName {
router["result_name"] = truncate(resultName, maxResultName)
}

for _, cat := range categories {
category, _ := cat.(map[string]any)
if category != nil {
name, _ := category["name"].(string)

if len(name) > maxCategoryName {
category["name"] = truncate(name, maxCategoryName)
}
}
}
}
}
return f, nil
}

// Migrate13_5 converts the `templating` object in [action:send_msg] actions to use a merged list of variables.
//
// @version 13_5 "13.5"
Expand Down
89 changes: 89 additions & 0 deletions flows/definition/migrations/specdata/templates.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,93 @@
{
"13.6.0": {
"actions": {
"add_contact_groups": [
".groups[*].name_match"
],
"add_contact_urn": [
".path"
],
"add_input_labels": [
".labels[*].name_match"
],
"call_classifier": [
".input"
],
"call_resthook": [],
"call_webhook": [
".body",
".headers.*",
".url"
],
"enter_flow": [],
"open_ticket": [
".assignee.email_match",
".body"
],
"play_audio": [
".audio_url"
],
"remove_contact_groups": [
".groups[*].name_match"
],
"request_optin": [],
"say_msg": [
".text"
],
"send_broadcast": [
".attachments[*]",
".contact_query",
".groups[*].name_match",
".legacy_vars[*]",
".quick_replies[*]",
".text"
],
"send_email": [
".addresses[*]",
".body",
".subject"
],
"send_msg": [
".attachments[*]",
".quick_replies[*]",
".template_variables[*]",
".text"
],
"set_contact_channel": [],
"set_contact_field": [
".value"
],
"set_contact_language": [
".language"
],
"set_contact_name": [
".name"
],
"set_contact_status": [],
"set_contact_timezone": [
".timezone"
],
"set_run_result": [
".value"
],
"start_session": [
".contact_query",
".groups[*].name_match",
".legacy_vars[*]"
],
"transfer_airtime": []
},
"routers": {
"random": [
".operand",
".cases[*].arguments[*]"
],
"switch": [
".operand",
".cases[*].arguments[*]"
]
}
},
"13.5.0": {
"actions": {
"add_contact_groups": [
Expand Down
Loading
Loading