Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: refine GetPrincipal methods (#1197)

Yilong Li362b6eee81e4
diff --git a/server/activity_manager.go b/server/activity_manager.go
index 714333bcf6..5fa44d53aa 100644
--- a/server/activity_manager.go
+++ b/server/activity_manager.go
@@ -80,10 +80,7 @@ func (m *ActivityManager) CreateActivity(ctx context.Context, create *api.Activi
meta.issue.Project = project
}
- principalFind := &api.PrincipalFind{
- ID: &create.CreatorID,
- }
- updater, err := m.s.store.FindPrincipal(ctx, principalFind)
+ updater, err := m.s.store.GetPrincipalByID(ctx, create.CreatorID)
if err != nil {
return nil, fmt.Errorf("failed to find updater for posting webhook event after changing the issue status: %v, error: %w", meta.issue.Name, err)
}
@@ -158,10 +155,7 @@ func (m *ActivityManager) getWebhookContext(ctx context.Context, activity *api.A
zap.Error(err))
return webhookCtx, err
}
- principalFind := &api.PrincipalFind{
- ID: &oldID,
- }
- oldAssignee, err = m.s.store.FindPrincipal(ctx, principalFind)
+ oldAssignee, err = m.s.store.GetPrincipalByID(ctx, oldID)
if err != nil {
m.s.l.Warn("Failed to post webhook event after changing the issue assignee, failed to find old assignee",
zap.String("issue_name", meta.issue.Name),
@@ -188,10 +182,7 @@ func (m *ActivityManager) getWebhookContext(ctx context.Context, activity *api.A
zap.Error(err))
return webhookCtx, err
}
- principalFind := &api.PrincipalFind{
- ID: &newID,
- }
- newAssignee, err = m.s.store.FindPrincipal(ctx, principalFind)
+ newAssignee, err = m.s.store.GetPrincipalByID(ctx, newID)
if err != nil {
m.s.l.Warn("Failed to post webhook event after changing the issue assignee, failed to find new assignee",
zap.String("issue_name", meta.issue.Name),
diff --git a/server/auth.go b/server/auth.go
index a1fd4e620d..0f7490c65b 100644
--- a/server/auth.go
+++ b/server/auth.go
@@ -58,11 +58,8 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted login request").SetInternal(err)
}
- principalFind := &api.PrincipalFind{
- Email: &login.Email,
- }
var err error
- user, err = s.store.FindPrincipal(ctx, principalFind)
+ user, err = s.store.GetPrincipalByEmail(ctx, login.Email)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user").SetInternal(err)
}
@@ -124,10 +121,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Fail to login via Gitlab, user is Archived")
}
- principalFind := &api.PrincipalFind{
- Email: &gitlabUserInfo.PublicEmail,
- }
- user, err = s.store.FindPrincipal(ctx, principalFind)
+ user, err = s.store.GetPrincipalByEmail(ctx, gitlabUserInfo.PublicEmail)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user").SetInternal(err)
}
diff --git a/server/database.go b/server/database.go
index e9b4e518ce..c1a7afdbc0 100644
--- a/server/database.go
+++ b/server/database.go
@@ -47,7 +47,6 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
}
// Pre-validate database labels.
if databaseCreate.Labels != nil && *databaseCreate.Labels != "" {
- // TODO(dragonly): compose Instance
if err := s.setDatabaseLabels(ctx, *databaseCreate.Labels, &api.Database{Name: databaseCreate.Name, Instance: instance} /* dummy database */, project, databaseCreate.CreatorID, true /* validateOnly */); err != nil {
return err
}
diff --git a/server/issue.go b/server/issue.go
index 2653bdb606..b9c1912480 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -288,9 +288,7 @@ func (s *Server) composeIssueRelationshipValidateOnly(ctx context.Context, issue
// Only allow Bot/Owner/DBA as the assignee, not Developer.
func (s *Server) validateAssigneeRoleByID(ctx context.Context, assigneeID int) error {
- assignee, err := s.store.FindPrincipal(ctx, &api.PrincipalFind{
- ID: &assigneeID,
- })
+ assignee, err := s.store.GetPrincipalByID(ctx, assigneeID)
if err != nil {
return err
}
diff --git a/server/jwt.go b/server/jwt.go
index d0ecf86168..e94db9324c 100644
--- a/server/jwt.go
+++ b/server/jwt.go
@@ -225,10 +225,7 @@ func JWTMiddleware(l *zap.Logger, principalStore *store.Store, next echo.Handler
}
// Even if there is no error, we still need to make sure the user still exists.
- principalFind := &api.PrincipalFind{
- ID: &principalID,
- }
- user, err := principalStore.FindPrincipal(ctx, principalFind)
+ user, err := principalStore.GetPrincipalByID(ctx, principalID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Server error to find user ID: %d", principalID)).SetInternal(err)
}
diff --git a/server/member.go b/server/member.go
index e5a7b835e6..4f8b7ae8ef 100644
--- a/server/member.go
+++ b/server/member.go
@@ -32,10 +32,7 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {
// Record activity
{
- principalFind := &api.PrincipalFind{
- ID: &member.PrincipalID,
- }
- user, err := s.store.FindPrincipal(ctx, principalFind)
+ user, err := s.store.GetPrincipalByID(ctx, member.PrincipalID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Server error to find user ID: %d", member.PrincipalID)).SetInternal(err)
}
@@ -121,10 +118,7 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {
// Record activity
{
- principalFind := &api.PrincipalFind{
- ID: &updatedMember.PrincipalID,
- }
- user, err := s.store.FindPrincipal(ctx, principalFind)
+ user, err := s.store.GetPrincipalByID(ctx, updatedMember.PrincipalID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Server error to find user ID: %d", updatedMember.PrincipalID)).SetInternal(err)
}
diff --git a/server/principal.go b/server/principal.go
index b609b88b10..12d3592333 100644
--- a/server/principal.go
+++ b/server/principal.go
@@ -66,7 +66,7 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("principalID"))).SetInternal(err)
}
- principal, err := s.store.FindPrincipal(ctx, &api.PrincipalFind{ID: &id})
+ principal, err := s.store.GetPrincipalByID(ctx, id)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("User ID not found: %d", id))
diff --git a/server/project_member.go b/server/project_member.go
index 4979d5fe18..c6595f0555 100644
--- a/server/project_member.go
+++ b/server/project_member.go
@@ -72,8 +72,7 @@ func (s *Server) registerProjectMemberRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Invalid role provider, expected: %v, got: %v", vcs.Type, projectMember.RoleProvider)).SetInternal(err)
}
- findPrincipal := &api.PrincipalFind{Email: &projectMember.Email}
- principal, err := s.store.FindPrincipal(ctx, findPrincipal)
+ principal, err := s.store.GetPrincipalByEmail(ctx, projectMember.Email)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch principal info").SetInternal(err)
}
@@ -164,8 +163,7 @@ func (s *Server) registerProjectMemberRoutes(g *echo.Group) {
}
} else {
// elsewise, we will create a MEMBER CREATE activity
- principalFind := &api.PrincipalFind{ID: &createdMember.PrincipalID}
- principal, err := s.store.FindPrincipal(ctx, principalFind)
+ principal, err := s.store.GetPrincipalByID(ctx, createdMember.PrincipalID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Fail to find the relevant principal of the member relation, principal ID: %v", principal.ID)).SetInternal(err)
}
diff --git a/store/principal.go b/store/principal.go
index 2adba25d67..f648de5dae 100644
--- a/store/principal.go
+++ b/store/principal.go
@@ -80,9 +80,9 @@ func (s *Store) GetPrincipalList(ctx context.Context) ([]*api.Principal, error)
return principalList, nil
}
-// FindPrincipal finds an instance of Principal
-// TODO(dragonly): refactor to GetPrincipalByEmail, and redirect callers using ID to GetPrincipalByID
-func (s *Store) FindPrincipal(ctx context.Context, find *api.PrincipalFind) (*api.Principal, error) {
+// GetPrincipalByEmail gets an instance of Principal
+func (s *Store) GetPrincipalByEmail(ctx context.Context, email string) (*api.Principal, error) {
+ find := &api.PrincipalFind{Email: &email}
principalRaw, err := s.getPrincipalRaw(ctx, find)
if err != nil {
return nil, fmt.Errorf("failed to find Principal with PrincipalFind[%+v], error[%w]", find, err)
@@ -112,9 +112,7 @@ func (s *Store) PatchPrincipal(ctx context.Context, patch *api.PrincipalPatch) (
// GetPrincipalByID gets an instance of Principal by ID
func (s *Store) GetPrincipalByID(ctx context.Context, id int) (*api.Principal, error) {
- principalFind := &api.PrincipalFind{
- ID: &id,
- }
+ principalFind := &api.PrincipalFind{ID: &id}
principalRaw, err := s.getPrincipalRaw(ctx, principalFind)
if err != nil {
return nil, err