Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow override of Task and Template git_branch #2278

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dredd/hooks/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -137,6 +138,7 @@ func resolveCapability(caps []string, resolved []string, uid string) {
Description: &desc,
ViewID: &view.ID,
App: db.AppAnsible,
GitBranch: &branch,
})

printError(err)
Expand Down
13 changes: 13 additions & 0 deletions api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,11 @@ definitions:
type: string
limit:
type: string
git_branch:
type:
- string
- 'null'

TaskOutput:
type: object
properties:
Expand Down Expand Up @@ -711,6 +716,9 @@ definitions:
app:
type: string
example: ansible
git_branch:
type: string
example: main
survey_vars:
type: array
items:
Expand Down Expand Up @@ -754,6 +762,9 @@ definitions:
type: boolean
app:
type: string
git_branch:
type: string
example: main
TemplateSurveyVar:
type: object
properties:
Expand Down Expand Up @@ -2164,6 +2175,8 @@ paths:
type: string
limit:
type: string
git_branch:
type: string
responses:
201:
description: Task queued
Expand Down
1 change: 1 addition & 0 deletions db/Migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func GetMigrations() []Migration {
{Version: "2.10.16"},
{Version: "2.10.24"},
{Version: "2.10.26"},
{Version: "2.10.28"},
}
}

Expand Down
1 change: 1 addition & 0 deletions db/Task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
3 changes: 3 additions & 0 deletions db/Template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions db/sql/migrations/v2.10.28.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table task add git_branch varchar(255);

alter table project__template add git_branch varchar(255);
13 changes: 9 additions & 4 deletions db/sql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
Expand Down Expand Up @@ -127,13 +130,15 @@ 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",
"pt.build_template_id",
"pt.start_version",
"pt.view_id",
"pt.`app`",
"pt.`git_branch`",
"pt.survey_vars",
"pt.start_version",
"pt.`type`",
Expand Down
10 changes: 10 additions & 0 deletions services/tasks/LocalJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ 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 != nil && *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())
Expand Down
Loading