Skip to content

Commit

Permalink
update 320 ua
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <[email protected]>
  • Loading branch information
PetrusZ committed Oct 30, 2024
1 parent 4acd20e commit 8e4a473
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.
5 changes: 5 additions & 0 deletions pkg/cli/initconfig/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import (
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb/template"
vmcommonrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb/vm"
statrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/stat/repository/mongodb"
"github.com/koderover/zadig/v2/pkg/shared/handler"
systemrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/system/repository/mongodb"
systemservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/system/service"
templateservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/templatestore/service"
sprintservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/sprint_management/service"
workflowservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/workflow/service/workflow"
configmongodb "github.com/koderover/zadig/v2/pkg/microservice/systemconfig/core/email/repository/mongodb"
userdb "github.com/koderover/zadig/v2/pkg/microservice/user/core/repository/mongodb"
Expand Down Expand Up @@ -252,6 +254,9 @@ func initSystemData() error {

templateservice.InitWorkflowTemplate()

ctx := handler.NewBackgroupContext()
sprintservice.InitAllProjectSprintTemplate(ctx)

// update offical plugins
workflowservice.UpdateOfficalPluginRepository(log.SugaredLogger())

Expand Down
53 changes: 30 additions & 23 deletions pkg/cli/upgradeassistant/cmd/migrate/320.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"gorm.io/gorm"

"github.com/koderover/zadig/v2/pkg/cli/upgradeassistant/internal/upgradepath"
templaterepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb/template"
sprintservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/sprint_management/service"
"github.com/koderover/zadig/v2/pkg/microservice/user/core/repository"
usermodels "github.com/koderover/zadig/v2/pkg/microservice/user/core/repository/models"
"github.com/koderover/zadig/v2/pkg/shared/handler"
Expand All @@ -38,10 +36,6 @@ func init() {
func V310ToV320() error {
ctx := handler.NewBackgroupContext()
ctx.Logger.Infof("-------- start init existed project's sprint template --------")
if err := initProjectSprintTemplate(ctx); err != nil {
ctx.Logger.Errorf("failed to init project sprint template, error: %s", err)
return err
}
if err := addGetSprintActionForReadOnlyRole(ctx); err != nil {
ctx.Logger.Errorf("failed to add get_sprint action for read-only role, error: %s", err)
return err
Expand All @@ -54,21 +48,6 @@ func V320ToV310() error {
return nil
}

func initProjectSprintTemplate(ctx *handler.Context) error {
projects, err := templaterepo.NewProductColl().List()
if err != nil {
err = fmt.Errorf("failed to list project list, error: %s", err)
ctx.Logger.Error(err)
return err
}

for _, project := range projects {
sprintservice.InitSprintTemplate(ctx, project.ProjectName)
}

return nil
}

func addGetSprintActionForReadOnlyRole(ctx *handler.Context) error {
action := &usermodels.Action{}
err := repository.DB.Where("action = ? AND resource = ?", "get_sprint", "SprintManagement").First(&action).Error
Expand All @@ -88,14 +67,42 @@ func addGetSprintActionForReadOnlyRole(ctx *handler.Context) error {
return nil
}

roleBindings := []*usermodels.RoleActionBinding{}
updateRoleIDs := []uint{}
for _, role := range roles {
roleActionBingding := []*usermodels.RoleActionBinding{}
err = repository.DB.Where("role_id = ?", role.ID).Find(&roleActionBingding).Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
err = fmt.Errorf("failed to list roleActionBingding, err: %v", err)
ctx.Logger.Error(err)
return err
}

found := false
for _, binding := range roleActionBingding {
if binding.ActionID == action.ID {
found = true
break
}

}

if !found {
updateRoleIDs = append(updateRoleIDs, role.ID)
}
}

roleBindings := []*usermodels.RoleActionBinding{}
for _, id := range updateRoleIDs {
roleBindings = append(roleBindings, &usermodels.RoleActionBinding{
RoleID: role.ID,
RoleID: id,
ActionID: action.ID,
})
}

if len(roleBindings) == 0 {
return nil
}

err = repository.DB.Create(roleBindings).Error
if err != nil {
return fmt.Errorf("failed to create roleBingdings, err: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ func (c *SprintTemplateColl) Update(ctx *handler.Context, obj *models.SprintTemp
}

func (c *SprintTemplateColl) UpsertByName(ctx *handler.Context, obj *models.SprintTemplate) error {
query := bson.M{"name": obj.Name}
change := bson.M{"$set": obj}
obj.UpdateTime = time.Now().Unix()
obj.UpdatedBy = ctx.GenUserBriefInfo()
query := bson.M{
"project_name": obj.ProjectName,
"name": obj.Name,
}
change := bson.M{"$set": obj}
_, err := c.UpdateOne(mongotool.SessionContext(ctx, c.Session), query, change, options.Update().SetUpsert(true))
return err
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/microservice/aslan/core/sprint_management/service/sprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package service

import (
"fmt"
"time"

"github.com/pkg/errors"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb"
templaterepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb/template"
"github.com/koderover/zadig/v2/pkg/shared/handler"
e "github.com/koderover/zadig/v2/pkg/tool/errors"
"github.com/koderover/zadig/v2/pkg/types"
Expand Down Expand Up @@ -349,3 +351,18 @@ func ListSprint(ctx *handler.Context, opt *ListSprintOption) (*ListSprintResp, e
Total: total,
}, nil
}

func InitAllProjectSprintTemplate(ctx *handler.Context) {
projects, err := templaterepo.NewProductColl().List()
if err != nil {
err = fmt.Errorf("failed to list project list, error: %s", err)
ctx.Logger.Error(err)
return
}

for _, project := range projects {
InitSprintTemplate(ctx, project.ProjectName)
}

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,24 @@ type ListSprintTemplateOption struct {
}

type ListSprintTemplateResp struct {
List []*models.SprintTemplate `json:"list"`
Total int64 `json:"total"`
List []*models.SprintTemplate `json:"list"`
// Total int64 `json:"total"`
}

func ListSprintTemplate(ctx *handler.Context, opt *ListSprintTemplateOption) (*ListSprintTemplateResp, error) {
var (
list []*commonmodels.SprintTemplate
total int64
err error
list []*commonmodels.SprintTemplate
// total int64
err error
)
list, err = mongodb.NewSprintTemplateColl().List(ctx, &mongodb.SprintTemplateListOption{ProjectName: opt.ProjectName})
if err != nil {
return nil, e.ErrListSprintTemplate.AddErr(errors.Wrap(err, "ListSprintTemplate"))
}

return &ListSprintTemplateResp{
List: list,
Total: total,
List: list,
// Total: total,
}, nil
}

Expand Down

0 comments on commit 8e4a473

Please sign in to comment.