Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/cli/initconfig/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func createOrUpdateMongodbIndex(ctx context.Context) {
commonrepo.NewLLMIntegrationColl(),
commonrepo.NewReleasePlanColl(),
commonrepo.NewReleasePlanLogColl(),
commonrepo.NewReleasePlanVersionColl(),
commonrepo.NewEnvServiceVersionColl(),
commonrepo.NewLabelColl(),
commonrepo.NewSprintTemplateColl(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type ReleasePlan struct {
ID primitive.ObjectID `bson:"_id,omitempty" yaml:"-" json:"id"`
Index int64 `bson:"index" yaml:"index" json:"index"`
Version int64 `bson:"version" yaml:"version" json:"version"`
Name string `bson:"name" yaml:"name" json:"name"`
Manager string `bson:"manager" yaml:"manager" json:"manager"`
// ManagerID is the user id of the manager
Expand Down Expand Up @@ -130,9 +131,28 @@ type ReleasePlanLog struct {
Before interface{} `bson:"before" json:"before"`
After interface{} `bson:"after" json:"after"`
Detail string `bson:"detail" json:"detail"`
Version int64 `bson:"version,omitempty" json:"version,omitempty"`
CreatedAt int64 `bson:"created_at" json:"created_at"`
}

func (ReleasePlanLog) TableName() string {
return "release_plan_log"
}

type ReleasePlanVersion struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
PlanID string `bson:"plan_id" json:"plan_id"`
Version int64 `bson:"version" json:"version"`
Operator string `bson:"operator" json:"operator"`
Account string `bson:"account" json:"account"`
SectionKey string `bson:"section_key,omitempty" json:"section_key,omitempty"`
SectionName string `bson:"section_name,omitempty" json:"section_name,omitempty"`
Verb string `bson:"verb,omitempty" json:"verb,omitempty"`
BaseSnapshot interface{} `bson:"base_snapshot,omitempty" json:"base_snapshot,omitempty"`
Snapshot interface{} `bson:"snapshot" json:"snapshot"`
CreatedAt int64 `bson:"created_at" json:"created_at"`
}

func (ReleasePlanVersion) TableName() string {
return "release_plan_version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (c *ReleasePlanColl) EnsureIndex(ctx context.Context) error {
Keys: bson.M{"update_time": 1},
Options: options.Index().SetUnique(false),
},
{
Keys: bson.M{"version": 1},
Options: options.Index().SetUnique(false),
},
}

_, err := c.Indexes().CreateMany(ctx, mod, mongotool.CreateIndexOptions(ctx))
Expand Down Expand Up @@ -121,6 +125,35 @@ func (c *ReleasePlanColl) UpdateByID(ctx context.Context, idString string, args
return err
}

func (c *ReleasePlanColl) UpdateVersionByID(ctx context.Context, idString string, version int64) error {
id, err := primitive.ObjectIDFromHex(idString)
if err != nil {
return fmt.Errorf("invalid id")
}

query := bson.M{"_id": id}
change := bson.M{"$set": bson.M{"version": version}}
_, err = c.UpdateOne(ctx, query, change)
return err
}

func (c *ReleasePlanColl) IncrementVersionByID(ctx context.Context, idString string) (int64, error) {
id, err := primitive.ObjectIDFromHex(idString)
if err != nil {
return 0, fmt.Errorf("invalid id")
}

query := bson.M{"_id": id}
change := bson.M{"$inc": bson.M{"version": 1}}
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)

result := new(models.ReleasePlan)
if err := c.FindOneAndUpdate(ctx, query, change, opts).Decode(result); err != nil {
return 0, err
}
return result.Version, nil
}

