mirror of
https://github.com/go-gitea/gitea
synced 2024-11-24 20:52:52 +01:00
Make repo model depends on org model but not opposite
This commit is contained in:
parent
b4abb6deff
commit
fc0b2ca1d3
11 changed files with 86 additions and 89 deletions
|
@ -56,7 +56,7 @@ func AddRepository(ctx context.Context, t *organization.Team, repo *repo_model.R
|
||||||
// addAllRepositories adds all repositories to the team.
|
// addAllRepositories adds all repositories to the team.
|
||||||
// If the team already has some repositories they will be left unchanged.
|
// If the team already has some repositories they will be left unchanged.
|
||||||
func addAllRepositories(ctx context.Context, t *organization.Team) error {
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("get org repos: %w", err)
|
return fmt.Errorf("get org repos: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -93,25 +93,24 @@ func RemoveAllRepositories(ctx context.Context, t *organization.Team) (err error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, committer, err := db.TxContext(ctx)
|
repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
|
||||||
|
TeamID: t.ID,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("GetTeamRepositories: %w", err)
|
||||||
}
|
|
||||||
defer committer.Close()
|
|
||||||
|
|
||||||
if err = removeAllRepositories(ctx, t); err != nil {
|
|
||||||
return 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
|
// removeAllRepositories removes all repositories from team and recalculates access
|
||||||
// Note: Shall not be called if team includes all repositories
|
// 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)
|
e := db.GetEngine(ctx)
|
||||||
// Delete all accesses.
|
// Delete all accesses.
|
||||||
for _, repo := range t.Repos {
|
for _, repo := range repos {
|
||||||
if err := access_model.RecalculateTeamAccesses(ctx, repo, t.ID); err != nil {
|
if err := access_model.RecalculateTeamAccesses(ctx, repo, t.ID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -269,11 +268,14 @@ func UpdateTeam(ctx context.Context, t *organization.Team, authChanged, includeA
|
||||||
|
|
||||||
// Update access for team members if needed.
|
// Update access for team members if needed.
|
||||||
if authChanged {
|
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)
|
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 {
|
if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
|
||||||
return fmt.Errorf("recalculateTeamAccesses: %w", err)
|
return fmt.Errorf("recalculateTeamAccesses: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -300,10 +302,6 @@ func DeleteTeam(ctx context.Context, t *organization.Team) error {
|
||||||
}
|
}
|
||||||
defer committer.Close()
|
defer committer.Close()
|
||||||
|
|
||||||
if err := t.LoadRepositories(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := t.LoadMembers(ctx); err != nil {
|
if err := t.LoadMembers(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -325,7 +323,13 @@ func DeleteTeam(ctx context.Context, t *organization.Team) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !t.IncludesAllRepositories {
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -431,7 +435,10 @@ func AddTeamMember(ctx context.Context, team *organization.Team, user *user_mode
|
||||||
// FIXME: Update watch repos batchly
|
// FIXME: Update watch repos batchly
|
||||||
if setting.Service.AutoWatchNewRepos {
|
if setting.Service.AutoWatchNewRepos {
|
||||||
// Get team and its repositories.
|
// 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)
|
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)
|
log.Error("watch repo failed: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(team.Repos)
|
}(repos)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -462,7 +469,10 @@ func removeTeamMember(ctx context.Context, team *organization.Team, user *user_m
|
||||||
|
|
||||||
team.NumMembers--
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -480,7 +490,7 @@ func removeTeamMember(ctx context.Context, team *organization.Team, user *user_m
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete access to team repositories.
|
// 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 {
|
if err := access_model.RecalculateUserAccess(ctx, repo, user.ID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
"code.gitea.io/gitea/models/perm"
|
"code.gitea.io/gitea/models/perm"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
|
||||||
"code.gitea.io/gitea/models/unit"
|
"code.gitea.io/gitea/models/unit"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
@ -79,7 +78,6 @@ type Team struct {
|
||||||
Name string
|
Name string
|
||||||
Description string
|
Description string
|
||||||
AccessMode perm.AccessMode `xorm:"'authorize'"`
|
AccessMode perm.AccessMode `xorm:"'authorize'"`
|
||||||
Repos []*repo_model.Repository `xorm:"-"`
|
|
||||||
Members []*user_model.User `xorm:"-"`
|
Members []*user_model.User `xorm:"-"`
|
||||||
NumRepos int
|
NumRepos int
|
||||||
NumMembers int
|
NumMembers int
|
||||||
|
@ -155,17 +153,6 @@ func (t *Team) IsMember(ctx context.Context, userID int64) bool {
|
||||||
return isMember
|
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.
|
// LoadMembers returns paginated members in team of organization.
|
||||||
func (t *Team) LoadMembers(ctx context.Context) (err error) {
|
func (t *Team) LoadMembers(ctx context.Context) (err error) {
|
||||||
t.Members, err = GetTeamMembers(ctx, &SearchMembersOptions{
|
t.Members, err = GetTeamMembers(ctx, &SearchMembersOptions{
|
||||||
|
|
|
@ -9,7 +9,6 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
"code.gitea.io/gitea/models/perm"
|
"code.gitea.io/gitea/models/perm"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
|
||||||
"code.gitea.io/gitea/models/unit"
|
"code.gitea.io/gitea/models/unit"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"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
|
// 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).
|
return teams, db.GetEngine(ctx).
|
||||||
Join("INNER", "team_repo", "team_repo.team_id = team.id").
|
Join("INNER", "team_repo", "team_repo.team_id = team.id").
|
||||||
Where("team.org_id = ?", repo.OwnerID).
|
Where("team.org_id = ?", orgID).
|
||||||
And("team_repo.repo_id=?", repo.ID).
|
And("team_repo.repo_id=?", repoID).
|
||||||
OrderBy("CASE WHEN name LIKE '" + OwnerTeamName + "' THEN '' ELSE name END").
|
OrderBy("CASE WHEN name LIKE '" + OwnerTeamName + "' THEN '' ELSE name END").
|
||||||
Find(&teams)
|
Find(&teams)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,6 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
"code.gitea.io/gitea/models/perm"
|
"code.gitea.io/gitea/models/perm"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
|
||||||
|
|
||||||
"xorm.io/builder"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TeamRepo represents an team-repository relation.
|
// TeamRepo represents an team-repository relation.
|
||||||
|
@ -31,29 +28,6 @@ func HasTeamRepo(ctx context.Context, orgID, teamID, repoID int64) bool {
|
||||||
return has
|
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
|
// AddTeamRepo adds a repo for an organization's team
|
||||||
func AddTeamRepo(ctx context.Context, orgID, teamID, repoID int64) error {
|
func AddTeamRepo(ctx context.Context, orgID, teamID, repoID int64) error {
|
||||||
_, err := db.GetEngine(ctx).Insert(&TeamRepo{
|
_, err := db.GetEngine(ctx).Insert(&TeamRepo{
|
||||||
|
|
40
models/repo/org_repo.go
Normal file
40
models/repo/org_repo.go
Normal 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)
|
||||||
|
}
|
|
@ -573,7 +573,7 @@ func GetTeamRepos(ctx *context.APIContext) {
|
||||||
// "$ref": "#/responses/notFound"
|
// "$ref": "#/responses/notFound"
|
||||||
|
|
||||||
team := ctx.Org.Team
|
team := ctx.Org.Team
|
||||||
teamRepos, err := organization.GetTeamRepositories(ctx, &organization.SearchTeamRepoOptions{
|
teamRepos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
|
||||||
ListOptions: utils.GetListOptions(ctx),
|
ListOptions: utils.GetListOptions(ctx),
|
||||||
TeamID: team.ID,
|
TeamID: team.ID,
|
||||||
})
|
})
|
||||||
|
|
|
@ -43,7 +43,7 @@ func ListTeams(ctx *context.APIContext) {
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
ctx.InternalServerError(err)
|
ctx.InternalServerError(err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -411,11 +411,15 @@ func TeamRepositories(ctx *context.Context) {
|
||||||
return
|
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)
|
ctx.ServerError("GetRepositories", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Data["Units"] = unit_model.Units
|
ctx.Data["Units"] = unit_model.Units
|
||||||
|
ctx.Data["TeamRepos"] = repos
|
||||||
ctx.HTML(http.StatusOK, tplTeamRepositories)
|
ctx.HTML(http.StatusOK, tplTeamRepositories)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ func Collaboration(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
ctx.Data["Collaborators"] = users
|
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 {
|
if err != nil {
|
||||||
ctx.ServerError("GetRepoTeams", err)
|
ctx.ServerError("GetRepoTeams", err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
<div class="ui{{if not $canAddRemove}} top{{end}} attached segment">
|
<div class="ui{{if not $canAddRemove}} top{{end}} attached segment">
|
||||||
<div class="flex-list">
|
<div class="flex-list">
|
||||||
{{range .Team.Repos}}
|
{{range $.TeamRepos}}
|
||||||
<div class="flex-item tw-items-center">
|
<div class="flex-item tw-items-center">
|
||||||
<div class="flex-item-leading">
|
<div class="flex-item-leading">
|
||||||
{{template "repo/icon" .}}
|
{{template "repo/icon" .}}
|
||||||
|
|
Loading…
Reference in a new issue