aboutsummaryrefslogtreecommitdiffstats
path: root/services/org
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2024-12-17 19:44:16 -0800
committerGitHub <noreply@github.com>2024-12-18 11:44:16 +0800
commite4c4629465f4e45ffdb9ef5eff6ef17e15ee4c2d (patch)
tree14ac77bb922dfdb9883630f6b2769973becb6f7e /services/org
parentb9457422934f33a37e0285d100007aae12e87ff7 (diff)
downloadgitea-e4c4629465f4e45ffdb9ef5eff6ef17e15ee4c2d.tar.gz
gitea-e4c4629465f4e45ffdb9ef5eff6ef17e15ee4c2d.zip
Move RepoTransfer from models to models/repo sub package (#32506)
`RepoTransfer` now is at models, but if we want to move it into `repo` model, it will depend on `Team`. So this PR also makes repo model depend on org model to make it possible. Just refactor, no code change. - [x] Move `DeleteOrganization` from `models/organization` to service layer - [x] Move `AccessibleTeamReposEnv` to `models/repo` - [x] Move `RepoTransfer` from `models` to `models/repo` - [x] Merge `getUserTeamIDs` and `GetUserTeamIDs`, Merge `GetUserTeams` and `getUserTeams`. - [x] Remove `Team`'s `Repos []*repo_model.Repository` to avoid dependency recycle.
Diffstat (limited to 'services/org')
-rw-r--r--services/org/org.go31
-rw-r--r--services/org/team.go29
-rw-r--r--services/org/team_test.go11
-rw-r--r--services/org/user.go2
4 files changed, 55 insertions, 18 deletions
diff --git a/services/org/org.go b/services/org/org.go
index c19572a123..471e6fcaf1 100644
--- a/services/org/org.go
+++ b/services/org/org.go
@@ -8,16 +8,45 @@ import (
"fmt"
"code.gitea.io/gitea/models"
+ actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/db"
org_model "code.gitea.io/gitea/models/organization"
packages_model "code.gitea.io/gitea/models/packages"
repo_model "code.gitea.io/gitea/models/repo"
+ secret_model "code.gitea.io/gitea/models/secret"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util"
repo_service "code.gitea.io/gitea/services/repository"
)
+// deleteOrganization deletes models associated to an organization.
+func deleteOrganization(ctx context.Context, org *org_model.Organization) error {
+ if org.Type != user_model.UserTypeOrganization {
+ return fmt.Errorf("%s is a user not an organization", org.Name)
+ }
+
+ if err := db.DeleteBeans(ctx,
+ &org_model.Team{OrgID: org.ID},
+ &org_model.OrgUser{OrgID: org.ID},
+ &org_model.TeamUser{OrgID: org.ID},
+ &org_model.TeamUnit{OrgID: org.ID},
+ &org_model.TeamInvite{OrgID: org.ID},
+ &secret_model.Secret{OwnerID: org.ID},
+ &user_model.Blocking{BlockerID: org.ID},
+ &actions_model.ActionRunner{OwnerID: org.ID},
+ &actions_model.ActionRunnerToken{OwnerID: org.ID},
+ ); err != nil {
+ return fmt.Errorf("DeleteBeans: %w", err)
+ }
+
+ if _, err := db.GetEngine(ctx).ID(org.ID).Delete(new(user_model.User)); err != nil {
+ return fmt.Errorf("Delete: %w", err)
+ }
+
+ return nil
+}
+
// DeleteOrganization completely and permanently deletes everything of organization.
func DeleteOrganization(ctx context.Context, org *org_model.Organization, purge bool) error {
ctx, committer, err := db.TxContext(ctx)
@@ -48,7 +77,7 @@ func DeleteOrganization(ctx context.Context, org *org_model.Organization, purge
return models.ErrUserOwnPackages{UID: org.ID}
}
- if err := org_model.DeleteOrganization(ctx, org); err != nil {
+ if err := deleteOrganization(ctx, org); err != nil {
return fmt.Errorf("DeleteOrganization: %w", err)
}
diff --git a/services/org/team.go b/services/org/team.go
index 3688e68433..ee3bd898ea 100644
--- a/services/org/team.go
+++ b/services/org/team.go
@@ -141,11 +141,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 {
- return fmt.Errorf("LoadRepositories: %w", err)
+ repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
+ TeamID: t.ID,
+ })
+ if err != nil {
+ return fmt.Errorf("GetTeamRepositories: %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)
}
@@ -172,10 +175,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
}
@@ -301,8 +300,11 @@ 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 {
- log.Error("team.LoadRepositories failed: %v", err)
+ repos, err := repo_model.GetTeamRepositories(ctx, &repo_model.SearchTeamRepoOptions{
+ TeamID: team.ID,
+ })
+ if err != nil {
+ log.Error("GetTeamRepositories failed: %v", err)
}
// FIXME: in the goroutine, it can't access the "ctx", it could only use db.DefaultContext at the moment
@@ -312,7 +314,7 @@ func AddTeamMember(ctx context.Context, team *organization.Team, user *user_mode
log.Error("watch repo failed: %v", err)
}
}
- }(team.Repos)
+ }(repos)
}
return nil
@@ -332,7 +334,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
}
@@ -350,7 +355,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
}
diff --git a/services/org/team_test.go b/services/org/team_test.go
index 98addac8f8..3791776e46 100644
--- a/services/org/team_test.go
+++ b/services/org/team_test.go
@@ -189,9 +189,12 @@ func TestIncludesAllRepositoriesTeams(t *testing.T) {
testTeamRepositories := func(teamID int64, repoIDs []int64) {
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
- assert.NoError(t, team.LoadRepositories(db.DefaultContext), "%s: GetRepositories", team.Name)
- assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name)
- assert.Len(t, team.Repos, len(repoIDs), "%s: repo count", team.Name)
+ repos, err := repo_model.GetTeamRepositories(db.DefaultContext, &repo_model.SearchTeamRepoOptions{
+ TeamID: team.ID,
+ })
+ assert.NoError(t, err, "%s: GetTeamRepositories", team.Name)
+ assert.Len(t, repos, team.NumRepos, "%s: len repo", team.Name)
+ assert.Len(t, repos, len(repoIDs), "%s: repo count", team.Name)
for i, rid := range repoIDs {
if rid > 0 {
assert.True(t, repo_service.HasRepository(db.DefaultContext, team, rid), "%s: HasRepository(%d) %d", rid, i)
@@ -310,5 +313,5 @@ func TestIncludesAllRepositoriesTeams(t *testing.T) {
assert.NoError(t, repo_service.DeleteRepositoryDirectly(db.DefaultContext, user, rid), "DeleteRepository %d", i)
}
}
- assert.NoError(t, organization.DeleteOrganization(db.DefaultContext, org), "DeleteOrganization")
+ assert.NoError(t, DeleteOrganization(db.DefaultContext, org, false), "DeleteOrganization")
}
diff --git a/services/org/user.go b/services/org/user.go
index 0627860fe7..0e74d006bb 100644
--- a/services/org/user.go
+++ b/services/org/user.go
@@ -60,7 +60,7 @@ func RemoveOrgUser(ctx context.Context, org *organization.Organization, user *us
}
// Delete all repository accesses and unwatch them.
- env, err := organization.AccessibleReposEnv(ctx, org, user.ID)
+ env, err := repo_model.AccessibleReposEnv(ctx, org, user.ID)
if err != nil {
return fmt.Errorf("AccessibleReposEnv: %w", err)
}