2019-11-15 13:36:11 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-15 13:36:11 +05:30
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2022-12-10 08:16:31 +05:30
|
|
|
"context"
|
2020-01-31 21:19:04 +05:30
|
|
|
"fmt"
|
|
|
|
|
2019-11-15 13:36:11 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-12-17 11:54:47 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-08-25 08:01:57 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-01-15 08:44:50 +05:30
|
|
|
"code.gitea.io/gitea/modules/sync"
|
2023-09-06 00:07:47 +05:30
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2019-11-15 13:36:11 +05:30
|
|
|
)
|
|
|
|
|
2020-01-15 08:44:50 +05:30
|
|
|
// repoWorkingPool represents a working pool to order the parallel changes to the same repository
|
2022-05-04 21:36:23 +05:30
|
|
|
// TODO: use clustered lock (unique queue? or *abuse* cache)
|
2020-01-15 08:44:50 +05:30
|
|
|
var repoWorkingPool = sync.NewExclusivePool()
|
|
|
|
|
2019-11-15 13:36:11 +05:30
|
|
|
// TransferOwnership transfers all corresponding setting from old user to new one.
|
2022-12-10 08:16:31 +05:30
|
|
|
func TransferOwnership(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error {
|
2023-02-18 17:41:03 +05:30
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
2019-11-15 13:36:11 +05:30
|
|
|
return err
|
|
|
|
}
|
2020-01-31 21:19:04 +05:30
|
|
|
for _, team := range teams {
|
|
|
|
if newOwner.ID != team.OrgID {
|
|
|
|
return fmt.Errorf("team %d does not belong to organization", team.ID)
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 13:36:11 +05:30
|
|
|
|
|
|
|
oldOwner := repo.Owner
|
|
|
|
|
2020-12-25 15:29:32 +05:30
|
|
|
repoWorkingPool.CheckIn(fmt.Sprint(repo.ID))
|
2020-01-31 21:19:04 +05:30
|
|
|
if err := models.TransferOwnership(doer, newOwner.Name, repo); err != nil {
|
2020-12-25 15:29:32 +05:30
|
|
|
repoWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
return err
|
|
|
|
}
|
2020-12-25 15:29:32 +05:30
|
|
|
repoWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
|
2022-12-10 08:16:31 +05:30
|
|
|
newRepo, err := repo_model.GetRepositoryByID(ctx, repo.ID)
|
2020-01-31 21:19:04 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, team := range teams {
|
2022-12-10 08:16:31 +05:30
|
|
|
if err := models.AddRepository(ctx, team, newRepo); err != nil {
|
2020-01-31 21:19:04 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 00:07:47 +05:30
|
|
|
notify_service.TransferRepository(ctx, doer, repo, oldOwner.Name)
|
2019-11-15 13:36:11 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
|
2023-03-01 03:47:51 +05:30
|
|
|
func ChangeRepositoryName(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, newRepoName string) error {
|
2021-12-17 11:54:47 +05:30
|
|
|
log.Trace("ChangeRepositoryName: %s/%s -> %s", doer.Name, repo.Name, newRepoName)
|
|
|
|
|
2019-11-15 13:36:11 +05:30
|
|
|
oldRepoName := repo.Name
|
|
|
|
|
2019-12-06 09:30:50 +05:30
|
|
|
// Change repository directory name. We must lock the local copy of the
|
|
|
|
// repo so that we can atomically rename the repo path and updates the
|
|
|
|
// local copy's origin accordingly.
|
2019-11-15 13:36:11 +05:30
|
|
|
|
2020-12-25 15:29:32 +05:30
|
|
|
repoWorkingPool.CheckIn(fmt.Sprint(repo.ID))
|
2021-12-12 21:18:20 +05:30
|
|
|
if err := repo_model.ChangeRepositoryName(doer, repo, newRepoName); err != nil {
|
2020-12-25 15:29:32 +05:30
|
|
|
repoWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
return err
|
|
|
|
}
|
2020-12-25 15:29:32 +05:30
|
|
|
repoWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
|
2021-12-17 11:54:47 +05:30
|
|
|
repo.Name = newRepoName
|
2023-09-06 00:07:47 +05:30
|
|
|
notify_service.RenameRepository(ctx, doer, repo, oldRepoName)
|
2019-11-15 13:36:11 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-01 06:17:30 +05:30
|
|
|
|
|
|
|
// StartRepositoryTransfer transfer a repo from one owner to a new one.
|
|
|
|
// it make repository into pending transfer state, if doer can not create repo for new owner.
|
2022-12-10 08:16:31 +05:30
|
|
|
func StartRepositoryTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error {
|
2021-03-01 06:17:30 +05:30
|
|
|
if err := models.TestRepositoryReadyForTransfer(repo.Status); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Admin is always allowed to transfer || user transfer repo back to his account
|
|
|
|
if doer.IsAdmin || doer.ID == newOwner.ID {
|
2022-12-10 08:16:31 +05:30
|
|
|
return TransferOwnership(ctx, doer, newOwner, repo, teams)
|
2021-03-01 06:17:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// If new owner is an org and user can create repos he can transfer directly too
|
|
|
|
if newOwner.IsOrganization() {
|
2022-12-10 08:16:31 +05:30
|
|
|
allowed, err := organization.CanCreateOrgRepo(ctx, newOwner.ID, doer.ID)
|
2021-03-01 06:17:30 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if allowed {
|
2022-12-10 08:16:31 +05:30
|
|
|
return TransferOwnership(ctx, doer, newOwner, repo, teams)
|
2021-03-01 06:17:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:00:35 +05:30
|
|
|
// In case the new owner would not have sufficient access to the repo, give access rights for read
|
2022-12-10 08:16:31 +05:30
|
|
|
hasAccess, err := access_model.HasAccess(ctx, newOwner.ID, repo)
|
2021-06-15 00:00:35 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !hasAccess {
|
2022-12-10 08:16:31 +05:30
|
|
|
if err := repo_module.AddCollaborator(ctx, repo, newOwner); err != nil {
|
2021-06-15 00:00:35 +05:30
|
|
|
return err
|
|
|
|
}
|
2022-12-10 08:16:31 +05:30
|
|
|
if err := repo_model.ChangeCollaborationAccessMode(ctx, repo, newOwner.ID, perm.AccessModeRead); err != nil {
|
2021-06-15 00:00:35 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:17:30 +05:30
|
|
|
// Make repo as pending for transfer
|
2021-12-10 06:57:50 +05:30
|
|
|
repo.Status = repo_model.RepositoryPendingTransfer
|
2022-12-10 08:16:31 +05:30
|
|
|
if err := models.CreatePendingRepositoryTransfer(ctx, doer, newOwner, repo.ID, teams); err != nil {
|
2021-03-01 06:17:30 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify users who are able to accept / reject transfer
|
2023-09-06 00:07:47 +05:30
|
|
|
notify_service.RepoPendingTransfer(ctx, doer, newOwner, repo)
|
2021-03-01 06:17:30 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|