Skip to content

Commit c209b83

Browse files
refactor: simplify release plan version semantics
Signed-off-by: huanghongbo-hhb <huanghongbo@koderover.com>
1 parent 0a4301b commit c209b83

9 files changed

Lines changed: 69 additions & 301 deletions

File tree

pkg/microservice/aslan/core/common/repository/models/release_plan.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,18 @@ type WorkflowReleaseJobSpec struct {
121121
}
122122

123123
type ReleasePlanLog struct {
124-
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
125-
PlanID string `bson:"plan_id" json:"plan_id"`
126-
SessionID string `bson:"session_id,omitempty" json:"session_id,omitempty"`
127-
Username string `bson:"username" json:"username"`
128-
Account string `bson:"account" json:"account"`
129-
Verb string `bson:"verb" json:"verb"`
130-
TargetName string `bson:"target_name" json:"target_name"`
131-
TargetType string `bson:"target_type" json:"target_type"`
132-
Before interface{} `bson:"before" json:"before"`
133-
After interface{} `bson:"after" json:"after"`
134-
Detail string `bson:"detail" json:"detail"`
135-
FromVersion int64 `bson:"from_version,omitempty" json:"from_version,omitempty"`
136-
ToVersion int64 `bson:"to_version,omitempty" json:"to_version,omitempty"`
137-
CreatedAt int64 `bson:"created_at" json:"created_at"`
124+
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
125+
PlanID string `bson:"plan_id" json:"plan_id"`
126+
Username string `bson:"username" json:"username"`
127+
Account string `bson:"account" json:"account"`
128+
Verb string `bson:"verb" json:"verb"`
129+
TargetName string `bson:"target_name" json:"target_name"`
130+
TargetType string `bson:"target_type" json:"target_type"`
131+
Before interface{} `bson:"before" json:"before"`
132+
After interface{} `bson:"after" json:"after"`
133+
Detail string `bson:"detail" json:"detail"`
134+
Version int64 `bson:"version,omitempty" json:"version,omitempty"`
135+
CreatedAt int64 `bson:"created_at" json:"created_at"`
138136
}
139137

