fix(be): null pointer in new task params field

This commit is contained in:
Denis Gukov 2024-11-24 23:28:00 +05:00
parent 289cd6d937
commit 89c9042d1e
4 changed files with 28 additions and 10 deletions

View File

@ -67,19 +67,35 @@ type Task struct {
InventoryID *int `db:"inventory_id" json:"inventory_id"` 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 { func (task *Task) GetParams(target interface{}) (err error) {
content, err := json.Marshal(task.TaskParams) content, err := json.Marshal(task.Params)
if err != nil { if err != nil {
return err return
} }
return json.Unmarshal(content, target) err = json.Unmarshal(content, target)
return
} }
func (task *Task) PreInsert(gorp.SqlExecutor) error { func (task *Task) PreInsert(gorp.SqlExecutor) error {
task.Created = task.Created.UTC() 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 return nil
} }

View File

@ -102,6 +102,8 @@ type Template struct {
App TemplateApp `db:"app" json:"app"` App TemplateApp `db:"app" json:"app"`
Tasks int `db:"tasks" json:"tasks" backup:"-"` Tasks int `db:"tasks" json:"tasks" backup:"-"`
TaskParams MapStringAnyField `db:"task_params" json:"task_params"`
} }
func (tpl *Template) Validate() error { func (tpl *Template) Validate() error {

View File

@ -431,13 +431,13 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) {
switch t.Template.App { switch t.Template.App {
case db.AppAnsible: case db.AppAnsible:
args, inputs, err = t.getPlaybookArgs(username, incomingVersion) args, inputs, err = t.getPlaybookArgs(username, incomingVersion)
params = db.AnsibleTaskParams{} params = &db.AnsibleTaskParams{}
case db.AppTerraform, db.AppTofu: case db.AppTerraform, db.AppTofu:
args, err = t.getTerraformArgs(username, incomingVersion) args, err = t.getTerraformArgs(username, incomingVersion)
params = db.TerraformTaskParams{} params = &db.TerraformTaskParams{}
default: default:
args, err = t.getShellArgs(username, incomingVersion) args, err = t.getShellArgs(username, incomingVersion)
params = db.DefaultTaskParams{} params = &db.DefaultTaskParams{}
} }
if err != nil { if err != nil {

View File

@ -262,11 +262,11 @@ export default {
responseType: 'json', responseType: 'json',
})).data; })).data;
this.user = (await axios({ this.user = this.item.user_id ? (await axios({
method: 'get', method: 'get',
url: `/api/users/${this.item.user_id}`, url: `/api/users/${this.item.user_id}`,
responseType: 'json', responseType: 'json',
})).data; })).data : null;
}, },
}, },
}; };