Skip to content

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

chore: branch protection backend (#8476)

Danny Xu89319e0cfbce
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 62ea03fe36..7616a23274 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -32,6 +32,7 @@ import (
webhookPlugin "github.com/bytebase/bytebase/backend/plugin/webhook"
"github.com/bytebase/bytebase/backend/store"
"github.com/bytebase/bytebase/backend/utils"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
@@ -2082,13 +2083,91 @@ func (s *ProjectService) GetSchemaGroup(ctx context.Context, request *v1pb.GetSc
}
// GetProjectProtectionRules gets a project protection rules.
-func (*ProjectService) GetProjectProtectionRules(_ context.Context, _ *v1pb.GetProjectProtectionRulesRequest) (*v1pb.ProtectionRules, error) {
- return nil, nil
+func (s *ProjectService) GetProjectProtectionRules(ctx context.Context, request *v1pb.GetProjectProtectionRulesRequest) (*v1pb.ProtectionRules, error) {
+ projectResourceID, err := common.TrimSuffix(request.Name, common.ProtectionRulesSuffix)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ ResourceID: &projectResourceID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ if project == nil {
+ return nil, status.Errorf(codes.NotFound, "project %q not found", projectResourceID)
+ }
+ if project.Deleted {
+ return nil, status.Errorf(codes.NotFound, "project %q has been deleted", projectResourceID)
+ }
+
+ resp := convertProtectionRules(project)
+ return resp, nil
}
// UpdateProjectProtectionRules updates a project protection rules.
-func (*ProjectService) UpdateProjectProtectionRules(_ context.Context, _ *v1pb.UpdateProjectProtectionRulesRequest) (*v1pb.ProtectionRules, error) {
- return nil, nil
+func (s *ProjectService) UpdateProjectProtectionRules(ctx context.Context, request *v1pb.UpdateProjectProtectionRulesRequest) (*v1pb.ProtectionRules, error) {
+ if request.ProtectionRules == nil {
+ return nil, status.Errorf(codes.InvalidArgument, "protection rules must be set")
+ }
+ projectResourceID, err := common.TrimSuffix(request.ProtectionRules.Name, common.ProtectionRulesSuffix)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ ResourceID: &projectResourceID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ if project == nil {
+ return nil, status.Errorf(codes.NotFound, "project %q not found", projectResourceID)
+ }
+ if project.Deleted {
+ return nil, status.Errorf(codes.NotFound, "project %q has been deleted", projectResourceID)
+ }
+
+ var rules []*storepb.ProtectionRule
+ for _, rule := range request.ProtectionRules.Rules {
+ rules = append(rules, &storepb.ProtectionRule{
+ Id: rule.Id,
+ Target: storepb.ProtectionRule_Target(rule.Target),
+ NameFilter: rule.NameFilter,
+ CreateAllowedRoles: rule.CreateAllowedRoles,
+ })
+ }
+
+ principalID := ctx.Value(common.PrincipalIDContextKey).(int)
+ setting := project.Setting
+ setting.ProtectionRules = rules
+ project, err = s.store.UpdateProjectV2(ctx, &store.UpdateProjectMessage{
+ UpdaterID: principalID,
+ ResourceID: project.ResourceID,
+ Setting: setting,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+
+ resp := convertProtectionRules(project)
+ return resp, nil
+}
+
+func convertProtectionRules(project *store.ProjectMessage) *v1pb.ProtectionRules {
+ resp := &v1pb.ProtectionRules{
+ Name: fmt.Sprintf("%s%s%s", common.ProjectNamePrefix, project.ResourceID, common.ProtectionRulesSuffix),
+ }
+ if project.Setting != nil {
+ for _, rule := range project.Setting.ProtectionRules {
+ resp.Rules = append(resp.Rules, &v1pb.ProtectionRule{
+ Id: rule.Id,
+ Target: v1pb.ProtectionRule_Target(rule.Target),
+ NameFilter: rule.NameFilter,
+ CreateAllowedRoles: rule.CreateAllowedRoles,
+ })
+ }
+ }
+ return resp
}
func (s *ProjectService) convertStoreToAPIDatabaseGroupFull(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) (*v1pb.DatabaseGroup, error) {
diff --git a/backend/common/resource_name.go b/backend/common/resource_name.go
index f669119bcd..29a050a667 100644
--- a/backend/common/resource_name.go
+++ b/backend/common/resource_name.go
@@ -45,10 +45,11 @@ const (
DeploymentConfigPrefix = "deploymentConfigs/"
ChangelistsPrefix = "changelists/"
- BackupSettingSuffix = "/backupSetting"
- SchemaSuffix = "/schema"
- MetadataSuffix = "/metadata"
- GitOpsInfoSuffix = "/gitOpsInfo"
+ BackupSettingSuffix = "/backupSetting"
+ SchemaSuffix = "/schema"
+ MetadataSuffix = "/metadata"
+ GitOpsInfoSuffix = "/gitOpsInfo"
+ ProtectionRulesSuffix = "/protectionRules"
)
// GetProjectID returns the project ID from a resource name.
diff --git a/backend/store/project.go b/backend/store/project.go
index 0f8c727159..acd0b71d03 100644
--- a/backend/store/project.go
+++ b/backend/store/project.go
@@ -7,9 +7,11 @@ import (
"strings"
"github.com/pkg/errors"
+ "google.golang.org/protobuf/encoding/protojson"
"github.com/bytebase/bytebase/backend/common"
api "github.com/bytebase/bytebase/backend/legacyapi"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
// ProjectMessage is the message for project.
@@ -24,6 +26,7 @@ type ProjectMessage struct {
SchemaChangeType api.ProjectSchemaChangeType
Webhooks []*ProjectWebhookMessage
DataClassificationConfigID string
+ Setting *storepb.Project
// The following fields are output only and not used for create().
UID int
Deleted bool
@@ -50,6 +53,7 @@ type UpdateProjectMessage struct {
Workflow *api.ProjectWorkflowType
SchemaChangeType *api.ProjectSchemaChangeType
DataClassificationConfigID *string
+ Setting *storepb.Project
Delete *bool
}
@@ -134,6 +138,13 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, cre
if err != nil {
return nil, err
}
+ if create.Setting == nil {
+ create.Setting = &storepb.Project{}
+ }
+ payload, err := protojson.Marshal(create.Setting)
+ if err != nil {
+ return nil, err
+ }
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
@@ -151,6 +162,7 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, cre
DBNameTemplate: create.DBNameTemplate,
SchemaChangeType: create.SchemaChangeType,
DataClassificationConfigID: create.DataClassificationConfigID,
+ Setting: create.Setting,
}
if err := tx.QueryRowContext(ctx, `
INSERT INTO project (
@@ -164,9 +176,10 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, cre
tenant_mode,
db_name_template,
schema_change_type,
- data_classification_config_id
+ data_classification_config_id,
+ setting
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
RETURNING id
`,
creatorID,
@@ -180,6 +193,7 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, cre
create.DBNameTemplate,
create.SchemaChangeType,
create.DataClassificationConfigID,
+ payload,
).Scan(
&project.UID,
); err != nil {
@@ -263,10 +277,19 @@ func (s *Store) updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdatePr
if v := patch.DataClassificationConfigID; v != nil {
set, args = append(set, fmt.Sprintf("data_classification_config_id = $%d", len(args)+1)), append(args, *v)
}
+ if v := patch.Setting; v != nil {
+ payload, err := protojson.Marshal(patch.Setting)
+ if err != nil {
+ return nil, err
+ }
+ set, args = append(set, fmt.Sprintf("setting = $%d", len(args)+1)), append(args, payload)
+ }
+
args = append(args, patch.ResourceID)
project := &ProjectMessage{}
var rowStatus string
+ var payload []byte
if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE project
SET `+strings.Join(set, ", ")+`
@@ -282,6 +305,7 @@ func (s *Store) updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdatePr
db_name_template,
schema_change_type,
data_classification_config_id,
+ setting,
row_status
`, len(args)),
args...,
@@ -296,6 +320,7 @@ func (s *Store) updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdatePr
&project.DBNameTemplate,
&project.SchemaChangeType,
&project.DataClassificationConfigID,
+ &payload,
&rowStatus,
); err != nil {
if err == sql.ErrNoRows {
@@ -303,6 +328,12 @@ func (s *Store) updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdatePr
}
return nil, err
}
+ setting := &storepb.Project{}
+ if err := protojsonUnmarshaler.Unmarshal(payload, setting); err != nil {
+ return nil, err
+ }
+ project.Setting = setting
+
projectWebhooks, err := s.findProjectWebhookImplV2(ctx, tx, &FindProjectWebhookMessage{ProjectID: &project.UID})
if err != nil {
return nil, err
@@ -337,6 +368,7 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject
db_name_template,
schema_change_type,
data_classification_config_id,
+ setting,
row_status
FROM project
WHERE `+strings.Join(where, " AND "),
@@ -349,6 +381,7 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject
for rows.Next() {
var projectMessage ProjectMessage
+ var payload []byte
var rowStatus string
if err := rows.Scan(
&projectMessage.UID,
@@ -361,10 +394,16 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject
&projectMessage.DBNameTemplate,
&projectMessage.SchemaChangeType,
&projectMessage.DataClassificationConfigID,
+ &payload,
&rowStatus,
); err != nil {
return nil, err
}
+ setting := &storepb.Project{}
+ if err := protojsonUnmarshaler.Unmarshal(payload, setting); err != nil {
+ return nil, err
+ }
+ projectMessage.Setting = setting
projectMessage.Deleted = convertRowStatusToDeleted(rowStatus)
projectMessages = append(projectMessages, &projectMessage)
}