aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-01-31 16:49:04 +0100
committerGitHub <noreply@github.com>2020-01-31 15:49:04 +0000
commit13bc82009c2def4e729f11340e74a14d6c6b32e8 (patch)
treeb545d0af8fdebbddb79fee55b8e643c9f392ff49 /services
parentd816f7018b0726f868fa0cddf02ffae184601395 (diff)
downloadgitea-13bc82009c2def4e729f11340e74a14d6c6b32e8.tar.gz
gitea-13bc82009c2def4e729f11340e74a14d6c6b32e8.zip
API endpoint for repo transfer (#9947)
* squash * optimize * fail before make any changes * fix-header
Diffstat (limited to 'services')
-rw-r--r--services/repository/transfer.go22
-rw-r--r--services/repository/transfer_test.go2
2 files changed, 21 insertions, 3 deletions
diff --git a/services/repository/transfer.go b/services/repository/transfer.go
index 978a966c41..d34c812b86 100644
--- a/services/repository/transfer.go
+++ b/services/repository/transfer.go
@@ -5,6 +5,8 @@
package repository
import (
+ "fmt"
+
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/sync"
@@ -16,20 +18,36 @@ import (
var repoWorkingPool = sync.NewExclusivePool()
// TransferOwnership transfers all corresponding setting from old user to new one.
-func TransferOwnership(doer *models.User, newOwnerName string, repo *models.Repository) error {
+func TransferOwnership(doer, newOwner *models.User, repo *models.Repository, teams []*models.Team) error {
if err := repo.GetOwner(); err != nil {
return err
}
+ for _, team := range teams {
+ if newOwner.ID != team.OrgID {
+ return fmt.Errorf("team %d does not belong to organization", team.ID)
+ }
+ }
oldOwner := repo.Owner
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
- if err := models.TransferOwnership(doer, newOwnerName, repo); err != nil {
+ if err := models.TransferOwnership(doer, newOwner.Name, repo); err != nil {
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
return err
}
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
+ newRepo, err := models.GetRepositoryByID(repo.ID)
+ if err != nil {
+ return err
+ }
+
+ for _, team := range teams {
+ if err := team.AddRepository(newRepo); err != nil {
+ return err
+ }
+ }
+
notification.NotifyTransferRepository(doer, repo, oldOwner.Name)
return nil
diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go
index 432527921f..9468e1ced2 100644
--- a/services/repository/transfer_test.go
+++ b/services/repository/transfer_test.go
@@ -32,7 +32,7 @@ func TestTransferOwnership(t *testing.T) {
doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
- assert.NoError(t, TransferOwnership(doer, "user2", repo))
+ assert.NoError(t, TransferOwnership(doer, doer, repo, nil))
transferredRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
assert.EqualValues(t, 2, transferredRepo.OwnerID)