From 792d42bf262d53e0190fab956c79fa466963b3b1 Mon Sep 17 00:00:00 2001 From: Brian Zoetewey Date: Mon, 16 Sep 2024 15:42:05 -0400 Subject: [PATCH] Allow override of Template git branch. Prefer Task override when running Local Job. --- .dredd/hooks/capabilities.go | 2 ++ api-docs.yml | 6 ++++++ db/Template.go | 3 +++ db/sql/migrations/v2.10.23.sql | 4 +++- db/sql/template.go | 13 +++++++++---- services/tasks/LocalJob.go | 6 ++++++ 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/.dredd/hooks/capabilities.go b/.dredd/hooks/capabilities.go index c9771257..a2bea099 100644 --- a/.dredd/hooks/capabilities.go +++ b/.dredd/hooks/capabilities.go @@ -125,6 +125,7 @@ func resolveCapability(caps []string, resolved []string, uid string) { case "template": args := "[]" desc := "Hello, World!" + branch := "main" res, err := store.CreateTemplate(db.Template{ ProjectID: userProject.ID, InventoryID: &inventoryID, @@ -137,6 +138,7 @@ func resolveCapability(caps []string, resolved []string, uid string) { Description: &desc, ViewID: &view.ID, App: db.AppAnsible, + GitBranch: &branch, }) printError(err) diff --git a/api-docs.yml b/api-docs.yml index 1532ddab..959acfb9 100644 --- a/api-docs.yml +++ b/api-docs.yml @@ -713,6 +713,9 @@ definitions: app: type: string example: ansible + git_branch: + type: string + example: main survey_vars: type: array items: @@ -756,6 +759,9 @@ definitions: type: boolean app: type: string + git_branch: + type: string + example: main TemplateSurveyVar: type: object properties: diff --git a/db/Template.go b/db/Template.go index e006cd7e..36fc55e1 100644 --- a/db/Template.go +++ b/db/Template.go @@ -88,6 +88,9 @@ type Template struct { Autorun bool `db:"autorun" json:"autorun"` + // override variables + GitBranch *string `db:"git_branch" json:"git_branch"` + // SurveyVarsJSON used internally for read from database. // It is not used for store survey vars to database. // Do not use it in your code. Use SurveyVars instead. diff --git a/db/sql/migrations/v2.10.23.sql b/db/sql/migrations/v2.10.23.sql index 7878e4e8..92f04fe2 100644 --- a/db/sql/migrations/v2.10.23.sql +++ b/db/sql/migrations/v2.10.23.sql @@ -1 +1,3 @@ -alter table `task` add `git_branch` varchar(255); +alter table task add git_branch varchar(255); + +alter table project__template add git_branch varchar(255); diff --git a/db/sql/template.go b/db/sql/template.go index d341cb33..724c2a37 100644 --- a/db/sql/template.go +++ b/db/sql/template.go @@ -18,8 +18,8 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e "id", "insert into project__template (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)"+ - "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "build_template_id, view_id, autorun, survey_vars, suppress_success_alerts, app, git_branch)"+ + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", template.ProjectID, template.InventoryID, template.RepositoryID, @@ -36,7 +36,8 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e template.Autorun, db.ObjectToJSON(template.SurveyVars), template.SuppressSuccessAlerts, - template.App) + template.App, + template.GitBranch) if err != nil { return @@ -82,7 +83,8 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { "autorun=?, "+ "survey_vars=?, "+ "suppress_success_alerts=?, "+ - "app=? "+ + "app=?, "+ + "`git_branch`=? "+ "where id=? and project_id=?", template.InventoryID, template.RepositoryID, @@ -100,6 +102,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { db.ObjectToJSON(template.SurveyVars), template.SuppressSuccessAlerts, template.App, + template.GitBranch, template.ID, template.ProjectID, ) @@ -127,6 +130,7 @@ func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db. "pt.repository_id", "pt.environment_id", "pt.name", + "pt.description", "pt.playbook", "pt.arguments", "pt.allow_override_args_in_task", @@ -134,6 +138,7 @@ func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db. "pt.start_version", "pt.view_id", "pt.`app`", + "pt.`git_branch`", "pt.survey_vars", "pt.start_version", "pt.`type`", diff --git a/services/tasks/LocalJob.go b/services/tasks/LocalJob.go index 7428c667..2744f907 100644 --- a/services/tasks/LocalJob.go +++ b/services/tasks/LocalJob.go @@ -465,6 +465,12 @@ func (t *LocalJob) prepareRun() error { return err } + // Override git branch from template if set + if t.Template.GitBranch != nil && *t.Template.GitBranch != "" { + t.Repository.GitBranch = *t.Template.GitBranch + } + + // Override git branch from task if set if t.Task.GitBranch != "" { t.Repository.GitBranch = t.Task.GitBranch }