140138
func (ReleasePlanLog) TableName() string {
@@ -144,7 +142,6 @@ func (ReleasePlanLog) TableName() string {
144142
type ReleasePlanVersion struct {
145143
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
146144
PlanID string `bson:"plan_id" json:"plan_id"`
147-
BaseVersion int64 `bson:"base_version,omitempty" json:"base_version,omitempty"`
148145
Version int64 `bson:"version" json:"version"`
149146
Operator string `bson:"operator" json:"operator"`
150147
Account string `bson:"account" json:"account"`

pkg/microservice/aslan/core/common/repository/mongodb/release_plan_log.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ func (c *ReleasePlanLogColl) EnsureIndex(ctx context.Context) error {
5252
{
5353
Keys: bson.D{{Key: "plan_id", Value: 1}, {Key: "created_at", Value: -1}},
5454
},
55-
{
56-
Keys: bson.D{{Key: "session_id", Value: 1}},
57-
},
5855
}
5956

6057
_, err := c.Indexes().CreateMany(ctx, mod, mongotool.CreateIndexOptions(ctx))
@@ -104,42 +101,3 @@ func (c *ReleasePlanLogColl) ListByOptions(opt *ListReleasePlanLogOption) ([]*mo
104101

105102
return resp, nil
106103
}
107-
108-
func (c *ReleasePlanLogColl) FillVersionsBySessionID(planID, sessionID string, fromVersion, toVersion int64) error {
109-
if sessionID == "" {
110-
return errors.New("empty session id")
111-
}
112-
113-
query := bson.M{
114-
"plan_id": planID,
115-
"session_id": sessionID,
116-
"$or": []bson.M{
117-
{"to_version": bson.M{"$exists": false}},
118-
{"to_version": 0},
119-
},
120-
}
121-
change := bson.M{"$set": bson.M{
122-
"from_version": fromVersion,
123-
"to_version": toVersion,
124-
}}
125-
126-
_, err := c.UpdateMany(context.Background(), query, change)
127-
return err
128-
}
129-
130-
func (c *ReleasePlanLogColl) CountPendingBySessionID(planID, sessionID string) (int64, error) {
131-
if sessionID == "" {
132-
return 0, errors.New("empty session id")
133-
}
134-
135-
query := bson.M{
136-
"plan_id": planID,
137-
"session_id": sessionID,
138-
"$or": []bson.M{
139-
{"to_version": bson.M{"$exists": false}},
140-
{"to_version": 0},
141-
},
142-
}
143-
144-
return c.CountDocuments(context.Background(), query)
145-
}

pkg/microservice/aslan/core/release_plan/handler/router.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ func (*Router) Inject(router *gin.RouterGroup) {
3131
v1.GET("/:id/collaboration/editors", GetReleasePlanCollaborationEditors)
3232
v1.GET("/:id/collaboration/ws", ReleasePlanCollaborationWS)
3333
v1.PUT("/:id", UpdateReleasePlan)
34-
v1.POST("/:id/versions/commit", CommitReleasePlanVersion)
35-
v1.GET("/:id/versions/:fromVersion/:toVersion/diff", GetReleasePlanVersionDiff)
34+
v1.GET("/:id/versions/:version/diff", GetReleasePlanVersionDiff)
3635
v1.GET("/:id/job/:jobID", GetReleasePlanJobDetail)
3736
v1.DELETE("/:id", DeleteReleasePlan)
3837

pkg/microservice/aslan/core/release_plan/service/collaboration.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ type ReleasePlanEditingSession struct {
6969
SectionType string `json:"section_type"`
7070
SectionName string `json:"section_name"`
7171
BaseVersion int64 `json:"base_version"`
72-
BaseSnapshot string `json:"base_snapshot,omitempty"`
7372
EditingStartedAt int64 `json:"editing_started_at"`
7473
LastHeartbeatAt int64 `json:"last_heartbeat_at"`
7574
}
@@ -291,7 +290,6 @@ func listActiveReleasePlanEditingSessions(planID string) ([]*ReleasePlanEditingS
291290
if session.PlanID != planID {
292291
continue
293292
}
294-
session.BaseSnapshot = ""
295293
resp = append(resp, session)
296294
}
297295

@@ -393,17 +391,6 @@ func OpenReleasePlanCollaborationWS(gCtx *gin.Context, ctx *handler.Context, pla
393391
return openReleasePlanCollaborationWS(gCtx, ctx, planID)
394392
}
395393

396-
func releasePlanSnapshotString(plan *models.ReleasePlan, sectionKey string) string {
397-
if plan == nil {
398-
return ""
399-
}
400-
sectionSnapshot, err := buildReleasePlanVersionSnapshot(plan, sectionKey)
401-
if err != nil {
402-
return ""
403-
}
404-
return encodeReleasePlanVersionSnapshot(sectionSnapshot)
405-
}
406-
407394
func openReleasePlanCollaborationWS(gCtx *gin.Context, ctx *handler.Context, planID string) error {
408395
ws, err := upgrader.Upgrade(gCtx.Writer, gCtx.Request, nil)
409396
if err != nil {
@@ -462,22 +449,21 @@ func openReleasePlanCollaborationWS(gCtx *gin.Context, ctx *handler.Context, pla
462449
SectionType: msg.SectionType,
463450
SectionName: msg.SectionName,
464451
BaseVersion: msg.BaseVersion,
465-
BaseSnapshot: releasePlanSnapshotString(plan, msg.SectionKey),
466452
EditingStartedAt: time.Now().Unix(),
467453
}
468454
if existingSession != nil {
469455
session.EditingStartedAt = existingSession.EditingStartedAt
470-
if existingSession.BaseSnapshot != "" {
471-
session.BaseSnapshot = existingSession.BaseSnapshot
472-
}
473456
if session.BaseVersion == 0 {
474457
session.BaseVersion = existingSession.BaseVersion
475458
}
476459
if existingSession.SectionKey != "" && existingSession.SectionKey != msg.SectionKey {
477460
session.EditingStartedAt = time.Now().Unix()
478-
session.BaseSnapshot = releasePlanSnapshotString(plan, msg.SectionKey)
461+
session.BaseVersion = 0
479462
}
480463
}
464+
if session.BaseVersion == 0 {
465+
session.BaseVersion = plan.Version
466+
}
481467
if err := persistReleasePlanEditingSession(session); err != nil {
482468
queueCollaborationClientMessage(client, &releasePlanCollabWSOutbound{Type: "error", Error: err.Error()})
483469
continue

pkg/microservice/aslan/core/release_plan/service/diff.go

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ const (
3636
)
3737

3838
type ReleasePlanVersionDiffResponse struct {
39-
PlanID string `json:"plan_id"`
40-
FromVersion int64 `json:"from_version"`
41-
ToVersion int64 `json:"to_version"`
42-
Groups []*ReleasePlanVersionDiffGroup `json:"groups"`
39+
PlanID string `json:"plan_id"`
40+
Version int64 `json:"version"`
41+
PreviousVersion int64 `json:"previous_version"`
42+
Groups []*ReleasePlanVersionDiffGroup `json:"groups"`
4343
}
4444

4545
type ReleasePlanVersionDiffGroup struct {
@@ -119,42 +119,22 @@ var releasePlanFieldLabels = map[string]string{
119119
"workwx_approval": "企业微信审批",
120120
}
121121

122-
func GetReleasePlanVersionDiff(planID string, fromVersion, toVersion int64) (*ReleasePlanVersionDiffResponse, error) {
123-
to, err := mongodb.NewReleasePlanVersionColl().Get(planID, toVersion)
122+
func GetReleasePlanVersionDiff(planID string, version int64) (*ReleasePlanVersionDiffResponse, error) {
123+
current, err := mongodb.NewReleasePlanVersionColl().Get(planID, version)
124124
if err != nil {
125-
return nil, errors.Wrap(err, "get to version")
125+
return nil, errors.Wrap(err, "get version")
126126
}
127127

128-
var fromData map[string]interface{}
129-
var toData map[string]interface{}
130-
groupKey, groupName, groupType := releasePlanVersionDiffGroup(to.SectionKey, to.SectionName)
131-
132-
if to.BaseVersion == fromVersion {
133-
fromData, err = toGenericMap(to.BaseSnapshot)
134-
if err != nil {
135-
return nil, errors.Wrap(err, "convert base snapshot")
136-
}
137-
toData, err = toGenericMap(to.Snapshot)
138-
if err != nil {
139-
return nil, errors.Wrap(err, "convert current snapshot")
140-
}
141-
} else {
142-
if fromVersion == 0 {
143-
return nil, errors.Errorf("release plan baseline diff v0 -> v%d is not available after subsequent version commits", toVersion)
144-
}
145-
from, err := mongodb.NewReleasePlanVersionColl().Get(planID, fromVersion)
146-
if err != nil {
147-
return nil, errors.Wrap(err, "get from version")
148-
}
149-
fromData, err = toGenericMap(from.Snapshot)
150-
if err != nil {
151-
return nil, errors.Wrap(err, "convert from snapshot")
152-
}
153-
toData, err = toGenericMap(to.Snapshot)
154-
if err != nil {
155-
return nil, errors.Wrap(err, "convert to snapshot")
156-
}
128+
fromData, err := toGenericMap(current.BaseSnapshot)
129+
if err != nil {
130+
return nil, errors.Wrap(err, "convert base snapshot")
157131
}
132+
toData, err := toGenericMap(current.Snapshot)
133+
if err != nil {
134+
return nil, errors.Wrap(err, "convert current snapshot")
135+
}
136+
137+
groupKey, groupName, groupType := releasePlanVersionDiffGroup(current.SectionKey, current.SectionName)
158138

159139
rawEntries := make([]*releasePlanRawDiffEntry, 0)
160140
diffReleasePlanValues("", fromData, toData, &rawEntries)
@@ -206,13 +186,20 @@ func GetReleasePlanVersionDiff(planID string, fromVersion, toVersion int64) (*Re
206186
}
207187

208188
return &ReleasePlanVersionDiffResponse{
209-
PlanID: planID,
210-
FromVersion: fromVersion,
211-
ToVersion: toVersion,
212-
Groups: groups,
189+
PlanID: planID,
190+
Version: version,
191+
PreviousVersion: previousReleasePlanVersion(version),
192+
Groups: groups,
213193
}, nil
214194
}
215195

196+
func previousReleasePlanVersion(version int64) int64 {
197+
if version <= 1 {
198+
return 0
199+
}
200+
return version - 1
201+
}
202+
216203
func toGenericMap(value interface{}) (map[string]interface{}, error) {
217204
if value == nil {
218205
return map[string]interface{}{}, nil
@@ -502,15 +489,6 @@ func classifyReleasePlanDiffTask(path string) (taskName, taskType string) {
502489
return
503490
}
504491

505-
func firstReleasePlanBracketSegment(path, prefix string) string {
506-
for _, segment := range strings.Split(path, ".") {
507-
if strings.HasPrefix(segment, prefix+"[") {
508-
return segment
509-
}
510-
}
511-
return prefix
512-
}
513-
514492
func releasePlanBracketSegments(path, prefix string) []string {
515493
resp := make([]string, 0)
516494
for _, segment := range strings.Split(path, ".") {

pkg/microservice/aslan/core/release_plan/service/openapi.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func OpenAPICreateReleasePlan(c *handler.Context, rawArgs *OpenAPICreateReleaseP
169169
go func() {
170170
sectionSnapshot, err := buildReleasePlanInputSnapshot(args)
171171
if err == nil {
172-
err = createReleasePlanVersion(planID, 0, 1, nil, sectionSnapshot, c.UserName, c.Account, releasePlanVersionSectionPlan, releasePlanVersionSectionName(releasePlanVersionSectionPlan, args.Name), VerbCreate)
172+
err = createReleasePlanVersion(planID, 1, nil, sectionSnapshot, c.UserName, c.Account, releasePlanVersionSectionPlan, releasePlanVersionSectionName(releasePlanVersionSectionPlan, args.Name), VerbCreate)
173173
}
174174
if err != nil {
175175
log.Errorf("create release plan version error: %v", err)
@@ -181,7 +181,7 @@ func OpenAPICreateReleasePlan(c *handler.Context, rawArgs *OpenAPICreateReleaseP
181181
Verb: VerbCreate,
182182
TargetName: args.Name,
183183
TargetType: TargetTypeReleasePlan,
184-
ToVersion: 1,
184+
Version: 1,
185185
CreatedAt: time.Now().Unix(),
186186
}); err != nil {
187187
log.Errorf("create release plan log error: %v", err)
@@ -225,6 +225,10 @@ type OpenAPIWorkflowReleaseJobSpec struct {
225225
}
226226

227227
func OpenAPICreateReleasePlanWithJobs(c *handler.Context, id string, rawArgs *OpenAPIUpdateReleasePlanWithJobsArgs) error {
228+
approveLock := getLock(id)
229+
approveLock.Lock()
230+
defer approveLock.Unlock()
231+
228232
plan, err := mongodb.NewReleasePlanColl().GetByID(context.Background(), id)
229233
if err != nil {
230234
return errors.Wrap(err, "get release plan error")
@@ -374,11 +378,7 @@ func OpenAPICreateReleasePlanWithJobs(c *handler.Context, id string, rawArgs *Op
374378
}
375379

376380
plan.Jobs = newJobs
377-
fromVersion, err := ensureReleasePlanBaselineVersion(c, id, plan)
378-
if err != nil {
379-
return errors.Wrap(err, "ensure release plan baseline version")
380-
}
381-
plan.Version = fromVersion + 1
381+
plan.Version = originalPlan.Version + 1
382382

383383
err = mongodb.NewReleasePlanColl().UpdateByID(c, id, plan)
384384
if err != nil {
@@ -393,19 +393,18 @@ func OpenAPICreateReleasePlanWithJobs(c *handler.Context, id string, rawArgs *Op
393393
if err != nil {
394394
return errors.Wrap(err, "build release plan current snapshot")
395395
}
396-
if err := createReleasePlanVersion(plan.ID.Hex(), fromVersion, plan.Version, baseSnapshot, currentSnapshot, c.UserName, c.Account, releasePlanVersionSectionPlan, releasePlanVersionSectionName(releasePlanVersionSectionPlan, plan.Name), VerbUpdate); err != nil {
396+
if err := createReleasePlanVersion(plan.ID.Hex(), plan.Version, baseSnapshot, currentSnapshot, c.UserName, c.Account, releasePlanVersionSectionPlan, releasePlanVersionSectionName(releasePlanVersionSectionPlan, plan.Name), VerbUpdate); err != nil {
397397
log.Errorf("create release plan version error: %v", err)
398398
}
399399
if err := createReleasePlanLog(&models.ReleasePlanLog{
400-
PlanID: plan.ID.Hex(),
401-
Username: c.UserName,
402-
Account: c.Account,
403-
Verb: VerbUpdate,
404-
TargetName: plan.Name,
405-
TargetType: TargetTypeReleasePlan,
406-
FromVersion: fromVersion,
407-
ToVersion: plan.Version,
408-
CreatedAt: time.Now().Unix(),
400+
PlanID: plan.ID.Hex(),
401+
Username: c.UserName,
402+
Account: c.Account,
403+
Verb: VerbUpdate,
404+
TargetName: plan.Name,
405+
TargetType: TargetTypeReleasePlan,
406+
Version: plan.Version,
407+
CreatedAt: time.Now().Unix(),
409408
}); err != nil {
410409
log.Errorf("create release plan log error: %v", err)
411410
}

0 commit comments

Comments
 (0)