feat(be): add schedule refs

This commit is contained in:
Denis Gukov 2024-11-26 08:55:26 +00:00
parent 9b205df1b9
commit aa8cbb0880
3 changed files with 80 additions and 206 deletions

View File

@ -690,21 +690,10 @@ func (d *BoltDb) getObjectRefs(projectID int, objectProps db.ObjectProps, object
return return
} }
// templates, err := d.getObjectRefsFrom(projectID, objectProps, intObjectID(objectID), db.ScheduleProps) refs.Schedules, err = d.getObjectRefsFrom(projectID, objectProps, intObjectID(objectID), db.ScheduleProps)
if err != nil {
// for _, st := range templates { return
// exists := false }
// for _, tpl := range refs.Templates {
// if tpl.ID == st.ID {
// exists = true
// break
// }
// }
// if exists {
// continue
// }
// refs.Templates = append(refs.Templates, st)
// }
refs.Integrations, err = d.getObjectRefsFrom(projectID, objectProps, intObjectID(objectID), db.IntegrationProps) refs.Integrations, err = d.getObjectRefsFrom(projectID, objectProps, intObjectID(objectID), db.IntegrationProps)
if err != nil { if err != nil {

View File

@ -2,9 +2,12 @@ package bolt
import ( import (
"fmt" "fmt"
"github.com/semaphoreui/semaphore/db"
"reflect" "reflect"
"testing" "testing"
"github.com/semaphoreui/semaphore/db"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
type test1 struct { type test1 struct {
@ -30,16 +33,11 @@ func TestMarshalObject_UserWithPwd(t *testing.T) {
} }
bytes, err := marshalObject(user) bytes, err := marshalObject(user)
require.NoError(t, err)
if err != nil {
t.Fatal(fmt.Errorf("function returns error: " + err.Error()))
}
str := string(bytes) str := string(bytes)
expected := `{"id":0,"created":"0001-01-01T00:00:00Z","username":"fiftin","name":"","email":"","password":"345345234523452345234","admin":false,"external":false,"alert":false}`
if str != `{"id":0,"created":"0001-01-01T00:00:00Z","username":"fiftin","name":"","email":"","password":"345345234523452345234","admin":false,"external":false,"alert":false}` { assert.Equal(t, expected, str)
t.Fatal(fmt.Errorf("incorrect marshalling result"))
}
fmt.Println(str) fmt.Println(str)
} }
@ -54,15 +52,11 @@ func TestMarshalObject(t *testing.T) {
} }
bytes, err := marshalObject(test1) bytes, err := marshalObject(test1)
require.NoError(t, err)
if err != nil {
t.Fatal(fmt.Errorf("function returns error: " + err.Error()))
}
str := string(bytes) str := string(bytes)
if str != `{"ID":0,"first_name":"Denis","last_name":"Gukov","password":"9347502348723","removed":false}` { expected := `{"ID":0,"first_name":"Denis","last_name":"Gukov","password":"9347502348723","removed":false}`
t.Fatal(fmt.Errorf("incorrect marshalling result")) assert.Equal(t, expected, str)
}
fmt.Println(str) fmt.Println(str)
} }
@ -75,89 +69,50 @@ func TestUnmarshalObject(t *testing.T) {
"password": "9347502348723" "password": "9347502348723"
}` }`
err := unmarshalObject([]byte(data), &test1) err := unmarshalObject([]byte(data), &test1)
if err != nil { require.NoError(t, err)
t.Fatal(fmt.Errorf("function returns error: " + err.Error()))
} assert.Equal(t, "Denis", test1.FirstName)
if test1.FirstName != "Denis" || assert.Equal(t, "Gukov", test1.LastName)
test1.LastName != "Gukov" || assert.Equal(t, "", test1.Password)
test1.Password != "" || assert.Equal(t, "", test1.PasswordRepeat)
test1.PasswordRepeat != "" || assert.Equal(t, "9347502348723", test1.PasswordHash)
test1.PasswordHash != "9347502348723" {
t.Fatal(fmt.Errorf("object unmarshalled incorrectly"))
}
} }
func TestSortObjects(t *testing.T) { func TestSortObjects(t *testing.T) {
objects := []db.Inventory{ objects := []db.Inventory{
{ {ID: 1, Name: "x"},
ID: 1, {ID: 2, Name: "a"},
Name: "x", {ID: 3, Name: "d"},
}, {ID: 4, Name: "b"},
{ {ID: 5, Name: "r"},
ID: 2,
Name: "a",
},
{
ID: 3,
Name: "d",
},
{
ID: 4,
Name: "b",
},
{
ID: 5,
Name: "r",
},
} }
err := sortObjects(&objects, "name", false) err := sortObjects(&objects, "name", false)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
expected := objects[0].Name == "a" && expected := []string{"a", "b", "d", "r", "x"}
objects[1].Name == "b" && for i, obj := range objects {
objects[2].Name == "d" && assert.Equal(t, expected[i], obj.Name)
objects[3].Name == "r" &&
objects[4].Name == "x"
if !expected {
t.Fatal(fmt.Errorf("objects not sorted"))
} }
} }
func TestGetFieldNameByTag(t *testing.T) { func TestGetFieldNameByTag(t *testing.T) {
f, err := getFieldNameByTagSuffix(reflect.TypeOf(test1{}), "db", "first_name") f, err := getFieldNameByTagSuffix(reflect.TypeOf(test1{}), "db", "first_name")
if err != nil { require.NoError(t, err)
t.Fatal(err.Error()) assert.Equal(t, "FirstName", f)
}
if f != "FirstName" {
t.Fatal()
}
} }
func TestGetFieldNameByTag2(t *testing.T) { func TestGetFieldNameByTag2(t *testing.T) {
f, err := getFieldNameByTagSuffix(reflect.TypeOf(db.UserWithPwd{}), "db", "id") f, err := getFieldNameByTagSuffix(reflect.TypeOf(db.UserWithPwd{}), "db", "id")
if err != nil { require.NoError(t, err)
t.Fatal(err.Error()) assert.Equal(t, "ID", f)
}
if f != "ID" {
t.Fatal()
}
} }
func TestIsObjectInUse(t *testing.T) { func TestIsObjectInUse(t *testing.T) {
store := CreateTestStore() store := CreateTestStore()
proj, err := store.CreateProject(db.Project{ proj, err := store.CreateProject(db.Project{Name: "test"})
Name: "test", require.NoError(t, err)
})
if err != nil {
t.Fatal(err.Error())
}
_, err = store.CreateTemplate(db.Template{ _, err = store.CreateTemplate(db.Template{
Name: "Test", Name: "Test",
@ -166,33 +121,18 @@ func TestIsObjectInUse(t *testing.T) {
InventoryID: &inventoryID, InventoryID: &inventoryID,
EnvironmentID: &environmentID, EnvironmentID: &environmentID,
}) })
require.NoError(t, err)
if err != nil {
t.Fatal(err.Error())
}
isUse, err := store.isObjectInUse(proj.ID, db.InventoryProps, intObjectID(10), db.TemplateProps) isUse, err := store.isObjectInUse(proj.ID, db.InventoryProps, intObjectID(10), db.TemplateProps)
require.NoError(t, err)
if err != nil { assert.True(t, isUse)
t.Fatal(err.Error())
}
if !isUse {
t.Fatal()
}
} }
func TestIsObjectInUse_Environment(t *testing.T) { func TestIsObjectInUse_Environment(t *testing.T) {
store := CreateTestStore() store := CreateTestStore()
proj, err := store.CreateProject(db.Project{ proj, err := store.CreateProject(db.Project{Name: "test"})
Name: "test", require.NoError(t, err)
})
if err != nil {
t.Fatal(err.Error())
}
_, err = store.CreateTemplate(db.Template{ _, err = store.CreateTemplate(db.Template{
Name: "Test", Name: "Test",
@ -201,33 +141,18 @@ func TestIsObjectInUse_Environment(t *testing.T) {
InventoryID: &inventoryID, InventoryID: &inventoryID,
EnvironmentID: &environmentID, EnvironmentID: &environmentID,
}) })
require.NoError(t, err)
if err != nil {
t.Fatal(err.Error())
}
isUse, err := store.isObjectInUse(proj.ID, db.EnvironmentProps, intObjectID(10), db.TemplateProps) isUse, err := store.isObjectInUse(proj.ID, db.EnvironmentProps, intObjectID(10), db.TemplateProps)
require.NoError(t, err)
if err != nil { assert.True(t, isUse)
t.Fatal(err.Error())
}
if !isUse {
t.Fatal()
}
} }
func TestIsObjectInUse_EnvironmentNil(t *testing.T) { func TestIsObjectInUse_EnvironmentNil(t *testing.T) {
store := CreateTestStore() store := CreateTestStore()
proj, err := store.CreateProject(db.Project{ proj, err := store.CreateProject(db.Project{Name: "test"})
Name: "test", require.NoError(t, err)
})
if err != nil {
t.Fatal(err)
}
_, err = store.CreateTemplate(db.Template{ _, err = store.CreateTemplate(db.Template{
Name: "Test", Name: "Test",
@ -236,20 +161,11 @@ func TestIsObjectInUse_EnvironmentNil(t *testing.T) {
InventoryID: &inventoryID, InventoryID: &inventoryID,
EnvironmentID: nil, EnvironmentID: nil,
}) })
require.NoError(t, err)
if err != nil {
t.Fatal(err)
}
isUse, err := store.isObjectInUse(proj.ID, db.EnvironmentProps, intObjectID(10), db.TemplateProps) isUse, err := store.isObjectInUse(proj.ID, db.EnvironmentProps, intObjectID(10), db.TemplateProps)
require.NoError(t, err)
if err != nil { assert.False(t, isUse)
t.Fatal(err)
}
if isUse {
t.Fatal()
}
} }
func TestBoltDb_CreateAPIToken(t *testing.T) { func TestBoltDb_CreateAPIToken(t *testing.T) {
@ -264,63 +180,35 @@ func TestBoltDb_CreateAPIToken(t *testing.T) {
Admin: true, Admin: true,
}, },
}) })
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
token, err := store.CreateAPIToken(db.APIToken{ token, err := store.CreateAPIToken(db.APIToken{
ID: "f349gyhgqirgysfgsfg34973dsfad", ID: "f349gyhgqirgysfgsfg34973dsfad",
UserID: user.ID, UserID: user.ID,
}) })
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
token2, err := store.GetAPIToken(token.ID) token2, err := store.GetAPIToken(token.ID)
if err != nil { require.NoError(t, err)
t.Fatal(err) assert.Equal(t, token.ID, token2.ID)
}
if token2.ID != token.ID {
t.Fatal()
}
tokens, err := store.GetAPITokens(user.ID) tokens, err := store.GetAPITokens(user.ID)
if err != nil { require.NoError(t, err)
t.Fatal(err) assert.Len(t, tokens, 1)
} assert.Equal(t, token.ID, tokens[0].ID)
if len(tokens) != 1 {
t.Fatal()
}
if tokens[0].ID != token.ID {
t.Fatal()
}
err = store.ExpireAPIToken(user.ID, token.ID) err = store.ExpireAPIToken(user.ID, token.ID)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
token2, err = store.GetAPIToken(token.ID) token2, err = store.GetAPIToken(token.ID)
if err != nil { require.NoError(t, err)
t.Fatal(err) assert.True(t, token2.Expired)
}
if !token2.Expired {
t.Fatal()
}
err = store.DeleteAPIToken(user.ID, token.ID) err = store.DeleteAPIToken(user.ID, token.ID)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = store.GetAPIToken(token.ID) _, err = store.GetAPIToken(token.ID)
if err == nil { assert.Error(t, err)
t.Fatal("Token not deleted")
}
} }
func TestBoltDb_GetRepositoryRefs(t *testing.T) { func TestBoltDb_GetRepositoryRefs(t *testing.T) {
@ -332,9 +220,7 @@ func TestBoltDb_GetRepositoryRefs(t *testing.T) {
GitBranch: "master", GitBranch: "master",
ProjectID: 1, ProjectID: 1,
}) })
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = store.CreateTemplate(db.Template{ _, err = store.CreateTemplate(db.Template{
Type: db.TemplateBuild, Type: db.TemplateBuild,
@ -345,9 +231,7 @@ func TestBoltDb_GetRepositoryRefs(t *testing.T) {
InventoryID: &inventoryID, InventoryID: &inventoryID,
EnvironmentID: &environmentID, EnvironmentID: &environmentID,
}) })
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
tpl2, err := store.CreateTemplate(db.Template{ tpl2, err := store.CreateTemplate(db.Template{
Type: db.TemplateBuild, Type: db.TemplateBuild,
@ -357,9 +241,7 @@ func TestBoltDb_GetRepositoryRefs(t *testing.T) {
InventoryID: &inventoryID, InventoryID: &inventoryID,
EnvironmentID: &environmentID, EnvironmentID: &environmentID,
}) })
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
_, err = store.CreateSchedule(db.Schedule{ _, err = store.CreateSchedule(db.Schedule{
CronFormat: "* * * * *", CronFormat: "* * * * *",
@ -367,17 +249,10 @@ func TestBoltDb_GetRepositoryRefs(t *testing.T) {
ProjectID: 1, ProjectID: 1,
RepositoryID: &repo1.ID, RepositoryID: &repo1.ID,
}) })
require.NoError(t, err)
if err != nil {
t.Fatal(err)
}
refs, err := store.GetRepositoryRefs(1, repo1.ID) refs, err := store.GetRepositoryRefs(1, repo1.ID)
if err != nil { require.NoError(t, err)
t.Fatal(err) assert.Len(t, refs.Templates, 1)
} assert.Len(t, refs.Schedules, 1)
if len(refs.Templates) != 2 {
t.Fatal()
}
} }

View File

@ -10,11 +10,11 @@ import (
"strings" "strings"
"github.com/Masterminds/squirrel" "github.com/Masterminds/squirrel"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/util"
"github.com/go-gorp/gorp/v3" "github.com/go-gorp/gorp/v3"
_ "github.com/go-sql-driver/mysql" // imports mysql driver _ "github.com/go-sql-driver/mysql" // imports mysql driver
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/util"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -373,6 +373,16 @@ func (d *SqlDb) getObjectRefs(projectID int, objectProps db.ObjectProps, objectI
return return
} }
refs.Schedules, err = d.getObjectRefsFrom(projectID, objectProps, objectID, db.ScheduleProps)
if err != nil {
return
}
refs.Integrations, err = d.getObjectRefsFrom(projectID, objectProps, objectID, db.IntegrationAliasProps)
if err != nil {
return
}
return return
} }