Skip to content

Commit

Permalink
Fix org creation (woodpecker-ci#4722)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty287 authored Jan 13, 2025
1 parent a3fcd41 commit e51df2d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
19 changes: 10 additions & 9 deletions server/store/datastore/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 14 additions & 11 deletions server/store/datastore/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
File renamed without changes.

0 comments on commit e51df2d

Please sign in to comment.