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: support multiple files in a commit for batch projects (#8339)
Danny Xu02a829a18f60
diff --git a/backend/server/webhook.go b/backend/server/webhook.goindex 0ecffaad1b..833aaebf32 100644--- a/backend/server/webhook.go+++ b/backend/server/webhook.go@@ -55,8 +55,9 @@ const (// issueNameTemplate should be consistent with UI issue names generated from the frontend except for the timestamp.// Because we cannot get the correct timezone of the client here.// Example: "[db-5] Alter schema: add an email column".- issueNameTemplate = "[%s] %s: %s"- sdlIssueNameTemplate = "[%s] %s"+ issueNameTemplate = "[%s] %s: %s"+ sdlIssueNameTemplate = "[%s] %s"+ batchIssueNameTemplate = "%s: %s")func (s *Server) registerWebhookRoutes(g *echo.Group) {@@ -1439,6 +1440,7 @@ func (s *Server) processFilesInProject(ctx context.Context, pushEvent vcs.PushEvif err := s.licenseService.IsFeatureEnabled(api.FeatureMultiTenancy); err != nil {return "", false, nil, echo.NewHTTPError(http.StatusForbidden, err.Error())}+ return s.processFilesInBatchProject(ctx, pushEvent, repoInfo, fileInfoList)}var migrationDetailList []*api.MigrationDetail@@ -1506,6 +1508,54 @@ func (s *Server) processFilesInProject(ctx context.Context, pushEvent vcs.PushEvreturn fmt.Sprintf("Created issue %q from push event", strings.Join(createdIssueList, ",")), true, activityCreateList, nil}+// processFilesInBatchProject creates issues for a batch project.+func (s *Server) processFilesInBatchProject(ctx context.Context, pushEvent vcs.PushEvent, repoInfo *repoInfo, fileInfoList []fileInfo) (string, bool, []*store.ActivityMessage, *echo.HTTPError) {+ var activityCreateList []*store.ActivityMessage+ var createdIssueList []string++ creatorID := s.getIssueCreatorID(ctx, pushEvent.CommitList[0].AuthorEmail)+ for _, fileInfo := range fileInfoList {+ if fileInfo.fType == fileTypeSchema {+ if fileInfo.repoInfo.project.SchemaChangeType == api.ProjectSchemaChangeTypeSDL {+ // Create one issue per schema file for SDL project.+ migrationDetailListForFile, activityCreateListForFile := s.prepareIssueFromSDLFile(ctx, repoInfo, pushEvent, fileInfo.migrationInfo, fileInfo.item.FileName)+ activityCreateList = append(activityCreateList, activityCreateListForFile...)+ if len(migrationDetailListForFile) != 0 {+ databaseName := fileInfo.migrationInfo.Database+ issueName := fmt.Sprintf(sdlIssueNameTemplate, databaseName, "Alter schema")+ issueDescription := fmt.Sprintf("Apply schema diff by file %s", strings.TrimPrefix(fileInfo.item.FileName, repoInfo.repository.BaseDirectory+"/"))+ if err := s.createIssueFromMigrationDetailsV2(ctx, repoInfo.project, issueName, issueDescription, pushEvent, creatorID, migrationDetailListForFile); err != nil {+ return "", false, activityCreateList, echo.NewHTTPError(http.StatusInternalServerError, "Failed to create issue").SetInternal(err)+ }+ createdIssueList = append(createdIssueList, issueName)+ }+ } else {+ slog.Debug("Ignored schema file for non-SDL project", slog.String("fileName", fileInfo.item.FileName), slog.String("type", string(fileInfo.item.ItemType)))+ }+ } else { // fileInfo.fType == fileTypeMigration+ migrationDetailListForFile, activityCreateListForFile := s.prepareIssueFromFile(ctx, repoInfo, pushEvent, fileInfo)+ if len(migrationDetailListForFile) != 1 {+ slog.Error("Unexpected number of file number")+ }+ migrationDetail := migrationDetailListForFile[0]+ activityCreateList = append(activityCreateList, activityCreateListForFile...)+ migrateType := "Change data"+ if migrationDetail.MigrationType == db.Migrate {+ migrateType = "Alter schema"+ }+ description := strings.ReplaceAll(fileInfoList[0].migrationInfo.Description, "_", " ")+ issueName := fmt.Sprintf(batchIssueNameTemplate, migrateType, description)+ issueDescription := fmt.Sprintf("By VCS file: %s\n", fileInfo.item.FileName)+ if err := s.createIssueFromMigrationDetailsV2(ctx, repoInfo.project, issueName, issueDescription, pushEvent, creatorID, migrationDetailListForFile); err != nil {+ return "", len(createdIssueList) != 0, activityCreateList, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create issue %s", issueName)).SetInternal(err)+ }+ createdIssueList = append(createdIssueList, issueName)+ }+ }++ return fmt.Sprintf("Created issue %q from push event", strings.Join(createdIssueList, ",")), true, activityCreateList, nil+}+func sortFilesBySchemaVersion(fileInfoList []fileInfo) []fileInfo {var ret []fileInforet = append(ret, fileInfoList...)diff --git a/backend/tests/tenant_test.go b/backend/tests/tenant_test.goindex fd80a8352a..49a9c7bbae 100644--- a/backend/tests/tenant_test.go+++ b/backend/tests/tenant_test.go@@ -202,7 +202,7 @@ func TestTenantVCS(t *testing.T) {vcsType v1pb.ExternalVersionControl_TypeexternalID stringrepositoryFullPath string- newWebhookPushEvent func(gitFile, beforeSHA, afterSHA string) any+ newWebhookPushEvent func(gitFiles []string, beforeSHA, afterSHA string) any}{{name: "GitLab",@@ -210,7 +210,7 @@ func TestTenantVCS(t *testing.T) {vcsType: v1pb.ExternalVersionControl_GITLAB,externalID: "121",repositoryFullPath: "test/schemaUpdate",- newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) any {+ newWebhookPushEvent: func(gitFiles []string, beforeSHA, afterSHA string) any {return gitlab.WebhookPushEvent{ObjectKind: gitlab.WebhookPush,Ref: "refs/heads/feature/foo",@@ -222,7 +222,7 @@ func TestTenantVCS(t *testing.T) {CommitList: []gitlab.WebhookCommit{{Timestamp: "2021-01-13T13:14:00Z",- AddedList: []string{gitFile},+ AddedList: gitFiles,},},}@@ -234,7 +234,7 @@ func TestTenantVCS(t *testing.T) {vcsType: v1pb.ExternalVersionControl_GITHUB,externalID: "octocat/Hello-World",repositoryFullPath: "octocat/Hello-World",- newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) any {+ newWebhookPushEvent: func(gitFiles []string, beforeSHA, afterSHA string) any {return github.WebhookPushEvent{Ref: "refs/heads/feature/foo",Before: beforeSHA,@@ -258,7 +258,7 @@ func TestTenantVCS(t *testing.T) {Name: "fake_github_author",Email: "fake_github_author@localhost",},- Added: []string{gitFile},+ Added: gitFiles,},},}@@ -441,7 +441,7 @@ func TestTenantVCS(t *testing.T) {{Path: gitFile, Type: vcs.FileDiffTypeAdded},})a.NoError(err)- payload, err := json.Marshal(test.newWebhookPushEvent(gitFile, "1", "2"))+ payload, err := json.Marshal(test.newWebhookPushEvent([]string{gitFile}, "1", "2"))a.NoError(err)err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)a.NoError(err)@@ -451,6 +451,8 @@ func TestTenantVCS(t *testing.T) {a.NoError(err)err = ctl.waitRollout(ctx, issue.Name, issue.Rollout)a.NoError(err)+ err = ctl.closeIssue(ctx, project, issue.Name)+ a.NoError(err)// Query schema.for _, testInstance := range testInstances {@@ -463,6 +465,45 @@ func TestTenantVCS(t *testing.T) {a.NoError(err)a.Equal(wantBookSchema, dbMetadata.Schema)}++ // Test a commit with multiple files.+ gitFile2 := "bbtest/ver2##migrate##create_a_test_table.sql"+ gitFile3 := "bbtest/ver3##migrate##create_a_test_table.sql"+ err = ctl.vcsProvider.AddFiles(test.externalID, map[string]string{gitFile2: migrationStatement2, gitFile3: migrationStatement3})+ a.NoError(err)+ err = ctl.vcsProvider.AddCommitsDiff(test.externalID, "2", "3", []vcs.FileDiff{+ {Path: gitFile2, Type: vcs.FileDiffTypeAdded},+ {Path: gitFile3, Type: vcs.FileDiffTypeAdded},+ })+ a.NoError(err)+ payload, err = json.Marshal(test.newWebhookPushEvent([]string{gitFile2, gitFile3}, "2", "3"))+ a.NoError(err)+ err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)+ a.NoError(err)+ // There are two issues created.+ listIssueResp, err := ctl.issueServiceClient.ListIssues(ctx, &v1pb.ListIssuesRequest{+ Parent: project.Name,+ Filter: "status = OPEN",+ })+ a.NoError(err)+ a.Len(listIssueResp.Issues, 2)+ for i := len(listIssueResp.Issues) - 1; i >= 0; i-- {+ issue = listIssueResp.Issues[i]+ err = ctl.waitRollout(ctx, issue.Name, issue.Rollout)+ a.NoError(err)+ }++ // Query schema.+ for _, testInstance := range testInstances {+ dbMetadata, err := ctl.databaseServiceClient.GetDatabaseSchema(ctx, &v1pb.GetDatabaseSchemaRequest{Name: fmt.Sprintf("%s/databases/%s/schema", testInstance.Name, databaseName)})+ a.NoError(err)+ a.Equal(want3BookSchema, dbMetadata.Schema)+ }+ for _, prodInstance := range prodInstances {+ dbMetadata, err := ctl.databaseServiceClient.GetDatabaseSchema(ctx, &v1pb.GetDatabaseSchemaRequest{Name: fmt.Sprintf("%s/databases/%s/schema", prodInstance.Name, databaseName)})+ a.NoError(err)+ a.Equal(want3BookSchema, dbMetadata.Schema)+ }})}}