diff --git a/app/api/controller/pullreq/label_assign.go b/app/api/controller/pullreq/label_assign.go index f476a5196..73a89326f 100644 --- a/app/api/controller/pullreq/label_assign.go +++ b/app/api/controller/pullreq/label_assign.go @@ -107,13 +107,15 @@ func activityPayload(out *label.AssignToPullReqOut) *types.PullRequestActivityLa } return &types.PullRequestActivityLabel{ - Label: out.Label.Key, - LabelColor: out.Label.Color, - LabelScope: out.Label.Scope, - Value: value, - ValueColor: valueColor, - OldValue: oldValue, - OldValueColor: oldValueColor, - Type: out.ActivityType, + PullRequestActivityLabelBase: types.PullRequestActivityLabelBase{ + Label: out.Label.Key, + LabelColor: out.Label.Color, + LabelScope: out.Label.Scope, + Value: value, + ValueColor: valueColor, + OldValue: oldValue, + OldValueColor: oldValueColor, + }, + Type: out.ActivityType, } } diff --git a/app/api/controller/pullreq/label_unassign.go b/app/api/controller/pullreq/label_unassign.go index 0f10bc87e..d26018806 100644 --- a/app/api/controller/pullreq/label_unassign.go +++ b/app/api/controller/pullreq/label_unassign.go @@ -61,12 +61,14 @@ func (c *Controller) UnassignLabel( color = &labelValue.Color } payload := &types.PullRequestActivityLabel{ - Label: label.Key, - LabelColor: label.Color, - LabelScope: label.Scope, - Value: value, - ValueColor: color, - Type: enum.LabelActivityUnassign, + PullRequestActivityLabelBase: types.PullRequestActivityLabelBase{ + Label: label.Key, + LabelColor: label.Color, + LabelScope: label.Scope, + Value: value, + ValueColor: color, + }, + Type: enum.LabelActivityUnassign, } if _, err := c.activityStore.CreateWithPayload( ctx, pullreq, session.Principal.ID, payload, nil); err != nil { diff --git a/app/api/controller/pullreq/pr_create.go b/app/api/controller/pullreq/pr_create.go index a80155dc5..c186c8bce 100644 --- a/app/api/controller/pullreq/pr_create.go +++ b/app/api/controller/pullreq/pr_create.go @@ -204,7 +204,9 @@ func (c *Controller) Create( return nil, fmt.Errorf("failed to prepare labels: %w", err) } - activitySeq += int64(len(labelAssignInputMap)) + if len(labelAssignInputMap) > 0 { + activitySeq++ + } err = controller.TxOptLock(ctx, c.tx, func(ctx context.Context) error { // Always re-fetch at the start of the transaction because the repo we have is from a cache. @@ -609,14 +611,37 @@ func (c *Controller) storeLabelAssignActivity( principalID int64, labelAssignOuts []*labelsvc.AssignToPullReqOut, ) { - for _, out := range labelAssignOuts { - payload := activityPayload(out) - pr.ActivitySeq++ - if _, err := c.activityStore.CreateWithPayload( - ctx, pr, principalID, payload, nil, - ); err != nil { - log.Ctx(ctx).Err(err).Msg("failed to write label assign pull req activity") + if len(labelAssignOuts) == 0 { + return + } + + pr.ActivitySeq++ + + payload := &types.PullRequestActivityLabels{ + Labels: make([]*types.PullRequestActivityLabelBase, len(labelAssignOuts)), + Type: enum.LabelActivityAssign, + } + + for i, out := range labelAssignOuts { + var value *string + var valueColor *enum.LabelColor + if out.NewLabelValue != nil { + value = &out.NewLabelValue.Value + valueColor = &out.NewLabelValue.Color } + payload.Labels[i] = &types.PullRequestActivityLabelBase{ + Label: out.Label.Key, + LabelColor: out.Label.Color, + LabelScope: out.Label.Scope, + Value: value, + ValueColor: valueColor, + } + } + + if _, err := c.activityStore.CreateWithPayload( + ctx, pr, principalID, payload, nil, + ); err != nil { + log.Ctx(ctx).Err(err).Msg("failed to write label assign pull req activity") } } diff --git a/types/pullreq_activity_payload.go b/types/pullreq_activity_payload.go index f589033b7..8c00f9852 100644 --- a/types/pullreq_activity_payload.go +++ b/types/pullreq_activity_payload.go @@ -179,17 +179,29 @@ func (a *PullRequestActivityPayloadBranchRestore) ActivityType() enum.PullReqAct return enum.PullReqActivityTypeBranchRestore } +type PullRequestActivityLabelBase struct { + Label string `json:"label"` + LabelColor enum.LabelColor `json:"label_color"` + LabelScope int64 `json:"label_scope"` + Value *string `json:"value,omitempty"` + ValueColor *enum.LabelColor `json:"value_color,omitempty"` + OldValue *string `json:"old_value,omitempty"` + OldValueColor *enum.LabelColor `json:"old_value_color,omitempty"` +} type PullRequestActivityLabel struct { - Label string `json:"label"` - LabelColor enum.LabelColor `json:"label_color"` - LabelScope int64 `json:"label_scope"` - Value *string `json:"value,omitempty"` - ValueColor *enum.LabelColor `json:"value_color,omitempty"` - OldValue *string `json:"old_value,omitempty"` - OldValueColor *enum.LabelColor `json:"old_value_color,omitempty"` - Type enum.PullReqLabelActivityType `json:"type"` + PullRequestActivityLabelBase + Type enum.PullReqLabelActivityType `json:"type"` } func (a *PullRequestActivityLabel) ActivityType() enum.PullReqActivityType { return enum.PullReqActivityTypeLabelModify } + +type PullRequestActivityLabels struct { + Type enum.PullReqLabelActivityType `json:"type"` + Labels []*PullRequestActivityLabelBase `json:"labels"` +} + +func (a *PullRequestActivityLabels) ActivityType() enum.PullReqActivityType { + return enum.PullReqActivityTypeLabelModify +}