From 89c9042d1e23c45fd0e488bfd43cb5534dbd3cfd Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sun, 24 Nov 2024 23:28:00 +0500 Subject: [PATCH] fix(be): null pointer in new task params field --- db/Task.go | 26 +++++++++++++++++++++----- db/Template.go | 2 ++ services/tasks/LocalJob.go | 6 +++--- web/src/components/TaskLogView.vue | 4 ++-- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/db/Task.go b/db/Task.go index ca2fccb3..dbf186aa 100644 --- a/db/Task.go +++ b/db/Task.go @@ -67,19 +67,35 @@ type Task struct { InventoryID *int `db:"inventory_id" json:"inventory_id"` - TaskParams MapStringAnyField `db:"params" json:"params"` + Params MapStringAnyField `db:"params" json:"params"` } -func (task *Task) GetParams(target interface{}) error { - content, err := json.Marshal(task.TaskParams) +func (task *Task) GetParams(target interface{}) (err error) { + content, err := json.Marshal(task.Params) if err != nil { - return err + return } - return json.Unmarshal(content, target) + err = json.Unmarshal(content, target) + return } func (task *Task) PreInsert(gorp.SqlExecutor) error { task.Created = task.Created.UTC() + + // Init params from old fields for backward compatibility + + if task.Debug { + task.Params["debug"] = true + } + + if task.DryRun { + task.Params["dry_run"] = true + } + + if task.Diff { + task.Params["diff"] = true + } + return nil } diff --git a/db/Template.go b/db/Template.go index 151eff71..d04f8e6c 100644 --- a/db/Template.go +++ b/db/Template.go @@ -102,6 +102,8 @@ type Template struct { App TemplateApp `db:"app" json:"app"` Tasks int `db:"tasks" json:"tasks" backup:"-"` + + TaskParams MapStringAnyField `db:"task_params" json:"task_params"` } func (tpl *Template) Validate() error { diff --git a/services/tasks/LocalJob.go b/services/tasks/LocalJob.go index 71cbb6d5..61cd13b2 100644 --- a/services/tasks/LocalJob.go +++ b/services/tasks/LocalJob.go @@ -431,13 +431,13 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) { switch t.Template.App { case db.AppAnsible: args, inputs, err = t.getPlaybookArgs(username, incomingVersion) - params = db.AnsibleTaskParams{} + params = &db.AnsibleTaskParams{} case db.AppTerraform, db.AppTofu: args, err = t.getTerraformArgs(username, incomingVersion) - params = db.TerraformTaskParams{} + params = &db.TerraformTaskParams{} default: args, err = t.getShellArgs(username, incomingVersion) - params = db.DefaultTaskParams{} + params = &db.DefaultTaskParams{} } if err != nil { diff --git a/web/src/components/TaskLogView.vue b/web/src/components/TaskLogView.vue index 1f5109e7..994650c4 100644 --- a/web/src/components/TaskLogView.vue +++ b/web/src/components/TaskLogView.vue @@ -262,11 +262,11 @@ export default { responseType: 'json', })).data; - this.user = (await axios({ + this.user = this.item.user_id ? (await axios({ method: 'get', url: `/api/users/${this.item.user_id}`, responseType: 'json', - })).data; + })).data : null; }, }, };