mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 20:35:24 +01:00
fix(be): refs for boltdb
This commit is contained in:
parent
05dd7c5653
commit
02a631fbd6
@ -511,19 +511,57 @@ func (d *BoltDb) getObjectRefs(projectID int, objectProps db.ObjectProps, object
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//refs.Schedules, err = d.getObjectRefsFrom(projectID, objectProps, intObjectID(objectID), db.ScheduleProps)
|
templates, err := d.getObjectRefsFrom(projectID, objectProps, intObjectID(objectID), db.ScheduleProps)
|
||||||
|
|
||||||
|
for _, st := range templates {
|
||||||
|
exists := false
|
||||||
|
for _, tpl := range refs.Templates {
|
||||||
|
if tpl.ID == st.ID {
|
||||||
|
exists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
refs.Templates = append(refs.Templates, st)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *BoltDb) getObjectRefsFrom(projectID int, objProps db.ObjectProps, objID objectID, referringObjectProps db.ObjectProps) (referringObjs []db.ObjectReferrer, err error) {
|
func (d *BoltDb) getObjectRefsFrom(projectID int, objProps db.ObjectProps, objID objectID, referringObjectProps db.ObjectProps) (referringObjs []db.ObjectReferrer, err error) {
|
||||||
|
referringObjs = make([]db.ObjectReferrer, 0)
|
||||||
_, err = objProps.GetReferringFieldsFrom(referringObjectProps.Type)
|
_, err = objProps.GetReferringFieldsFrom(referringObjectProps.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
referringObjects := reflect.New(reflect.SliceOf(referringObjectProps.Type))
|
var referringObjects reflect.Value
|
||||||
|
|
||||||
|
if referringObjectProps.Type == db.ScheduleProps.Type {
|
||||||
|
schedules := make([]db.Schedule, 0)
|
||||||
|
err = d.getObjects(projectID, db.ScheduleProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool {
|
||||||
|
return isObjectReferredBy(objProps, objID, referringObj)
|
||||||
|
}, &schedules)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, schedule := range schedules {
|
||||||
|
var template db.Template
|
||||||
|
template, err = d.GetTemplate(projectID, schedule.TemplateID)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
referringObjs = append(referringObjs, db.ObjectReferrer{
|
||||||
|
ID: template.ID,
|
||||||
|
Name: template.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
referringObjects = reflect.New(reflect.SliceOf(referringObjectProps.Type))
|
||||||
err = d.getObjects(projectID, referringObjectProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool {
|
err = d.getObjects(projectID, referringObjectProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool {
|
||||||
return isObjectReferredBy(objProps, objID, referringObj)
|
return isObjectReferredBy(objProps, objID, referringObj)
|
||||||
}, referringObjects.Interface())
|
}, referringObjects.Interface())
|
||||||
@ -538,6 +576,7 @@ func (d *BoltDb) getObjectRefsFrom(projectID int, objProps db.ObjectProps, objID
|
|||||||
Name: referringObjects.Elem().Index(i).FieldByName("Name").String(),
|
Name: referringObjects.Elem().Index(i).FieldByName("Name").String(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -357,31 +357,52 @@ func TestBoltDb_GetRepositoryRefs(t *testing.T) {
|
|||||||
|
|
||||||
repo1, err := store.CreateRepository(db.Repository{
|
repo1, err := store.CreateRepository(db.Repository{
|
||||||
Name: "repo1",
|
Name: "repo1",
|
||||||
ProjectID: 1,
|
|
||||||
GitURL: "git@example.com/repo1",
|
GitURL: "git@example.com/repo1",
|
||||||
GitBranch: "master",
|
GitBranch: "master",
|
||||||
|
ProjectID: 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = store.CreateTemplate(db.Template{
|
_, err = store.CreateTemplate(db.Template{
|
||||||
ProjectID: 1,
|
|
||||||
Type: db.TemplateBuild,
|
Type: db.TemplateBuild,
|
||||||
Name: "tpl1",
|
Name: "tpl1",
|
||||||
Playbook: "build.yml",
|
Playbook: "build.yml",
|
||||||
RepositoryID: repo1.ID,
|
RepositoryID: repo1.ID,
|
||||||
|
ProjectID: 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tpl2, err := store.CreateTemplate(db.Template{
|
||||||
|
Type: db.TemplateBuild,
|
||||||
|
Name: "tpl12",
|
||||||
|
Playbook: "build.yml",
|
||||||
|
ProjectID: 1,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = store.CreateSchedule(db.Schedule{
|
||||||
|
CronFormat: "* * * * *",
|
||||||
|
TemplateID: tpl2.ID,
|
||||||
|
ProjectID: 1,
|
||||||
|
RepositoryID: &repo1.ID,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
refs, err := store.GetRepositoryRefs(1, repo1.ID)
|
refs, err := store.GetRepositoryRefs(1, repo1.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(refs.Templates) != 1 {
|
if len(refs.Templates) != 2 {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ func TestMigration_2_8_40_Apply(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = r.Put([]byte("0000000001"),
|
err = r.Put([]byte("0000000001"),
|
||||||
[]byte("{\"id\":\"1\",\"project_id\":\"1\",\"name\": \"test123\"}"))
|
[]byte("{\"id\":\"1\",\"project_id\":\"1\",\"alias\": \"test123\"}"))
|
||||||
|
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
@ -54,8 +54,8 @@ func TestMigration_2_8_40_Apply(t *testing.T) {
|
|||||||
t.Fatal("invalid name")
|
t.Fatal("invalid name")
|
||||||
}
|
}
|
||||||
|
|
||||||
if repo["name"] != nil {
|
if repo["alias"] != nil {
|
||||||
t.Fatal("name must be deleted")
|
t.Fatal("alias must be deleted")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,10 +369,6 @@ func (d *SqlDb) getObjectRefs(projectID int, objectProps db.ObjectProps, objectI
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *SqlDb) getObjectRefCount(projectID int, objectProps db.ObjectProps, objectID int) (int, error) {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *SqlDb) getObjectRefsFrom(
|
func (d *SqlDb) getObjectRefsFrom(
|
||||||
projectID int,
|
projectID int,
|
||||||
objectProps db.ObjectProps,
|
objectProps db.ObjectProps,
|
||||||
|
Loading…
Reference in New Issue
Block a user