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: return nil, nil when nothing found in db (#989)
Yilong Liac54a682ba4b
diff --git a/server/database.go b/server/database.goindex cfabe7a0be..1bfd24b673 100644--- a/server/database.go+++ b/server/database.go@@ -782,6 +782,9 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch data source by ID %d", dataSourceID)).SetInternal(err)}+ if dataSource == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Data source not found with ID %d", dataSourceID))+ }if dataSource.DatabaseID != databaseID {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("data source not found by ID %d and database ID %d", dataSourceID, databaseID))}@@ -861,11 +864,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {ID: &dataSourceID,DatabaseID: &databaseID,}- dataSourceRaw, err := s.store.GetDataSource(ctx, dataSourceFind)+ dataSourceOld, err := s.store.GetDataSource(ctx, dataSourceFind)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find data source").SetInternal(err)}- if dataSourceRaw == nil || dataSourceRaw.DatabaseID != databaseID {+ if dataSourceOld == nil || dataSourceOld.DatabaseID != databaseID {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("data source not found by ID %d and database ID %d", dataSourceID, databaseID))}@@ -882,13 +885,13 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {dataSourcePatch.Password = &password}- dataSource, err := s.store.PatchDataSource(ctx, dataSourcePatch)+ dataSourceNew, err := s.store.PatchDataSource(ctx, dataSourcePatch)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update data source with ID %d", dataSourceID)).SetInternal(err)}c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- if err := jsonapi.MarshalPayload(c.Response().Writer, dataSource); err != nil {+ if err := jsonapi.MarshalPayload(c.Response().Writer, dataSourceNew); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal patch data source response").SetInternal(err)}return nildiff --git a/server/project.go b/server/project.goindex fadae18351..5cb0665601 100644--- a/server/project.go+++ b/server/project.go@@ -214,6 +214,9 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find VCS for creating repository: %d", repositoryCreate.VCSID)).SetInternal(err)}+ if vcs == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS not found with ID: %d", repositoryCreate.VCSID))+ }repositoryCreate.WebhookURLHost = fmt.Sprintf("%s:%d", s.host, s.port)repositoryCreate.WebhookEndpointID = uuid.New().String()@@ -380,6 +383,9 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update repository for project ID: %d", projectID)).SetInternal(err)}+ if vcs == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS not found with ID: %d", repo.VCSID))+ }// Update the webhook after we successfully update the repository.// This is because in case the webhook update fails, we can still have a reconcile process to reconcile the webhook state.// If we update it before we update the repository, then if the repository update fails, then the reconcile process will reconcile the webhook to the pre-update state which is likely not intended.@@ -452,11 +458,14 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Repository not found for project ID: %d", projectID))}- repository := repoRawList[0]- vcs, err := s.store.GetVCSByID(ctx, repository.VCSID)+ repo := repoRawList[0]+ vcs, err := s.store.GetVCSByID(ctx, repo.VCSID)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete repository for project ID: %d", projectID)).SetInternal(err)}+ if vcs == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS not found with ID: %d", repo.VCSID))+ }repositoryDelete := &api.RepositoryDelete{ProjectID: projectID,@@ -475,17 +484,17 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {common.OauthContext{ClientID: vcs.ApplicationID,ClientSecret: vcs.Secret,- AccessToken: repository.AccessToken,- RefreshToken: repository.RefreshToken,- Refresher: s.refreshToken(ctx, repository.ID),+ AccessToken: repo.AccessToken,+ RefreshToken: repo.RefreshToken,+ Refresher: s.refreshToken(ctx, repo.ID),},vcs.InstanceURL,- repository.ExternalID,- repository.ExternalWebhookID,+ repo.ExternalID,+ repo.ExternalWebhookID,)if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete webhook ID %s for project ID: %v", repository.ExternalWebhookID, projectID)).SetInternal(err)+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete webhook ID %s for project ID: %v", repo.ExternalWebhookID, projectID)).SetInternal(err)}c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)diff --git a/server/project_member.go b/server/project_member.goindex 3b0a1a7eef..ca409ccfc1 100644--- a/server/project_member.go+++ b/server/project_member.go@@ -47,6 +47,9 @@ func (s *Server) registerProjectMemberRoutes(g *echo.Group) {if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find VCS for sync project member: %d", repo.VCSID)).SetInternal(err)}+ if vcs == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("VCS not found with ID: %d", repo.VCSID))+ }vcsProjectMemberList, err := vcsPlugin.Get(vcs.Type, vcsPlugin.ProviderConfig{Logger: s.l}).FetchRepositoryActiveMemberList(ctx,common.OauthContext{ClientID: vcs.ApplicationID,diff --git a/store/data_source.go b/store/data_source.goindex 5d2f51e902..281644c2c8 100644--- a/store/data_source.go+++ b/store/data_source.go@@ -75,6 +75,9 @@ func (s *Store) GetDataSource(ctx context.Context, find *api.DataSourceFind) (*aif err != nil {return nil, fmt.Errorf("Failed to get data source with DataSourceFind[%+v], error[%w]", find, err)}+ if dataSourceRaw == nil {+ return nil, nil+ }dataSource, err := s.composeDataSource(ctx, dataSourceRaw)if err != nil {return nil, fmt.Errorf("Failed to compose data source with dataSourceRaw[%+v], error[%w]", dataSourceRaw, err)@@ -195,7 +198,7 @@ func (s *Store) getDataSourceRaw(ctx context.Context, find *api.DataSourceFind)}if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("data source not found with DataSourceFind[%+v]", find)}+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d data sources with filter %+v, expect 1", len(list), find)}}diff --git a/store/environment.go b/store/environment.goindex 9b7961e7fd..74e108187b 100644--- a/store/environment.go+++ b/store/environment.go@@ -93,6 +93,9 @@ func (s *Store) GetEnvironmentByID(ctx context.Context, id int) (*api.Environmenif err != nil {return nil, fmt.Errorf("Failed to get environment with ID[%d], error[%w]", id, err)}+ if envRaw == nil {+ return nil, nil+ }env, err := s.composeEnvironment(ctx, envRaw)if err != nil {@@ -197,7 +200,7 @@ func (s *Store) getEnvironmentByIDRaw(ctx context.Context, id int) (*environment}if len(envRawList) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("environment not found with EnvironmentFind[%+v]", find)}+ return nil, nil} else if len(envRawList) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d environments with filter %+v, expect 1", len(envRawList), find)}}diff --git a/store/member.go b/store/member.goindex 17e06ba5c3..a70d6d8509 100644--- a/store/member.go+++ b/store/member.go@@ -83,7 +83,10 @@ func (s *Store) GetMemberByPrincipalID(ctx context.Context, id int) (*api.Memberfind := &api.MemberFind{PrincipalID: &id}memberRaw, err := s.getMemberRaw(ctx, find)if err != nil {- return nil, fmt.Errorf("Failed to find Member with MemberFind[%+v], error[%w]", find, err)+ return nil, fmt.Errorf("Failed to get Member with PrincipalID[%d], error[%w]", id, err)+ }+ if memberRaw == nil {+ return nil, nil}member, err := s.composeMember(ctx, memberRaw)if err != nil {@@ -97,7 +100,10 @@ func (s *Store) GetMemberByID(ctx context.Context, id int) (*api.Member, error)find := &api.MemberFind{ID: &id}memberRaw, err := s.getMemberRaw(ctx, find)if err != nil {- return nil, fmt.Errorf("Failed to find Member with MemberFind[%+v], error[%w]", find, err)+ return nil, fmt.Errorf("Failed to get Member with ID[%d], error[%w]", id, err)+ }+ if memberRaw == nil {+ return nil, nil}member, err := s.composeMember(ctx, memberRaw)if err != nil {@@ -195,7 +201,7 @@ func (s *Store) getMemberRaw(ctx context.Context, find *api.MemberFind) (*member}if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("member not found with MemberFind[%+v]", find)}+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d members with filter %+v, expect 1", len(list), find)}}diff --git a/store/principal.go b/store/principal.goindex 3ccb301681..9a75e0f365 100644--- a/store/principal.go+++ b/store/principal.go@@ -81,8 +81,9 @@ func (s *Store) GetPrincipalList(ctx context.Context) ([]*api.Principal, error)}// FindPrincipal finds an instance of Principal+// TODO(dragonly): refactor to GetPrincipalByEmail, and redirect callers using ID to GetPrincipalByIDfunc (s *Store) FindPrincipal(ctx context.Context, find *api.PrincipalFind) (*api.Principal, error) {- principalRaw, err := s.findPrincipalRaw(ctx, find)+ principalRaw, err := s.getPrincipalRaw(ctx, find)if err != nil {return nil, fmt.Errorf("Failed to find Principal with PrincipalFind[%+v], error[%w]", find, err)}@@ -114,12 +115,12 @@ func (s *Store) GetPrincipalByID(ctx context.Context, id int) (*api.Principal, eprincipalFind := &api.PrincipalFind{ID: &id,}- principalRaw, err := s.findPrincipalRaw(ctx, principalFind)+ principalRaw, err := s.getPrincipalRaw(ctx, principalFind)if err != nil {return nil, err}- if id > 0 && principalRaw == nil {- return nil, fmt.Errorf("Principal not found with ID[%d], error[%w]", id, err)+ if principalRaw == nil {+ return nil, nil}principal, err := s.composePrincipal(ctx, principalRaw)@@ -180,9 +181,9 @@ func (s *Store) findPrincipalRawList(ctx context.Context) ([]*principalRaw, erroreturn list, nil}-// findPrincipalRaw retrieves an instance of principalRaw based on find.+// getPrincipalRaw retrieves an instance of principalRaw based on find.// Returns ECONFLICT if finding more than 1 matching records.-func (s *Store) findPrincipalRaw(ctx context.Context, find *api.PrincipalFind) (*principalRaw, error) {+func (s *Store) getPrincipalRaw(ctx context.Context, find *api.PrincipalFind) (*principalRaw, error) {if find.ID != nil {principalRaw := &principalRaw{}has, err := s.cache.FindCache(api.PrincipalCache, *find.ID, principalRaw)@@ -249,19 +250,18 @@ func (s *Store) composePrincipal(ctx context.Context, raw *principalRaw) (*api.Pif principal.ID == api.SystemBotID {principal.Role = api.Owner} else {- memberFind := &api.MemberFind{- PrincipalID: &principal.ID,- }- memberRaw, err := s.getMemberRaw(ctx, memberFind)+ find := &api.MemberFind{PrincipalID: &principal.ID}+ memberRaw, err := s.getMemberRaw(ctx, find)if err != nil {- if common.ErrorCode(err) == common.NotFound {- s.l.Error("Principal has not been assigned a role.",- zap.Int("id", principal.ID),- zap.String("name", principal.Name),- )- }return nil, err}+ if memberRaw == nil {+ s.l.Error("Principal has not been assigned a role.",+ zap.Int("id", principal.ID),+ zap.String("name", principal.Name),+ )+ return nil, fmt.Errorf("Member with PrincipalID[%d] not exist, error[%w]", principal.ID, err)+ }principal.Role = memberRaw.Role}return principal, nildiff --git a/store/vcs.go b/store/vcs.goindex 8aca318bb4..ac50bbf375 100644--- a/store/vcs.go+++ b/store/vcs.go@@ -87,6 +87,9 @@ func (s *Store) GetVCSByID(ctx context.Context, id int) (*api.VCS, error) {if err != nil {return nil, err}+ if vcsRaw == nil {+ return nil, nil+ }vcs, err := s.composeVCS(ctx, vcsRaw)if err != nil {@@ -192,7 +195,7 @@ func (s *Store) getVCSRaw(ctx context.Context, id int) (*vcsRaw, error) {}if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("VCS not found with VCSFind[%+v]", find)}+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d VCSs with filter %+v, expect 1", len(list), find)}}