2019-11-15 17:06:11 +09:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2020-02-01 00:49:04 +09:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-15 17:06:11 +09:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2020-01-15 12:14:50 +09:00
|
|
|
"code.gitea.io/gitea/modules/sync"
|
2019-12-06 13:00:50 +09:00
|
|
|
|
|
|
|
"github.com/unknwon/com"
|
2019-11-15 17:06:11 +09:00
|
|
|
)
|
|
|
|
|
2020-01-15 12:14:50 +09:00
|
|
|
// repoWorkingPool represents a working pool to order the parallel changes to the same repository
|
|
|
|
var repoWorkingPool = sync.NewExclusivePool()
|
|
|
|
|
2019-11-15 17:06:11 +09:00
|
|
|
// TransferOwnership transfers all corresponding setting from old user to new one.
|
2020-02-01 00:49:04 +09:00
|
|
|
func TransferOwnership(doer, newOwner *models.User, repo *models.Repository, teams []*models.Team) error {
|
2019-11-15 17:06:11 +09:00
|
|
|
if err := repo.GetOwner(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-01 00:49:04 +09: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 17:06:11 +09:00
|
|
|
|
|
|
|
oldOwner := repo.Owner
|
|
|
|
|
2020-01-15 12:14:50 +09:00
|
|
|
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
|
2020-02-01 00:49:04 +09:00
|
|
|
if err := models.TransferOwnership(doer, newOwner.Name, repo); err != nil {
|
2020-01-15 12:14:50 +09:00
|
|
|
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
2019-11-15 17:06:11 +09:00
|
|
|
return err
|
|
|
|
}
|
2020-01-15 12:14:50 +09:00
|
|
|
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
2019-11-15 17:06:11 +09:00
|
|
|
|
2020-02-01 00:49:04 +09:00
|
|
|
newRepo, err := models.GetRepositoryByID(repo.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, team := range teams {
|
|
|
|
if err := team.AddRepository(newRepo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:06:11 +09:00
|
|
|
notification.NotifyTransferRepository(doer, repo, oldOwner.Name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
|
|
|
|
func ChangeRepositoryName(doer *models.User, repo *models.Repository, newRepoName string) error {
|
|
|
|
oldRepoName := repo.Name
|
|
|
|
|
2019-12-06 13:00:50 +09: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 17:06:11 +09:00
|
|
|
|
2020-01-15 12:14:50 +09:00
|
|
|
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
|
2019-12-06 13:00:50 +09:00
|
|
|
if err := models.ChangeRepositoryName(doer, repo, newRepoName); err != nil {
|
2020-01-15 12:14:50 +09:00
|
|
|
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
2019-11-15 17:06:11 +09:00
|
|
|
return err
|
|
|
|
}
|
2020-01-15 12:14:50 +09:00
|
|
|
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
2019-11-15 17:06:11 +09:00
|
|
|
|
|
|
|
notification.NotifyRenameRepository(doer, repo, oldRepoName)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|