feat: board batch clone (#1861)

* feat: sync user in ldap to mysql

* feat:board batch clone

* Revert "feat: sync user in ldap to mysql"

This reverts commit 6063c34f0ee400ad094756cddc08f79f9f9e2ef9.

* fix: use transactions to store board and payload

* fix: the busigroup is incorrectly specified

* chore: adjust import order of pkg

* refactor: batch clone the board codes

* fix: set value='' in reterr if err==nil

* refactor: move AtomicAdd to board.go

* chore: adjust import order of pkg

* chore: adjust import order of pkg
This commit is contained in:
Deke Wang 2024-02-18 10:03:08 +08:00 committed by GitHub
parent 2e6cb0f21d
commit 53ada6cc40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 74 additions and 12 deletions

View File

@ -273,6 +273,7 @@ func (rt *Router) Config(r *gin.Engine) {
pages.GET("/busi-group/:id/boards", rt.auth(), rt.user(), rt.perm("/dashboards"), rt.bgro(), rt.boardGets)
pages.POST("/busi-group/:id/boards", rt.auth(), rt.user(), rt.perm("/dashboards/add"), rt.bgrw(), rt.boardAdd)
pages.POST("/busi-group/:id/board/:bid/clone", rt.auth(), rt.user(), rt.perm("/dashboards/add"), rt.bgrw(), rt.boardClone)
pages.POST("/busi-group/:id/boards/clones", rt.auth(), rt.user(), rt.perm("/dashboards/add"), rt.boardBatchClone)
pages.GET("/board/:bid", rt.boardGet)
pages.GET("/board/:bid/pure", rt.boardPureGet)

View File

@ -7,8 +7,8 @@ import (
"github.com/ccfos/nightingale/v6/models"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/toolkits/pkg/ginx"
"github.com/toolkits/pkg/i18n"
"github.com/toolkits/pkg/str"
)
@ -294,17 +294,7 @@ func (rt *Router) boardClone(c *gin.Context) {
me := c.MustGet("user").(*models.User)
bo := rt.Board(ginx.UrlParamInt64(c, "bid"))
newBoard := &models.Board{
Name: bo.Name + " Cloned",
Tags: bo.Tags,
GroupId: bo.GroupId,
CreateBy: me.Username,
UpdateBy: me.Username,
}
if bo.Ident != "" {
newBoard.Ident = uuid.NewString()
}
newBoard := bo.Clone(me.Username, bo.GroupId)
ginx.Dangerous(newBoard.Add(rt.Ctx))
@ -318,3 +308,37 @@ func (rt *Router) boardClone(c *gin.Context) {
ginx.NewRender(c).Message(nil)
}
type boardsForm struct {
BoardIds []int64 `json:"board_ids"`
}
func (rt *Router) boardBatchClone(c *gin.Context) {
me := c.MustGet("user").(*models.User)
bgid := ginx.UrlParamInt64(c, "id")
rt.bgrwCheck(c, bgid)
var f boardsForm
ginx.BindJSON(c, &f)
reterr := make(map[string]string, len(f.BoardIds))
lang := c.GetHeader("X-Language")
for _, bid := range f.BoardIds {
bo := rt.Board(bid)
newBoard := bo.Clone(me.Username, bgid)
payload, err := models.BoardPayloadGet(rt.Ctx, bo.Id)
if err != nil {
reterr[newBoard.Name] = i18n.Sprintf(lang, err.Error())
continue
}
if err = newBoard.AtomicAdd(rt.Ctx, payload); err != nil {
reterr[newBoard.Name] = i18n.Sprintf(lang, err.Error())
} else {
reterr[newBoard.Name] = ""
}
}
ginx.NewRender(c).Data(reterr, nil)
}

View File

@ -5,6 +5,8 @@ import (
"time"
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/toolkits/pkg/str"
"gorm.io/gorm"
@ -54,6 +56,22 @@ func (b *Board) Verify() error {
return nil
}
func (b *Board) Clone(operatorName string, newBgid int64) *Board {
clone := &Board{
Name: b.Name + " Cloned",
Tags: b.Tags,
GroupId: newBgid,
CreateBy: operatorName,
UpdateBy: operatorName,
}
if b.Ident != "" {
clone.Ident = uuid.NewString()
}
return clone
}
func (b *Board) CanRenameIdent(ctx *ctx.Context, ident string) (bool, error) {
if ident == "" {
return true, nil
@ -91,6 +109,25 @@ func (b *Board) Add(ctx *ctx.Context) error {
return Insert(ctx, b)
}
func (b *Board) AtomicAdd(c *ctx.Context, payload string) error {
return DB(c).Transaction(func(tx *gorm.DB) error {
tCtx := &ctx.Context{
DB: tx,
}
if err := b.Add(tCtx); err != nil {
return err
}
if payload != "" {
if err := BoardPayloadSave(tCtx, b.Id, payload); err != nil {
return err
}
}
return nil
})
}
func (b *Board) Update(ctx *ctx.Context, selectField interface{}, selectFields ...interface{}) error {
if err := b.Verify(); err != nil {
return err