2019-11-15 09:06:11 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-15 09:06:11 +01:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2022-12-10 03:46:31 +01:00
|
|
|
"context"
|
2020-01-31 16:49:04 +01:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-15 09:06:11 +01:00
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 02:27:50 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 08:29:02 +02:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 12:58:28 +01:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 12:09:36 +02:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-12-17 07:24:47 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-11-15 09:06:11 +01:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2022-08-25 04:31:57 +02:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-01-15 04:14:50 +01:00
|
|
|
"code.gitea.io/gitea/modules/sync"
|
2019-11-15 09:06:11 +01:00
|
|
|
)
|
|
|
|
|
2020-01-15 04:14:50 +01:00
|
|
|
// repoWorkingPool represents a working pool to order the parallel changes to the same repository
|
2022-05-04 18:06:23 +02:00
|
|
|
// TODO: use clustered lock (unique queue? or *abuse* cache)
|
2020-01-15 04:14:50 +01:00
|
|
|
var repoWorkingPool = sync.NewExclusivePool()
|
|
|
|
|
2019-11-15 09:06:11 +01:00
|
|
|
// TransferOwnership transfers all corresponding setting from old user to new one.
|
2022-12-10 03:46:31 +01:00
|
|
|
func TransferOwnership(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error {
|
2023-02-18 13:11:03 +01:00
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
2019-11-15 09:06:11 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-01-31 16:49:04 +01:00
|
|
|
for _, team := range teams {
|
|
|
|
if newOwner.ID != team.OrgID {
|
|
|
|
return fmt.Errorf("team %d does not belong to organization", team.ID)
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 09:06:11 +01:00
|
|
|
|
|
|
|
oldOwner := repo.Owner
|
|
|
|
|
2020-12-25 10:59:32 +01:00
|
|
|
repoWorkingPool.CheckIn(fmt.Sprint(repo.ID))
|
2020-01-31 16:49:04 +01:00
|
|
|
if err := models.TransferOwnership(doer, newOwner.Name, repo); err != nil {
|
2020-12-25 10:59:32 +01:00
|
|
|
repoWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2019-11-15 09:06:11 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-12-25 10:59:32 +01:00
|
|
|
repoWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2019-11-15 09:06:11 +01:00
|
|
|
|
2022-12-10 03:46:31 +01:00
|
|
|
newRepo, err := repo_model.GetRepositoryByID(ctx, repo.ID)
|
2020-01-31 16:49:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, team := range teams {
|
2022-12-10 03:46:31 +01:00
|
|
|
if err := models.AddRepository(ctx, team, newRepo); err != nil {
|
2020-01-31 16:49:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 03:46:31 +01:00
|
|
|
notification.NotifyTransferRepository(ctx, doer, repo, oldOwner.Name)
|
2019-11-15 09:06:11 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
|
2021-12-10 02:27:50 +01:00
|
|
|
func ChangeRepositoryName(doer *user_model.User, repo *repo_model.Repository, newRepoName string) error {
|
2021-12-17 07:24:47 +01:00
|
|
|
log.Trace("ChangeRepositoryName: %s/%s -> %s", doer.Name, repo.Name, newRepoName)
|
|
|
|
|
2019-11-15 09:06:11 +01:00
|
|
|
oldRepoName := repo.Name
|
|
|
|
|
2019-12-06 05:00:50 +01:00
|
|
|
// 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 09:06:11 +01:00
|
|
|
|
2020-12-25 10:59:32 +01:00
|
|
|
repoWorkingPool.CheckIn(fmt.Sprint(repo.ID))
|
2021-12-12 16:48:20 +01:00
|
|
|
if err := repo_model.ChangeRepositoryName(doer, repo, newRepoName); err != nil {
|
2020-12-25 10:59:32 +01:00
|
|
|
repoWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2019-11-15 09:06:11 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-12-25 10:59:32 +01:00
|
|
|
repoWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2019-11-15 09:06:11 +01:00
|
|
|
|
2021-12-17 07:24:47 +01:00
|
|
|
repo.Name = newRepoName
|
2022-11-19 09:12:33 +01:00
|
|
|
notification.NotifyRenameRepository(db.DefaultContext, doer, repo, oldRepoName)
|
2019-11-15 09:06:11 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-01 01:47:30 +01:00
|
|
|
|
|
|
|
// 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 03:46:31 +01:00
|
|
|
func StartRepositoryTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error {
|
2021-03-01 01:47:30 +01:00
|
|
|
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 03:46:31 +01:00
|
|
|
return TransferOwnership(ctx, doer, newOwner, repo, teams)
|
2021-03-01 01:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If new owner is an org and user can create repos he can transfer directly too
|
|
|
|
if newOwner.IsOrganization() {
|
2022-12-10 03:46:31 +01:00
|
|
|
allowed, err := organization.CanCreateOrgRepo(ctx, newOwner.ID, doer.ID)
|
2021-03-01 01:47:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if allowed {
|
2022-12-10 03:46:31 +01:00
|
|
|
return TransferOwnership(ctx, doer, newOwner, repo, teams)
|
2021-03-01 01:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:30:35 +02:00
|
|
|
// In case the new owner would not have sufficient access to the repo, give access rights for read
|
2022-12-10 03:46:31 +01:00
|
|
|
hasAccess, err := access_model.HasAccess(ctx, newOwner.ID, repo)
|
2021-06-14 20:30:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !hasAccess {
|
2022-12-10 03:46:31 +01:00
|
|
|
if err := repo_module.AddCollaborator(ctx, repo, newOwner); err != nil {
|
2021-06-14 20:30:35 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-12-10 03:46:31 +01:00
|
|
|
if err := repo_model.ChangeCollaborationAccessMode(ctx, repo, newOwner.ID, perm.AccessModeRead); err != nil {
|
2021-06-14 20:30:35 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:47:30 +01:00
|
|
|
// Make repo as pending for transfer
|
2021-12-10 02:27:50 +01:00
|
|
|
repo.Status = repo_model.RepositoryPendingTransfer
|
2022-12-10 03:46:31 +01:00
|
|
|
if err := models.CreatePendingRepositoryTransfer(ctx, doer, newOwner, repo.ID, teams); err != nil {
|
2021-03-01 01:47:30 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify users who are able to accept / reject transfer
|
2022-12-10 03:46:31 +01:00
|
|
|
notification.NotifyRepoPendingTransfer(ctx, doer, newOwner, repo)
|
2021-03-01 01:47:30 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|