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 review service GetReview (#5181)
p0ny9607b0036258
diff --git a/backend/api/v1/common.go b/backend/api/v1/common.goindex cf700f2a96..f3422ce9cc 100644--- a/backend/api/v1/common.go+++ b/backend/api/v1/common.go@@ -27,6 +27,7 @@ const (bookmarkPrefix = "bookmarks/"externalVersionControlPrefix = "externalVersionControls/"riskPrefix = "risks/"+ reviewPrefix = "reviews/"deploymentConfigSuffix = "/deploymentConfig"backupSettingSuffix = "/backupSetting"@@ -175,6 +176,18 @@ func getRiskID(name string) (int64, error) {return riskID, nil}+func getReviewID(name string) (int, error) {+ tokens, err := getNameParentTokens(name, projectNamePrefix, reviewPrefix)+ if err != nil {+ return 0, err+ }+ reviewID, err := strconv.Atoi(tokens[1])+ if err != nil {+ return 0, errors.Errorf("invalid review ID %q", tokens[1])+ }+ return reviewID, nil+}+func trimSuffix(name, suffix string) (string, error) {if !strings.HasSuffix(name, suffix) {return "", errors.Errorf("invalid request %q with suffix %q", name, suffix)diff --git a/backend/api/v1/review_service.go b/backend/api/v1/review_service.gonew file mode 100644index 0000000000..51ae73b16f--- /dev/null+++ b/backend/api/v1/review_service.go@@ -0,0 +1,116 @@+package v1++import (+ "context"+ "fmt"++ "google.golang.org/grpc/codes"+ "google.golang.org/grpc/status"+ "google.golang.org/protobuf/encoding/protojson"++ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/store"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"+)++// ReviewService implements the review service.+type ReviewService struct {+ v1pb.UnimplementedReviewServiceServer+ store *store.Store+}++// NewReviewService creates a new ReviewService.+func NewReviewService(store *store.Store) *ReviewService {+ return &ReviewService{+ store: store,+ }+}++// GetReview gets a review.+// Currently, only review.ApprovalTemplates and review.Approvers are set.+func (s *ReviewService) GetReview(ctx context.Context, request *v1pb.GetReviewRequest) (*v1pb.Review, error) {+ reviewID, err := getReviewID(request.Name)+ if err != nil {+ return nil, status.Errorf(codes.InvalidArgument, err.Error())+ }+ issue, err := s.store.GetIssueV2(ctx, &store.FindIssueMessage{UID: &reviewID})+ if err != nil {+ return nil, status.Errorf(codes.Internal, "failed to get issue, error: %v", err)+ }+ review, err := convertToReview(ctx, s.store, issue)+ if err != nil {+ return nil, status.Errorf(codes.Internal, "failed to convert to review, error: %v", err)+ }+ return review, nil+}++// ApproveReview approves the approval flow of the review.+func (*ReviewService) ApproveReview(context.Context, *v1pb.ApproveReviewRequest) (*v1pb.Review, error) {+ return nil, status.Errorf(codes.Unimplemented, "method ApproveReview not implemented")+}++func convertToReview(ctx context.Context, store *store.Store, issue *store.IssueMessage) (*v1pb.Review, error) {+ issuePayload := &storepb.IssuePayload{}+ if err := protojson.Unmarshal([]byte(issue.Payload), issuePayload); err != nil {+ return nil, errors.Wrap(err, "failed to unmarshal issue payload")+ }++ review := &v1pb.Review{}+ if issuePayload.Approval != nil {+ for _, template := range issuePayload.Approval.ApprovalTemplates {+ review.ApprovalTemplates = append(review.ApprovalTemplates, convertToApprovalTemplate(template))+ }+ for _, approver := range issuePayload.Approval.Approvers {+ convertedApprover := &v1pb.Review_Approver{Status: v1pb.Review_Approver_Status(approver.Status)}+ user, err := store.GetUserByID(ctx, int(approver.PrincipalId))+ if err != nil {+ return nil, errors.Wrapf(err, "failed to find user by id %v", approver.PrincipalId)+ }+ convertedApprover.Principal = fmt.Sprintf("user:%s", user.Email)++ review.Approvers = append(review.Approvers, convertedApprover)+ }+ }++ return review, nil+}++func convertToApprovalTemplate(template *storepb.ApprovalTemplate) *v1pb.ApprovalTemplate {+ return &v1pb.ApprovalTemplate{+ Flow: convertToApprovalFlow(template.Flow),+ Title: template.Title,+ Description: template.Description,+ }+}++func convertToApprovalFlow(flow *storepb.ApprovalFlow) *v1pb.ApprovalFlow {+ convertedFlow := &v1pb.ApprovalFlow{}+ for _, step := range flow.Steps {+ convertedFlow.Steps = append(convertedFlow.Steps, convertToApprovalStep(step))+ }+ return convertedFlow+}++func convertToApprovalStep(step *storepb.ApprovalStep) *v1pb.ApprovalStep {+ convertedStep := &v1pb.ApprovalStep{+ Type: v1pb.ApprovalStep_Type(step.Type),+ }+ for _, node := range step.Nodes {+ convertedStep.Nodes = append(convertedStep.Nodes, convertToApprovalNode(node))+ }+ return convertedStep+}++func convertToApprovalNode(node *storepb.ApprovalNode) *v1pb.ApprovalNode {+ if v, ok := node.Payload.(*storepb.ApprovalNode_GroupValue_); ok {+ return &v1pb.ApprovalNode{+ Type: v1pb.ApprovalNode_ANY_IN_GROUP,+ Payload: &v1pb.ApprovalNode_GroupValue_{+ GroupValue: v1pb.ApprovalNode_GroupValue(v.GroupValue),+ },+ }+ }+ return &v1pb.ApprovalNode{}+}diff --git a/backend/api/v1/review_service_test.go b/backend/api/v1/review_service_test.gonew file mode 100644index 0000000000..ea612310b4--- /dev/null+++ b/backend/api/v1/review_service_test.go@@ -0,0 +1,38 @@+package v1++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"+)++func TestConvertToApprovalNode(t *testing.T) {+ tests := []struct {+ node *storepb.ApprovalNode+ want *v1pb.ApprovalNode+ }{+ {+ node: &storepb.ApprovalNode{+ Type: storepb.ApprovalNode_ANY_IN_GROUP,+ Payload: &storepb.ApprovalNode_GroupValue_{+ GroupValue: storepb.ApprovalNode_WORKSPACE_DBA,+ },+ },+ want: &v1pb.ApprovalNode{+ Type: v1pb.ApprovalNode_ANY_IN_GROUP,+ Payload: &v1pb.ApprovalNode_GroupValue_{+ GroupValue: v1pb.ApprovalNode_WORKSPACE_DBA,+ },+ },+ },+ }++ a := require.New(t)+ for _, test := range tests {+ got := convertToApprovalNode(test.node)+ a.Equal(test.want, got)+ }+}diff --git a/backend/server/server.go b/backend/server/server.goindex 8acad17b12..eafefc0aaf 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -515,6 +515,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {v1pb.RegisterSQLServiceServer(s.grpcServer, v1.NewSQLService())v1pb.RegisterExternalVersionControlServiceServer(s.grpcServer, v1.NewExternalVersionControlService(s.store))v1pb.RegisterRiskServiceServer(s.grpcServer, v1.NewRiskService(s.store))+ v1pb.RegisterReviewServiceServer(s.grpcServer, v1.NewReviewService(s.store))reflection.Register(s.grpcServer)// REST gateway proxy.