Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: delete unused code (part 4) (#7980)
Danny Xu5d9363df7698
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.godeleted file mode 100644index 1d7f110fbb..0000000000--- a/backend/runner/taskrun/scheduler.go+++ /dev/null@@ -1,801 +0,0 @@-// Package taskrun implements a runner for executing tasks.-package taskrun--import (- "context"- "encoding/json"- "fmt"- "net/http"-- "github.com/labstack/echo/v4"- "github.com/pkg/errors"-- "go.uber.org/zap"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/log"- "github.com/bytebase/bytebase/backend/component/activity"- "github.com/bytebase/bytebase/backend/component/state"- enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"- api "github.com/bytebase/bytebase/backend/legacyapi"- metricAPI "github.com/bytebase/bytebase/backend/metric"- "github.com/bytebase/bytebase/backend/plugin/metric"- "github.com/bytebase/bytebase/backend/runner/metricreport"- "github.com/bytebase/bytebase/backend/store"- "github.com/bytebase/bytebase/backend/utils"-)--var (- taskCancellationImplemented = map[api.TaskType]bool{- api.TaskDatabaseSchemaUpdateGhostSync: true,- }- applicableTaskStatusTransition = map[api.TaskStatus][]api.TaskStatus{- api.TaskPendingApproval: {api.TaskPending, api.TaskDone},- api.TaskPending: {api.TaskCanceled, api.TaskRunning, api.TaskPendingApproval, api.TaskDone},- api.TaskRunning: {api.TaskDone, api.TaskFailed, api.TaskCanceled},- api.TaskDone: {},- api.TaskFailed: {api.TaskPendingApproval, api.TaskDone},- api.TaskCanceled: {api.TaskPendingApproval, api.TaskDone},- }- allowedSkippedTaskStatus = map[api.TaskStatus]bool{- api.TaskPendingApproval: true,- api.TaskFailed: true,- }- terminatedTaskStatus = map[api.TaskStatus]bool{- api.TaskDone: true,- api.TaskCanceled: true,- api.TaskFailed: true,- }-)--// NewScheduler creates a new task scheduler.-func NewScheduler(- store *store.Store,- activityManager *activity.Manager,- licenseService enterpriseAPI.LicenseService,- stateCfg *state.State,- metricReporter *metricreport.Reporter) *Scheduler {- return &Scheduler{- store: store,- activityManager: activityManager,- licenseService: licenseService,- stateCfg: stateCfg,- metricReporter: metricReporter,- }-}--// Scheduler is the task scheduler.-type Scheduler struct {- store *store.Store- activityManager *activity.Manager- licenseService enterpriseAPI.LicenseService- stateCfg *state.State- metricReporter *metricreport.Reporter-}--// PatchTask patches the statement, earliest allowed time and rollbackEnabled for a task.-func (s *Scheduler) PatchTask(ctx context.Context, task *store.TaskMessage, taskPatch *api.TaskPatch, issue *store.IssueMessage) error {- if taskPatch.SheetID != nil {- if err := canUpdateTaskStatement(task); err != nil {- return err- }- }-- // Reset because we are trying to build- // the RollbackSheetID again and there could be previous runs.- if taskPatch.RollbackEnabled != nil && *taskPatch.RollbackEnabled {- empty := ""- pending := api.RollbackSQLStatusPending- taskPatch.RollbackSQLStatus = &pending- taskPatch.RollbackSheetID = nil- taskPatch.RollbackError = &empty- }- // if *taskPatch.RollbackEnabled == false, we don't reset- // 1. they are meaningless anyway if rollback is disabled- // 2. they will be reset when enabling- // 3. we cancel the generation after writing to db. The rollback sql generation may finish and write to db, too.-- taskPatched, err := s.store.UpdateTaskV2(ctx, taskPatch)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update task \"%v\"", task.Name)).SetInternal(err)- }- if issue.NeedAttention && issue.Project.Workflow == api.UIWorkflow {- needAttention := false- if _, err := s.store.UpdateIssueV2(ctx, issue.UID, &store.UpdateIssueMessage{NeedAttention: &needAttention}, api.SystemBotID); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to try to patch issue assignee_need_attention after updating task statement").SetInternal(err)- }- }-- // enqueue or cancel after it's written to the database.- if taskPatch.RollbackEnabled != nil {- // Enqueue the rollback sql generation if the task done.- if *taskPatch.RollbackEnabled && taskPatched.Status == api.TaskDone {- s.stateCfg.RollbackGenerate.Store(taskPatched.ID, taskPatched)- } else {- // Cancel running rollback sql generation.- if v, ok := s.stateCfg.RollbackCancel.Load(taskPatched.ID); ok {- if cancel, ok := v.(context.CancelFunc); ok {- cancel()- }- }- // We don't erase the keys for RollbackCancel and RollbackGenerate here because they will eventually be erased by the rollback runner.- }- }-- // Trigger task checks.- if taskPatch.SheetID != nil {- dbSchema, err := s.store.GetDBSchema(ctx, *task.DatabaseID)- if err != nil {- return err- }- if dbSchema == nil {- return errors.Errorf("database schema ID not found %v", task.DatabaseID)- }- // it's ok to fail.- s.stateCfg.IssueExternalApprovalRelayCancelChan <- issue.UID- }-- if taskPatch.SheetID != nil {- oldSheetID, err := utils.GetTaskSheetID(task.Payload)- if err != nil {- return errors.Wrap(err, "failed to get old sheet ID")- }- newSheetID := *taskPatch.SheetID-- // create a task sheet update activity- payload, err := json.Marshal(api.ActivityPipelineTaskStatementUpdatePayload{- TaskID: taskPatched.ID,- OldSheetID: oldSheetID,- NewSheetID: newSheetID,- TaskName: task.Name,- IssueName: issue.Title,- })- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity after updating task sheet: %v", taskPatched.Name).SetInternal(err)- }- if _, err := s.activityManager.CreateActivity(ctx, &store.ActivityMessage{- CreatorUID: taskPatch.UpdaterID,- ContainerUID: taskPatched.PipelineID,- Type: api.ActivityPipelineTaskStatementUpdate,- Payload: string(payload),- Level: api.ActivityInfo,- }, &activity.Metadata{- Issue: issue,- }); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create activity after updating task statement: %v", taskPatched.Name)).SetInternal(err)- }- }-- // Earliest allowed time update activity.- if taskPatch.EarliestAllowedTs != nil {- // create an activity- payload, err := json.Marshal(api.ActivityPipelineTaskEarliestAllowedTimeUpdatePayload{- TaskID: taskPatched.ID,- OldEarliestAllowedTs: task.EarliestAllowedTs,- NewEarliestAllowedTs: taskPatched.EarliestAllowedTs,- TaskName: task.Name,- IssueName: issue.Title,- })- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrapf(err, "failed to marshal earliest allowed time activity payload: %v", task.Name))- }- activityCreate := &store.ActivityMessage{- CreatorUID: taskPatch.UpdaterID,- ContainerUID: taskPatched.PipelineID,- Type: api.ActivityPipelineTaskEarliestAllowedTimeUpdate,- Payload: string(payload),- Level: api.ActivityInfo,- }- if _, err := s.activityManager.CreateActivity(ctx, activityCreate, &activity.Metadata{- Issue: issue,- }); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create activity after updating task earliest allowed time: %v", taskPatched.Name)).SetInternal(err)- }- }- return nil-}--// CanPrincipalChangeTaskStatus validates if the principal has the privilege to update task status, judging from the principal role and the environment policy.-func (s *Scheduler) CanPrincipalChangeTaskStatus(ctx context.Context, principalID int, task *store.TaskMessage, toStatus api.TaskStatus) (bool, error) {- // The creator can cancel task.- if toStatus == api.TaskCanceled {- if principalID == task.CreatorID {- return true, nil- }- }- // the workspace owner and DBA roles can always change task status.- user, err := s.store.GetUserByID(ctx, principalID)- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to get principal by ID %d", principalID)- }- if user == nil {- return false, common.Errorf(common.NotFound, "principal not found by ID %d", principalID)- }- if user.Role == api.Owner || user.Role == api.DBA {- return true, nil- }-- issue, err := s.store.GetIssueV2(ctx, &store.FindIssueMessage{PipelineID: &task.PipelineID})- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to find issue")- }- if issue == nil {- return false, common.Errorf(common.NotFound, "issue not found by pipeline ID: %d", task.PipelineID)- }- groupValue, err := s.getGroupValueForTask(ctx, issue, task)- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to get assignee group value for taskID %d", task.ID)- }- if groupValue == nil {- return false, nil- }- // as the policy says, the project owner has the privilege to change task status.- if *groupValue == api.AssigneeGroupValueProjectOwner {- policy, err := s.store.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{UID: &issue.Project.UID})- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to get project %d policy", issue.Project.UID)- }- for _, binding := range policy.Bindings {- if binding.Role != api.Owner {- continue- }- for _, member := range binding.Members {- if member.ID == principalID {- return true, nil- }- }- }- }- return false, nil-}--func (s *Scheduler) getGroupValueForTask(ctx context.Context, issue *store.IssueMessage, task *store.TaskMessage) (*api.AssigneeGroupValue, error) {- environmentID := api.UnknownID- stages, err := s.store.ListStageV2(ctx, task.PipelineID)- if err != nil {- return nil, err- }- for _, stage := range stages {- if stage.ID == task.StageID {- environmentID = stage.EnvironmentID- break- }- }- if environmentID == api.UnknownID {- return nil, common.Errorf(common.NotFound, "failed to find environmentID by task.StageID %d", task.StageID)- }-- policy, err := s.store.GetPipelineApprovalPolicy(ctx, environmentID)- if err != nil {- return nil, common.Wrapf(err, common.Internal, "failed to get pipeline approval policy by environmentID %d", environmentID)- }-- for _, assigneeGroup := range policy.AssigneeGroupList {- if assigneeGroup.IssueType == issue.Type {- return &assigneeGroup.Value, nil- }- }- return nil, nil-}--var (- allowedStatementUpdateTaskTypes = map[api.TaskType]bool{- api.TaskDatabaseCreate: true,- api.TaskDatabaseSchemaUpdate: true,- api.TaskDatabaseSchemaUpdateSDL: true,- api.TaskDatabaseDataUpdate: true,- api.TaskDatabaseSchemaUpdateGhostSync: true,- }- allowedPatchStatementStatus = map[api.TaskStatus]bool{- api.TaskPendingApproval: true,- api.TaskFailed: true,- }-)--func canUpdateTaskStatement(task *store.TaskMessage) *echo.HTTPError {- if ok := allowedStatementUpdateTaskTypes[task.Type]; !ok {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("cannot update statement for task type %q", task.Type))- }- // Allow frontend to change the SQL statement of- // 1. a PendingApproval task which hasn't started yet;- // 2. a Failed task which can be retried.- if !allowedPatchStatementStatus[task.Status] {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("cannot update task in %q status", task.Status))- }- return nil-}--// PatchTaskStatus patches a single task.-func (s *Scheduler) PatchTaskStatus(ctx context.Context, task *store.TaskMessage, taskStatusPatch *api.TaskStatusPatch) (err error) {- defer func() {- if err != nil {- log.Error("Failed to change task status.",- zap.Int("id", task.ID),- zap.String("name", task.Name),- zap.String("old_status", string(task.Status)),- zap.String("new_status", string(taskStatusPatch.Status)),- zap.Error(err))- }- }()-- if !isTaskStatusTransitionAllowed(task.Status, taskStatusPatch.Status) {- return &common.Error{- Code: common.Invalid,- Err: errors.Errorf("invalid task status transition from %v to %v. Applicable transition(s) %v", task.Status, taskStatusPatch.Status, applicableTaskStatusTransition[task.Status]),- }- }-- if taskStatusPatch.Skipped != nil && *taskStatusPatch.Skipped && !allowedSkippedTaskStatus[task.Status] {- return &common.Error{- Code: common.Invalid,- Err: errors.Errorf("cannot skip task whose status is %v", task.Status),- }- }-- if taskStatusPatch.Status == api.TaskCanceled {- if !taskCancellationImplemented[task.Type] {- return common.Errorf(common.NotImplemented, "Canceling task type %s is not supported", task.Type)- }- cancelAny, ok := s.stateCfg.RunningTasksCancel.Load(task.ID)- if !ok {- return errors.New("failed to cancel task")- }- cancel := cancelAny.(context.CancelFunc)- cancel()- result, err := json.Marshal(api.TaskRunResultPayload{- Detail: "Task cancellation requested.",- })- if err != nil {- return errors.Wrapf(err, "failed to marshal TaskRunResultPayload")- }- resultStr := string(result)- taskStatusPatch.Result = &resultStr- }-- taskPatched, err := s.store.UpdateTaskStatusV2(ctx, taskStatusPatch)- if err != nil {- return errors.Wrapf(err, "failed to change task %v(%v) status", task.ID, task.Name)- }-- // Most tasks belong to a pipeline which in turns belongs to an issue. The followup code- // behaves differently depending on whether the task is wrapped in an issue.- // TODO(tianzhou): Refactor the followup code into chained onTaskStatusChange hook.- issue, err := s.store.GetIssueV2(ctx, &store.FindIssueMessage{PipelineID: &task.PipelineID})- if err != nil {- return errors.Wrapf(err, "failed to fetch containing issue after changing the task status: %v", task.Name)- }-- // Create an activity- if err := s.createTaskStatusUpdateActivity(ctx, task, taskStatusPatch, issue); err != nil {- return err- }-- // Cancel every task depending on the canceled task.- if taskPatched.Status == api.TaskCanceled {- if err := s.cancelDependingTasks(ctx, taskPatched); err != nil {- return errors.Wrapf(err, "failed to cancel depending tasks for task %d", taskPatched.ID)- }- }-- if issue != nil {- if err := s.onTaskStatusPatched(ctx, issue, taskPatched); err != nil {- return err- }- }-- return nil-}--func isTaskStatusTransitionAllowed(fromStatus, toStatus api.TaskStatus) bool {- for _, allowedStatus := range applicableTaskStatusTransition[fromStatus] {- if allowedStatus == toStatus {- return true- }- }- return false-}--func (s *Scheduler) createTaskStatusUpdateActivity(ctx context.Context, task *store.TaskMessage, taskStatusPatch *api.TaskStatusPatch, issue *store.IssueMessage) error {- var issueName string- if issue != nil {- issueName = issue.Title- }- payload, err := json.Marshal(api.ActivityPipelineTaskStatusUpdatePayload{- TaskID: task.ID,- OldStatus: task.Status,- NewStatus: taskStatusPatch.Status,- IssueName: issueName,- TaskName: task.Name,- })- if err != nil {- return errors.Wrapf(err, "failed to marshal activity after changing the task status: %v", task.Name)- }-- level := api.ActivityInfo- if taskStatusPatch.Status == api.TaskFailed {- level = api.ActivityError- }- activityCreate := &store.ActivityMessage{- CreatorUID: taskStatusPatch.UpdaterID,- ContainerUID: task.PipelineID,- Type: api.ActivityPipelineTaskStatusUpdate,- Level: level,- Payload: string(payload),- }- if taskStatusPatch.Comment != nil {- activityCreate.Comment = *taskStatusPatch.Comment- }-- if _, err := s.activityManager.CreateActivity(ctx, activityCreate, &activity.Metadata{Issue: issue}); err != nil {- return err- }- return nil-}--func (s *Scheduler) cancelDependingTasks(ctx context.Context, task *store.TaskMessage) error {- queue := []int{task.ID}- seen := map[int]bool{task.ID: true}- var idList []int- dags, err := s.store.ListTaskDags(ctx, &store.TaskDAGFind{PipelineID: &task.PipelineID, StageID: &task.StageID})- if err != nil {- return err- }- for len(queue) != 0 {- fromTaskID := queue[0]- queue = queue[1:]- for _, dag := range dags {- if dag.FromTaskID != fromTaskID {- continue- }- if seen[dag.ToTaskID] {- return errors.Errorf("found a cycle in task dag, visit task %v twice", dag.ToTaskID)- }- seen[dag.ToTaskID] = true- idList = append(idList, dag.ToTaskID)- queue = append(queue, dag.ToTaskID)- }- }- if err := s.store.BatchPatchTaskStatus(ctx, idList, api.TaskCanceled, api.SystemBotID); err != nil {- return errors.Wrapf(err, "failed to change task %v's status to %s", idList, api.TaskCanceled)- }- return nil-}--// GetDefaultAssignee gets the default assignee for an issue.-func (s *Scheduler) GetDefaultAssignee(ctx context.Context, environmentID int, projectID int, issueType api.IssueType) (*store.UserMessage, error) {- policy, err := s.store.GetPipelineApprovalPolicy(ctx, environmentID)- if err != nil {- return nil, errors.Wrapf(err, "failed to GetPipelineApprovalPolicy for environmentID %d", environmentID)- }- if policy.Value == api.PipelineApprovalValueManualNever {- // use SystemBot for auto approval tasks.- systemBot, err := s.store.GetUserByID(ctx, api.SystemBotID)- if err != nil {- return nil, err- }- return systemBot, nil- }-- var groupValue *api.AssigneeGroupValue- for i, group := range policy.AssigneeGroupList {- if group.IssueType == issueType {- groupValue = &policy.AssigneeGroupList[i].Value- break- }- }- if groupValue == nil || *groupValue == api.AssigneeGroupValueWorkspaceOwnerOrDBA {- user, err := s.getAnyWorkspaceOwner(ctx)- if err != nil {- return nil, errors.Wrap(err, "failed to get a workspace owner or DBA")- }- return user, nil- } else if *groupValue == api.AssigneeGroupValueProjectOwner {- projectOwner, err := s.getAnyProjectOwner(ctx, projectID)- if err != nil {- return nil, errors.Wrap(err, "failed to get a project owner")- }- return projectOwner, nil- }- // never reached- return nil, errors.New("invalid assigneeGroupValue")-}--// getAnyWorkspaceOwner finds a default assignee from the workspace owners.-func (s *Scheduler) getAnyWorkspaceOwner(ctx context.Context) (*store.UserMessage, error) {- // There must be at least one non-systembot owner in the workspace.- owner := api.Owner- users, err := s.store.ListUsers(ctx, &store.FindUserMessage{Role: &owner})- if err != nil {- return nil, errors.Wrapf(err, "failed to get role %v", api.Owner)- }- for _, user := range users {- if user.ID == api.SystemBotID {- continue- }- return user, nil- }- return nil, errors.New("failed to get a workspace owner or DBA")-}--// getAnyProjectOwner gets a default assignee from the project owners.-func (s *Scheduler) getAnyProjectOwner(ctx context.Context, projectID int) (*store.UserMessage, error) {- policy, err := s.store.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{UID: &projectID})- if err != nil {- return nil, err- }- // Find the project member that is not workspace owner or DBA first.- for _, binding := range policy.Bindings {- if binding.Role != api.Owner || len(binding.Members) == 0 {- continue- }- for _, user := range binding.Members {- if user.Role != api.Owner && user.Role != api.DBA {- return user, nil- }- }- }- for _, binding := range policy.Bindings {- if binding.Role == api.Owner && len(binding.Members) > 0 {- return binding.Members[0], nil- }- }- return nil, errors.New("failed to get a project owner")-}--// CanPrincipalBeAssignee checks if a principal could be the assignee of an issue, judging by the principal role and the environment policy.-func (s *Scheduler) CanPrincipalBeAssignee(ctx context.Context, principalID int, environmentID int, projectID int, issueType api.IssueType) (bool, error) {- policy, err := s.store.GetPipelineApprovalPolicy(ctx, environmentID)- if err != nil {- return false, err- }- var groupValue *api.AssigneeGroupValue- for i, group := range policy.AssigneeGroupList {- if group.IssueType == issueType {- groupValue = &policy.AssigneeGroupList[i].Value- break- }- }- if groupValue == nil || *groupValue == api.AssigneeGroupValueWorkspaceOwnerOrDBA {- // no value is set, fallback to default.- // the assignee group is the workspace owner or DBA.- user, err := s.store.GetUserByID(ctx, principalID)- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to get principal by ID %d", principalID)- }- if user == nil {- return false, common.Errorf(common.NotFound, "principal not found by ID %d", principalID)- }- if s.licenseService.IsFeatureEnabled(api.FeatureRBAC) != nil {- user.Role = api.Owner- }- if user.Role == api.Owner || user.Role == api.DBA {- return true, nil- }- } else if *groupValue == api.AssigneeGroupValueProjectOwner {- // the assignee group is the project owner.- if s.licenseService.IsFeatureEnabled(api.FeatureRBAC) != nil {- // nolint:nilerr- return true, nil- }- policy, err := s.store.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{UID: &projectID})- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to get project %d policy", projectID)- }- for _, binding := range policy.Bindings {- if binding.Role != api.Owner {- continue- }- for _, member := range binding.Members {- if member.ID == principalID {- return true, nil- }- }- }- }- return false, nil-}--func (s *Scheduler) onTaskStatusPatched(ctx context.Context, issue *store.IssueMessage, taskPatched *store.TaskMessage) error {- s.metricReporter.Report(ctx, &metric.Metric{- Name: metricAPI.TaskStatusMetricName,- Value: 1,- Labels: map[string]any{- "type": taskPatched.Type,- "value": taskPatched.Status,- },- })-- stages, err := s.store.ListStageV2(ctx, taskPatched.PipelineID)- if err != nil {- return errors.Wrap(err, "failed to list stages")- }- var taskStage, nextStage *store.StageMessage- for i, stage := range stages {- if stage.ID == taskPatched.StageID {- taskStage = stages[i]- if i+1 < len(stages) {- nextStage = stages[i+1]- }- break- }- }- if taskStage == nil {- return errors.New("failed to find corresponding stage of the task in the issue pipeline")- }-- if !taskStage.Active && nextStage != nil {- // Every task in this stage has finished, we are moving to the next stage.- // The current assignee doesn't fit in the new assignee group, we will reassign a new one based on the new assignee group.- func() {- environmentID := nextStage.EnvironmentID- ok, err := s.CanPrincipalBeAssignee(ctx, issue.Assignee.ID, environmentID, issue.Project.UID, issue.Type)- if err != nil {- log.Error("failed to check if the current assignee still fits in the new assignee group", zap.Error(err))- return- }- if !ok {- // reassign the issue to a new assignee if the current one doesn't fit.- assignee, err := s.GetDefaultAssignee(ctx, environmentID, issue.Project.UID, issue.Type)- if err != nil {- log.Error("failed to get a default assignee", zap.Error(err))- return- }- if _, err := s.store.UpdateIssueV2(ctx, issue.UID, &store.UpdateIssueMessage{Assignee: assignee}, api.SystemBotID); err != nil {- log.Error("failed to update the issue assignee", zap.Error(err))- return- }- }- }()- }- if !taskStage.Active && nextStage == nil {- // Every task in the pipeline has finished.- // Resolve the issue automatically for the user.- if err := utils.ChangeIssueStatus(ctx, s.store, s.activityManager, issue, api.IssueDone, api.SystemBotID, ""); err != nil {- log.Error("failed to change the issue status to done automatically after completing every task", zap.Error(err))- }- }-- tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: &taskPatched.PipelineID, StageID: &taskPatched.StageID})- if err != nil {- return errors.Wrap(err, "failed to list tasks")- }- stageTaskHasPendingApproval := false- stageTaskAllTerminated := true- for _, task := range tasks {- if task.Status == api.TaskPendingApproval {- stageTaskHasPendingApproval = true- }- if !terminatedTaskStatus[task.Status] {- stageTaskAllTerminated = false- }- }-- // every task in the stage terminated- // create "stage ends" activity.- if stageTaskAllTerminated {- if err := func() error {- createActivityPayload := api.ActivityPipelineStageStatusUpdatePayload{- StageID: taskStage.ID,- StageStatusUpdateType: api.StageStatusUpdateTypeEnd,- IssueName: issue.Title,- StageName: taskStage.Name,- }- bytes, err := json.Marshal(createActivityPayload)- if err != nil {- return errors.Wrap(err, "failed to marshal ActivityPipelineStageStatusUpdate payload")- }- activityCreate := &store.ActivityMessage{- CreatorUID: api.SystemBotID,- ContainerUID: *issue.PipelineUID,- Type: api.ActivityPipelineStageStatusUpdate,- Level: api.ActivityInfo,- Payload: string(bytes),- }- if _, err := s.activityManager.CreateActivity(ctx, activityCreate, &activity.Metadata{- Issue: issue,- }); err != nil {- return errors.Wrap(err, "failed to create activity")- }- return nil- }(); err != nil {- log.Error("failed to create ActivityPipelineStageStatusUpdate activity", zap.Error(err))- }- }-- // every task in the stage completes and this is not the last stage.- // create "stage begins" activity.- if !taskStage.Active && nextStage != nil {- if err := func() error {- createActivityPayload := api.ActivityPipelineStageStatusUpdatePayload{- StageID: nextStage.ID,- StageStatusUpdateType: api.StageStatusUpdateTypeBegin,- IssueName: issue.Title,- StageName: nextStage.Name,- }- bytes, err := json.Marshal(createActivityPayload)- if err != nil {- return errors.Wrap(err, "failed to marshal ActivityPipelineStageStatusUpdate payload")- }- activityCreate := &store.ActivityMessage{- CreatorUID: api.SystemBotID,- ContainerUID: *issue.PipelineUID,- Type: api.ActivityPipelineStageStatusUpdate,- Level: api.ActivityInfo,- Payload: string(bytes),- }- if _, err := s.activityManager.CreateActivity(ctx, activityCreate, &activity.Metadata{- Issue: issue,- }); err != nil {- return errors.Wrap(err, "failed to create activity")- }- return nil- }(); err != nil {- log.Error("failed to create ActivityPipelineStageStatusUpdate activity", zap.Error(err))- }- }-- // there isn't a pendingApproval task- // we need to set issue.AssigneeNeedAttention to false for UI workflow.- if taskPatched.Status == api.TaskPending && issue.Project.Workflow == api.UIWorkflow && !stageTaskHasPendingApproval {- needAttention := false- if _, err := s.store.UpdateIssueV2(ctx, issue.UID, &store.UpdateIssueMessage{NeedAttention: &needAttention}, api.SystemBotID); err != nil {- log.Error("failed to patch issue assigneeNeedAttention after finding out that there isn't any pendingApproval task in the stage", zap.Error(err))- }- }-- return nil-}--// CanPrincipalChangeIssueStageTaskStatus returns whether the principal can change the task status.-func (s *Scheduler) CanPrincipalChangeIssueStageTaskStatus(ctx context.Context, user *store.UserMessage, issue *store.IssueMessage, stageEnvironmentID int, toStatus api.TaskStatus) (bool, error) {- // the workspace owner and DBA roles can always change task status.- if user.Role == api.Owner || user.Role == api.DBA {- return true, nil- }- // The creator can cancel task.- if toStatus == api.TaskCanceled {- if user.ID == issue.Creator.ID {- return true, nil- }- }- groupValue, err := s.getGroupValueForIssueTypeEnvironment(ctx, issue.Type, stageEnvironmentID)- if err != nil {- return false, errors.Wrapf(err, "failed to get assignee group value for issueID %d", issue.UID)- }- // as the policy says, the project owner has the privilege to change task status.- if groupValue == api.AssigneeGroupValueProjectOwner {- policy, err := s.store.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{UID: &issue.Project.UID})- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to get project %d policy", issue.Project.UID)- }- for _, binding := range policy.Bindings {- if binding.Role != api.Owner {- continue- }- for _, member := range binding.Members {- if member.ID == user.ID {- return true, nil- }- }- }- }- return false, nil-}--func (s *Scheduler) getGroupValueForIssueTypeEnvironment(ctx context.Context, issueType api.IssueType, environmentID int) (api.AssigneeGroupValue, error) {- defaultGroupValue := api.AssigneeGroupValueWorkspaceOwnerOrDBA- policy, err := s.store.GetPipelineApprovalPolicy(ctx, environmentID)- if err != nil {- return defaultGroupValue, errors.Wrapf(err, "failed to get pipeline approval policy by environmentID %d", environmentID)- }- if policy == nil {- return defaultGroupValue, nil- }-- for _, assigneeGroup := range policy.AssigneeGroupList {- if assigneeGroup.IssueType == issueType {- return assigneeGroup.Value, nil- }- }- return defaultGroupValue, nil-}diff --git a/backend/server/issue.go b/backend/server/issue.goindex 16b506da34..507c47b7c1 100644--- a/backend/server/issue.go+++ b/backend/server/issue.go@@ -251,28 +251,6 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {return err}updateIssueMessage.Assignee = assignee- if issue.PipelineUID != nil {- stages, err := s.store.ListStageV2(ctx, *issue.PipelineUID)- if err != nil {- return err- }- activeStage := utils.GetActiveStage(stages)- // When all stages have finished, assignee can be anyone such as creator.- if activeStage != nil {- ok, err := s.taskScheduler.CanPrincipalBeAssignee(ctx, assignee.ID, activeStage.EnvironmentID, issue.Project.UID, issue.Type)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to check if the assignee can be changed").SetInternal(err)- }- if !ok {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Cannot set assignee with user id %d", *issuePatch.AssigneeID)).SetInternal(err)- }- }- }- // set AssigneeNeedAttention to false on assignee change- if issue.Project.Workflow == api.UIWorkflow {- needAttention := false- updateIssueMessage.NeedAttention = &needAttention- }}updatedIssue, err := s.store.UpdateIssueV2(ctx, id, updateIssueMessage, updaterID)@@ -505,11 +483,6 @@ func (s *Server) createGrantRequestIssue(ctx context.Context, issueCreate *api.I}func (s *Server) setTaskProgressForIssue(issue *api.Issue) {- if s.taskScheduler == nil || issue.Pipeline == nil {- // readonly server doesn't have a TaskScheduler.- // Skip issues without pipelines.- return- }for _, stage := range issue.Pipeline.StageList {for _, task := range stage.TaskList {if progress, ok := s.stateCfg.TaskProgress.Load(task.ID); ok {diff --git a/backend/server/server.go b/backend/server/server.goindex 71ffdeb329..f3b106a215 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -154,8 +154,6 @@ const (// Server is the Bytebase server.type Server struct {// Asynchronous runners.- // TODO(d): deprecate taskScheduler.- taskScheduler *taskrun.SchedulerTaskSchedulerV2 *taskrun.SchedulerV2PlanCheckScheduler *plancheck.SchedulerMetricReporter *metricreport.Reporter@@ -478,8 +476,6 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {s.TaskSchedulerV2.Register(api.TaskDatabaseRestorePITRRestore, taskrun.NewPITRRestoreExecutor(storeInstance, s.dbFactory, s.s3Client, s.SchemaSyncer, s.stateCfg, profile))s.TaskSchedulerV2.Register(api.TaskDatabaseRestorePITRCutover, taskrun.NewPITRCutoverExecutor(storeInstance, s.dbFactory, s.SchemaSyncer, s.BackupRunner, s.ActivityManager, profile))- s.taskScheduler = taskrun.NewScheduler(storeInstance, s.ActivityManager, s.licenseService, s.stateCfg, s.MetricReporter)-s.RollbackRunner = rollbackrun.NewRunner(&profile, storeInstance, s.dbFactory, s.stateCfg)s.MailSender = mail.NewSender(s.store, s.stateCfg)s.RelayRunner = relay.NewRunner(storeInstance, s.ActivityManager, s.stateCfg)@@ -551,7 +547,6 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {s.registerIssueRoutes(apiGroup)s.registerIssueSubscriberRoutes(apiGroup)s.registerTaskRoutes(apiGroup)- s.registerStageRoutes(apiGroup)// Register healthz endpoint.e.GET("/healthz", func(c echo.Context) error {diff --git a/backend/server/stage.go b/backend/server/stage.godeleted file mode 100644index 25a2d45de7..0000000000--- a/backend/server/stage.go+++ /dev/null@@ -1,114 +0,0 @@-package server--import (- "fmt"- "net/http"- "strconv"-- "github.com/google/jsonapi"- "github.com/labstack/echo/v4"-- api "github.com/bytebase/bytebase/backend/legacyapi"- "github.com/bytebase/bytebase/backend/store"- "github.com/bytebase/bytebase/backend/utils"-)--func (s *Server) registerStageRoutes(g *echo.Group) {- // This function patches the status of all tasks in the stage.- g.PATCH("/pipeline/:pipelineID/stage/:stageID/status", func(c echo.Context) error {- ctx := c.Request().Context()- stageID, err := strconv.Atoi(c.Param("stageID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Stage ID is not a number: %s", c.Param("stageID"))).SetInternal(err)- }- pipelineID, err := strconv.Atoi(c.Param("pipelineID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Pipeline ID is not a number: %s", c.Param("pipelineID"))).SetInternal(err)- }- stages, err := s.store.ListStageV2(ctx, pipelineID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find stage %v", stageID)).SetInternal(err)- }-- var stage *store.StageMessage- for _, v := range stages {- if v.ID == stageID {- stage = v- break- }- }- if stage == nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid stage %v", stageID))- }- activeStage := utils.GetActiveStage(stages)- if activeStage == nil {- return echo.NewHTTPError(http.StatusBadRequest, "all stages are done")- }-- currentPrincipalID := c.Get(getPrincipalIDContextKey()).(int)- stageAllTaskStatusPatch := &api.StageAllTaskStatusPatch{- ID: stageID,- UpdaterID: currentPrincipalID,- }- if err := jsonapi.UnmarshalPayload(c.Request().Body, stageAllTaskStatusPatch); err != nil {- return echo.NewHTTPError(http.StatusBadRequest, "Malformed update stage tasks status request").SetInternal(err)- }-- if stageAllTaskStatusPatch.Status != api.TaskPending {- return echo.NewHTTPError(http.StatusBadRequest, "Only support status transitioning from PENDING_APPROVAL to PENDING")- }-- issue, err := s.store.GetIssueV2(ctx, &store.FindIssueMessage{PipelineID: &pipelineID})- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue with pipeline ID %d", pipelineID)).SetInternal(err)- }- if issue == nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Issue not found with pipeline ID %d", pipelineID))- }- approved, err := utils.CheckIssueApproved(issue)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to check if the issue is approved").SetInternal(err)- }- if !approved {- return echo.NewHTTPError(http.StatusBadRequest, "Cannot patch task status because the issue is not approved")- }-- pendingApprovalStatus := []api.TaskStatus{api.TaskPendingApproval}- tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: &pipelineID, StageID: &stageID, StatusList: &pendingApprovalStatus})- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get tasks").SetInternal(err)- }- if len(tasks) == 0 {- return echo.NewHTTPError(http.StatusInternalServerError, "No task to approve in the stage")- }-- // pick any task in the stage to validate- // because all tasks in the same stage share the issue & environment.- ok, err := s.taskScheduler.CanPrincipalChangeTaskStatus(ctx, currentPrincipalID, tasks[0], stageAllTaskStatusPatch.Status)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to validate if the principal can change task status").SetInternal(err)- }- if !ok {- return echo.NewHTTPError(http.StatusForbidden, "Not allowed to change task status")- }-- if stageAllTaskStatusPatch.Status == api.TaskPending {- if tasks[0].StageID != activeStage.ID {- return echo.NewHTTPError(http.StatusBadRequest, "We can only approve the earliest stage with incompleted tasks")- }- }-- var taskIDList []int- for _, task := range tasks {- taskIDList = append(taskIDList, task.ID)- }- if err := s.store.BatchPatchTaskStatus(ctx, taskIDList, api.TaskPending, currentPrincipalID); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update task %q status", taskIDList)).SetInternal(err)- }- if err := s.ActivityManager.BatchCreateTaskStatusUpdateApprovalActivity(ctx, tasks, currentPrincipalID, issue, stage.Name); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "failed to create task status update activity").SetInternal(err)- }-- return c.String(http.StatusOK, "")- })-}diff --git a/backend/server/task.go b/backend/server/task.goindex dc66ea6a68..8040f55449 100644--- a/backend/server/task.go+++ b/backend/server/task.go@@ -1,15 +1,19 @@package serverimport (+ "context"+ "encoding/json""fmt""net/http""strconv""github.com/google/jsonapi""github.com/labstack/echo/v4"+ "github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/component/activity"api "github.com/bytebase/bytebase/backend/legacyapi""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/utils"@@ -69,7 +73,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {taskPatch := *taskPatchtaskPatch.ID = task.ID// TODO(d): patch tasks in batch.- if err := s.taskScheduler.PatchTask(ctx, task, &taskPatch, issue); err != nil {+ if err := patchTask(ctx, s.store, s.ActivityManager, task, &taskPatch, issue); err != nil {return err}}@@ -153,7 +157,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusBadRequest, "cannot generate rollback SQL statement for a non-DML task")}- if err := s.taskScheduler.PatchTask(ctx, task, taskPatch, issue); err != nil {+ if err := patchTask(ctx, s.store, s.ActivityManager, task, taskPatch, issue); err != nil {return err}@@ -191,121 +195,43 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {}return nil})+}- g.PATCH("/pipeline/:pipelineID/task/:taskID/status", func(c echo.Context) error {- ctx := c.Request().Context()- taskID, err := strconv.Atoi(c.Param("taskID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Task ID is not a number: %s", c.Param("taskID"))).SetInternal(err)- }-- currentPrincipalID := c.Get(getPrincipalIDContextKey()).(int)- taskStatusPatch := &api.TaskStatusPatch{- ID: taskID,- UpdaterID: currentPrincipalID,- }- if err := jsonapi.UnmarshalPayload(c.Request().Body, taskStatusPatch); err != nil {- return echo.NewHTTPError(http.StatusBadRequest, "Malformed update task status request").SetInternal(err)- }-- task, err := s.store.GetTaskV2ByID(ctx, taskID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to update task status").SetInternal(err)- }- if task == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Task not found with ID %d", taskID))- }-- ok, err := s.taskScheduler.CanPrincipalChangeTaskStatus(ctx, currentPrincipalID, task, taskStatusPatch.Status)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to validate if the principal can change task status").SetInternal(err)- }- if !ok {- return echo.NewHTTPError(http.StatusForbidden, "Not allowed to change task status")- }-- if taskStatusPatch.Status == api.TaskPending {- issue, err := s.store.GetIssueV2(ctx, &store.FindIssueMessage{PipelineID: &task.PipelineID})- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue with pipeline ID %d", task.PipelineID)).SetInternal(err)- }- if issue == nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Issue not found with pipeline ID %d", task.PipelineID))- }- approved, err := utils.CheckIssueApproved(issue)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to check if the issue is approved").SetInternal(err)- }- if !approved {- return echo.NewHTTPError(http.StatusBadRequest, "Cannot patch task status because the issue is not approved")- }-- stages, err := s.store.ListStageV2(ctx, task.PipelineID)- if err != nil {- return err- }- activeStage := utils.GetActiveStage(stages)- if activeStage == nil {- return echo.NewHTTPError(http.StatusBadRequest, "All tasks are done already")- }- if task.StageID != activeStage.ID {- return echo.NewHTTPError(http.StatusBadRequest, "Tasks in the prior stage are not done yet")- }- }-- if taskStatusPatch.Status == api.TaskDone {- // the user marks the task as DONE, set Skipped to true and SkippedReason to Comment.- skipped := true- taskStatusPatch.Skipped = &skipped- taskStatusPatch.SkippedReason = taskStatusPatch.Comment- }-- if err := s.taskScheduler.PatchTaskStatus(ctx, task, taskStatusPatch); err != nil {- if common.ErrorCode(err) == common.Invalid {- return echo.NewHTTPError(http.StatusBadRequest, common.ErrorMessage(err))- }- if common.ErrorCode(err) == common.NotImplemented {- return echo.NewHTTPError(http.StatusNotImplemented, common.ErrorMessage(err))- }- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update task \"%v\" status", task.Name)).SetInternal(err)- }-- composedTask, err := s.store.GetTaskByID(ctx, task.ID)- if err != nil {- return err- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- if err := jsonapi.MarshalPayload(c.Response().Writer, composedTask); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal update task \"%v\" status response", task.Name)).SetInternal(err)- }- return nil- })-- g.POST("/pipeline/:pipelineID/task/:taskID/check", func(c echo.Context) error {- ctx := c.Request().Context()- taskID, err := strconv.Atoi(c.Param("taskID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Task ID is not a number: %s", c.Param("taskID"))).SetInternal(err)- }-- task, err := s.store.GetTaskV2ByID(ctx, taskID)+// patchTask patches the statement for a task.+func patchTask(ctx context.Context, stores *store.Store, activityManager *activity.Manager, task *store.TaskMessage, taskPatch *api.TaskPatch, issue *store.IssueMessage) error {+ taskPatched, err := stores.UpdateTaskV2(ctx, taskPatch)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update task \"%v\"", task.Name)).SetInternal(err)+ }+ if taskPatch.SheetID != nil {+ oldSheetID, err := utils.GetTaskSheetID(task.Payload)if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to update task status").SetInternal(err)- }- if task == nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Task not found with ID %d", taskID))- }-- composedTask, err := s.store.GetTaskByID(ctx, task.ID)+ return errors.Wrap(err, "failed to get old sheet ID")+ }+ newSheetID := *taskPatch.SheetID++ // create a task sheet update activity+ payload, err := json.Marshal(api.ActivityPipelineTaskStatementUpdatePayload{+ TaskID: taskPatched.ID,+ OldSheetID: oldSheetID,+ NewSheetID: newSheetID,+ TaskName: task.Name,+ IssueName: issue.Title,+ })if err != nil {- return err- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- if err := jsonapi.MarshalPayload(c.Response().Writer, composedTask); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal update task \"%v\" status response", task.Name)).SetInternal(err)- }- return nil- })+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity after updating task sheet: %v", taskPatched.Name).SetInternal(err)+ }+ if _, err := activityManager.CreateActivity(ctx, &store.ActivityMessage{+ CreatorUID: taskPatch.UpdaterID,+ ContainerUID: taskPatched.PipelineID,+ Type: api.ActivityPipelineTaskStatementUpdate,+ Payload: string(payload),+ Level: api.ActivityInfo,+ }, &activity.Metadata{+ Issue: issue,+ }); err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create activity after updating task statement: %v", taskPatched.Name)).SetInternal(err)+ }+ }+ return nil}diff --git a/backend/server/webhook.go b/backend/server/webhook.goindex 7ed6c656f7..262c1c2f7d 100644--- a/backend/server/webhook.go+++ b/backend/server/webhook.go@@ -2124,7 +2124,7 @@ func (s *Server) tryUpdateTasksFromModifiedFile(ctx context.Context, databases [SheetID: &sheet.UID,UpdaterID: api.SystemBotID,}- if err := s.taskScheduler.PatchTask(ctx, task, &taskPatch, issue); err != nil {+ if err := patchTask(ctx, s.store, s.ActivityManager, task, &taskPatch, issue); err != nil {log.Error("Failed to patch task with the same migration version", zap.Int("issueID", issue.UID), zap.Int("taskID", task.ID), zap.Error(err))return nil}