mirror of
https://github.com/semaphoreui/semaphore.git
synced 2025-01-20 23:39:56 +01:00
feat(be): recursive retreaving task version. Now we can make pipelines :)
This commit is contained in:
parent
51fc40e9c8
commit
d8afdb19d8
@ -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)
|
||||
extraVars["semaphore_task_version"], err = t.task.GetVersion(t.store)
|
||||
if err != nil {
|
||||
panic("Deploy task has no build task")
|
||||
}
|
||||
version = *buildTask.Version
|
||||
}
|
||||
extraVars["semaphore_task_version"] = version
|
||||
}
|
||||
}
|
||||
|
||||
@ -787,17 +779,10 @@ 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)
|
||||
version, err := t.task.GetVersion(t.store)
|
||||
if err != nil {
|
||||
panic("Deploy task has no build task")
|
||||
}
|
||||
version = *buildTask.Version
|
||||
}
|
||||
env = append(env, "SEMAPHORE_TASK_VERSION="+version)
|
||||
}
|
||||
}
|
||||
|
27
db/Task.go
27
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:
|
||||
|
107
db/bolt/Task_test.go
Normal file
107
db/bolt/Task_test.go
Normal file
@ -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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user