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: deprecate legacy sync sheet API (#6571)
Danny Xu6eca1f133aa6
diff --git a/backend/server/project.go b/backend/server/project.godeleted file mode 100644index 1c05a5ced8..0000000000--- a/backend/server/project.go+++ /dev/null@@ -1,283 +0,0 @@-package server--import (- "encoding/json"- "fmt"- "net/http"- "path/filepath"- "regexp"- "strconv"- "strings"- "time"-- "github.com/labstack/echo/v4"- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/common"- api "github.com/bytebase/bytebase/backend/legacyapi"- vcsPlugin "github.com/bytebase/bytebase/backend/plugin/vcs"- "github.com/bytebase/bytebase/backend/store"- "github.com/bytebase/bytebase/backend/utils"-)--func (s *Server) registerProjectRoutes(g *echo.Group) {- g.POST("/project/:projectID/sync-sheet", func(c echo.Context) error {- // TODO(tianzhou): uncomment this after adding the test harness to using Enterprise version.- // if !s.licenseService.IsFeatureEnabled(api.FeatureVCSSheetSync) {- // return echo.NewHTTPError(http.StatusForbidden, api.FeatureVCSSheetSync.AccessErrorMessage())- // }-- ctx := c.Request().Context()- currentPrincipalID := c.Get(getPrincipalIDContextKey()).(int)- projectID, err := strconv.Atoi(c.Param("projectID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project ID is not a number: %s", c.Param("projectID"))).SetInternal(err)- }-- project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{UID: &projectID})- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Project not found: %d", projectID)).SetInternal(err)- }- if project == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project not found by ID: %d", projectID))- }- if project.Deleted {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("project %s is deleted", project.Title))- }- if project.Workflow != api.VCSWorkflow {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid workflow type: %s, need %s to enable this function", project.Workflow, api.VCSWorkflow))- }-- repo, err := s.store.GetRepositoryV2(ctx, &store.FindRepositoryMessage{ProjectResourceID: &project.ResourceID})- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find relevant VCS repo, Project ID: %d", projectID)).SetInternal(err)- }- if repo == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Repository not found by project ID: %d", projectID))- }-- vcs, err := s.store.GetExternalVersionControlV2(ctx, repo.VCSUID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find VCS for sync sheet: %d", repo.VCSUID)).SetInternal(err)- }- if vcs == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS not found by ID: %d", repo.VCSUID))- }-- basePath := filepath.Dir(repo.SheetPathTemplate)- // TODO(Steven): The repo.branchFilter could be `test/*` which cannot be the ref value.- fileList, err := vcsPlugin.Get(vcs.Type, vcsPlugin.ProviderConfig{}).FetchRepositoryFileList(ctx,- common.OauthContext{- ClientID: vcs.ApplicationID,- ClientSecret: vcs.Secret,- AccessToken: repo.AccessToken,- RefreshToken: repo.RefreshToken,- Refresher: utils.RefreshToken(ctx, s.store, repo.WebURL),- },- vcs.InstanceURL,- repo.ExternalID,- repo.BranchFilter,- basePath,- )- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch repository file list from VCS, instance URL: %s", vcs.InstanceURL)).SetInternal(err)- }-- for _, file := range fileList {- sheetInfo, err := parseSheetInfo(file.Path, repo.SheetPathTemplate)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to parse sheet info from template").SetInternal(err)- }- if sheetInfo.SheetName == "" {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("sheet name cannot be empty from sheet path %s with template %s", file.Path, repo.SheetPathTemplate)).SetInternal(err)- }-- fileContent, err := vcsPlugin.Get(vcs.Type, vcsPlugin.ProviderConfig{}).ReadFileContent(ctx,- common.OauthContext{- ClientID: vcs.ApplicationID,- ClientSecret: vcs.Secret,- AccessToken: repo.AccessToken,- RefreshToken: repo.RefreshToken,- Refresher: utils.RefreshToken(ctx, s.store, repo.WebURL),- },- vcs.InstanceURL,- repo.ExternalID,- file.Path,- repo.BranchFilter,- )- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch file content from VCS, instance URL: %s, repo ID: %s, file path: %s, branch: %s", vcs.InstanceURL, repo.ExternalID, file.Path, repo.BranchFilter)).SetInternal(err)- }-- fileMeta, err := vcsPlugin.Get(vcs.Type, vcsPlugin.ProviderConfig{}).ReadFileMeta(ctx,- common.OauthContext{- ClientID: vcs.ApplicationID,- ClientSecret: vcs.Secret,- AccessToken: repo.AccessToken,- RefreshToken: repo.RefreshToken,- Refresher: utils.RefreshToken(ctx, s.store, repo.WebURL),- },- vcs.InstanceURL,- repo.ExternalID,- file.Path,- repo.BranchFilter,- )- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch file meta from VCS, instance URL: %s, repo ID: %s, file path: %s, branch: %s", vcs.InstanceURL, repo.ExternalID, file.Path, repo.BranchFilter)).SetInternal(err)- }-- lastCommit, err := vcsPlugin.Get(vcs.Type, vcsPlugin.ProviderConfig{}).FetchCommitByID(ctx,- common.OauthContext{- ClientID: vcs.ApplicationID,- ClientSecret: vcs.Secret,- AccessToken: repo.AccessToken,- RefreshToken: repo.RefreshToken,- Refresher: utils.RefreshToken(ctx, s.store, repo.WebURL),- },- vcs.InstanceURL,- repo.ExternalID,- fileMeta.LastCommitID,- )- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch commit data from VCS, instance URL: %s, repo ID: %s, commit ID: %s", vcs.InstanceURL, repo.ExternalID, fileMeta.LastCommitID)).SetInternal(err)- }-- sheetVCSPayload := &api.SheetVCSPayload{- FileName: fileMeta.Name,- FilePath: fileMeta.Path,- Size: fileMeta.Size,- Author: lastCommit.AuthorName,- LastCommitID: lastCommit.ID,- LastSyncTs: time.Now().Unix(),- }- payload, err := json.Marshal(sheetVCSPayload)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal sheetVCSPayload").SetInternal(err)- }-- var databaseID *int- // In non-tenant mode, we can set a databaseId for sheet with ENV_ID and DB_NAME,- // and ENV_ID and DB_NAME is either both present or neither present.- if project.TenantMode != api.TenantModeDisabled {- if sheetInfo.EnvironmentID != "" && sheetInfo.DatabaseName != "" {- databases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{ProjectID: &project.ResourceID, DatabaseName: &sheetInfo.DatabaseName})- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find database list with name: %s, project ID: %d", sheetInfo.DatabaseName, projectID)).SetInternal(err)- }- for _, database := range databases {- database := database // create a new var "database".- if database.EnvironmentID == sheetInfo.EnvironmentID {- databaseID = &database.UID- break- }- }- }- }-- var sheetSource api.SheetSource- switch vcs.Type {- case vcsPlugin.GitLab:- sheetSource = api.SheetFromGitLab- case vcsPlugin.GitHub:- sheetSource = api.SheetFromGitHub- case vcsPlugin.Bitbucket:- sheetSource = api.SheetFromBitbucket- }- vscSheetType := api.SheetForSQL- sheetFind := &api.SheetFind{- Name: &sheetInfo.SheetName,- ProjectID: &project.UID,- Source: &sheetSource,- Type: &vscSheetType,- }- sheet, err := s.store.GetSheet(ctx, sheetFind, currentPrincipalID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find sheet with name: %s, project ID: %d", sheetInfo.SheetName, projectID)).SetInternal(err)- }-- if sheet == nil {- sheetCreate := api.SheetCreate{- ProjectID: projectID,- CreatorID: currentPrincipalID,- Name: sheetInfo.SheetName,- Statement: fileContent,- Visibility: api.ProjectSheet,- Source: sheetSource,- Type: api.SheetForSQL,- Payload: string(payload),- }- if databaseID != nil {- sheetCreate.DatabaseID = databaseID- }-- if _, err := s.store.CreateSheet(ctx, &sheetCreate); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create sheet from VCS").SetInternal(err)- }- } else {- payloadString := string(payload)- sheetPatch := api.SheetPatch{- ID: sheet.ID,- UpdaterID: currentPrincipalID,- Statement: &fileContent,- Payload: &payloadString,- }- if databaseID != nil {- sheetPatch.DatabaseID = databaseID- }-- if _, err := s.store.PatchSheet(ctx, &sheetPatch); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch sheet from VCS").SetInternal(err)- }- }- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- return nil- })-}--// SheetInfo represents the sheet related information from sheetPathTemplate.-type SheetInfo struct {- EnvironmentID string- DatabaseName string- SheetName string-}--// parseSheetInfo matches sheetPath against sheetPathTemplate. If sheetPath matches, then it will derive SheetInfo from the sheetPath.-// Both sheetPath and sheetPathTemplate are the full file path(including the base directory) of the repository.-func parseSheetInfo(sheetPath string, sheetPathTemplate string) (*SheetInfo, error) {- placeholderList := []string{- "ENV_ID",- "DB_NAME",- "NAME",- }- sheetPathRegex := sheetPathTemplate- for _, placeholder := range placeholderList {- sheetPathRegex = strings.ReplaceAll(sheetPathRegex, fmt.Sprintf("{{%s}}", placeholder), fmt.Sprintf("(?P<%s>[a-zA-Z0-9\\+\\-\\=\\_\\#\\!\\$\\. ]+)", placeholder))- }- sheetRegex, err := regexp.Compile(fmt.Sprintf("^%s$", sheetPathRegex))- if err != nil {- return nil, errors.Wrapf(err, "invalid sheet path template %q", sheetPathTemplate)- }- if !sheetRegex.MatchString(sheetPath) {- return nil, errors.Errorf("sheet path %q does not match sheet path template %q", sheetPath, sheetPathTemplate)- }-- matchList := sheetRegex.FindStringSubmatch(sheetPath)- sheetInfo := &SheetInfo{}- for _, placeholder := range placeholderList {- index := sheetRegex.SubexpIndex(placeholder)- if index >= 0 {- switch placeholder {- case "ENV_ID":- sheetInfo.EnvironmentID = matchList[index]- case "DB_NAME":- sheetInfo.DatabaseName = matchList[index]- case "NAME":- sheetInfo.SheetName = matchList[index]- }- }- }-- return sheetInfo, nil-}diff --git a/backend/server/project_test.go b/backend/server/project_test.godeleted file mode 100644index 3ec1600c18..0000000000--- a/backend/server/project_test.go+++ /dev/null@@ -1,93 +0,0 @@-package server--import (- "testing"-- "github.com/pkg/errors"- "github.com/stretchr/testify/require"-)--func TestParseSheetInfo(t *testing.T) {- tests := []struct {- filePath string- sheetPathTemplate string- want *SheetInfo- err error- }{- {- filePath: "sheet/test.sql",- sheetPathTemplate: "sheet/{{NAME}}.sql",- want: &SheetInfo{- EnvironmentID: "",- DatabaseName: "",- SheetName: "test",- },- err: nil,- },- {- filePath: "sheet/dev##TEST##test.sql",- sheetPathTemplate: "sheet/{{ENV_ID}}##{{DB_NAME}}##{{NAME}}.sql",- want: &SheetInfo{- EnvironmentID: "dev",- DatabaseName: "TEST",- SheetName: "test",- },- err: nil,- },- {- filePath: "sheet/dev##test.sql",- sheetPathTemplate: "sheet/{{ENV_ID}}##{{NAME}}.sql",- want: &SheetInfo{- EnvironmentID: "dev",- DatabaseName: "",- SheetName: "test",- },- err: nil,- },- {- filePath: "sheet/employee##test.sql",- sheetPathTemplate: "sheet/{{DB_NAME}}##{{NAME}}.sql",- want: &SheetInfo{- EnvironmentID: "",- DatabaseName: "employee",- SheetName: "test",- },- err: nil,- },- {- filePath: "sheet/db-name.sql",- sheetPathTemplate: "sheet/{{DB_NAME}}.sql",- want: &SheetInfo{- EnvironmentID: "",- DatabaseName: "db-name",- SheetName: "",- },- err: nil,- },- {- filePath: "sheet/db/test.sql",- sheetPathTemplate: "sheet/{{NAME}}.sql",- want: nil,- err: errors.Errorf("sheet path \"sheet/db/test.sql\" does not match sheet path template \"sheet/{{NAME}}.sql\""),- },- {- filePath: "my-sheet/test.sql",- sheetPathTemplate: "sheet/{{NAME}}.sql",- want: nil,- err: errors.Errorf("sheet path \"my-sheet/test.sql\" does not match sheet path template \"sheet/{{NAME}}.sql\""),- },- }-- for _, test := range tests {- result, err := parseSheetInfo(test.filePath, test.sheetPathTemplate)- if err != nil {- if test.err != nil {- require.Equal(t, test.err.Error(), err.Error())- } else {- t.Error(err)- }- } else {- require.Equal(t, test.want, result)- }- }-}diff --git a/backend/server/server.go b/backend/server/server.goindex dc68d56e00..43761ec785 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -511,7 +511,6 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {return aclMiddleware(s, internalAPIPrefix, ce, next, profile.Readonly)})s.registerOAuthRoutes(apiGroup)- s.registerProjectRoutes(apiGroup)s.registerDatabaseRoutes(apiGroup)s.registerIssueRoutes(apiGroup)s.registerIssueSubscriberRoutes(apiGroup)diff --git a/backend/tests/sheet.go b/backend/tests/sheet.godeleted file mode 100644index a4d02716aa..0000000000--- a/backend/tests/sheet.go+++ /dev/null@@ -1,11 +0,0 @@-package tests--import (- "fmt"-)--// syncSheet syncs sheets with project.-func (ctl *controller) syncSheet(projectID int) error {- _, err := ctl.post(fmt.Sprintf("/project/%d/sync-sheet", projectID), nil)- return err-}diff --git a/backend/tests/sheet_vcs_test.go b/backend/tests/sheet_vcs_test.goindex a4546e2938..e6df8c19fd 100644--- a/backend/tests/sheet_vcs_test.go+++ b/backend/tests/sheet_vcs_test.go@@ -3,7 +3,6 @@ package testsimport ("context""fmt"- "strconv""strings""testing"@@ -71,8 +70,6 @@ func TestSheetVCS(t *testing.T) {// Create a project.project, err := ctl.createProject(ctx)a.NoError(err)- projectUID, err := strconv.Atoi(project.Uid)- a.NoError(err)// Create a repository.ctl.vcsProvider.CreateRepository(test.externalID)@@ -119,7 +116,7 @@ func TestSheetVCS(t *testing.T) {sheetsBefore := resp.Sheetsa.NoError(err)- err = ctl.syncSheet(projectUID)+ _, err = ctl.sheetServiceClient.SyncSheets(ctx, &v1pb.SyncSheetsRequest{Parent: project.Name})a.NoError(err)resp, err = ctl.sheetServiceClient.SearchSheets(ctx, &v1pb.SearchSheetsRequest{