Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move RepoTransfer from models to models/repo sub package #32506

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion models/activities/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func activityQueryCondition(ctx context.Context, opts GetFeedsOptions) (builder.
}

if opts.RequestedTeam != nil {
env := organization.OrgFromUser(opts.RequestedUser).AccessibleTeamReposEnv(ctx, opts.RequestedTeam)
env := repo_model.AccessibleTeamReposEnv(ctx, organization.OrgFromUser(opts.RequestedUser), opts.RequestedTeam)
teamRepoIDs, err := env.RepoIDs(1, opts.RequestedUser.NumRepos)
if err != nil {
return nil, fmt.Errorf("GetTeamRepositories: %w", err)
Expand Down
42 changes: 0 additions & 42 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,48 +72,6 @@ func (err ErrDeleteLastAdminUser) Error() string {
return fmt.Sprintf("can not delete the last admin user [uid: %d]", err.UID)
}

// ErrNoPendingRepoTransfer is an error type for repositories without a pending
// transfer request
type ErrNoPendingRepoTransfer struct {
RepoID int64
}

func (err ErrNoPendingRepoTransfer) Error() string {
return fmt.Sprintf("repository doesn't have a pending transfer [repo_id: %d]", err.RepoID)
}

// IsErrNoPendingTransfer is an error type when a repository has no pending
// transfers
func IsErrNoPendingTransfer(err error) bool {
_, ok := err.(ErrNoPendingRepoTransfer)
return ok
}

func (err ErrNoPendingRepoTransfer) Unwrap() error {
return util.ErrNotExist
}

// ErrRepoTransferInProgress represents the state of a repository that has an
// ongoing transfer
type ErrRepoTransferInProgress struct {
Uname string
Name string
}

// IsErrRepoTransferInProgress checks if an error is a ErrRepoTransferInProgress.
func IsErrRepoTransferInProgress(err error) bool {
_, ok := err.(ErrRepoTransferInProgress)
return ok
}

func (err ErrRepoTransferInProgress) Error() string {
return fmt.Sprintf("repository is already being transferred [uname: %s, name: %s]", err.Uname, err.Name)
}

func (err ErrRepoTransferInProgress) Unwrap() error {
return util.ErrAlreadyExist
}

// ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error.
type ErrInvalidCloneAddr struct {
Host string
Expand Down
2 changes: 1 addition & 1 deletion models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
54 changes: 32 additions & 22 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
Loading
Loading