Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: impl external approal rejection (#6675)
p0ny0cd926f2c9f4
diff --git a/backend/plugin/app/relay/relay_client.go b/backend/plugin/app/relay/relay_client.goindex 0f2f1f5259..dae5c0ec5f 100644--- a/backend/plugin/app/relay/relay_client.go+++ b/backend/plugin/app/relay/relay_client.go@@ -87,35 +87,41 @@ func (c *Client) UpdateStatus(relayEndpoint string, uri string) error {return nil}+// Status is the status of the external approval.+type Status string+// GetStatus is the response message to get the status of an external approval.type GetStatus struct {- Status string `json:"status"`+ Status Status `json:"status"`}const (- doneStatus = "DONE"+ // StatusApproved means that the external approval is approved.+ StatusApproved Status = "APPROVED"+ // StatusRejected means that the external approval is rejected.+ StatusRejected Status = "REJECTED")// GetStatus gets the status of an external approval.-func (c *Client) GetStatus(relayEndpoint string, uri string) (bool, error) {+func (c *Client) GetStatus(relayEndpoint string, uri string) (Status, error) {req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/approvals/status", relayEndpoint), strings.NewReader(""))if err != nil {- return false, err+ return "", err}q := req.URL.Query()q.Add("uri", uri)req.URL.RawQuery = q.Encode()resp, err := c.client.Do(req)if err != nil {- return false, err+ return "", err}if resp.StatusCode != http.StatusOK {- return false, errors.Errorf("failed to get external approval status with status %v", resp.Status)+ return "", errors.Errorf("failed to get external approval status with status %v", resp.Status)}responseBody := &GetStatus{}err = json.NewDecoder(resp.Body).Decode(responseBody)if err != nil {- return false, err+ return "", err}- return responseBody.Status == doneStatus, nil+ return responseBody.Status, nil}diff --git a/backend/runner/relay/runner.go b/backend/runner/relay/runner.goindex 81b7ab32dc..8ae69e265c 100644--- a/backend/runner/relay/runner.go+++ b/backend/runner/relay/runner.go@@ -92,6 +92,44 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {}}+func (r *Runner) checkExternalApproval(ctx context.Context, approval *store.ExternalApprovalMessage) error {+ payload := &api.ExternalApprovalPayloadRelay{}+ if err := json.Unmarshal([]byte(approval.Payload), payload); err != nil {+ return errors.Wrapf(err, "failed to unmarshal external approval payload")+ }+ node, err := getExternalApprovalByID(ctx, r.store, payload.ExternalApprovalNodeID)+ if err != nil {+ return errors.Wrapf(err, "failed to get external approval node %s", payload.ExternalApprovalNodeID)+ }+ uri := payload.URI+ status, err := r.Client.GetStatus(node.Endpoint, uri)+ if err != nil {+ return errors.Wrapf(err, "failed to get external approval status, id: %v, endpoint: %s, uri: %s", node.Id, node.Endpoint, uri)+ }+ if status == relayplugin.StatusApproved {+ if err := r.approveExternalApprovalNode(ctx, approval.IssueUID); err != nil {+ return err+ }+ if _, err := r.store.UpdateExternalApprovalV2(ctx, &store.UpdateExternalApprovalMessage{+ ID: approval.ID,+ RowStatus: api.Archived,+ }); err != nil {+ return err+ }+ } else if status == relayplugin.StatusRejected {+ if err := r.rejectExternalApprovalNode(ctx, approval.IssueUID); err != nil {+ return err+ }+ if _, err := r.store.UpdateExternalApprovalV2(ctx, &store.UpdateExternalApprovalMessage{+ ID: approval.ID,+ RowStatus: api.Archived,+ }); err != nil {+ return err+ }+ }+ return nil+}+func getExternalApprovalByID(ctx context.Context, s *store.Store, externalApprovalID string) (*storepb.ExternalApprovalSetting_Node, error) {setting, err := s.GetWorkspaceExternalApprovalSetting(ctx)if err != nil {@@ -383,31 +421,87 @@ func (r *Runner) approveExternalApprovalNode(ctx context.Context, issueUID int)return nil}-func (r *Runner) checkExternalApproval(ctx context.Context, approval *store.ExternalApprovalMessage) error {- payload := &api.ExternalApprovalPayloadRelay{}- if err := json.Unmarshal([]byte(approval.Payload), payload); err != nil {- return errors.Wrapf(err, "failed to unmarshal external approval payload")+func (r *Runner) rejectExternalApprovalNode(ctx context.Context, issueUID int) error {+ issue, err := r.store.GetIssueV2(ctx, &store.FindIssueMessage{+ UID: &issueUID,+ })+ if err != nil {+ return errors.Wrapf(err, "failed to get issue")}- node, err := getExternalApprovalByID(ctx, r.store, payload.ExternalApprovalNodeID)+ payload := &storepb.IssuePayload{}+ if err := protojson.Unmarshal([]byte(issue.Payload), payload); err != nil {+ return status.Errorf(codes.Internal, "failed to unmarshal issue payload, error: %v", err)+ }+ if payload.Approval == nil {+ return status.Errorf(codes.Internal, "issue payload approval is nil")+ }+ if !payload.Approval.ApprovalFindingDone {+ return status.Errorf(codes.FailedPrecondition, "approval template finding is not done")+ }+ if payload.Approval.ApprovalFindingError != "" {+ return status.Errorf(codes.FailedPrecondition, "approval template finding failed: %v", payload.Approval.ApprovalFindingError)+ }+ if len(payload.Approval.ApprovalTemplates) != 1 {+ return status.Errorf(codes.Internal, "expecting one approval template but got %v", len(payload.Approval.ApprovalTemplates))+ }++ rejectedStep := utils.FindRejectedStep(payload.Approval.ApprovalTemplates[0], payload.Approval.Approvers)+ if rejectedStep != nil {+ return status.Errorf(codes.InvalidArgument, "cannot reject because the review has been rejected")+ }++ step := utils.FindNextPendingStep(payload.Approval.ApprovalTemplates[0], payload.Approval.Approvers)+ if step == nil {+ return status.Errorf(codes.InvalidArgument, "the review has been approved")+ }++ payload.Approval.Approvers = append(payload.Approval.Approvers, &storepb.IssuePayloadApproval_Approver{+ Status: storepb.IssuePayloadApproval_Approver_REJECTED,+ PrincipalId: int32(api.SystemBotID),+ })++ payloadBytes, err := protojson.Marshal(payload)if err != nil {- return errors.Wrapf(err, "failed to get external approval node %s", payload.ExternalApprovalNodeID)+ return status.Errorf(codes.Internal, "failed to marshal issue payload, error: %v", err)}- uri := payload.URI- done, err := r.Client.GetStatus(node.Endpoint, uri)+ payloadStr := string(payloadBytes)++ issue, err = r.store.UpdateIssueV2(ctx, issue.UID, &store.UpdateIssueMessage{+ Payload: &payloadStr,+ }, api.SystemBotID)if err != nil {- return errors.Wrapf(err, "failed to get external approval status, id: %v, endpoint: %s, uri: %s", node.Id, node.Endpoint, uri)+ return status.Errorf(codes.Internal, "failed to update issue, error: %v", err)}- if done {- if err := r.approveExternalApprovalNode(ctx, approval.IssueUID); err != nil {++ // It's ok to fail to create activity.+ if err := func() error {+ activityPayload, err := protojson.Marshal(&storepb.ActivityIssueCommentCreatePayload{+ Event: &storepb.ActivityIssueCommentCreatePayload_ApprovalEvent_{+ ApprovalEvent: &storepb.ActivityIssueCommentCreatePayload_ApprovalEvent{+ Status: storepb.ActivityIssueCommentCreatePayload_ApprovalEvent_REJECTED,+ },+ },+ IssueName: issue.Title,+ })+ if err != nil {return err}- if _, err := r.store.UpdateExternalApprovalV2(ctx, &store.UpdateExternalApprovalMessage{- ID: approval.ID,- RowStatus: api.Archived,- }); err != nil {+ create := &api.ActivityCreate{+ CreatorID: api.SystemBotID,+ ContainerID: issue.UID,+ Type: api.ActivityIssueCommentCreate,+ Level: api.ActivityInfo,+ Comment: "",+ Payload: string(activityPayload),+ }+ if _, err := r.activityManager.CreateActivity(ctx, create, &activity.Metadata{}); err != nil {return err}+ return nil+ }(); err != nil {+ log.Error("failed to create activity after rejecting review", zap.Error(err))}+return nil}