From 59b367b82107bce71e41908e7aeea368ad34df0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20Silva?= Date: Tue, 1 Apr 2025 00:20:19 -0300 Subject: [PATCH 1/2] feat(template): implements extra variables in the template --- api-docs.yml | 6 ++++++ db/Migration.go | 1 + db/Template.go | 6 ++++++ db/sql/migrations/v2.13.6.sql | 1 + db/sql/template.go | 9 ++++++--- services/tasks/LocalJob.go | 9 +++++++++ web/src/components/TemplateForm.vue | 26 ++++++++++++++++++++++++++ web/src/lang/en.js | 3 +++ 8 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 db/sql/migrations/v2.13.6.sql diff --git a/api-docs.yml b/api-docs.yml index dbadbb26b..8f0f87653 100644 --- a/api-docs.yml +++ b/api-docs.yml @@ -796,6 +796,9 @@ definitions: type: integer autorun: type: boolean + extra_vars_json: + type: string + example: '{}' Template: type: object @@ -860,6 +863,9 @@ definitions: type: array items: $ref: "#/definitions/TemplateVault" + extra_vars_json: + type: string + example: '{}' TemplateSurveyVar: type: object diff --git a/db/Migration.go b/db/Migration.go index 44d134185..7f83abfad 100644 --- a/db/Migration.go +++ b/db/Migration.go @@ -83,6 +83,7 @@ func GetMigrations() []Migration { {Version: "2.12.5"}, {Version: "2.12.15"}, {Version: "2.13.0"}, + {Version: "2.13.6"}, } } diff --git a/db/Template.go b/db/Template.go index e0df5ce2c..373241fea 100644 --- a/db/Template.go +++ b/db/Template.go @@ -146,6 +146,8 @@ type Template struct { TaskParams MapStringAnyField `db:"task_params" json:"task_params"` RunnerTag *string `db:"runner_tag" json:"runner_tag"` + + ExtraVarsJSON string `db:"extra_vars_json" json:"extra_vars_json"` } func (tpl *Template) FillParams(target interface{}) error { @@ -193,6 +195,10 @@ func (tpl *Template) Validate() error { } } + if tpl.ExtraVarsJSON != "" && !json.Valid([]byte(tpl.ExtraVarsJSON)) { + return &ValidationError{"Extra variables must be valid JSON"} + } + return nil } diff --git a/db/sql/migrations/v2.13.6.sql b/db/sql/migrations/v2.13.6.sql new file mode 100644 index 000000000..1edb09e3e --- /dev/null +++ b/db/sql/migrations/v2.13.6.sql @@ -0,0 +1 @@ +alter table `project__template` add column `extra_vars_json` longtext not null default '{}'; \ No newline at end of file diff --git a/db/sql/template.go b/db/sql/template.go index 5da8e13ee..0f6fc3eff 100644 --- a/db/sql/template.go +++ b/db/sql/template.go @@ -19,12 +19,12 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e "project_id, inventory_id, repository_id, environment_id, name, "+ "playbook, arguments, allow_override_args_in_task, description, `type`, "+ "start_version, build_template_id, view_id, autorun, survey_vars, "+ - "suppress_success_alerts, app, git_branch, runner_tag, task_params)"+ + "suppress_success_alerts, app, git_branch, runner_tag, task_params, extra_vars_json)"+ "values ("+ "?, ?, ?, ?, ?, "+ "?, ?, ?, ?, ?, "+ "?, ?, ?, ?, ?, "+ - "?, ?, ?, ?, ?)", + "?, ?, ?, ?, ?, ?)", template.ProjectID, template.InventoryID, template.RepositoryID, @@ -48,6 +48,7 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e template.GitBranch, template.RunnerTag, template.TaskParams, + template.ExtraVarsJSON, ) if err != nil { @@ -97,7 +98,8 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { "app=?, "+ "`git_branch`=?, "+ "task_params=?, "+ - "runner_tag=? "+ + "runner_tag=?, "+ + "extra_vars_json=? "+ "where id=? and project_id=?", template.InventoryID, template.RepositoryID, @@ -118,6 +120,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { template.GitBranch, template.TaskParams, template.RunnerTag, + template.ExtraVarsJSON, template.ID, template.ProjectID, ) diff --git a/services/tasks/LocalJob.go b/services/tasks/LocalJob.go index b2058fcbd..571951b9f 100644 --- a/services/tasks/LocalJob.go +++ b/services/tasks/LocalJob.go @@ -100,6 +100,7 @@ func (t *LocalJob) getEnvironmentExtraVars(username string, incomingVersion *str func (t *LocalJob) getEnvironmentExtraVarsJSON(username string, incomingVersion *string) (str string, err error) { extraVars := make(map[string]interface{}) extraSecretVars := make(map[string]interface{}) + extraTemplateVars := make(map[string]interface{}) if t.Environment.JSON != "" { err = json.Unmarshal([]byte(t.Environment.JSON), &extraVars) @@ -115,7 +116,15 @@ func (t *LocalJob) getEnvironmentExtraVarsJSON(username string, incomingVersion } t.Secret = "{}" + if t.Template.ExtraVarsJSON != "" && t.Template.ExtraVarsJSON != "{}" { + err = json.Unmarshal([]byte(t.Template.ExtraVarsJSON), &extraTemplateVars) + if err != nil { + return + } + } + maps.Copy(extraVars, extraSecretVars) + maps.Copy(extraVars, extraTemplateVars) taskDetails := make(map[string]interface{}) diff --git a/web/src/components/TemplateForm.vue b/web/src/components/TemplateForm.vue index f8af640cf..874e17610 100644 --- a/web/src/components/TemplateForm.vue +++ b/web/src/components/TemplateForm.vue @@ -328,6 +328,18 @@ v-if="needField('allow_debug')" /> +
+ {{ $t('templateExtraVariablesJson') }} + +
+ @@ -345,6 +357,10 @@ color: #a4a4a4 !important; } +.CodeMirror { + height: 100%; +} + .TemplateFormBody { display: flex; flex-direction: column; @@ -380,6 +396,7 @@ import axios from 'axios'; import ItemFormBase from '@/components/ItemFormBase'; +import { codemirror } from 'vue-codemirror'; import 'codemirror/lib/codemirror.css'; import 'codemirror/mode/vue/vue.js'; import 'codemirror/addon/lint/json-lint.js'; @@ -397,6 +414,7 @@ export default { TemplateVaults, ArgsPicker, SurveyVars, + codemirror, }, props: { @@ -568,6 +586,10 @@ export default { }, async afterLoadData() { + if (!this.item.extra_vars_json) { + this.item.extra_vars_json = '{}'; + } + if (this.sourceItemId) { const item = (await axios({ url: `/api/project/${this.projectId}/templates/${this.sourceItemId}`, @@ -699,6 +721,10 @@ export default { this.item.app = this.app; this.item.arguments = JSON.stringify(this.args); + + if (!this.item.extra_vars_json) { + this.item.extra_vars_json = '{}'; + } }, async afterSave(newItem) { diff --git a/web/src/lang/en.js b/web/src/lang/en.js index 9c56f9507..5ce96dde8 100644 --- a/web/src/lang/en.js +++ b/web/src/lang/en.js @@ -321,4 +321,7 @@ export default { tags: 'Tags', limit: 'Limit', + + templateExtraVariablesJson: 'Extra variables', + enterTemplateExtraVariablesJson: 'Enter extra variables JSON...', }; From cae9d7fef327c62a14d3312826578fe8068e2769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20Silva?= Date: Tue, 1 Apr 2025 01:06:03 -0300 Subject: [PATCH 2/2] fix(template): remove default value for extra_vars_json --- api-docs.yml | 2 -- db/sql/migrations/v2.13.6.sql | 2 +- web/src/components/TemplateForm.vue | 8 -------- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/api-docs.yml b/api-docs.yml index 8f0f87653..fa96f861c 100644 --- a/api-docs.yml +++ b/api-docs.yml @@ -798,7 +798,6 @@ definitions: type: boolean extra_vars_json: type: string - example: '{}' Template: type: object @@ -865,7 +864,6 @@ definitions: $ref: "#/definitions/TemplateVault" extra_vars_json: type: string - example: '{}' TemplateSurveyVar: type: object diff --git a/db/sql/migrations/v2.13.6.sql b/db/sql/migrations/v2.13.6.sql index 1edb09e3e..0ca9b5c7b 100644 --- a/db/sql/migrations/v2.13.6.sql +++ b/db/sql/migrations/v2.13.6.sql @@ -1 +1 @@ -alter table `project__template` add column `extra_vars_json` longtext not null default '{}'; \ No newline at end of file +alter table `project__template` add column `extra_vars_json` longtext null; \ No newline at end of file diff --git a/web/src/components/TemplateForm.vue b/web/src/components/TemplateForm.vue index 874e17610..b927b9a3b 100644 --- a/web/src/components/TemplateForm.vue +++ b/web/src/components/TemplateForm.vue @@ -586,10 +586,6 @@ export default { }, async afterLoadData() { - if (!this.item.extra_vars_json) { - this.item.extra_vars_json = '{}'; - } - if (this.sourceItemId) { const item = (await axios({ url: `/api/project/${this.projectId}/templates/${this.sourceItemId}`, @@ -721,10 +717,6 @@ export default { this.item.app = this.app; this.item.arguments = JSON.stringify(this.args); - - if (!this.item.extra_vars_json) { - this.item.extra_vars_json = '{}'; - } }, async afterSave(newItem) {