diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-11-15 16:06:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-15 16:06:11 +0800 |
commit | 21ae9838e04233bbf9930d368d935fd1f9b68c34 (patch) | |
tree | 610abb6f1ec2df235d469276b36f6d26e00e4653 /services | |
parent | b30d744e0986dcf716d4f57dac8c72a494bb226b (diff) | |
download | gitea-21ae9838e04233bbf9930d368d935fd1f9b68c34.tar.gz gitea-21ae9838e04233bbf9930d368d935fd1f9b68c34.zip |
Move transfer repository and rename repository on a service package and start action notification (#8573)
* move transfer repository and rename repository on a service package and start action notification
* remove unused codes
* fix lint
* fix bugs
* fix test
* fix test
* fix test
* fix lint
* update go mod and sum
Diffstat (limited to 'services')
-rw-r--r-- | services/pull/merge.go | 12 | ||||
-rw-r--r-- | services/repository/main_test.go | 16 | ||||
-rw-r--r-- | services/repository/transfer.go | 54 | ||||
-rw-r--r-- | services/repository/transfer_test.go | 50 |
4 files changed, 130 insertions, 2 deletions
diff --git a/services/pull/merge.go b/services/pull/merge.go index 5fbf550ad2..7dc3c07338 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -424,8 +424,16 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor log.Error("setMerged [%d]: %v", pr.ID, err) } - if err = models.MergePullRequestAction(doer, pr.Issue.Repo, pr.Issue); err != nil { - log.Error("MergePullRequestAction [%d]: %v", pr.ID, err) + if err := models.NotifyWatchers(&models.Action{ + ActUserID: doer.ID, + ActUser: doer, + OpType: models.ActionMergePullRequest, + Content: fmt.Sprintf("%d|%s", pr.Issue.Index, pr.Issue.Title), + RepoID: pr.Issue.Repo.ID, + Repo: pr.Issue.Repo, + IsPrivate: pr.Issue.Repo.IsPrivate, + }); err != nil { + log.Error("NotifyWatchers [%d]: %v", pr.ID, err) } // Reset cached commit count diff --git a/services/repository/main_test.go b/services/repository/main_test.go new file mode 100644 index 0000000000..f13f358635 --- /dev/null +++ b/services/repository/main_test.go @@ -0,0 +1,16 @@ +// 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 ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models" +) + +func TestMain(m *testing.M) { + models.MainTest(m, filepath.Join("..", "..")) +} diff --git a/services/repository/transfer.go b/services/repository/transfer.go new file mode 100644 index 0000000000..2d20dcd1a2 --- /dev/null +++ b/services/repository/transfer.go @@ -0,0 +1,54 @@ +// 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 ( + "fmt" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/notification" +) + +// TransferOwnership transfers all corresponding setting from old user to new one. +func TransferOwnership(doer *models.User, newOwnerName string, repo *models.Repository) error { + if err := repo.GetOwner(); err != nil { + return err + } + + oldOwner := repo.Owner + + if err := models.TransferOwnership(doer, newOwnerName, repo); err != nil { + return err + } + + if err := models.NewRepoRedirect(oldOwner.ID, repo.ID, repo.Name, repo.Name); err != nil { + return fmt.Errorf("NewRepoRedirect: %v", err) + } + + 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 + + if err := models.ChangeRepositoryName(doer, repo, newRepoName); err != nil { + return err + } + + if err := repo.GetOwner(); err != nil { + return err + } + + if err := models.NewRepoRedirect(repo.Owner.ID, repo.ID, oldRepoName, newRepoName); err != nil { + return err + } + + notification.NotifyRenameRepository(doer, repo, oldRepoName) + + return nil +} diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go new file mode 100644 index 0000000000..432527921f --- /dev/null +++ b/services/repository/transfer_test.go @@ -0,0 +1,50 @@ +// 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 ( + "sync" + "testing" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/notification/action" + + "github.com/stretchr/testify/assert" + "github.com/unknwon/com" +) + +var notifySync sync.Once + +func registerNotifier() { + notifySync.Do(func() { + notification.RegisterNotifier(action.NewNotifier()) + }) +} + +func TestTransferOwnership(t *testing.T) { + registerNotifier() + + assert.NoError(t, models.PrepareTestDatabase()) + + 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)) + + transferredRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + assert.EqualValues(t, 2, transferredRepo.OwnerID) + + assert.False(t, com.IsExist(models.RepoPath("user3", "repo3"))) + assert.True(t, com.IsExist(models.RepoPath("user2", "repo3"))) + models.AssertExistsAndLoadBean(t, &models.Action{ + OpType: models.ActionTransferRepo, + ActUserID: 2, + RepoID: 3, + Content: "user3/repo3", + }) + + models.CheckConsistencyFor(t, &models.Repository{}, &models.User{}, &models.Team{}) +} |