Skip to content

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

refactor(issue): replace Creator object with CreatorEmail string and update webhook to include name/email (#18448)

Danny Xu7c03a65ee8d7
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.go
index 275382c4c9..805e6dbb49 100644
--- a/backend/api/v1/issue_service.go
+++ b/backend/api/v1/issue_service.go
@@ -997,7 +997,7 @@ func (s *IssueService) RequestIssue(ctx context.Context, req *connect.Request[v1
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("user not found"))
}
- canRequest := canRequestIssue(issue.Creator, user)
+ canRequest := canRequestIssue(issue.CreatorEmail, user)
if !canRequest {
return nil, connect.NewError(connect.CodePermissionDenied, errors.Errorf("cannot request issues because you are not the issue creator"))
}
@@ -1529,6 +1529,6 @@ func (s *IssueService) isUserReviewer(ctx context.Context, issue *store.IssueMes
return roles[role]
}
-func canRequestIssue(issueCreator *store.UserMessage, user *store.UserMessage) bool {
- return issueCreator.ID == user.ID
+func canRequestIssue(issueCreatorEmail string, user *store.UserMessage) bool {
+ return issueCreatorEmail == user.Email
}
diff --git a/backend/api/v1/issue_service_converter.go b/backend/api/v1/issue_service_converter.go
index 71369e76ac..86cbf7c6b6 100644
--- a/backend/api/v1/issue_service_converter.go
+++ b/backend/api/v1/issue_service_converter.go
@@ -95,7 +95,7 @@ func (s *IssueService) convertToIssue(ctx context.Context, issue *store.IssueMes
Description: issue.Description,
Type: convertToIssueType(issue.Type),
Status: convertToIssueStatus(issue.Status),
- Creator: common.FormatUserEmail(issue.Creator.Email),
+ Creator: common.FormatUserEmail(issue.CreatorEmail),
CreateTime: timestamppb.New(issue.CreatedAt),
UpdateTime: timestamppb.New(issue.UpdatedAt),
GrantRequest: convertedGrantRequest,
@@ -352,7 +352,7 @@ func convertToIssueComment(issueName string, ic *store.IssueCommentMessage) *v1p
CreateTime: timestamppb.New(ic.CreatedAt),
UpdateTime: timestamppb.New(ic.UpdatedAt),
Name: fmt.Sprintf("%s/%s%d", issueName, common.IssueCommentNamePrefix, ic.UID),
- Creator: common.FormatUserEmail(ic.Creator.Email),
+ Creator: common.FormatUserEmail(ic.CreatorEmail),
}
switch e := ic.Payload.Event.(type) {
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 00a8956b6a..e649a8fc04 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -1009,7 +1009,7 @@ func (s *ProjectService) TestWebhook(ctx context.Context, req *connect.Request[v
Link: fmt.Sprintf("%s/projects/%s/webhooks/%s", externalURL, project.ResourceID, fmt.Sprintf("%s-%d", slug.Make(webhook.Payload.GetTitle()), webhook.ID)),
ActorID: common.SystemBotID,
ActorName: "Bytebase",
- ActorEmail: s.store.GetSystemBotUser(ctx).Email,
+ ActorEmail: common.SystemBotEmail,
CreatedTS: time.Now().Unix(),
Issue: &webhookplugin.Issue{
ID: 1,
@@ -1017,7 +1017,10 @@ func (s *ProjectService) TestWebhook(ctx context.Context, req *connect.Request[v
Status: "OPEN",
Type: "bb.issue.database.create",
Description: "This is a test issue",
- Creator: s.store.GetSystemBotUser(ctx),
+ Creator: webhookplugin.Creator{
+ Name: "Bytebase",
+ Email: common.SystemBotEmail,
+ },
},
Project: &webhookplugin.Project{
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index 8f35c061b3..269b5a3f86 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -1053,7 +1053,7 @@ func GetValidRolloutPolicyForEnvironment(ctx context.Context, stores *store.Stor
func (s *RolloutService) canUserRunEnvironmentTasks(ctx context.Context, user *store.UserMessage, project *store.ProjectMessage, issue *store.IssueMessage, environment string, _ string) (bool, error) {
// For data export issues, only the creator can run tasks.
if issue != nil && issue.Type == storepb.Issue_DATABASE_EXPORT {
- return issue.Creator.Email == user.Email, nil
+ return issue.CreatorEmail == user.Email, nil
}
// Users with bb.taskRuns.create can always create task runs.
diff --git a/backend/api/v1/rollout_service_converter.go b/backend/api/v1/rollout_service_converter.go
index 7caa2d24a8..16b4d19884 100644
--- a/backend/api/v1/rollout_service_converter.go
+++ b/backend/api/v1/rollout_service_converter.go
@@ -39,7 +39,7 @@ func convertToTaskRun(ctx context.Context, s *store.Store, stateCfg *state.State
stageID := common.FormatStageID(taskRun.Environment)
t := &v1pb.TaskRun{
Name: common.FormatTaskRun(taskRun.ProjectID, taskRun.PipelineUID, stageID, taskRun.TaskUID, taskRun.ID),
- Creator: common.FormatUserEmail(taskRun.Creator.Email),
+ Creator: common.FormatUserEmail(taskRun.CreatorEmail),
CreateTime: timestamppb.New(taskRun.CreatedAt),
UpdateTime: timestamppb.New(taskRun.UpdatedAt),
Status: convertToTaskRunStatus(taskRun.Status),
diff --git a/backend/component/webhook/event.go b/backend/component/webhook/event.go
index 7791f76b3a..8a34b66b5a 100644
--- a/backend/component/webhook/event.go
+++ b/backend/component/webhook/event.go
@@ -26,13 +26,13 @@ func NewIssue(i *store.IssueMessage) *Issue {
return nil
}
return &Issue{
- UID: i.UID,
- Status: i.Status.String(),
- Type: i.Type.String(),
- Title: i.Title,
- Description: i.Description,
- Creator: i.Creator,
- Approval: i.Payload.GetApproval(),
+ UID: i.UID,
+ Status: i.Status.String(),
+ Type: i.Type.String(),
+ Title: i.Title,
+ Description: i.Description,
+ CreatorEmail: i.CreatorEmail,
+ Approval: i.Payload.GetApproval(),
}
}
@@ -50,13 +50,13 @@ func NewRollout(r *store.PipelineMessage) *Rollout {
}
type Issue struct {
- UID int
- Status string
- Type string
- Title string
- Description string
- Creator *store.UserMessage
- Approval *storepb.IssuePayloadApproval
+ UID int
+ Status string
+ Type string
+ Title string
+ Description string
+ CreatorEmail string
+ Approval *storepb.IssuePayloadApproval
}
type Project struct {
diff --git a/backend/component/webhook/manager.go b/backend/component/webhook/manager.go
index 21a727d93a..11f0640e00 100644
--- a/backend/component/webhook/manager.go
+++ b/backend/component/webhook/manager.go
@@ -174,7 +174,15 @@ func (m *Manager) getWebhookContextFromEvent(ctx context.Context, e *Event, even
case storepb.Activity_NOTIFY_ISSUE_APPROVED:
title = "Issue approved"
titleZh = "工单审批通过"
- mentionUsers = append(mentionUsers, e.Issue.Creator)
+ creatorUser, err := m.store.GetUserByEmail(ctx, e.Issue.CreatorEmail)
+ if err != nil {
+ slog.Warn("failed to get creator user for issue notification",
+ slog.String("issue_name", e.Issue.Title),
+ log.BBError(err))
+ // Continue without mentioning the creator if unable to fetch
+ } else {
+ mentionUsers = append(mentionUsers, creatorUser)
+ }
case storepb.Activity_NOTIFY_PIPELINE_ROLLOUT:
u := e.IssueRolloutReady
@@ -182,7 +190,14 @@ func (m *Manager) getWebhookContextFromEvent(ctx context.Context, e *Event, even
titleZh = "工单待发布"
var usersGetters []UsersGetter
if u.RolloutPolicy.GetAutomatic() {
- usersGetters = append(usersGetters, getUsersFromUsers(e.Issue.Creator))
+ creatorUser, err := m.store.GetUserByEmail(ctx, e.Issue.CreatorEmail)
+ if err != nil {
+ slog.Warn("failed to get creator user for issue notification",
+ slog.String("issue_name", e.Issue.Title),
+ log.BBError(err))
+ } else {
+ usersGetters = append(usersGetters, getUsersFromUsers(creatorUser))
+ }
} else {
for _, role := range u.RolloutPolicy.GetRoles() {
role := strings.TrimPrefix(role, "roles/")
@@ -235,13 +250,25 @@ func (m *Manager) getWebhookContextFromEvent(ctx context.Context, e *Event, even
MentionEndUsers: mentionEndUsers,
}
if e.Issue != nil {
+ creatorName := e.Issue.CreatorEmail
+ creatorUser, err := m.store.GetUserByEmail(ctx, e.Issue.CreatorEmail)
+ if err != nil {
+ slog.Warn("failed to get creator user for webhook context",
+ slog.String("issue_name", e.Issue.Title),
+ log.BBError(err))
+ } else {
+ creatorName = creatorUser.Name
+ }
webhookCtx.Issue = &webhook.Issue{
ID: e.Issue.UID,
Name: e.Issue.Title,
Status: e.Issue.Status,
Type: e.Issue.Type,
Description: e.Issue.Description,
- Creator: e.Issue.Creator,
+ Creator: webhook.Creator{
+ Name: creatorName,
+ Email: e.Issue.CreatorEmail,
+ },
}
}
if e.Rollout != nil {
diff --git a/backend/plugin/webhook/webhook.go b/backend/plugin/webhook/webhook.go
index 2266df712e..e03b5861ee 100644
--- a/backend/plugin/webhook/webhook.go
+++ b/backend/plugin/webhook/webhook.go
@@ -48,7 +48,12 @@ type Issue struct {
Status string
Type string
Description string
- Creator *store.UserMessage
+ Creator Creator
+}
+
+type Creator struct {
+ Name string
+ Email string
}
type Rollout struct {
diff --git a/backend/plugin/webhook/webhook_test.go b/backend/plugin/webhook/webhook_test.go
index abf042bee9..b212f995d0 100644
--- a/backend/plugin/webhook/webhook_test.go
+++ b/backend/plugin/webhook/webhook_test.go
@@ -121,4 +121,61 @@ func TestContext_getMetaList(t *testing.T) {
}
a.Equal(want, context.GetMetaList())
})
+ t.Run("Issue with Creator", func(t *testing.T) {
+ a := require.New(t)
+ context := Context{
+ Issue: &Issue{
+ Name: "Issue 101",
+ Description: "Fix critical bug",
+ Creator: Creator{
+ Name: "Alice",
+ Email: "[email protected]",
+ },
+ },
+ }
+ want := []Meta{
+ {
+ Name: "Issue",
+ Value: "Issue 101",
+ },
+ {
+ Name: "Issue Creator",
+ Value: "Alice ([email protected])",
+ },
+ {
+ Name: "Issue Description",
+ Value: "Fix critical bug",
+ },
+ }
+ a.Equal(want, context.GetMetaList())
+ })
+
+ t.Run("Issue with Creator Zh", func(t *testing.T) {
+ a := require.New(t)
+ context := Context{
+ Issue: &Issue{
+ Name: "Issue 101",
+ Description: "Fix critical bug",
+ Creator: Creator{
+ Name: "Alice",
+ Email: "[email protected]",
+ },
+ },
+ }
+ want := []Meta{
+ {
+ Name: "工单",
+ Value: "Issue 101",
+ },
+ {
+ Name: "工单创建者",
+ Value: "Alice ([email protected])",
+ },
+ {
+ Name: "工单描述",
+ Value: "Fix critical bug",
+ },
+ }
+ a.Equal(want, context.GetMetaListZh())
+ })
}
diff --git a/backend/runner/taskrun/data_export_executor.go b/backend/runner/taskrun/data_export_executor.go
index 6b96dad97f..3613d77bcd 100644
--- a/backend/runner/taskrun/data_export_executor.go
+++ b/backend/runner/taskrun/data_export_executor.go
@@ -74,7 +74,14 @@ func (exec *DataExportExecutor) RunOnce(ctx context.Context, _ context.Context,
Format: v1pb.ExportFormat(task.Payload.GetFormat()),
Password: "", /* do not pass the password, we will encrypt the files will password when users download them */
}
- bytes, _, exportErr := apiv1.DoExport(ctx, exec.store, exec.dbFactory, exec.license, exportRequest, issue.Creator /* user */, instance, database, nil /* access check */, exec.schemaSyncer, dataSource)
+ creatorUser, err := exec.store.GetUserByEmail(ctx, issue.CreatorEmail)
+ if err != nil {
+ return true, nil, errors.Wrapf(err, "failed to get creator user for issue %d", issue.UID)
+ }
+ if creatorUser == nil {
+ return true, nil, errors.Errorf("creator user not found for issue %d", issue.UID)
+ }
+ bytes, _, exportErr := apiv1.DoExport(ctx, exec.store, exec.dbFactory, exec.license, exportRequest, creatorUser /* user */, instance, database, nil /* access check */, exec.schemaSyncer, dataSource)
if exportErr != nil {
return true, nil, errors.Wrap(exportErr, "failed to export data")
}
diff --git a/backend/store/issue.go b/backend/store/issue.go
index bce5fe1f15..26734dbf4e 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -45,13 +45,12 @@ type IssueMessage struct {
// The following fields are output only and not used for create().
UID int
- Creator *UserMessage
CreatedAt time.Time
UpdatedAt time.Time
// Internal fields.
projectID string
- creatorEmail string
+ CreatorEmail string
}
// UpdateIssueMessage is the message for updating an issue.
@@ -418,7 +417,7 @@ func (s *Store) ListIssues(ctx context.Context, find *FindIssueMessage) ([]*Issu
var typeString string
if err := rows.Scan(
&issue.UID,
- &issue.creatorEmail,
+ &issue.CreatorEmail,
&issue.CreatedAt,
&issue.UpdatedAt,
&issue.projectID,
@@ -466,11 +465,6 @@ func (s *Store) ListIssues(ctx context.Context, find *FindIssueMessage) ([]*Issu
return nil, err
}
issue.Project = project
- creator, err := s.GetUserByEmail(ctx, issue.creatorEmail)
- if err != nil {
- return nil, err
- }
- issue.Creator = creator
s.issueCache.Add(issue.UID, issue)
if issue.PipelineUID != nil {
diff --git a/backend/store/issue_comment.go b/backend/store/issue_comment.go
index 4dc4682e27..c9db9f9b63 100644
--- a/backend/store/issue_comment.go
+++ b/backend/store/issue_comment.go
@@ -13,14 +13,12 @@ import (
)
type IssueCommentMessage struct {
- UID int
- CreatedAt time.Time
- UpdatedAt time.Time
- IssueUID int
- Payload *storepb.IssueCommentPayload
- Creator *UserMessage
-
- creatorEmail string
+ UID int
+ CreatedAt time.Time
+ UpdatedAt time.Time
+ IssueUID int
+ Payload *storepb.IssueCommentPayload
+ CreatorEmail string
}
type FindIssueCommentMessage struct {
@@ -99,7 +97,7 @@ func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMess
var p []byte
if err := rows.Scan(
&ic.UID,
- &ic.creatorEmail,
+ &ic.CreatorEmail,
&ic.CreatedAt,
&ic.UpdatedAt,
&ic.IssueUID,
@@ -117,14 +115,6 @@ func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMess
return nil, errors.Wrapf(err, "rows err")
}
- for _, ic := range issueComments {
- creator, err := s.GetUserByEmail(ctx, ic.creatorEmail)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get creator")
- }
- ic.Creator = creator
- }
-
return issueComments, nil
}
@@ -173,12 +163,7 @@ func (s *Store) CreateIssueComment(ctx context.Context, create *IssueCommentMess
return nil, errors.Wrapf(err, "failed to insert")
}
- c, err := s.GetUserByEmail(ctx, creator)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get creator")
- }
- create.Creator = c
- create.creatorEmail = creator
+ create.CreatorEmail = creator
return create, nil
}
diff --git a/backend/store/pipeline.go b/backend/store/pipeline.go
index 8a84086fd7..bec20c3c17 100644
--- a/backend/store/pipeline.go
+++ b/backend/store/pipeline.go
@@ -3,6 +3,7 @@ package store
import (
"context"
"database/sql"
+ "strings"
"time"
"github.com/google/cel-go/cel"
@@ -317,7 +318,7 @@ func (s *Store) ListPipelines(ctx context.Context, find *PipelineFind) ([]*Pipel
}
// GetListRolloutFilter parses a CEL filter expression into a query builder query for listing rollouts.
-func (s *Store) GetListRolloutFilter(ctx context.Context, filter string) (*qb.Query, error) {
+func (s *Store) GetListRolloutFilter(_ context.Context, filter string) (*qb.Query, error) { // nolint:revive
if filter == "" {
return nil, nil
}
@@ -352,11 +353,11 @@ func (s *Store) GetListRolloutFilter(ctx context.Context, filter string) (*qb.Qu
variable, value := getVariableAndValueFromExpr(expr)
switch variable {
case "creator":
- user, err := s.getUserByIdentifier(ctx, value.(string))
- if err != nil {
- return nil, errors.Errorf("failed to get user %v with error %v", value, err.Error())
+ creatorEmail := strings.TrimPrefix(value.(string), "users/")
+ if creatorEmail == "" {
+ return nil, errors.New("invalid empty creator identifier")
}
- return qb.Q().Space("pipeline.creator = ?", user.Email), nil
+ return qb.Q().Space("pipeline.creator = ?", creatorEmail), nil
case "task_type":
taskType, ok := value.(string)
if !ok {
diff --git a/backend/store/plan.go b/backend/store/plan.go
index b4bc2581dd..68f63f84e0 100644
--- a/backend/store/plan.go
+++ b/backend/store/plan.go
@@ -285,7 +285,7 @@ func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) error
}
// GetListPlanFilter parses a CEL filter expression into a query builder query for listing plans.
-func (s *Store) GetListPlanFilter(ctx context.Context, filter string) (*qb.Query, error) {
+func (s *Store) GetListPlanFilter(_ context.Context, filter string) (*qb.Query, error) { // nolint:revive
if filter == "" {
return nil, nil
}
@@ -320,11 +320,11 @@ func (s *Store) GetListPlanFilter(ctx context.Context, filter string) (*qb.Query
variable, value := getVariableAndValueFromExpr(expr)
switch variable {
case "creator":
- user, err := s.getUserByIdentifier(ctx, value.(string))
- if err != nil {
- return nil, errors.Errorf("failed to get user %v with error %v", value, err.Error())
+ creatorEmail := strings.TrimPrefix(value.(string), "users/")
+ if creatorEmail == "" {
+ return nil, errors.New("invalid empty creator identifier")
}
- return qb.Q().Space("plan.creator = ?", user.Email), nil
+ return qb.Q().Space("plan.creator = ?", creatorEmail), nil
case "has_pipeline":
hasPipeline, ok := value.(bool)
if !ok {
@@ -429,18 +429,3 @@ func (s *Store) GetListPlanFilter(ctx context.Context, filter string) (*qb.Query
}
return qb.Q().Space("(?)", q), nil
}
-
-func (s *Store) getUserByIdentifier(ctx context.Context, identifier string) (*UserMessage, error) {
- email := strings.TrimPrefix(identifier, "users/")
- if email == "" {
- return nil, errors.New("invalid empty creator identifier")
- }
- user, err := s.GetUserByEmail(ctx, email)
- if err != nil {
- return nil, errors.Errorf(`failed to find user "%s" with error: %v`, email, err)
- }
- if user == nil {
- return nil, errors.Errorf("cannot found user %s", email)
- }
- return user, nil
-}
diff --git a/backend/store/task_run.go b/backend/store/task_run.go
index fd3fe82e2d..36aff65fd0 100644
--- a/backend/store/task_run.go
+++ b/backend/store/task_run.go
@@ -26,7 +26,6 @@ type TaskRunMessage struct {
// Output only.
ID int
CreatorEmail string
- Creator *UserMessage
CreatedAt time.Time
UpdatedAt time.Time
ProjectID string
@@ -164,15 +163,6 @@ func (s *Store) ListTaskRuns(ctx context.Context, find *FindTaskRunMessage) ([]*
if err := rows.Err(); err != nil {
return nil, err
}
-
- for _, taskRun := range taskRuns {
- creator, err := s.GetUserByEmail(ctx, taskRun.CreatorEmail)
- if err != nil {
- return nil, err
- }
- taskRun.Creator = creator
- }
-
return taskRuns, nil
}