0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-21 23:03:39 +01:00

Make repo model depends on org model but not opposite

This commit is contained in:
Lunny Xiao 2024-11-11 00:37:00 -08:00
parent f888e45432
commit 13b67e227b
No known key found for this signature in database
GPG key ID: C3B7C91B632F738A
11 changed files with 86 additions and 89 deletions

View file

@ -56,7 +56,7 @@ func AddRepository(ctx context.Context, t *organization.Team, repo *repo_model.R
// addAllRepositories adds all repositories to the team.
// If the team already has some repositories they will be left unchanged.
func addAllRepositories(ctx context.Context, t *organization.Team) error {
orgRepos, err := organization.GetOrgRepositories(ctx, t.OrgID)
orgRepos, err := repo_model.GetOrgRepositories(ctx, t.OrgID)
if err != nil {
return fmt.Errorf("get org repos: %w", err)
}
@ -93,25 +93,24 @@ func RemoveAllRepositories(ctx context.Context, t *organization.Team) (err error
return nil
}
ctx, committer, err := db.TxContext(ctx)
repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
TeamID: t.ID,
})
if err != nil {
return err
}
defer committer.Close()
if err = removeAllRepositories(ctx, t); err != nil {
return err
return fmt.Errorf("GetTeamRepositories: %w", err)
}
return committer.Commit()
return db.WithTx(ctx, func(ctx context.Context) error {
return removeAllRepositories(ctx, t, repos)
})
}
// removeAllRepositories removes all repositories from team and recalculates access
// Note: Shall not be called if team includes all repositories
func removeAllRepositories(ctx context.Context, t *organization.Team) (err error) {
func removeAllRepositories(ctx context.Context, t *organization.Team, repos []*repo_model.Repository) (err error) {
e := db.GetEngine(ctx)
// Delete all accesses.
for _, repo := range t.Repos {
for _, repo := range repos {
if err := access_model.RecalculateTeamAccesses(ctx, repo, t.ID); err != nil {
return err
}
@ -269,11 +268,14 @@ func UpdateTeam(ctx context.Context, t *organization.Team, authChanged, includeA
// Update access for team members if needed.
if authChanged {
if err = t.LoadRepositories(ctx); err != nil {
repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
TeamID: t.ID,
})
if err != nil {
return fmt.Errorf("LoadRepositories: %w", err)
}
for _, repo := range t.Repos {
for _, repo := range repos {
if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
return fmt.Errorf("recalculateTeamAccesses: %w", err)
}
@ -300,10 +302,6 @@ func DeleteTeam(ctx context.Context, t *organization.Team) error {
}
defer committer.Close()
if err := t.LoadRepositories(ctx); err != nil {
return err
}
if err := t.LoadMembers(ctx); err != nil {
return err
}
@ -325,7 +323,13 @@ func DeleteTeam(ctx context.Context, t *organization.Team) error {
}
if !t.IncludesAllRepositories {
if err := removeAllRepositories(ctx, t); err != nil {
repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
TeamID: t.ID,
})
if err != nil {
return fmt.Errorf("GetTeamRepositories: %w", err)
}
if err := removeAllRepositories(ctx, t, repos); err != nil {
return err
}
}
@ -431,7 +435,10 @@ func AddTeamMember(ctx context.Context, team *organization.Team, user *user_mode
// FIXME: Update watch repos batchly
if setting.Service.AutoWatchNewRepos {
// Get team and its repositories.
if err := team.LoadRepositories(ctx); err != nil {
repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
TeamID: team.ID,
})
if err != nil {
log.Error("team.LoadRepositories failed: %v", err)
}
@ -442,7 +449,7 @@ func AddTeamMember(ctx context.Context, team *organization.Team, user *user_mode
log.Error("watch repo failed: %v", err)
}
}
}(team.Repos)
}(repos)
}
return nil
@ -462,7 +469,10 @@ func removeTeamMember(ctx context.Context, team *organization.Team, user *user_m
team.NumMembers--
if err := team.LoadRepositories(ctx); err != nil {
repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
TeamID: team.ID,
})
if err != nil {
return err
}
@ -480,7 +490,7 @@ func removeTeamMember(ctx context.Context, team *organization.Team, user *user_m
}
// Delete access to team repositories.
for _, repo := range team.Repos {
for _, repo := range repos {
if err := access_model.RecalculateUserAccess(ctx, repo, user.ID); err != nil {
return err
}

View file

@ -1,17 +0,0 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package organization
import (
"context"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
)
// GetOrgRepositories get repos belonging to the given organization
func GetOrgRepositories(ctx context.Context, orgID int64) (repo_model.RepositoryList, error) {
var orgRepos []*repo_model.Repository
return orgRepos, db.GetEngine(ctx).Where("owner_id = ?", orgID).Find(&orgRepos)
}

View file

@ -11,7 +11,6 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
@ -78,9 +77,8 @@ type Team struct {
LowerName string
Name string
Description string
AccessMode perm.AccessMode `xorm:"'authorize'"`
Repos []*repo_model.Repository `xorm:"-"`
Members []*user_model.User `xorm:"-"`
AccessMode perm.AccessMode `xorm:"'authorize'"`
Members []*user_model.User `xorm:"-"`
NumRepos int
NumMembers int
Units []*TeamUnit `xorm:"-"`
@ -155,17 +153,6 @@ func (t *Team) IsMember(ctx context.Context, userID int64) bool {
return isMember
}
// LoadRepositories returns paginated repositories in team of organization.
func (t *Team) LoadRepositories(ctx context.Context) (err error) {
if t.Repos != nil {
return nil
}
t.Repos, err = GetTeamRepositories(ctx, &SearchTeamRepoOptions{
TeamID: t.ID,
})
return err
}
// LoadMembers returns paginated members in team of organization.
func (t *Team) LoadMembers(ctx context.Context) (err error) {
t.Members, err = GetTeamMembers(ctx, &SearchMembersOptions{

View file

@ -9,7 +9,6 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"xorm.io/builder"
@ -98,11 +97,11 @@ func SearchTeam(ctx context.Context, opts *SearchTeamOptions) (TeamList, int64,
}
// GetRepoTeams gets the list of teams that has access to the repository
func GetRepoTeams(ctx context.Context, repo *repo_model.Repository) (teams TeamList, err error) {
func GetRepoTeams(ctx context.Context, orgID, repoID int64) (teams TeamList, err error) {
return teams, db.GetEngine(ctx).
Join("INNER", "team_repo", "team_repo.team_id = team.id").
Where("team.org_id = ?", repo.OwnerID).
And("team_repo.repo_id=?", repo.ID).
Where("team.org_id = ?", orgID).
And("team_repo.repo_id=?", repoID).
OrderBy("CASE WHEN name LIKE '" + OwnerTeamName + "' THEN '' ELSE name END").
Find(&teams)
}

View file

@ -8,9 +8,6 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"xorm.io/builder"
)
// TeamRepo represents an team-repository relation.
@ -31,29 +28,6 @@ func HasTeamRepo(ctx context.Context, orgID, teamID, repoID int64) bool {
return has
}
type SearchTeamRepoOptions struct {
db.ListOptions
TeamID int64
}
// GetRepositories returns paginated repositories in team of organization.
func GetTeamRepositories(ctx context.Context, opts *SearchTeamRepoOptions) (repo_model.RepositoryList, error) {
sess := db.GetEngine(ctx)
if opts.TeamID > 0 {
sess = sess.In("id",
builder.Select("repo_id").
From("team_repo").
Where(builder.Eq{"team_id": opts.TeamID}),
)
}
if opts.PageSize > 0 {
sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
var repos []*repo_model.Repository
return repos, sess.OrderBy("repository.name").
Find(&repos)
}
// AddTeamRepo adds a repo for an organization's team
func AddTeamRepo(ctx context.Context, orgID, teamID, repoID int64) error {
_, err := db.GetEngine(ctx).Insert(&TeamRepo{

40
models/repo/org_repo.go Normal file
View file

@ -0,0 +1,40 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"context"
"code.gitea.io/gitea/models/db"
"xorm.io/builder"
)
// GetOrgRepositories get repos belonging to the given organization
func GetOrgRepositories(ctx context.Context, orgID int64) (RepositoryList, error) {
var orgRepos []*Repository
return orgRepos, db.GetEngine(ctx).Where("owner_id = ?", orgID).Find(&orgRepos)
}
type SearchTeamRepoOptions struct {
db.ListOptions
TeamID int64
}
// GetRepositories returns paginated repositories in team of organization.
func GetTeamRepositories(ctx context.Context, opts *SearchTeamRepoOptions) (RepositoryList, error) {
sess := db.GetEngine(ctx)
if opts.TeamID > 0 {
sess = sess.In("id",
builder.Select("repo_id").
From("team_repo").
Where(builder.Eq{"team_id": opts.TeamID}),
)
}
if opts.PageSize > 0 {
sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
var repos []*Repository
return repos, sess.OrderBy("repository.name").
Find(&repos)
}

View file

@ -573,7 +573,7 @@ func GetTeamRepos(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
team := ctx.Org.Team
teamRepos, err := organization.GetTeamRepositories(ctx, &organization.SearchTeamRepoOptions{
teamRepos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
ListOptions: utils.GetListOptions(ctx),
TeamID: team.ID,
})

View file

@ -43,7 +43,7 @@ func ListTeams(ctx *context.APIContext) {
return
}
teams, err := organization.GetRepoTeams(ctx, ctx.Repo.Repository)
teams, err := organization.GetRepoTeams(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.ID)
if err != nil {
ctx.InternalServerError(err)
return

View file

@ -411,11 +411,15 @@ func TeamRepositories(ctx *context.Context) {
return
}
if err := ctx.Org.Team.LoadRepositories(ctx); err != nil {
repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
TeamID: ctx.Org.Team.ID,
})
if err != nil {
ctx.ServerError("GetRepositories", err)
return
}
ctx.Data["Units"] = unit_model.Units
ctx.Data["TeamRepos"] = repos
ctx.HTML(http.StatusOK, tplTeamRepositories)
}

View file

@ -33,7 +33,7 @@ func Collaboration(ctx *context.Context) {
}
ctx.Data["Collaborators"] = users
teams, err := organization.GetRepoTeams(ctx, ctx.Repo.Repository)
teams, err := organization.GetRepoTeams(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.ID)
if err != nil {
ctx.ServerError("GetRepoTeams", err)
return

View file

@ -27,7 +27,7 @@
{{end}}
<div class="ui{{if not $canAddRemove}} top{{end}} attached segment">
<div class="flex-list">
{{range .Team.Repos}}
{{range $.TeamRepos}}
<div class="flex-item tw-items-center">
<div class="flex-item-leading">
{{template "repo/icon" .}}