mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-06 22:33:47 +02:00
* move error-related code for groups to its own file * update group avatar logic remove unused/duplicate logic * update `FindGroupsOptions.ToConds()` allow passing `-1` as the `ParentGroupID`, meaning "find matching groups regardless of the parent group id" * add `DedupeBy` function to container module this removes duplicate items from a slice using a custom function * add `SliceMap` util works like javascripts's `Array.prototoype.map`, taking in a slice and transforming each element with the provided function * add group service functions included so far: - avatar uploading/deletion - group deletion - group creation - group moving (including moving item inside a group) - group update - team management - add team - remove team - update team permissions - recalculating team access (in event of group move) - group searching (only used in frontend/web components for now)
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package group
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
group_model "code.gitea.io/gitea/models/group"
|
|
"code.gitea.io/gitea/models/organization"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
func NewGroup(ctx context.Context, g *group_model.Group) (err error) {
|
|
if len(g.Name) == 0 {
|
|
return util.NewInvalidArgumentErrorf("empty group name")
|
|
}
|
|
has, err := db.ExistByID[user_model.User](ctx, g.OwnerID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !has {
|
|
return organization.ErrOrgNotExist{ID: g.OwnerID}
|
|
}
|
|
g.LowerName = strings.ToLower(g.Name)
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
|
|
if err = db.Insert(ctx, g); err != nil {
|
|
return
|
|
}
|
|
|
|
if err = RecalculateGroupAccess(ctx, g, true); err != nil {
|
|
return
|
|
}
|
|
|
|
return committer.Commit()
|
|
}
|
|
|
|
func MoveRepositoryToGroup(ctx context.Context, repo *repo_model.Repository, newGroupID int64, groupSortOrder int) error {
|
|
sess := db.GetEngine(ctx)
|
|
repo.GroupID = newGroupID
|
|
repo.GroupSortOrder = groupSortOrder
|
|
cnt, err := sess.
|
|
Table("repository").
|
|
ID(repo.ID).
|
|
MustCols("group_id").
|
|
Update(repo)
|
|
log.Info("updated %d rows?", cnt)
|
|
return err
|
|
}
|
|
|
|
func MoveGroupItem(ctx context.Context, itemID, newParent int64, isGroup bool, newPos int) (err error) {
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
|
|
if isGroup {
|
|
group, err := group_model.GetGroupByID(ctx, itemID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if group.ParentGroupID != newParent || group.SortOrder != newPos {
|
|
if err = group_model.MoveGroup(ctx, group, newParent, newPos); err != nil {
|
|
return err
|
|
}
|
|
if err = RecalculateGroupAccess(ctx, group, false); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
} else {
|
|
repo, err := repo_model.GetRepositoryByID(ctx, itemID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if repo.GroupID != newParent || repo.GroupSortOrder != newPos {
|
|
if err = MoveRepositoryToGroup(ctx, repo, newParent, newPos); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return committer.Commit()
|
|
}
|