Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Add activity when updating issue field

天舟34ddd1610836
diff --git a/backend/api/activity.go b/backend/api/activity.go
index 6aff318d9f..5e0e0dc4cf 100644
--- a/backend/api/activity.go
+++ b/backend/api/activity.go
@@ -36,6 +36,12 @@ func (e ActivityType) String() string {
// So we annotate with json tag using camelCase naming which is consistent with normal
// json naming convention. More importantly, frontend code can simply use JSON.parse to
// convert to the expected struct there.
+type ActivityIssueFieldUpdatePayload struct {
+ FieldId IssueFieldId `json:"fieldId"`
+ OldValue string `json:"oldValue,omitempty"`
+ NewValue string `json:"newValue,omitempty"`
+}
+
type ActivityIssueStatusUpdatePayload struct {
OldStatus IssueStatus `json:"oldStatus,omitempty"`
NewStatus IssueStatus `json:"newStatus,omitempty"`
diff --git a/backend/api/issue.go b/backend/api/issue.go
index 9b28854a40..2a7339149c 100644
--- a/backend/api/issue.go
+++ b/backend/api/issue.go
@@ -53,6 +53,20 @@ func (e IssueType) String() string {
return "bb.unknown"
}
+// It has to be string type because the id for stage field contain multiple parts.
+type IssueFieldId string
+
+const (
+ IssueFieldName IssueFieldId = "1"
+ IssueFieldStatus IssueFieldId = "2"
+ IssueFieldAssignee IssueFieldId = "3"
+ IssueFieldDescription IssueFieldId = "4"
+ IssueFieldProject IssueFieldId = "5"
+ IssueFieldSubscriberList IssueFieldId = "6"
+ IssueFieldSql IssueFieldId = "7"
+ IssueFieldRollbackSql IssueFieldId = "8"
+)
+
type IssuePayload struct {
}
@@ -135,9 +149,6 @@ type IssuePatch struct {
// Value is assigned from the jwt subject field passed by the client.
UpdaterId int
- // Related fields
- ProjectId *int `jsonapi:"attr,projectId"`
-
// Domain specific fields
Name *string `jsonapi:"attr,name"`
// Status is only set manualy via IssueStatusPatch
diff --git a/backend/server/issue.go b/backend/server/issue.go
index 4399175b4f..95f9717505 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
+ "strings"
"github.com/bytebase/bytebase"
"github.com/bytebase/bytebase/api"
@@ -159,24 +160,107 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, issuePatch); err != nil {
- return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch issue request").SetInternal(err)
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted update issue request").SetInternal(err)
}
- issue, err := s.IssueService.PatchIssue(context.Background(), issuePatch)
+ issueFind := &api.IssueFind{
+ ID: &id,
+ }
+ issue, err := s.IssueService.FindIssue(context.Background(), issueFind)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Issue ID not found: %d", id))
}
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch issue ID: %v", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue ID when updating issue: %v", id)).SetInternal(err)
}
- if err := s.ComposeIssueRelationship(context.Background(), issue, c.Get(getIncludeKey()).([]string)); err != nil {
+ updatedIssue, err := s.IssueService.PatchIssue(context.Background(), issuePatch)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Issue ID not found: %d", id))
+ }
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update issue ID: %v", id)).SetInternal(err)
+ }
+
+ payloadList := [][]byte{}
+ if issuePatch.Name != nil && *issuePatch.Name != issue.Name {
+ payload, err := json.Marshal(api.ActivityIssueFieldUpdatePayload{
+ FieldId: api.IssueFieldName,
+ OldValue: issue.Name,
+ NewValue: *issuePatch.Name,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal activity after changing issue name: %v", updatedIssue.Name)).SetInternal(err)
+ }
+ payloadList = append(payloadList, payload)
+ }
+ if issuePatch.Description != nil && *issuePatch.Description != issue.Description {
+ payload, err := json.Marshal(api.ActivityIssueFieldUpdatePayload{
+ FieldId: api.IssueFieldDescription,
+ OldValue: issue.Description,
+ NewValue: *issuePatch.Description,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal activity after changing issue description: %v", updatedIssue.Name)).SetInternal(err)
+ }
+ payloadList = append(payloadList, payload)
+ }
+ if issuePatch.AssigneeId != nil && *issuePatch.AssigneeId != issue.AssigneeId {
+ payload, err := json.Marshal(api.ActivityIssueFieldUpdatePayload{
+ FieldId: api.IssueFieldAssignee,
+ OldValue: strconv.Itoa(issue.AssigneeId),
+ NewValue: strconv.Itoa(*issuePatch.AssigneeId),
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal activity after changing issue assignee: %v", updatedIssue.Name)).SetInternal(err)
+ }
+ payloadList = append(payloadList, payload)
+ }
+ if issuePatch.SubscriberIdList != nil {
+ oldSubscriberIdList := []string{}
+ for _, item := range issue.SubscriberIdList {
+ oldSubscriberIdList = append(oldSubscriberIdList, strconv.Itoa(item))
+ }
+ oldSubscriberIdStr := strings.Join(oldSubscriberIdList, ",")
+
+ newSubscriberIdList := []string{}
+ for _, item := range *issuePatch.SubscriberIdList {
+ newSubscriberIdList = append(newSubscriberIdList, strconv.Itoa(item))
+ }
+ newSubscriberIdStr := strings.Join(newSubscriberIdList, ",")
+
+ if newSubscriberIdStr != oldSubscriberIdStr {
+ payload, err := json.Marshal(api.ActivityIssueFieldUpdatePayload{
+ FieldId: api.IssueFieldSubscriberList,
+ OldValue: oldSubscriberIdStr,
+ NewValue: newSubscriberIdStr,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal activity after changing issue subscribers: %v", updatedIssue.Name)).SetInternal(err)
+ }
+ payloadList = append(payloadList, payload)
+ }
+ }
+
+ for _, payload := range payloadList {
+ _, err = s.ActivityService.CreateActivity(context.Background(), &api.ActivityCreate{
+ CreatorId: c.Get(GetPrincipalIdContextKey()).(int),
+ ContainerId: issue.ID,
+ Type: api.ActivityIssueFieldUpdate,
+ Payload: string(payload),
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create activity after updating issue: %v", updatedIssue.Name)).SetInternal(err)
+ }
+ }
+
+ if err := s.ComposeIssueRelationship(context.Background(), updatedIssue, c.Get(getIncludeKey()).([]string)); err != nil {
return err
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := jsonapi.MarshalPayload(c.Response().Writer, issue); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal issue ID response: %v", id)).SetInternal(err)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, updatedIssue); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal update issue response: %v", updatedIssue.Name)).SetInternal(err)
}
return nil
})
diff --git a/backend/store/issue.go b/backend/store/issue.go
index e5940dfa14..8ce3badeec 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -316,6 +316,13 @@ func (s *IssueService) patchIssue(ctx context.Context, tx *Tx, patch *api.IssueP
if v := patch.AssigneeId; v != nil {
set, args = append(set, "assignee_id = ?"), append(args, *v)
}
+ if v := patch.SubscriberIdList; v != nil {
+ subscriberIdList := []string{}
+ for _, item := range *patch.SubscriberIdList {
+ subscriberIdList = append(subscriberIdList, strconv.Itoa(item))
+ }
+ set, args = append(set, "subscriber_id_list = ?"), append(args, strings.Join(subscriberIdList, ","))
+ }
if v := patch.Payload; v != nil {
payload, err := json.Marshal(*patch.Payload)
if err != nil {
diff --git a/frontend/src/types/activity.ts b/frontend/src/types/activity.ts
index 813610daf9..d2a5160ad9 100644
--- a/frontend/src/types/activity.ts
+++ b/frontend/src/types/activity.ts
@@ -14,11 +14,9 @@ export type IssueActionType =
export type ActionType = IssueActionType;
export type ActionFieldUpdatePayload = {
- changeList: {
- fieldId: FieldId;
- oldValue?: string;
- newValue?: string;
- }[];
+ fieldId: FieldId;
+ oldValue?: string;
+ newValue?: string;
};
export type ActionIssueStatusUpdatePayload = {
diff --git a/frontend/src/types/issue.ts b/frontend/src/types/issue.ts
index 2c23a4a830..317b14997a 100644
--- a/frontend/src/types/issue.ts
+++ b/frontend/src/types/issue.ts
@@ -65,9 +65,6 @@ export type IssueCreate = {
};
export type IssuePatch = {
- // Related fields
- projectId?: ProjectId;
-
// Standard fields
updaterId: PrincipalId;
@@ -76,8 +73,6 @@ export type IssuePatch = {
description?: string;
assigneeId?: PrincipalId;
subscriberIdList?: PrincipalId[];
- sql?: string;
- rollbackSql?: string;
payload?: IssuePayload;
};
diff --git a/frontend/src/views/IssueActivityPanel.vue b/frontend/src/views/IssueActivityPanel.vue
index 6cb4fd5011..c35c700ba4 100644
--- a/frontend/src/views/IssueActivityPanel.vue
+++ b/frontend/src/views/IssueActivityPanel.vue
@@ -670,14 +670,9 @@ export default {
);
return list.filter((activity: Activity) => {
if (activity.actionType == "bb.issue.field.update") {
- let containUserVisibleChange = false;
- for (const update of (activity.payload as ActionFieldUpdatePayload)
- ?.changeList || []) {
- if (update.fieldId != IssueBuiltinFieldId.SUBSCRIBER_LIST) {
- containUserVisibleChange = true;
- break;
- }
- }
+ let containUserVisibleChange =
+ (activity.payload as ActionFieldUpdatePayload).fieldId !=
+ IssueBuiltinFieldId.SUBSCRIBER_LIST;
return containUserVisibleChange;
}
return true;
@@ -826,82 +821,72 @@ export default {
case "bb.issue.comment.create":
return "commented";
case "bb.issue.field.update": {
- const updateInfoList: string[] = [];
- for (const update of (activity.payload as ActionFieldUpdatePayload)
- ?.changeList || []) {
- let name = "Unknown Field";
- let oldValue = undefined;
- let newValue = undefined;
-
- switch (update.fieldId) {
- case IssueBuiltinFieldId.ASSIGNEE: {
- if (update.oldValue && update.newValue) {
- const oldName = store.getters["principal/principalById"](
- update.oldValue
- ).name;
-
- const newName = store.getters["principal/principalById"](
- update.newValue
- ).name;
-
- updateInfoList.push(
- `re-assigned issue from ${oldName} to ${newName}`
- );
- } else if (!update.oldValue && update.newValue) {
- const newName = store.getters["principal/principalById"](
- update.newValue
- ).name;
-
- updateInfoList.push(`assigned issue to ${newName}`);
- } else if (update.oldValue && !update.newValue) {
- const oldName = store.getters["principal/principalById"](
- update.oldValue
- ).name;
-
- updateInfoList.push(`un-assigned issue from ${oldName}`);
- }
+ const update = activity.payload as ActionFieldUpdatePayload;
+
+ let name = "Unknown Field";
+ let oldValue = undefined;
+ let newValue = undefined;
+
+ switch (update.fieldId) {
+ case IssueBuiltinFieldId.ASSIGNEE: {
+ if (update.oldValue && update.newValue) {
+ const oldName = store.getters["principal/principalById"](
+ update.oldValue
+ ).name;
+
+ const newName = store.getters["principal/principalById"](
+ update.newValue
+ ).name;
+
+ return `reassigned issue from ${oldName} to ${newName}`;
+ } else if (!update.oldValue && update.newValue) {
+ const newName = store.getters["principal/principalById"](
+ update.newValue
+ ).name;
+
+ return `assigned issue to ${newName}`;
+ } else if (update.oldValue && !update.newValue) {
+ const oldName = store.getters["principal/principalById"](
+ update.oldValue
+ ).name;
+
+ return `unassigned issue from ${oldName}`;
+ } else {
+ return `invalid assignee update`;
}
- case IssueBuiltinFieldId.SUBSCRIBER_LIST: {
- continue;
+ }
+ // We don't display subscriber change for now
+ case IssueBuiltinFieldId.SUBSCRIBER_LIST:
+ break;
+ case IssueBuiltinFieldId.DESCRIPTION:
+ // Description could be very long, so we don't display it.
+ return "changed description";
+ case IssueBuiltinFieldId.NAME:
+ case IssueBuiltinFieldId.PROJECT:
+ case IssueBuiltinFieldId.SQL:
+ case IssueBuiltinFieldId.ROLLBACK_SQL: {
+ if (update.fieldId == IssueBuiltinFieldId.NAME) {
+ name = "name";
+ } else if (update.fieldId == IssueBuiltinFieldId.SQL) {
+ name = "SQL";
+ } else if (update.fieldId == IssueBuiltinFieldId.ROLLBACK_SQL) {
+ name = "Rollback SQL";
}
- case IssueBuiltinFieldId.NAME:
- case IssueBuiltinFieldId.DESCRIPTION:
- case IssueBuiltinFieldId.PROJECT:
- case IssueBuiltinFieldId.SQL:
- case IssueBuiltinFieldId.ROLLBACK_SQL: {
- if (update.fieldId == IssueBuiltinFieldId.NAME) {
- name = "name";
- } else if (update.fieldId == IssueBuiltinFieldId.DESCRIPTION) {
- name = "description";
- // Description could be very long, so we don't display it.
- oldValue = "";
- newValue = "";
- } else if (update.fieldId == IssueBuiltinFieldId.SQL) {
- name = "SQL";
- } else if (update.fieldId == IssueBuiltinFieldId.ROLLBACK_SQL) {
- name = "Rollback SQL";
- }
-
- oldValue = update.oldValue;
- newValue = update.newValue;
- if (oldValue && newValue) {
- updateInfoList.push(
- `changed ${name} from "${oldValue}" to "${newValue}"`
- );
- } else if (oldValue) {
- updateInfoList.push(`unset "${name} from "${oldValue}"`);
- } else if (newValue) {
- updateInfoList.push(`set ${name} to "${newValue}"`);
- } else {
- updateInfoList.push(`changed ${name}`);
- }
+
+ oldValue = update.oldValue;
+ newValue = update.newValue;
+ if (oldValue && newValue) {
+ return `changed ${name} from "${oldValue}" to "${newValue}"`;
+ } else if (oldValue) {
+ return `unset "${name} from "${oldValue}"`;
+ } else if (newValue) {
+ return `set ${name} to "${newValue}"`;
+ } else {
+ return `changed ${name} update`;
}
}
}
- if (updateInfoList.length > 0) {
- return updateInfoList.join("; ");
- }
return "updated";
}
case "bb.issue.status.update": {