Skip to content

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

feat: issue comments for approval (#11528)

p0nya8fe9a447243
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.go
index f28e84d459..de821977ae 100644
--- a/backend/api/v1/issue_service.go
+++ b/backend/api/v1/issue_service.go
@@ -892,7 +892,7 @@ func (s *IssueService) ApproveIssue(ctx context.Context, request *v1pb.ApproveIs
return nil, status.Errorf(codes.Internal, "failed to check if the approval is approved, error: %v", err)
}
- newApprovers, activityCreates, err := utils.HandleIncomingApprovalSteps(ctx, s.store, s.relayRunner.Client, issue, payload.Approval)
+ newApprovers, activityCreates, issueComments, err := utils.HandleIncomingApprovalSteps(ctx, s.store, s.relayRunner.Client, issue, payload.Approval)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to handle incoming approval steps, error: %v", err)
}
@@ -933,6 +933,30 @@ func (s *IssueService) ApproveIssue(ctx context.Context, request *v1pb.ApproveIs
}
}
+ if err := func() error {
+ p := &storepb.IssueCommentPayload{
+ Event: &storepb.IssueCommentPayload_Approval_{
+ Approval: &storepb.IssueCommentPayload_Approval{
+ Status: storepb.IssueCommentPayload_Approval_APPROVED,
+ },
+ },
+ }
+ if err := s.store.CreateIssueComment(ctx, &store.IssueCommentMessage{
+ IssueUID: issue.UID,
+ Payload: p,
+ }, user.ID); err != nil {
+ return err
+ }
+ for _, ic := range issueComments {
+ if err := s.store.CreateIssueComment(ctx, ic, api.SystemBotID); err != nil {
+ return err
+ }
+ }
+ return nil
+ }(); err != nil {
+ slog.Warn("failed to create issue comment", log.BBError(err))
+ }
+
// It's ok to fail to create activity.
if err := func() error {
activityPayload, err := protojson.Marshal(&storepb.ActivityIssueCommentCreatePayload{
@@ -1173,6 +1197,22 @@ func (s *IssueService) RejectIssue(ctx context.Context, request *v1pb.RejectIssu
return nil, status.Errorf(codes.Internal, "failed to update issue, error: %v", err)
}
+ if err := func() error {
+ p := &storepb.IssueCommentPayload{
+ Event: &storepb.IssueCommentPayload_Approval_{
+ Approval: &storepb.IssueCommentPayload_Approval{
+ Status: storepb.IssueCommentPayload_Approval_REJECTED,
+ },
+ },
+ }
+ return s.store.CreateIssueComment(ctx, &store.IssueCommentMessage{
+ IssueUID: issue.UID,
+ Payload: p,
+ }, user.ID)
+ }(); err != nil {
+ slog.Warn("failed to create issue comment", log.BBError(err))
+ }
+
// It's ok to fail to create activity.
if err := func() error {
activityPayload, err := protojson.Marshal(&storepb.ActivityIssueCommentCreatePayload{
@@ -1258,7 +1298,7 @@ func (s *IssueService) RequestIssue(ctx context.Context, request *v1pb.RequestIs
}
payload.Approval.Approvers = newApprovers
- newApprovers, activityCreates, err := utils.HandleIncomingApprovalSteps(ctx, s.store, s.relayRunner.Client, issue, payload.Approval)
+ newApprovers, activityCreates, issueComments, err := utils.HandleIncomingApprovalSteps(ctx, s.store, s.relayRunner.Client, issue, payload.Approval)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to handle incoming approval steps, error: %v", err)
}
@@ -1273,6 +1313,30 @@ func (s *IssueService) RequestIssue(ctx context.Context, request *v1pb.RequestIs
return nil, status.Errorf(codes.Internal, "failed to update issue, error: %v", err)
}
+ if err := func() error {
+ p := &storepb.IssueCommentPayload{
+ Event: &storepb.IssueCommentPayload_Approval_{
+ Approval: &storepb.IssueCommentPayload_Approval{
+ Status: storepb.IssueCommentPayload_Approval_PENDING,
+ },
+ },
+ }
+ if err := s.store.CreateIssueComment(ctx, &store.IssueCommentMessage{
+ IssueUID: issue.UID,
+ Payload: p,
+ }, user.ID); err != nil {
+ return err
+ }
+ for _, ic := range issueComments {
+ if err := s.store.CreateIssueComment(ctx, ic, api.SystemBotID); err != nil {
+ return err
+ }
+ }
+ return nil
+ }(); err != nil {
+ slog.Warn("failed to create issue comment", log.BBError(err))
+ }
+
// It's ok to fail to create activity.
if err := func() error {
activityPayload, err := protojson.Marshal(&storepb.ActivityIssueCommentCreatePayload{
diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.go
index 3b9e9a1a44..272992d6d1 100644
--- a/backend/runner/approval/runner.go
+++ b/backend/runner/approval/runner.go
@@ -200,7 +200,7 @@ func (r *Runner) findApprovalTemplateForIssue(ctx context.Context, issue *store.
payload.Approval.ApprovalTemplates = []*storepb.ApprovalTemplate{approvalTemplate}
}
- newApprovers, activityCreates, err := utils.HandleIncomingApprovalSteps(ctx, r.store, r.relayRunner.Client, issue, payload.Approval)
+ newApprovers, activityCreates, issueComments, err := utils.HandleIncomingApprovalSteps(ctx, r.store, r.relayRunner.Client, issue, payload.Approval)
if err != nil {
err = errors.Wrapf(err, "failed to handle incoming approval steps")
if updateErr := updateIssueApprovalPayload(ctx, r.store, issue, &storepb.IssuePayloadApproval{
@@ -217,6 +217,17 @@ func (r *Runner) findApprovalTemplateForIssue(ctx context.Context, issue *store.
return false, errors.Wrap(err, "failed to update issue payload")
}
+ if err := func() error {
+ for _, ic := range issueComments {
+ if err := r.store.CreateIssueComment(ctx, ic, api.SystemBotID); err != nil {
+ return err
+ }
+ }
+ return nil
+ }(); err != nil {
+ slog.Warn("failed to create issue comment", log.BBError(err))
+ }
+
// It's ok to fail to create activity.
if err := func() error {
for _, create := range activityCreates {
diff --git a/backend/runner/relay/runner.go b/backend/runner/relay/runner.go
index cbf565dd87..6442349b8c 100644
--- a/backend/runner/relay/runner.go
+++ b/backend/runner/relay/runner.go
@@ -260,7 +260,7 @@ func (r *Runner) ApproveExternalApprovalNode(ctx context.Context, issueUID int)
return errors.Wrapf(err, "failed to check if the approval is approved")
}
- newApprovers, activityCreates, err := utils.HandleIncomingApprovalSteps(ctx, r.store, r.Client, issue, payload.Approval)
+ newApprovers, activityCreates, issueComments, err := utils.HandleIncomingApprovalSteps(ctx, r.store, r.Client, issue, payload.Approval)
if err != nil {
return errors.Wrapf(err, "failed to handle incoming approval steps")
}
@@ -275,6 +275,30 @@ func (r *Runner) ApproveExternalApprovalNode(ctx context.Context, issueUID int)
return errors.Wrapf(err, "failed to update issue")
}
+ if err := func() error {
+ p := &storepb.IssueCommentPayload{
+ Event: &storepb.IssueCommentPayload_Approval_{
+ Approval: &storepb.IssueCommentPayload_Approval{
+ Status: storepb.IssueCommentPayload_Approval_APPROVED,
+ },
+ },
+ }
+ if err := r.store.CreateIssueComment(ctx, &store.IssueCommentMessage{
+ IssueUID: issue.UID,
+ Payload: p,
+ }, api.SystemBotID); err != nil {
+ return err
+ }
+ for _, ic := range issueComments {
+ if err := r.store.CreateIssueComment(ctx, ic, api.SystemBotID); err != nil {
+ return err
+ }
+ }
+ return nil
+ }(); err != nil {
+ slog.Warn("failed to create issue comment", log.BBError(err))
+ }
+
// It's ok to fail to create activity.
if err := func() error {
activityPayload, err := protojson.Marshal(&storepb.ActivityIssueCommentCreatePayload{
@@ -423,6 +447,22 @@ func (r *Runner) RejectExternalApprovalNode(ctx context.Context, issueUID int) e
return errors.Wrapf(err, "failed to update issue")
}
+ if err := func() error {
+ p := &storepb.IssueCommentPayload{
+ Event: &storepb.IssueCommentPayload_Approval_{
+ Approval: &storepb.IssueCommentPayload_Approval{
+ Status: storepb.IssueCommentPayload_Approval_REJECTED,
+ },
+ },
+ }
+ return r.store.CreateIssueComment(ctx, &store.IssueCommentMessage{
+ IssueUID: issue.UID,
+ Payload: p,
+ }, api.SystemBotID)
+ }(); err != nil {
+ slog.Warn("failed to create issue comment", log.BBError(err))
+ }
+
// It's ok to fail to create activity.
if err := func() error {
activityPayload, err := protojson.Marshal(&storepb.ActivityIssueCommentCreatePayload{
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index fef7bfb835..4dd52dfc9d 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -413,9 +413,9 @@ func CheckIssueApproved(issue *store.IssueMessage) (bool, error) {
// HandleIncomingApprovalSteps handles incoming approval steps.
// - Blocks approval steps if no user can approve the step.
// - creates external approvals for external approval nodes.
-func HandleIncomingApprovalSteps(ctx context.Context, s *store.Store, relayClient *relay.Client, issue *store.IssueMessage, approval *storepb.IssuePayloadApproval) ([]*storepb.IssuePayloadApproval_Approver, []*store.ActivityMessage, error) {
+func HandleIncomingApprovalSteps(ctx context.Context, s *store.Store, relayClient *relay.Client, issue *store.IssueMessage, approval *storepb.IssuePayloadApproval) ([]*storepb.IssuePayloadApproval_Approver, []*store.ActivityMessage, []*store.IssueCommentMessage, error) {
if len(approval.ApprovalTemplates) == 0 {
- return nil, nil, nil
+ return nil, nil, nil, nil
}
getActivityCreate := func(status storepb.ActivityIssueCommentCreatePayload_ApprovalEvent_Status, comment string) (*store.ActivityMessage, error) {
@@ -443,16 +443,17 @@ func HandleIncomingApprovalSteps(ctx context.Context, s *store.Store, relayClien
var approvers []*storepb.IssuePayloadApproval_Approver
var activities []*store.ActivityMessage
+ var issueComments []*store.IssueCommentMessage
step := FindNextPendingStep(approval.ApprovalTemplates[0], approval.Approvers)
if step == nil {
- return nil, nil, nil
+ return nil, nil, nil, nil
}
if len(step.Nodes) != 1 {
- return nil, nil, errors.Errorf("expecting one node but got %v", len(step.Nodes))
+ return nil, nil, nil, errors.Errorf("expecting one node but got %v", len(step.Nodes))
}
if step.Type != storepb.ApprovalStep_ANY {
- return nil, nil, errors.Errorf("expecting ANY step type but got %v", step.Type)
+ return nil, nil, nil, errors.Errorf("expecting ANY step type but got %v", step.Type)
}
node := step.Nodes[0]
if v, ok := node.GetPayload().(*storepb.ApprovalNode_ExternalNodeId); ok {
@@ -461,14 +462,26 @@ func HandleIncomingApprovalSteps(ctx context.Context, s *store.Store, relayClien
Status: storepb.IssuePayloadApproval_Approver_REJECTED,
PrincipalId: api.SystemBotID,
})
+
activity, err := getActivityCreate(storepb.ActivityIssueCommentCreatePayload_ApprovalEvent_REJECTED, fmt.Sprintf("failed to handle external node, err: %v", err))
if err != nil {
- return nil, nil, err
+ return nil, nil, nil, err
}
activities = append(activities, activity)
+
+ issueComments = append(issueComments, &store.IssueCommentMessage{
+ IssueUID: issue.UID,
+ Payload: &storepb.IssueCommentPayload{
+ Event: &storepb.IssueCommentPayload_Approval_{
+ Approval: &storepb.IssueCommentPayload_Approval{
+ Status: storepb.IssueCommentPayload_Approval_APPROVED,
+ },
+ },
+ },
+ })
}
}
- return approvers, activities, nil
+ return approvers, activities, issueComments, nil
}
func handleApprovalNodeExternalNode(ctx context.Context, s *store.Store, relayClient *relay.Client, issue *store.IssueMessage, externalNodeID string) error {