From e51df2db5e3f4351a15115edb4724f4b379c54e9 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Mon, 13 Jan 2025 11:09:44 +0200 Subject: [PATCH] Fix org creation (#4722) --- server/store/datastore/org.go | 19 +++++++------- server/store/datastore/user.go | 25 +++++++++++-------- .../datastore/{users_test.go => user_test.go} | 0 3 files changed, 24 insertions(+), 20 deletions(-) rename server/store/datastore/{users_test.go => user_test.go} (100%) diff --git a/server/store/datastore/org.go b/server/store/datastore/org.go index 59fa18f804a..e9d305d51ef 100644 --- a/server/store/datastore/org.go +++ b/server/store/datastore/org.go @@ -44,10 +44,14 @@ func (s storage) OrgGet(id int64) (*model.Org, error) { } func (s storage) OrgUpdate(org *model.Org) error { + return s.orgUpdate(s.engine.NewSession(), org) +} + +func (s storage) orgUpdate(sess *xorm.Session, org *model.Org) error { // sanitize org.Name = strings.ToLower(org.Name) // update - _, err := s.engine.ID(org.ID).AllCols().Update(org) + _, err := sess.ID(org.ID).AllCols().Update(org) return err } @@ -75,17 +79,14 @@ func (s storage) orgDelete(sess *xorm.Session, id int64) error { } func (s storage) OrgFindByName(name string) (*model.Org, error) { + return s.orgFindByName(s.engine.NewSession(), name) +} + +func (s storage) orgFindByName(sess *xorm.Session, name string) (*model.Org, error) { // sanitize name = strings.ToLower(name) org := new(model.Org) - has, err := s.engine.Where("name = ?", name).Get(org) - if err != nil { - return nil, fmt.Errorf("failed to check if org exists: %w", err) - } - if !has { - return nil, nil - } - return org, nil + return org, wrapGet(sess.Where("name = ?", name).Get(org)) } func (s storage) OrgRepoList(org *model.Org, p *model.ListOptions) ([]*model.Repo, error) { diff --git a/server/store/datastore/user.go b/server/store/datastore/user.go index deeddf54275..990e7894ff5 100644 --- a/server/store/datastore/user.go +++ b/server/store/datastore/user.go @@ -15,11 +15,13 @@ package datastore import ( + "errors" "fmt" "xorm.io/xorm" "go.woodpecker-ci.org/woodpecker/v3/server/model" + "go.woodpecker-ci.org/woodpecker/v3/server/store/types" ) func (s storage) GetUser(id int64) (*model.User, error) { @@ -62,17 +64,18 @@ func (s storage) CreateUser(user *model.User) error { IsUser: true, } - existingOrg, err := s.OrgFindByName(org.Name) - if err != nil { + existingOrg, err := s.orgFindByName(sess, org.Name) + if err != nil && !errors.Is(err, types.RecordNotExist) { return fmt.Errorf("failed to check if org exists: %w", err) } - if existingOrg != nil { - if existingOrg.Name == user.Login { - err = s.OrgUpdate(org) - if err != nil { - return fmt.Errorf("failed to update existing org: %w", err) - } + if !errors.Is(err, types.RecordNotExist) { + org = existingOrg + org.IsUser = true + org.Name = user.Login + err = s.orgUpdate(sess, org) + if err != nil { + return fmt.Errorf("failed to update existing org: %w", err) } } else { err = s.orgCreate(org, sess) @@ -99,15 +102,15 @@ func (s storage) DeleteUser(user *model.User) error { } if err := s.orgDelete(sess, user.OrgID); err != nil { - return err + return fmt.Errorf("failed to delete org: %w", err) } if err := wrapDelete(sess.ID(user.ID).Delete(new(model.User))); err != nil { - return err + return fmt.Errorf("failed to delete user: %w", err) } if _, err := sess.Where("user_id = ?", user.ID).Delete(new(model.Perm)); err != nil { - return err + return fmt.Errorf("failed to delete perms: %w", err) } return sess.Commit() diff --git a/server/store/datastore/users_test.go b/server/store/datastore/user_test.go similarity index 100% rename from server/store/datastore/users_test.go rename to server/store/datastore/user_test.go