func (c *ReleasePlanColl) DeleteByID(ctx context.Context, idString string) error {
id, err := primitive.ObjectIDFromHex(idString)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ func (c *ReleasePlanLogColl) GetCollectionName() string {
}

func (c *ReleasePlanLogColl) EnsureIndex(ctx context.Context) error {
return nil
mod := []mongo.IndexModel{
{
Keys: bson.D{{Key: "plan_id", Value: 1}, {Key: "created_at", Value: -1}},
},
}

_, err := c.Indexes().CreateMany(ctx, mod, mongotool.CreateIndexOptions(ctx))
return err
}

func (c *ReleasePlanLogColl) Create(args *models.ReleasePlanLog) error {
Expand Down Expand Up @@ -76,7 +83,7 @@ func (c *ReleasePlanLogColl) ListByOptions(opt *ListReleasePlanLogOption) ([]*mo
ctx := context.Background()
opts := options.Find()
if opt.IsSort {
opts.SetSort(bson.D{{"create_time", -1}})
opts.SetSort(bson.D{{"created_at", -1}})
}
if opt.PlanID != "" {
query["plan_id"] = opt.PlanID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2026 The KodeRover Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mongodb

import (
"context"

"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
mongotool "github.com/koderover/zadig/v2/pkg/tool/mongo"
)

type ReleasePlanVersionColl struct {
*mongo.Collection

coll string
}

func NewReleasePlanVersionColl() *ReleasePlanVersionColl {
name := models.ReleasePlanVersion{}.TableName()
return &ReleasePlanVersionColl{
Collection: mongotool.Database(config.MongoDatabase()).Collection(name),
coll: name,
}
}

func (c *ReleasePlanVersionColl) GetCollectionName() string {
return c.coll
}

func (c *ReleasePlanVersionColl) EnsureIndex(ctx context.Context) error {
mod := []mongo.IndexModel{
{
Keys: bson.D{{Key: "plan_id", Value: 1}, {Key: "version", Value: 1}},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{{Key: "plan_id", Value: 1}, {Key: "created_at", Value: -1}},
},
}

_, err := c.Indexes().CreateMany(ctx, mod, mongotool.CreateIndexOptions(ctx))
return err
}

func (c *ReleasePlanVersionColl) Create(args *models.ReleasePlanVersion) error {
if args == nil {
return errors.New("nil ReleasePlanVersion")
}

_, err := c.InsertOne(context.Background(), args)
return err
}

func (c *ReleasePlanVersionColl) Get(planID string, version int64) (*models.ReleasePlanVersion, error) {
resp := new(models.ReleasePlanVersion)
err := c.FindOne(context.Background(), bson.M{
"plan_id": planID,
"version": version,
}).Decode(resp)
return resp, err
}

func (c *ReleasePlanVersionColl) GetLatest(planID string) (*models.ReleasePlanVersion, error) {
resp := new(models.ReleasePlanVersion)
err := c.FindOne(context.Background(), bson.M{
"plan_id": planID,
}, options.FindOne().SetSort(bson.D{{Key: "version", Value: -1}})).Decode(resp)
return resp, err
}
81 changes: 81 additions & 0 deletions pkg/microservice/aslan/core/release_plan/handler/release_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package handler
import (
"fmt"
"strings"
"strconv"

"github.com/gin-gonic/gin"

Expand Down Expand Up @@ -78,6 +79,56 @@ func GetReleasePlanLogs(c *gin.Context) {
ctx.Resp, ctx.RespErr = service.GetReleasePlanLogs(c.Param("id"))
}

func GetReleasePlanCollaborationEditors(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()

if err != nil {
ctx.Logger.Errorf("failed to generate authorization info for user: %s, error: %s", ctx.UserID, err)
ctx.RespErr = fmt.Errorf("authorization Info Generation failed: err %s", err)
ctx.UnAuthorized = true
return
}

if !ctx.Resources.IsSystemAdmin && !ctx.Resources.SystemActions.ReleasePlan.View {
ctx.UnAuthorized = true
return
}

err = commonutil.CheckZadigEnterpriseLicense()
if err != nil {
ctx.RespErr = err
return
}

ctx.Resp, ctx.RespErr = service.GetReleasePlanCollaborationEditors(c.Param("id"))
}

func ReleasePlanCollaborationWS(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()

if err != nil {
ctx.Logger.Errorf("failed to generate authorization info for user: %s, error: %s", ctx.UserID, err)
ctx.RespErr = fmt.Errorf("authorization Info Generation failed: err %s", err)
ctx.UnAuthorized = true
return
}

if !ctx.Resources.IsSystemAdmin && !ctx.Resources.SystemActions.ReleasePlan.View {
ctx.UnAuthorized = true
return
}

err = commonutil.CheckZadigEnterpriseLicense()
if err != nil {
ctx.RespErr = err
return
}

ctx.RespErr = service.OpenReleasePlanCollaborationWS(c, ctx, c.Param("id"))
}

func CreateReleasePlan(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
Expand Down Expand Up @@ -189,6 +240,36 @@ func UpdateReleasePlan(c *gin.Context) {
ctx.RespErr = service.UpdateReleasePlan(ctx, c.Param("id"), req)
}

func GetReleasePlanVersionDiff(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()

if err != nil {
ctx.RespErr = fmt.Errorf("authorization Info Generation failed: err %s", err)
ctx.UnAuthorized = true
return
}

if !ctx.Resources.IsSystemAdmin && !ctx.Resources.SystemActions.ReleasePlan.View {
ctx.UnAuthorized = true
return
}

err = commonutil.CheckZadigEnterpriseLicense()
if err != nil {
ctx.RespErr = err
return
}

version, err := strconv.ParseInt(c.Param("version"), 10, 64)
if err != nil {
ctx.RespErr = e.ErrInvalidParam.AddDesc(err.Error())
return
}

ctx.Resp, ctx.RespErr = service.GetReleasePlanVersionDiff(c.Param("id"), version)
}

func GetReleasePlanJobDetail(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
Expand Down
3 changes: 3 additions & 0 deletions pkg/microservice/aslan/core/release_plan/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func (*Router) Inject(router *gin.RouterGroup) {
v1.POST("/:id/copy", CopyReleasePlan)
v1.GET("/:id", GetReleasePlan)
v1.GET("/:id/logs", GetReleasePlanLogs)
v1.GET("/:id/collaboration/editors", GetReleasePlanCollaborationEditors)
v1.GET("/:id/collaboration/ws", ReleasePlanCollaborationWS)
v1.PUT("/:id", UpdateReleasePlan)
v1.GET("/:id/versions/:version/diff", GetReleasePlanVersionDiff)
v1.GET("/:id/job/:jobID", GetReleasePlanJobDetail)
v1.DELETE("/:id", DeleteReleasePlan)

Expand Down
Loading
Loading