MCPcopy Create free account
hub / github.com/bytebase/bytebase / buildCELVariablesForRoleGrant

Function buildCELVariablesForRoleGrant

backend/runner/approval/runner.go:704–774  ·  view source on GitHub ↗

buildCELVariablesForRoleGrant builds CEL variables for ROLE_GRANT issues.

(ctx context.Context, stores *store.Store, issue *store.IssueMessage)

Source from the content-addressed store, hash-verified

702
703// buildCELVariablesForRoleGrant builds CEL variables for ROLE_GRANT issues.
704func buildCELVariablesForRoleGrant(ctx context.Context, stores *store.Store, issue *store.IssueMessage) ([]map[string]any, bool, error) {
705 payload := issue.Payload
706 if payload.RoleGrant == nil {
707 return nil, false, errors.New("role grant payload not found")
708 }
709
710 factors, err := common.GetQueryExportFactors(payload.GetRoleGrant().GetCondition().GetExpression())
711 if err != nil {
712 return nil, false, errors.Wrap(err, "failed to get query export factors")
713 }
714
715 // Default to max int if expiration is not set (no expiration)
716 expirationDays := int64(math.MaxInt32)
717 if payload.RoleGrant.Expiration != nil {
718 expirationDays = int64(payload.RoleGrant.Expiration.AsDuration().Hours() / 24)
719 }
720
721 baseVars := map[string]any{
722 common.CELAttributeResourceProjectID: issue.ProjectID,
723 common.CELAttributeRequestExpirationDays: expirationDays,
724 common.CELAttributeRequestRole: payload.RoleGrant.Role,
725 }
726
727 // If no specific databases, create one entry per environment
728 if len(factors.Databases) == 0 {
729 issueProject, err := stores.GetProjectByResourceID(ctx, issue.ProjectID)
730 if err != nil {
731 return nil, false, err
732 }
733 if issueProject == nil {
734 return nil, false, errors.Errorf("project %s not found", issue.ProjectID)
735 }
736 environments, err := stores.GetEnvironment(ctx, issueProject.Workspace)
737 if err != nil {
738 return nil, false, err
739 }
740 var celVarsList []map[string]any
741 for _, environment := range environments.GetEnvironments() {
742 celVars := maps.Clone(baseVars)
743 celVars[common.CELAttributeResourceEnvironmentID] = environment.Id
744 celVarsList = append(celVarsList, celVars)
745 }
746 if len(celVarsList) == 0 {
747 celVarsList = append(celVarsList, baseVars)
748 }
749 return celVarsList, true, nil
750 }
751
752 // Build one entry per database
753 databases, err := getDatabasesForRoleGrant(ctx, stores, issue.ProjectID, factors.Databases)
754 if err != nil {
755 return nil, false, errors.Wrap(err, "failed to retrieve databases for role grant")
756 }
757
758 var celVarsList []map[string]any
759 for _, database := range databases {
760 celVars := maps.Clone(baseVars)
761 if database.EffectiveEnvironmentID != nil {

Callers 1

Calls 9

GetQueryExportFactorsFunction · 0.92
getDatabasesForRoleGrantFunction · 0.85
ErrorfMethod · 0.80
GetExpressionMethod · 0.45
GetConditionMethod · 0.45
GetRoleGrantMethod · 0.45
GetEnvironmentMethod · 0.45
GetEnvironmentsMethod · 0.45

Tested by

no test coverage detected