diff --git a/api/tasks/runner.go b/api/tasks/runner.go index f8346be9..340ab7ff 100644 --- a/api/tasks/runner.go +++ b/api/tasks/runner.go @@ -656,18 +656,10 @@ func (t *task) getExtraVars() (str string, err error) { if t.template.Type != db.TemplateTask { extraVars["semaphore_task_type"] = t.template.Type - var version string - switch t.template.Type { - case db.TemplateBuild: - version = *t.task.Version - case db.TemplateDeploy: - buildTask, err := t.store.GetTask(t.task.ProjectID, *t.task.BuildTaskID) - if err != nil { - panic("Deploy task has no build task") - } - version = *buildTask.Version + extraVars["semaphore_task_version"], err = t.task.GetVersion(t.store) + if err != nil { + panic("Deploy task has no build task") } - extraVars["semaphore_task_version"] = version } } @@ -787,16 +779,9 @@ func (t *task) setCmdEnvironment(cmd *exec.Cmd, gitSSHCommand string) { if t.template.Type != db.TemplateTask { env = append(env, "SEMAPHORE_TASK_TYPE="+string(t.template.Type)) - var version string - switch t.template.Type { - case db.TemplateBuild: - version = *t.task.Version - case db.TemplateDeploy: - buildTask, err := t.store.GetTask(t.task.ProjectID, *t.task.BuildTaskID) - if err != nil { - panic("Deploy task has no build task") - } - version = *buildTask.Version + version, err := t.task.GetVersion(t.store) + if err != nil { + panic("Deploy task has no build task") } env = append(env, "SEMAPHORE_TASK_VERSION="+version) } diff --git a/db/Task.go b/db/Task.go index 192d1eb0..bc33f25d 100644 --- a/db/Task.go +++ b/db/Task.go @@ -1,6 +1,7 @@ package db import ( + "fmt" "time" ) @@ -39,6 +40,32 @@ type Task struct { Version *string `db:"version" json:"version"` } +func (task *Task) GetVersion(d Store) (string, error) { + tpl, err := d.GetTemplate(task.ProjectID, task.TemplateID) + if err != nil { + return "", err + } + + switch tpl.Type { + case TemplateTask: + return "", fmt.Errorf("only build and deploy tasks has versions") + case TemplateBuild: + if task.Version == nil { + return "", fmt.Errorf("build task must have version") + } + return *task.Version, nil + case TemplateDeploy: + var buildTask Task + buildTask, err = d.GetTask(task.ProjectID, *task.BuildTaskID) + if err != nil { + return "", err + } + return buildTask.GetVersion(d) + default: + return "", fmt.Errorf("unknown task type") + } +} + func (task *Task) ValidateNewTask(template Template) error { switch template.Type { case TemplateBuild: diff --git a/db/bolt/Task_test.go b/db/bolt/Task_test.go new file mode 100644 index 00000000..d04e9510 --- /dev/null +++ b/db/bolt/Task_test.go @@ -0,0 +1,107 @@ +package bolt + +import ( + "github.com/ansible-semaphore/semaphore/db" + "math/rand" + "strconv" + "testing" + "time" +) + +func createTestBoltDb() BoltDb { + r := rand.New(rand.NewSource(time.Now().UTC().UnixNano())) + fn := "/tmp/test_semaphore_db_" + strconv.Itoa(r.Int()) + return BoltDb{ + Filename: fn, + } +} + +func createTestStore() db.Store { + store := createTestBoltDb() + err := store.Connect() + if err != nil { + panic(err) + } + return &store +} + +func TestTask_GetVersion(t *testing.T) { + VERSION := "1.54.48" + + store := createTestStore() + + build, err := store.CreateTemplate(db.Template{ + ProjectID: 0, + Type: db.TemplateBuild, + Alias: "Build", + Playbook: "build.yml", + }) + if err != nil { + t.Fatal(err) + } + + deploy, err := store.CreateTemplate(db.Template{ + ProjectID: 0, + Type: db.TemplateDeploy, + BuildTemplateID: &build.ID, + Alias: "Deploy", + Playbook: "deploy.yml", + }) + if err != nil { + t.Fatal(err) + } + + deploy2, err := store.CreateTemplate(db.Template{ + ProjectID: 0, + Type: db.TemplateDeploy, + BuildTemplateID: &deploy.ID, + Alias: "Deploy2", + Playbook: "deploy2.yml", + }) + if err != nil { + t.Fatal(err) + } + + buildTask, err := store.CreateTask(db.Task{ + ProjectID: 0, + TemplateID: build.ID, + Version: &VERSION, + }) + if err != nil { + t.Fatal(err) + } + + deployTask, err := store.CreateTask(db.Task{ + ProjectID: 0, + TemplateID: deploy.ID, + BuildTaskID: &buildTask.ID, + }) + if err != nil { + t.Fatal(err) + } + + deploy2Task, err := store.CreateTask(db.Task{ + ProjectID: 0, + TemplateID: deploy2.ID, + BuildTaskID: &deployTask.ID, + }) + if err != nil { + t.Fatal(err) + } + + version, err := deployTask.GetVersion(store) + if err != nil { + t.Fatal(err) + } + if version != VERSION { + t.Fatal() + } + + version, err = deploy2Task.GetVersion(store) + if err != nil { + t.Fatal(err) + } + if version != VERSION { + t.Fatal() + } +}