From fa24bc9ab08b7de007b9353f8bfd5a9b8a3c4353 Mon Sep 17 00:00:00 2001 From: Brian Zoetewey Date: Tue, 13 Aug 2024 13:14:08 -0400 Subject: [PATCH 1/5] Allow override of Task git_branch --- api-docs.yml | 4 ++++ db/Migration.go | 1 + db/Task.go | 1 + db/sql/migrations/v2.10.23.sql | 1 + services/tasks/LocalJob.go | 4 ++++ 5 files changed, 11 insertions(+) create mode 100644 db/sql/migrations/v2.10.23.sql diff --git a/api-docs.yml b/api-docs.yml index 62bf2e280..1532ddab9 100644 --- a/api-docs.yml +++ b/api-docs.yml @@ -652,6 +652,8 @@ definitions: type: string limit: type: string + git_branch: + type: string TaskOutput: type: object properties: @@ -2164,6 +2166,8 @@ paths: type: string limit: type: string + git_branch: + type: string responses: 201: description: Task queued diff --git a/db/Migration.go b/db/Migration.go index b2e6f1a2a..0b6f9414a 100644 --- a/db/Migration.go +++ b/db/Migration.go @@ -71,6 +71,7 @@ func GetMigrations() []Migration { {Version: "2.10.12"}, {Version: "2.10.15"}, {Version: "2.10.16"}, + {Version: "2.10.23"}, {Version: "2.10.24"}, {Version: "2.10.26"}, } diff --git a/db/Task.go b/db/Task.go index fd8ee4fcf..9a578dbb7 100644 --- a/db/Task.go +++ b/db/Task.go @@ -27,6 +27,7 @@ type Task struct { Limit string `db:"hosts_limit" json:"limit"` Secret string `db:"-" json:"secret"` Arguments *string `db:"arguments" json:"arguments"` + GitBranch string `db:"git_branch" json:"git_branch"` UserID *int `db:"user_id" json:"user_id"` IntegrationID *int `db:"integration_id" json:"integration_id"` diff --git a/db/sql/migrations/v2.10.23.sql b/db/sql/migrations/v2.10.23.sql new file mode 100644 index 000000000..7878e4e84 --- /dev/null +++ b/db/sql/migrations/v2.10.23.sql @@ -0,0 +1 @@ +alter table `task` add `git_branch` varchar(255); diff --git a/services/tasks/LocalJob.go b/services/tasks/LocalJob.go index 73f1b0522..7428c6678 100644 --- a/services/tasks/LocalJob.go +++ b/services/tasks/LocalJob.go @@ -465,6 +465,10 @@ func (t *LocalJob) prepareRun() error { return err } + if t.Task.GitBranch != "" { + t.Repository.GitBranch = t.Task.GitBranch + } + if t.Repository.GetType() == db.RepositoryLocal { if _, err := os.Stat(t.Repository.GitURL); err != nil { t.Log("Failed in finding static repository at " + t.Repository.GitURL + ": " + err.Error()) From 792d42bf262d53e0190fab956c79fa466963b3b1 Mon Sep 17 00:00:00 2001 From: Brian Zoetewey Date: Mon, 16 Sep 2024 15:42:05 -0400 Subject: [PATCH 2/5] 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 c9771257a..a2bea0997 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 1532ddab9..959acfb97 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 e006cd7e1..36fc55e1a 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 7878e4e84..92f04fe2a 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 d341cb33b..724c2a37d 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 7428c6678..2744f907f 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 } From 9c99e6bc9d13ba7ad9977676c92ed5cfadea8c26 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sat, 19 Oct 2024 19:12:44 +0500 Subject: [PATCH 3/5] chore(be): rename migration file --- db/Migration.go | 2 +- db/sql/migrations/{v2.10.23.sql => v2.10.28.sql} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename db/sql/migrations/{v2.10.23.sql => v2.10.28.sql} (100%) diff --git a/db/Migration.go b/db/Migration.go index 0b6f9414a..85a8794a5 100644 --- a/db/Migration.go +++ b/db/Migration.go @@ -71,9 +71,9 @@ func GetMigrations() []Migration { {Version: "2.10.12"}, {Version: "2.10.15"}, {Version: "2.10.16"}, - {Version: "2.10.23"}, {Version: "2.10.24"}, {Version: "2.10.26"}, + {Version: "2.10.28"}, } } diff --git a/db/sql/migrations/v2.10.23.sql b/db/sql/migrations/v2.10.28.sql similarity index 100% rename from db/sql/migrations/v2.10.23.sql rename to db/sql/migrations/v2.10.28.sql From c4d98b1136d90aaab94b537bcf300cde3fe011a9 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sat, 19 Oct 2024 19:16:32 +0500 Subject: [PATCH 4/5] fix(be): use pointer for git branch --- db/Task.go | 2 +- services/tasks/LocalJob.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/db/Task.go b/db/Task.go index 9a578dbb7..0bdb1ef3f 100644 --- a/db/Task.go +++ b/db/Task.go @@ -27,7 +27,7 @@ type Task struct { Limit string `db:"hosts_limit" json:"limit"` Secret string `db:"-" json:"secret"` Arguments *string `db:"arguments" json:"arguments"` - GitBranch string `db:"git_branch" json:"git_branch"` + GitBranch *string `db:"git_branch" json:"git_branch"` UserID *int `db:"user_id" json:"user_id"` IntegrationID *int `db:"integration_id" json:"integration_id"` diff --git a/services/tasks/LocalJob.go b/services/tasks/LocalJob.go index 2744f907f..0d0192d23 100644 --- a/services/tasks/LocalJob.go +++ b/services/tasks/LocalJob.go @@ -471,8 +471,8 @@ func (t *LocalJob) prepareRun() error { } // Override git branch from task if set - if t.Task.GitBranch != "" { - t.Repository.GitBranch = t.Task.GitBranch + if t.Task.GitBranch != nil && *t.Task.GitBranch != "" { + t.Repository.GitBranch = *t.Task.GitBranch } if t.Repository.GetType() == db.RepositoryLocal { From eb91bf05292b052cb6aa82245624d9a92e8d8a55 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sat, 19 Oct 2024 20:23:02 +0500 Subject: [PATCH 5/5] fix(api): allow null for git_branch --- api-docs.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api-docs.yml b/api-docs.yml index 959acfb97..a765c0f0e 100644 --- a/api-docs.yml +++ b/api-docs.yml @@ -653,7 +653,10 @@ definitions: limit: type: string git_branch: - type: string + type: + - string + - 'null' + TaskOutput: type: object properties: