Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: deprecate legacy (#8433)
Danny Xuf628bfd121c1
diff --git a/backend/api/gitops/legacy.go b/backend/api/gitops/legacy.gonew file mode 100644index 0000000000..848f5047c7--- /dev/null+++ b/backend/api/gitops/legacy.go@@ -0,0 +1,30 @@+package gitops++import "github.com/bytebase/bytebase/backend/plugin/db"++// migrationDetail is the detail for database migration such as Migrate, Data.+type migrationDetail struct {+ // migrationType is the type of a migration.+ // migrationType can be empty for gh-ost type of migration.+ migrationType db.MigrationType+ // databaseID is the ID of a database.+ // This should be unset when a project is in tenant mode. The ProjectID is derived from IssueCreate.+ databaseID int+ // sheetID is the ID of a sheet. Statement and sheet ID is mutually exclusive.+ sheetID int+ // schemaVersion is parsed from VCS file name.+ // It is automatically generated in the UI workflow.+ schemaVersion string+}++// MigrationFileYAMLDatabase contains the information of a database in a YAML+// format migration file.+type MigrationFileYAMLDatabase struct {+ Name string `yaml:"name"` // The name of the database+}++// MigrationFileYAML contains the information in a YAML format migration file.+type MigrationFileYAML struct {+ Databases []MigrationFileYAMLDatabase `yaml:"databases"` // The list of databases and how to identify them+ Statement string `yaml:"statement"` // The SQL statement to be executed to specified list of databases+}diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.goindex 22f642c050..b16d9e1afa 100644--- a/backend/api/gitops/webhook.go+++ b/backend/api/gitops/webhook.go@@ -1445,7 +1445,7 @@ func (s *Service) processFilesInProject(ctx context.Context, pushEvent vcs.PushEreturn s.processFilesInBatchProject(ctx, pushEvent, repoInfo, fileInfoList)}- var migrationDetailList []*api.MigrationDetail+ var migrationDetailList []*migrationDetailvar activityCreateList []*store.ActivityMessagevar createdIssueList []stringvar fileNameList []string@@ -1492,7 +1492,7 @@ func (s *Service) processFilesInProject(ctx context.Context, pushEvent vcs.PushE// Create one issue per push event for DDL project, or non-schema files for SDL project.migrateType := "Change data"for _, d := range migrationDetailList {- if d.MigrationType == db.Migrate {+ if d.migrationType == db.Migrate {migrateType = "Alter schema"break}@@ -1542,7 +1542,7 @@ func (s *Service) processFilesInBatchProject(ctx context.Context, pushEvent vcs.migrationDetail := migrationDetailListForFile[0]activityCreateList = append(activityCreateList, activityCreateListForFile...)migrateType := "Change data"- if migrationDetail.MigrationType == db.Migrate {+ if migrationDetail.migrationType == db.Migrate {migrateType = "Alter schema"}description := strings.ReplaceAll(fileInfoList[0].migrationInfo.Description, "_", " ")@@ -1578,11 +1578,11 @@ func sortFilesBySchemaVersion(fileInfoList []fileInfo) []fileInfo {return ret}-func (s *Service) createIssueFromMigrationDetailsV2(ctx context.Context, project *store.ProjectMessage, issueName, issueDescription string, pushEvent vcs.PushEvent, creatorID int, migrationDetailList []*api.MigrationDetail) error {+func (s *Service) createIssueFromMigrationDetailsV2(ctx context.Context, project *store.ProjectMessage, issueName, issueDescription string, pushEvent vcs.PushEvent, creatorID int, migrationDetailList []*migrationDetail) error {var steps []*v1pb.Plan_Step- if len(migrationDetailList) == 1 && migrationDetailList[0].DatabaseID == 0 {+ if len(migrationDetailList) == 1 && migrationDetailList[0].databaseID == 0 {migrationDetail := migrationDetailList[0]- changeType := getChangeType(migrationDetail.MigrationType)+ changeType := getChangeType(migrationDetail.migrationType)steps = []*v1pb.Plan_Step{{Specs: []*v1pb.Plan_Spec{@@ -1591,8 +1591,8 @@ func (s *Service) createIssueFromMigrationDetailsV2(ctx context.Context, projectChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{Type: changeType,Target: fmt.Sprintf("projects/%s/deploymentConfigs/default", project.ResourceID),- Sheet: fmt.Sprintf("projects/%s/sheets/%d", project.ResourceID, migrationDetail.SheetID),- SchemaVersion: migrationDetail.SchemaVersion,+ Sheet: fmt.Sprintf("projects/%s/sheets/%d", project.ResourceID, migrationDetail.sheetID),+ SchemaVersion: migrationDetail.schemaVersion,},},},@@ -1610,16 +1610,16 @@ func (s *Service) createIssueFromMigrationDetailsV2(ctx context.Context, project}allSteps := make([]*v1pb.Plan_Step, len(environments))for _, migrationDetail := range migrationDetailList {- if migrationDetail.DatabaseID == 0 {+ if migrationDetail.databaseID == 0 {// TODO(d): should never reach this.return errors.Errorf("tenant database is not supported yet")}- database, err := s.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{UID: &migrationDetail.DatabaseID})+ database, err := s.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{UID: &migrationDetail.databaseID})if err != nil {return err}if database == nil {- return errors.Errorf("database %d not found", migrationDetail.DatabaseID)+ return errors.Errorf("database %d not found", migrationDetail.databaseID)}environment, err := s.store.GetEnvironmentV2(ctx, &store.FindEnvironmentMessage{ResourceID: &database.EffectiveEnvironmentID})if err != nil {@@ -1637,10 +1637,10 @@ func (s *Service) createIssueFromMigrationDetailsV2(ctx context.Context, projectstep.Specs = append(step.Specs, &v1pb.Plan_Spec{Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{- Type: getChangeType(migrationDetail.MigrationType),+ Type: getChangeType(migrationDetail.migrationType),Target: fmt.Sprintf("instances/%s/databases/%s", database.InstanceID, database.DatabaseName),- Sheet: fmt.Sprintf("projects/%s/sheets/%d", project.ResourceID, migrationDetail.SheetID),- SchemaVersion: migrationDetail.SchemaVersion,+ Sheet: fmt.Sprintf("projects/%s/sheets/%d", project.ResourceID, migrationDetail.sheetID),+ SchemaVersion: migrationDetail.schemaVersion,},},})@@ -1893,7 +1893,7 @@ func (s *Service) readFileContent(ctx context.Context, pushEvent vcs.PushEvent,// prepareIssueFromSDLFile returns the migration info and a list of update// schema details derived from the given push event for SDL.-func (s *Service) prepareIssueFromSDLFile(ctx context.Context, repoInfo *repoInfo, pushEvent vcs.PushEvent, schemaInfo *db.MigrationInfo, file string) ([]*api.MigrationDetail, []*store.ActivityMessage) {+func (s *Service) prepareIssueFromSDLFile(ctx context.Context, repoInfo *repoInfo, pushEvent vcs.PushEvent, schemaInfo *db.MigrationInfo, file string) ([]*migrationDetail, []*store.ActivityMessage) {dbName := schemaInfo.Databaseif dbName == "" && repoInfo.project.TenantMode == api.TenantModeDisabled {slog.Debug("Ignored schema file without a database name", slog.String("file", file))@@ -1926,12 +1926,12 @@ func (s *Service) prepareIssueFromSDLFile(ctx context.Context, repoInfo *repoInfreturn nil, []*store.ActivityMessage{activityCreate}}- var migrationDetailList []*api.MigrationDetail+ var migrationDetailList []*migrationDetailif repoInfo.project.TenantMode == api.TenantModeTenant {- return []*api.MigrationDetail{+ return []*migrationDetail{{- MigrationType: db.MigrateSDL,- SheetID: sheet.UID,+ migrationType: db.MigrateSDL,+ sheetID: sheet.UID,},}, nil}@@ -1944,10 +1944,10 @@ func (s *Service) prepareIssueFromSDLFile(ctx context.Context, repoInfo *repoInffor _, database := range databases {migrationDetailList = append(migrationDetailList,- &api.MigrationDetail{- MigrationType: db.MigrateSDL,- DatabaseID: database.UID,- SheetID: sheet.UID,+ &migrationDetail{+ migrationType: db.MigrateSDL,+ databaseID: database.UID,+ sheetID: sheet.UID,},)}@@ -1962,7 +1962,7 @@ func (s *Service) prepareIssueFromFile(repoInfo *repoInfo,pushEvent vcs.PushEvent,fileInfo fileInfo,-) ([]*api.MigrationDetail, []*store.ActivityMessage) {+) ([]*migrationDetail, []*store.ActivityMessage) {content, err := s.readFileContent(ctx, pushEvent, repoInfo, fileInfo.item.FileName)if err != nil {return nil, []*store.ActivityMessage{@@ -1998,16 +1998,16 @@ func (s *Service) prepareIssueFromFile(return nil, []*store.ActivityMessage{activityCreate}}- return []*api.MigrationDetail{+ return []*migrationDetail{{- MigrationType: fileInfo.migrationInfo.Type,- SheetID: sheet.UID,- SchemaVersion: fmt.Sprintf("%s-%s", fileInfo.migrationInfo.Version, fileInfo.migrationInfo.Type.GetVersionTypeSuffix()),+ migrationType: fileInfo.migrationInfo.Type,+ sheetID: sheet.UID,+ schemaVersion: fmt.Sprintf("%s-%s", fileInfo.migrationInfo.Version, fileInfo.migrationInfo.Type.GetVersionTypeSuffix()),},}, nil}- var migrationFile api.MigrationFileYAML+ var migrationFile MigrationFileYAMLerr = yaml.Unmarshal([]byte(content), &migrationFile)if err != nil {return nil, []*store.ActivityMessage{@@ -2035,7 +2035,7 @@ func (s *Service) prepareIssueFromFile(return nil, []*store.ActivityMessage{activityCreate}}- var migrationDetailList []*api.MigrationDetail+ var migrationDetailList []*migrationDetailfor _, database := range migrationFile.Databases {dbList, err := s.findProjectDatabases(ctx, repoInfo.project.UID, database.Name, "")if err != nil {@@ -2051,11 +2051,11 @@ func (s *Service) prepareIssueFromFile(for _, db := range dbList {migrationDetailList = append(migrationDetailList,- &api.MigrationDetail{- MigrationType: fileInfo.migrationInfo.Type,- DatabaseID: db.UID,- SheetID: sheet.UID,- SchemaVersion: fmt.Sprintf("%s-%s", fileInfo.migrationInfo.Version, fileInfo.migrationInfo.Type.GetVersionTypeSuffix()),+ &migrationDetail{+ migrationType: fileInfo.migrationInfo.Type,+ databaseID: db.UID,+ sheetID: sheet.UID,+ schemaVersion: fmt.Sprintf("%s-%s", fileInfo.migrationInfo.Version, fileInfo.migrationInfo.Type.GetVersionTypeSuffix()),},)}@@ -2086,14 +2086,14 @@ func (s *Service) prepareIssueFromFile(return nil, []*store.ActivityMessage{activityCreate}}- var migrationDetailList []*api.MigrationDetail+ var migrationDetailList []*migrationDetailfor _, database := range databases {migrationDetailList = append(migrationDetailList,- &api.MigrationDetail{- MigrationType: fileInfo.migrationInfo.Type,- DatabaseID: database.UID,- SheetID: sheet.UID,- SchemaVersion: fmt.Sprintf("%s-%s", fileInfo.migrationInfo.Version, fileInfo.migrationInfo.Type.GetVersionTypeSuffix()),+ &migrationDetail{+ migrationType: fileInfo.migrationInfo.Type,+ databaseID: database.UID,+ sheetID: sheet.UID,+ schemaVersion: fmt.Sprintf("%s-%s", fileInfo.migrationInfo.Version, fileInfo.migrationInfo.Type.GetVersionTypeSuffix()),},)}diff --git a/backend/legacyapi/issue.go b/backend/legacyapi/issue.goindex 7accda6abe..af2d6c4933 100644--- a/backend/legacyapi/issue.go+++ b/backend/legacyapi/issue.go@@ -1,10 +1,5 @@package api-import (- "github.com/bytebase/bytebase/backend/plugin/db"- "github.com/bytebase/bytebase/backend/plugin/vcs"-)-// IssueStatus is the status of an issue.type IssueStatus string@@ -48,176 +43,3 @@ const (// IssueFieldSQL is the field ID for SQL.IssueFieldSQL IssueFieldID = "7")--// Issue is the API message for an issue.-type Issue struct {- ID int `jsonapi:"primary,issue"`-- // Standard fields- CreatorID int- Creator *Principal `jsonapi:"relation,creator"`- CreatedTs int64 `jsonapi:"attr,createdTs"`- UpdaterID int- Updater *Principal `jsonapi:"relation,updater"`- UpdatedTs int64 `jsonapi:"attr,updatedTs"`-- // Related fields- ProjectID int- Project *Project `jsonapi:"relation,project"`- PipelineID *int- Pipeline *Pipeline `jsonapi:"relation,pipeline"`- // The requested grant in GrantRequest format.- GrantRequest string `jsonapi:"attr,grantRequest"`-- // Domain specific fields- Name string `jsonapi:"attr,name"`- Status IssueStatus `jsonapi:"attr,status"`- Type IssueType `jsonapi:"attr,type"`- Description string `jsonapi:"attr,description"`- AssigneeID int `jsonapi:"attr,assigneeId"`- Assignee *Principal `jsonapi:"relation,assignee"`- SubscriberList []*Principal `jsonapi:"relation,subscriberList"`- Payload string `jsonapi:"attr,payload"`-}--// IssueResponse is the API message for an issue response.-type IssueResponse struct {- Issues []*Issue `jsonapi:"relation,issues"`- NextToken string `jsonapi:"attr,nextToken"`-}--// IssueCreate is the API message for creating an issue.-type IssueCreate struct {- ProjectID int `jsonapi:"attr,projectId"`- Name string `jsonapi:"attr,name"`- Type IssueType `jsonapi:"attr,type"`- Description string `jsonapi:"attr,description"`- AssigneeID int `jsonapi:"attr,assigneeId"`- Payload string `jsonapi:"attr,payload"`- // CreateContext is used to create the issue pipeline and not persisted.- // The context format depends on the issue type. For example, create database issue corresponds to CreateDatabaseContext.- // This consolidates the pipeline generation to backend because both frontend and VCS pipeline could create issues and- // we want the complexity resides in the backend.- CreateContext string `jsonapi:"attr,createContext"`- // ValidateOnly validates the request and previews the review, but does not actually post it.- ValidateOnly bool `jsonapi:"attr,validateOnly"`-}--// CreateDatabaseContext is the issue create context for creating a database.-type CreateDatabaseContext struct {- // InstanceID is the ID of an instance.- InstanceID int `json:"instanceId"`- // DatabaseName is the name of the database.- DatabaseName string `json:"databaseName"`- // TableName is the name of the table, if it is not empty, Bytebase should create a table after creating the database.- // For example, in MongoDB, it only creates the database when we first store data in that database.- TableName string `json:"tableName"`- // CharacterSet is the character set of the database.- CharacterSet string `json:"characterSet"`- // Collation is the collation of the database.- Collation string `json:"collation"`- // Cluster is the cluster of the database. This is only applicable to ClickHouse for "ON CLUSTER <<cluster>>".- Cluster string `json:"cluster"`- // Owner is the owner of the database. This is only applicable to Postgres for "WITH OWNER <<owner>>".- Owner string `json:"owner"`- // BackupID is the ID of the backup.- BackupID int `json:"backupId"`- // Labels is a json-encoded string from a list of DatabaseLabel.- // See definition in api.Database.- Labels string `jsonapi:"attr,labels,omitempty"`-}--// RollbackDetail is the detail for rolling back a task.-type RollbackDetail struct {- // IssueID is the id of the issue to rollback.- IssueID int `json:"issueId"`- // TaskID is the task id to rollback.- TaskID int `json:"taskId"`-}--// MigrationDetail is the detail for database migration such as Migrate, Data.-type MigrationDetail struct {- // MigrationType is the type of a migration.- // MigrationType can be empty for gh-ost type of migration.- MigrationType db.MigrationType `json:"migrationType"`- // DatabaseID is the ID of a database.- // This should be unset when a project is in tenant mode. The ProjectID is derived from IssueCreate.- DatabaseID int `json:"databaseId"`- // SheetID is the ID of a sheet. Statement and sheet ID is mutually exclusive.- SheetID int `json:"sheetId"`- // DatabaseGroupName is the name of a database group.- DatabaseGroupName string `json:"databaseGroupName"`- // SchemaGroupName is the name of a schema group.- SchemaGroupName string `json:"schemaGroupName"`- // EarliestAllowedTs the earliest execution time of the change at system local Unix timestamp in seconds.- EarliestAllowedTs int64 `json:"earliestAllowedTs"`- // SchemaVersion is parsed from VCS file name.- // It is automatically generated in the UI workflow.- SchemaVersion string `json:"schemaVersion"`- // If RollbackEnabled, build the RollbackSheetID of the task.- RollbackEnabled bool `json:"rollbackEnabled"`- // if RollbackDetail is not nil, then this task is for rolling back another task.- RollbackDetail *RollbackDetail `json:"rollbackDetail"`- // Statement used by grouping batch change, Bytebase use it to render.- Statement string `json:"statement"`-}--// MigrationContext is the issue create context for database migration such as Migrate, Data.-type MigrationContext struct {- // DetailList is the details of schema update.- // When a project is in tenant mode, there should be one item in the list.- DetailList []*MigrationDetail `json:"detailList"`- // VCSPushEvent is the event information for VCS push.- VCSPushEvent *vcs.PushEvent `json:"vcsPushEvent"`-}--// MigrationFileYAMLDatabase contains the information of a database in a YAML-// format migration file.-type MigrationFileYAMLDatabase struct {- Name string `yaml:"name"` // The name of the database-}--// MigrationFileYAML contains the information in a YAML format migration file.-type MigrationFileYAML struct {- Databases []MigrationFileYAMLDatabase `yaml:"databases"` // The list of databases and how to identify them- Statement string `yaml:"statement"` // The SQL statement to be executed to specified list of databases-}--// PITRContext is the issue create context for performing a PITR in a database.-type PITRContext struct {- DatabaseID int `json:"databaseId"`-- // CreateDatabaseCtx will be not nil if user want to restore to a new database.- // The new database should be under the same project as the original database.- CreateDatabaseCtx *CreateDatabaseContext `json:"createDatabaseContext"`-- // BackupID and PointInTimeTs only allow one non-nil.-- // BackupID is not nil if the user just restore a full backup only.- BackupID *int `json:"backupId"`-- // After the PITR operations, the database will be recovered to the state at this time.- // Represented in UNIX timestamp in seconds.- PointInTimeTs *int64 `json:"pointInTimeTs"`-}--// IssuePatch is the API message for patching an issue.-type IssuePatch struct {- Name *string `jsonapi:"attr,name"`- Description *string `jsonapi:"attr,description"`- AssigneeID *int `jsonapi:"attr,assigneeId"`- Payload *string `jsonapi:"attr,payload"`-}--// IssueStatusPatch is the API message for patching status of an issue.-type IssueStatusPatch struct {- ID int `jsonapi:"primary,issueStatusPatch"`-- // Standard fields- // Value is assigned from the jwt subject field passed by the client.- UpdaterID int-- // Domain specific fields- Status IssueStatus `jsonapi:"attr,status"`- Comment string `jsonapi:"attr,comment"`-}diff --git a/backend/legacyapi/pipeline.go b/backend/legacyapi/pipeline.godeleted file mode 100644index 4faa9ae119..0000000000--- a/backend/legacyapi/pipeline.go+++ /dev/null@@ -1,12 +0,0 @@-package api--// Pipeline is the API message for pipelines.-type Pipeline struct {- ID int `jsonapi:"primary,pipeline"`-- // Related fields- StageList []*Stage `jsonapi:"relation,stage"`-- // Domain specific fields- Name string `jsonapi:"attr,name"`-}diff --git a/backend/legacyapi/stage.go b/backend/legacyapi/stage.goindex 5224c8dd4b..50bdacdcda 100644--- a/backend/legacyapi/stage.go+++ b/backend/legacyapi/stage.go@@ -14,18 +14,3 @@ const (// - CANCELEDStageStatusUpdateTypeEnd StageStatusUpdateType = "END")--// Stage is the API message for a stage.-type Stage struct {- ID int `jsonapi:"primary,stage"`-- // Related fields- // Just returns PipelineID otherwise would cause circular dependency.- PipelineID int `jsonapi:"attr,pipelineId"`- EnvironmentID int- Environment *Environment `jsonapi:"relation,environment"`- TaskList []*Task `jsonapi:"relation,task"`-- // Domain specific fields- Name string `jsonapi:"attr,name"`-}diff --git a/backend/legacyapi/task.go b/backend/legacyapi/task.goindex d0073c25a5..12ffb30907 100644--- a/backend/legacyapi/task.go+++ b/backend/legacyapi/task.go@@ -2,8 +2,6 @@ package apiimport ("encoding/json"-- "github.com/bytebase/bytebase/backend/common")// TaskStatus is the status of a task.@@ -338,22 +336,3 @@ type TaskPatch struct {RollbackSheetID *intRollbackError *string}--// TaskStatusPatch is the API message for patching a task status.-type TaskStatusPatch struct {- ID int-- // Standard fields- // Value is assigned from the jwt subject field passed by the client.- UpdaterID int-- // Domain specific fields- Status TaskStatus `jsonapi:"attr,status"`- Code *common.Code- Comment *string `jsonapi:"attr,comment"`- Result *string- // Skipped is set to true if frontend sets the Status to DONE.- // And SkippedReason is Comment.- Skipped *bool- SkippedReason *string-}diff --git a/backend/store/issue.go b/backend/store/issue.goindex ef2e45671f..02e6c33123 100644--- a/backend/store/issue.go+++ b/backend/store/issue.go@@ -31,319 +31,6 @@ func init() {}}-// GetIssueByID gets an instance of Issue.-func (s *Store) GetIssueByID(ctx context.Context, id int) (*api.Issue, error) {- issue, err := s.GetIssueV2(ctx, &FindIssueMessage{UID: &id})- if err != nil {- return nil, errors.Wrapf(err, "failed to get Issue with ID %d", id)- }- if issue == nil {- return nil, nil- }- composedIssue, err := s.composeIssue(ctx, issue)- if err != nil {- return nil, errors.Wrapf(err, "failed to compose issue %d", id)- }- return composedIssue, nil-}--// FindIssueStripped finds a list of issues in stripped format.-// We do not load the pipeline in order to reduce the size of the response payload and the complexity of composing the issue list.-func (s *Store) FindIssueStripped(ctx context.Context, find *FindIssueMessage) ([]*api.Issue, error) {- issues, err := s.ListIssueV2(ctx, find)- if err != nil {- return nil, errors.Wrapf(err, "failed to find Issue list with IssueFind[%+v]", find)- }- var composedIssues []*api.Issue- for _, issue := range issues {- composedIssue, err := s.composeIssueStripped(ctx, issue)- if err != nil {- return nil, errors.Wrapf(err, "failed to compose issue %d", issue.UID)- }- // If no specified project, filter out issues belonging to archived project- if composedIssue == nil || composedIssue.Project == nil || composedIssue.Project.RowStatus == api.Archived {- continue- }- composedIssues = append(composedIssues, composedIssue)- }-- return composedIssues, nil-}--//-// private functions-//--// Note: MUST keep in sync with composeIssueValidateOnly.-func (s *Store) composeIssue(ctx context.Context, issue *IssueMessage) (*api.Issue, error) {- composedIssue := &api.Issue{- ID: issue.UID,- CreatorID: issue.Creator.ID,- CreatedTs: issue.CreatedTime.Unix(),- UpdaterID: issue.Updater.ID,- UpdatedTs: issue.UpdatedTime.Unix(),- ProjectID: issue.Project.UID,- PipelineID: issue.PipelineUID,- Name: issue.Title,- Status: issue.Status,- Type: issue.Type,- Description: issue.Description,- AssigneeID: issue.Assignee.ID,- Payload: issue.Payload,- }-- creator, err := s.GetPrincipalByID(ctx, issue.Creator.ID)- if err != nil {- return nil, err- }- composedIssue.Creator = creator- updater, err := s.GetPrincipalByID(ctx, issue.Updater.ID)- if err != nil {- return nil, err- }- composedIssue.Updater = updater- assignee, err := s.GetPrincipalByID(ctx, issue.Assignee.ID)- if err != nil {- return nil, err- }- composedIssue.Assignee = assignee-- for _, subscriber := range issue.Subscribers {- composedSubscriber, err := s.GetPrincipalByID(ctx, subscriber.ID)- if err != nil {- return nil, err- }- composedIssue.SubscriberList = append(composedIssue.SubscriberList, composedSubscriber)- }-- composedProject, err := s.GetProjectByID(ctx, issue.Project.UID)- if err != nil {- return nil, err- }- composedIssue.Project = composedProject-- if issue.PipelineUID != nil {- pipeline, err := s.GetPipelineV2ByID(ctx, *issue.PipelineUID)- if err != nil {- return nil, errors.Wrapf(err, "failed to get pipeline with ID %d", *issue.PipelineUID)- }- if pipeline == nil {- return nil, nil- }- composedPipeline, err := s.composePipeline(ctx, pipeline)- if err != nil {- return nil, errors.Wrapf(err, "failed to compose pipeline")- }- composedIssue.Pipeline = composedPipeline- }-- return composedIssue, nil-}--// composeIssueStripped is a stripped version of compose issue only used in listing issues-// for reducing the cost and payload of composing a full issue.-func (s *Store) composeIssueStripped(ctx context.Context, issue *IssueMessage) (*api.Issue, error) {- composedIssue := &api.Issue{- ID: issue.UID,- CreatorID: issue.Creator.ID,- CreatedTs: issue.CreatedTime.Unix(),- UpdaterID: issue.Updater.ID,- UpdatedTs: issue.UpdatedTime.Unix(),- ProjectID: issue.Project.UID,- PipelineID: issue.PipelineUID,- Name: issue.Title,- Status: issue.Status,- Type: issue.Type,- Description: issue.Description,- AssigneeID: issue.Assignee.ID,- Payload: issue.Payload,- }-- creator, err := s.GetPrincipalByID(ctx, issue.Creator.ID)- if err != nil {- return nil, err- }- composedIssue.Creator = creator- updater, err := s.GetPrincipalByID(ctx, issue.Updater.ID)- if err != nil {- return nil, err- }- composedIssue.Updater = updater- assignee, err := s.GetPrincipalByID(ctx, issue.Assignee.ID)- if err != nil {- return nil, err- }- composedIssue.Assignee = assignee-- for _, subscriber := range issue.Subscribers {- composedSubscriber, err := s.GetPrincipalByID(ctx, subscriber.ID)- if err != nil {- return nil, err- }- composedIssue.SubscriberList = append(composedIssue.SubscriberList, composedSubscriber)- }-- composedProject, err := s.GetProjectByID(ctx, issue.Project.UID)- if err != nil {- return nil, err- }- composedIssue.Project = composedProject-- // Creating a stripped pipeline.- if issue.PipelineUID != nil {- pipeline, err := s.GetPipelineV2ByID(ctx, *issue.PipelineUID)- if err != nil {- return nil, errors.Wrapf(err, "failed to get Pipeline with ID %d", issue.PipelineUID)- }- if pipeline == nil {- return nil, nil- }- composedPipeline, err := s.composeSimplePipeline(ctx, pipeline)- if err != nil {- return nil, err- }- composedIssue.Pipeline = composedPipeline- }-- return composedIssue, nil-}--func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {- composedPipeline := &api.Pipeline{- ID: pipeline.ID,- Name: pipeline.Name,- }-- tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &pipeline.ID})- if err != nil {- return nil, errors.Wrapf(err, "failed to find Task list for pipeline %v", pipeline.ID)- }- taskRuns, err := s.ListTaskRun(ctx, &TaskRunFind{PipelineID: &pipeline.ID})- if err != nil {- return nil, err- }- var composedTasks []*api.Task- for _, task := range tasks {- composedTask := task.toTask()- creator, err := s.GetPrincipalByID(ctx, task.CreatorID)- if err != nil {- return nil, err- }- composedTask.Creator = creator- updater, err := s.GetPrincipalByID(ctx, task.UpdaterID)- if err != nil {- return nil, err- }- composedTask.Updater = updater-- for _, taskRun := range taskRuns {- if taskRun.TaskUID == task.ID {- composedTaskRun := taskRun.toTaskRun()- creator, err := s.GetPrincipalByID(ctx, composedTaskRun.CreatorID)- if err != nil {- return nil, err- }- composedTaskRun.Creator = creator- updater, err := s.GetPrincipalByID(ctx, composedTaskRun.UpdaterID)- if err != nil {- return nil, err- }- composedTaskRun.Updater = updater- composedTask.TaskRunList = append(composedTask.TaskRunList, composedTaskRun)- }- }-- instance, err := s.GetInstanceByID(ctx, task.InstanceID)- if err != nil {- return nil, err- }- if instance == nil {- return nil, errors.Errorf("instance not found with ID %v", task.InstanceID)- }- composedTask.Instance = instance- if task.DatabaseID != nil {- database, err := s.GetDatabase(ctx, &api.DatabaseFind{ID: task.DatabaseID})- if err != nil {- return nil, err- }- if database == nil {- return nil, errors.Errorf("database not found with ID %v", task.DatabaseID)- }- composedTask.Database = database- }-- composedTasks = append(composedTasks, composedTask)- }-- stages, err := s.ListStageV2(ctx, pipeline.ID)- if err != nil {- return nil, errors.Wrapf(err, "failed to find stages for pipeline %d", pipeline.ID)- }- var composedStages []*api.Stage- for _, stage := range stages {- environment, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)- if err != nil {- return nil, err- }- composedStage := &api.Stage{- ID: stage.ID,- PipelineID: stage.PipelineID,- EnvironmentID: stage.EnvironmentID,- Environment: environment,- Name: stage.Name,- }- for _, composedTask := range composedTasks {- if composedTask.StageID == stage.ID {- composedStage.TaskList = append(composedStage.TaskList, composedTask)- }- }-- composedStages = append(composedStages, composedStage)- }- composedPipeline.StageList = composedStages-- return composedPipeline, nil-}--func (s *Store) composeSimplePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {- tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &pipeline.ID})- if err != nil {- return nil, errors.Wrapf(err, "failed to find tasks for pipeline %d", pipeline.ID)- }-- stages, err := s.ListStageV2(ctx, pipeline.ID)- if err != nil {- return nil, errors.Wrapf(err, "failed to find stage list")- }- var composedStages []*api.Stage- for _, stage := range stages {- environment, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)- if err != nil {- return nil, err- }- composedStage := &api.Stage{- ID: stage.ID,- Name: stage.Name,- EnvironmentID: stage.EnvironmentID,- Environment: environment,- PipelineID: stage.PipelineID,- }-- for _, task := range tasks {- if task.StageID == stage.ID {- composedStage.TaskList = append(composedStage.TaskList, task.toTask())- }- }- composedStages = append(composedStages, composedStage)- }-- return &api.Pipeline{- ID: pipeline.ID,- Name: pipeline.Name,- StageList: composedStages,- }, nil-}-// IssueMessage is the mssage for issues.type IssueMessage struct {Project *ProjectMessage