diff --git a/internal/services/configstore/command/command.go b/internal/services/configstore/command/command.go index a3af427..286864e 100644 --- a/internal/services/configstore/command/command.go +++ b/internal/services/configstore/command/command.go @@ -275,7 +275,7 @@ func (s *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest) return err } if u != nil { - return util.NewErrBadRequest(errors.Errorf("user with name %q already exists", u.UserName)) + return util.NewErrBadRequest(errors.Errorf("user with name %q already exists", u.Name)) } if req.CreateUserLARequest != nil { @@ -301,8 +301,8 @@ func (s *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest) } user := &types.User{ - ID: uuid.NewV4().String(), - UserName: req.UserName, + ID: uuid.NewV4().String(), + Name: req.UserName, } if req.CreateUserLARequest != nil { if user.LinkedAccounts == nil { @@ -599,7 +599,7 @@ func (s *CommandHandler) UpdateUserLA(ctx context.Context, req *UpdateUserLARequ la, ok := user.LinkedAccounts[req.LinkedAccountID] if !ok { - return util.NewErrBadRequest(errors.Errorf("linked account id %q for user %q doesn't exist", req.LinkedAccountID, user.UserName)) + return util.NewErrBadRequest(errors.Errorf("linked account id %q for user %q doesn't exist", req.LinkedAccountID, user.Name)) } rs, err = s.readDB.GetRemoteSource(tx, la.RemoteSourceID) diff --git a/internal/services/configstore/configstore_test.go b/internal/services/configstore/configstore_test.go index c67a914..88d9caa 100644 --- a/internal/services/configstore/configstore_test.go +++ b/internal/services/configstore/configstore_test.go @@ -394,7 +394,7 @@ func TestProjectGroupsAndProjects(t *testing.T) { time.Sleep(2 * time.Second) t.Run("create a project in user root project group", func(t *testing.T) { - _, err := cs.ch.CreateProject(ctx, &types.Project{Name: "project01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.UserName)}}) + _, err := cs.ch.CreateProject(ctx, &types.Project{Name: "project01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.Name)}}) if err != nil { t.Fatalf("unexpected err: %v", err) } @@ -406,7 +406,7 @@ func TestProjectGroupsAndProjects(t *testing.T) { } }) t.Run("create a projectgroup in user root project group", func(t *testing.T) { - _, err := cs.ch.CreateProjectGroup(ctx, &types.ProjectGroup{Name: "projectgroup01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.UserName)}}) + _, err := cs.ch.CreateProjectGroup(ctx, &types.ProjectGroup{Name: "projectgroup01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.Name)}}) if err != nil { t.Fatalf("unexpected err: %v", err) } @@ -418,7 +418,7 @@ func TestProjectGroupsAndProjects(t *testing.T) { } }) t.Run("create a project in user non root project group with same name as a root project", func(t *testing.T) { - _, err := cs.ch.CreateProject(ctx, &types.Project{Name: "project01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.UserName, "projectgroup01")}}) + _, err := cs.ch.CreateProject(ctx, &types.Project{Name: "project01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.Name, "projectgroup01")}}) if err != nil { t.Fatalf("unexpected err: %+#v", err) } @@ -432,8 +432,8 @@ func TestProjectGroupsAndProjects(t *testing.T) { t.Run("create duplicated project in user root project group", func(t *testing.T) { projectName := "project01" - expectedErr := fmt.Sprintf("project with name %q, path %q already exists", projectName, path.Join("user", user.UserName, projectName)) - _, err := cs.ch.CreateProject(ctx, &types.Project{Name: projectName, Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.UserName)}}) + expectedErr := fmt.Sprintf("project with name %q, path %q already exists", projectName, path.Join("user", user.Name, projectName)) + _, err := cs.ch.CreateProject(ctx, &types.Project{Name: projectName, Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.Name)}}) if err.Error() != expectedErr { t.Fatalf("expected err %v, got err: %v", expectedErr, err) } @@ -449,8 +449,8 @@ func TestProjectGroupsAndProjects(t *testing.T) { t.Run("create duplicated project in user non root project group", func(t *testing.T) { projectName := "project01" - expectedErr := fmt.Sprintf("project with name %q, path %q already exists", projectName, path.Join("user", user.UserName, "projectgroup01", projectName)) - _, err := cs.ch.CreateProject(ctx, &types.Project{Name: projectName, Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.UserName, "projectgroup01")}}) + expectedErr := fmt.Sprintf("project with name %q, path %q already exists", projectName, path.Join("user", user.Name, "projectgroup01", projectName)) + _, err := cs.ch.CreateProject(ctx, &types.Project{Name: projectName, Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.Name, "projectgroup01")}}) if err.Error() != expectedErr { t.Fatalf("expected err %v, got err: %v", expectedErr, err) } @@ -488,7 +488,7 @@ func TestProjectGroupsAndProjects(t *testing.T) { wg := sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) - go cs.ch.CreateProject(ctx, &types.Project{Name: "project02", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.UserName)}}) + go cs.ch.CreateProject(ctx, &types.Project{Name: "project02", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("user", user.Name)}}) wg.Done() } wg.Wait() diff --git a/internal/services/configstore/readdb/projectgroup.go b/internal/services/configstore/readdb/projectgroup.go index 914b2d8..1bbcf34 100644 --- a/internal/services/configstore/readdb/projectgroup.go +++ b/internal/services/configstore/readdb/projectgroup.go @@ -132,7 +132,7 @@ func (r *ReadDB) GetProjectGroupPath(tx *db.Tx, group *types.ProjectGroup) (stri if user == nil { return "", errors.Errorf("cannot find user with id %q", rootGroupID) } - p = path.Join("user", user.UserName) + p = path.Join("user", user.Name) } for _, group := range groups { diff --git a/internal/services/configstore/readdb/user.go b/internal/services/configstore/readdb/user.go index fc8da9e..efbbbe4 100644 --- a/internal/services/configstore/readdb/user.go +++ b/internal/services/configstore/readdb/user.go @@ -50,7 +50,7 @@ func (r *ReadDB) insertUser(tx *db.Tx, data []byte) error { if err := r.deleteUser(tx, user.ID); err != nil { return err } - q, args, err := userInsert.Values(user.ID, user.UserName, data).ToSql() + q, args, err := userInsert.Values(user.ID, user.Name, data).ToSql() if err != nil { return errors.Wrap(err, "failed to build query") } diff --git a/internal/services/gateway/api/user.go b/internal/services/gateway/api/user.go index 961bc6e..9f2db8b 100644 --- a/internal/services/gateway/api/user.go +++ b/internal/services/gateway/api/user.go @@ -188,7 +188,7 @@ type UserResponse struct { func createUserResponse(u *types.User) *UserResponse { user := &UserResponse{ ID: u.ID, - UserName: u.UserName, + UserName: u.Name, Tokens: make([]string, 0, len(u.Tokens)), } for tokenName := range u.Tokens { diff --git a/internal/services/gateway/command/project.go b/internal/services/gateway/command/project.go index 8a2a79a..ad55279 100644 --- a/internal/services/gateway/command/project.go +++ b/internal/services/gateway/command/project.go @@ -63,7 +63,7 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe return nil, errors.Errorf("user doesn't have a linked account for remote source %q", rs.Name) } - gitsource, err := c.GetGitSource(ctx, rs, user.UserName, la) + gitsource, err := c.GetGitSource(ctx, rs, user.Name, la) if err != nil { return nil, errors.Wrapf(err, "failed to create gitsource client") } @@ -82,7 +82,7 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe parentID := req.ParentID if parentID == "" { // create project in current user namespace - parentID = path.Join("user", user.UserName) + parentID = path.Join("user", user.Name) } p := &types.Project{ @@ -109,7 +109,7 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe } func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, user *types.User, la *types.LinkedAccount, project *types.Project) error { - gitsource, err := c.GetGitSource(ctx, rs, user.UserName, la) + gitsource, err := c.GetGitSource(ctx, rs, user.Name, la) if err != nil { return errors.Wrapf(err, "failed to create gitsource client") } @@ -162,7 +162,7 @@ func (c *CommandHandler) ReconfigProject(ctx context.Context, projectRef string) la := user.LinkedAccounts[p.LinkedAccountID] c.log.Infof("la: %s", util.Dump(la)) if la == nil { - return errors.Errorf("linked account %q in user %q doesn't exist", p.LinkedAccountID, user.UserName) + return errors.Errorf("linked account %q in user %q doesn't exist", p.LinkedAccountID, user.Name) } rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID) diff --git a/internal/services/gateway/command/projectgroup.go b/internal/services/gateway/command/projectgroup.go index 158f3e3..ea3d917 100644 --- a/internal/services/gateway/command/projectgroup.go +++ b/internal/services/gateway/command/projectgroup.go @@ -43,7 +43,7 @@ func (c *CommandHandler) CreateProjectGroup(ctx context.Context, req *CreateProj parentID := req.ParentID if parentID == "" { // create projectGroup in current user namespace - parentID = path.Join("user", user.UserName) + parentID = path.Join("user", user.Name) } p := &types.ProjectGroup{ diff --git a/internal/services/gateway/command/user.go b/internal/services/gateway/command/user.go index 6c24515..8bcc1b5 100644 --- a/internal/services/gateway/command/user.go +++ b/internal/services/gateway/command/user.go @@ -61,7 +61,7 @@ func (c *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create user")) } - c.log.Infof("user %s created, ID: %s", u.UserName, u.ID) + c.log.Infof("user %s created, ID: %s", u.Name, u.ID) return u, nil } @@ -369,7 +369,7 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) ( } c.log.Infof("la: %s", util.Dump(la)) if la == nil { - return nil, errors.Errorf("linked account for user %q for remote source %q doesn't exist", user.UserName, rs.Name) + return nil, errors.Errorf("linked account for user %q for remote source %q doesn't exist", user.Name, rs.Name) } // Update oauth tokens if they have changed since the getuserinfo request may have updated them @@ -390,12 +390,12 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) ( Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt, } - c.log.Infof("updating user %q linked account", user.UserName) - la, resp, err = c.configstoreClient.UpdateUserLA(ctx, user.UserName, la.ID, creq) + c.log.Infof("updating user %q linked account", user.Name) + la, resp, err = c.configstoreClient.UpdateUserLA(ctx, user.Name, la.ID, creq) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to update user")) } - c.log.Infof("linked account %q for user %q updated", la.ID, user.UserName) + c.log.Infof("linked account %q for user %q updated", la.ID, user.Name) } // generate jwt token diff --git a/internal/services/gateway/handlers/auth.go b/internal/services/gateway/handlers/auth.go index c839a31..0996317 100644 --- a/internal/services/gateway/handlers/auth.go +++ b/internal/services/gateway/handlers/auth.go @@ -75,7 +75,7 @@ func (h *AuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // pass userid to handlers via context ctx = context.WithValue(ctx, "userid", user.ID) - ctx = context.WithValue(ctx, "username", user.UserName) + ctx = context.WithValue(ctx, "username", user.Name) h.next.ServeHTTP(w, r.WithContext(ctx)) return } @@ -124,7 +124,7 @@ func (h *AuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // pass userid to handlers via context ctx = context.WithValue(ctx, "userid", user.ID) - ctx = context.WithValue(ctx, "username", user.UserName) + ctx = context.WithValue(ctx, "username", user.Name) h.next.ServeHTTP(w, r.WithContext(ctx)) return } diff --git a/internal/services/gateway/webhook.go b/internal/services/gateway/webhook.go index 5a7a851..ca1b9d1 100644 --- a/internal/services/gateway/webhook.go +++ b/internal/services/gateway/webhook.go @@ -160,14 +160,14 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) { la := user.LinkedAccounts[project.LinkedAccountID] h.log.Infof("la: %s", util.Dump(la)) if la == nil { - return http.StatusInternalServerError, "", errors.Errorf("linked account %q in user %q doesn't exist", project.LinkedAccountID, user.UserName) + return http.StatusInternalServerError, "", errors.Errorf("linked account %q in user %q doesn't exist", project.LinkedAccountID, user.Name) } rs, _, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID) if err != nil { return http.StatusInternalServerError, "", errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID) } - gitSource, err = h.ch.GetGitSource(ctx, rs, user.UserName, la) + gitSource, err = h.ch.GetGitSource(ctx, rs, user.Name, la) if err != nil { return http.StatusInternalServerError, "", errors.Wrapf(err, "failed to create gitea client") } diff --git a/internal/services/types/types.go b/internal/services/types/types.go index a3770ba..219760c 100644 --- a/internal/services/types/types.go +++ b/internal/services/types/types.go @@ -48,7 +48,7 @@ type User struct { ID string `json:"id,omitempty"` - UserName string `json:"user_name,omitempty"` + Name string `json:"name,omitempty"` LinkedAccounts map[string]*LinkedAccount `json:"linked_accounts,omitempty"`