Skip to content

Commit 21ae983

Browse files
authored
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
1 parent b30d744 commit 21ae983

File tree

16 files changed

+216
-253
lines changed

16 files changed

+216
-253
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ require (
8080
github.com/prometheus/client_golang v1.1.0
8181
github.com/prometheus/procfs v0.0.4 // indirect
8282
github.com/remyoudompheng/bigfft v0.0.0-20190321074620-2f0d2b0e0001 // indirect
83-
github.com/russross/blackfriday v2.0.0+incompatible // indirect
8483
github.com/russross/blackfriday/v2 v2.0.1
8584
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
8685
github.com/satori/go.uuid v1.2.0

go.sum

-45
Large diffs are not rendered by default.

models/action.go

-46
Original file line numberDiff line numberDiff line change
@@ -522,52 +522,6 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
522522
return nil
523523
}
524524

525-
func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
526-
if err = notifyWatchers(e, &Action{
527-
ActUserID: doer.ID,
528-
ActUser: doer,
529-
OpType: ActionTransferRepo,
530-
RepoID: repo.ID,
531-
Repo: repo,
532-
IsPrivate: repo.IsPrivate,
533-
Content: path.Join(oldOwner.Name, repo.Name),
534-
}); err != nil {
535-
return fmt.Errorf("notifyWatchers: %v", err)
536-
}
537-
538-
// Remove watch for organization.
539-
if oldOwner.IsOrganization() {
540-
if err = watchRepo(e, oldOwner.ID, repo.ID, false); err != nil {
541-
return fmt.Errorf("watchRepo [false]: %v", err)
542-
}
543-
}
544-
545-
return nil
546-
}
547-
548-
// TransferRepoAction adds new action for transferring repository,
549-
// the Owner field of repository is assumed to be new owner.
550-
func TransferRepoAction(doer, oldOwner *User, repo *Repository) error {
551-
return transferRepoAction(x, doer, oldOwner, repo)
552-
}
553-
554-
func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue) error {
555-
return notifyWatchers(e, &Action{
556-
ActUserID: doer.ID,
557-
ActUser: doer,
558-
OpType: ActionMergePullRequest,
559-
Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
560-
RepoID: repo.ID,
561-
Repo: repo,
562-
IsPrivate: repo.IsPrivate,
563-
})
564-
}
565-
566-
// MergePullRequestAction adds new action for merging pull request.
567-
func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error {
568-
return mergePullRequestAction(x, actUser, repo, pull)
569-
}
570-
571525
// GetFeedsOptions options for retrieving feeds
572526
type GetFeedsOptions struct {
573527
RequestedUser *User

models/action_test.go

-48
Original file line numberDiff line numberDiff line change
@@ -332,54 +332,6 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
332332
CheckConsistencyFor(t, &Action{})
333333
}
334334

335-
func TestTransferRepoAction(t *testing.T) {
336-
assert.NoError(t, PrepareTestDatabase())
337-
338-
user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
339-
user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
340-
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
341-
342-
repo.OwnerID = user4.ID
343-
repo.Owner = user4
344-
345-
actionBean := &Action{
346-
OpType: ActionTransferRepo,
347-
ActUserID: user2.ID,
348-
ActUser: user2,
349-
RepoID: repo.ID,
350-
Repo: repo,
351-
IsPrivate: repo.IsPrivate,
352-
}
353-
AssertNotExistsBean(t, actionBean)
354-
assert.NoError(t, TransferRepoAction(user2, user2, repo))
355-
AssertExistsAndLoadBean(t, actionBean)
356-
357-
_, err := x.ID(repo.ID).Cols("owner_id").Update(repo)
358-
assert.NoError(t, err)
359-
CheckConsistencyFor(t, &Action{})
360-
}
361-
362-
func TestMergePullRequestAction(t *testing.T) {
363-
assert.NoError(t, PrepareTestDatabase())
364-
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
365-
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
366-
repo.Owner = user
367-
issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
368-
369-
actionBean := &Action{
370-
OpType: ActionMergePullRequest,
371-
ActUserID: user.ID,
372-
ActUser: user,
373-
RepoID: repo.ID,
374-
Repo: repo,
375-
IsPrivate: repo.IsPrivate,
376-
}
377-
AssertNotExistsBean(t, actionBean)
378-
assert.NoError(t, MergePullRequestAction(user, repo, issue))
379-
AssertExistsAndLoadBean(t, actionBean)
380-
CheckConsistencyFor(t, &Action{})
381-
}
382-
383335
func TestGetFeeds(t *testing.T) {
384336
// test with an individual user
385337
assert.NoError(t, PrepareTestDatabase())

models/repo.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -1792,8 +1792,13 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
17921792

17931793
if err = watchRepo(sess, doer.ID, repo.ID, true); err != nil {
17941794
return fmt.Errorf("watchRepo: %v", err)
1795-
} else if err = transferRepoAction(sess, doer, owner, repo); err != nil {
1796-
return fmt.Errorf("transferRepoAction: %v", err)
1795+
}
1796+
1797+
// Remove watch for organization.
1798+
if owner.IsOrganization() {
1799+
if err = watchRepo(sess, owner.ID, repo.ID, false); err != nil {
1800+
return fmt.Errorf("watchRepo [false]: %v", err)
1801+
}
17971802
}
17981803

17991804
// Rename remote repository to new path and delete local copy.
@@ -1824,23 +1829,21 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
18241829
}
18251830

18261831
// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
1827-
func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) {
1828-
oldRepoName = strings.ToLower(oldRepoName)
1832+
func ChangeRepositoryName(doer *User, repo *Repository, newRepoName string) (err error) {
18291833
newRepoName = strings.ToLower(newRepoName)
18301834
if err = IsUsableRepoName(newRepoName); err != nil {
18311835
return err
18321836
}
18331837

1834-
has, err := IsRepositoryExist(u, newRepoName)
1835-
if err != nil {
1836-
return fmt.Errorf("IsRepositoryExist: %v", err)
1837-
} else if has {
1838-
return ErrRepoAlreadyExist{u.Name, newRepoName}
1838+
if err := repo.GetOwner(); err != nil {
1839+
return err
18391840
}
18401841

1841-
repo, err := GetRepositoryByName(u.ID, oldRepoName)
1842+
has, err := IsRepositoryExist(repo.Owner, newRepoName)
18421843
if err != nil {
1843-
return fmt.Errorf("GetRepositoryByName: %v", err)
1844+
return fmt.Errorf("IsRepositoryExist: %v", err)
1845+
} else if has {
1846+
return ErrRepoAlreadyExist{repo.Owner.Name, newRepoName}
18441847
}
18451848

18461849
// Change repository directory name. We must lock the local copy of the
@@ -1849,14 +1852,14 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error)
18491852
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
18501853
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
18511854

1852-
newRepoPath := RepoPath(u.Name, newRepoName)
1855+
newRepoPath := RepoPath(repo.Owner.Name, newRepoName)
18531856
if err = os.Rename(repo.RepoPath(), newRepoPath); err != nil {
18541857
return fmt.Errorf("rename repository directory: %v", err)
18551858
}
18561859

18571860
wikiPath := repo.WikiPath()
18581861
if com.IsExist(wikiPath) {
1859-
if err = os.Rename(wikiPath, WikiPath(u.Name, newRepoName)); err != nil {
1862+
if err = os.Rename(wikiPath, WikiPath(repo.Owner.Name, newRepoName)); err != nil {
18601863
return fmt.Errorf("rename repository wiki: %v", err)
18611864
}
18621865
}
@@ -1868,7 +1871,7 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error)
18681871
}
18691872

18701873
// If there was previously a redirect at this location, remove it.
1871-
if err = deleteRepoRedirect(sess, u.ID, newRepoName); err != nil {
1874+
if err = deleteRepoRedirect(sess, repo.OwnerID, newRepoName); err != nil {
18721875
return fmt.Errorf("delete repo redirect: %v", err)
18731876
}
18741877

models/repo_test.go

-24
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/markup"
1616

1717
"github.com/stretchr/testify/assert"
18-
"github.com/unknwon/com"
1918
)
2019

2120
func TestRepo(t *testing.T) {
@@ -142,29 +141,6 @@ func TestRepoAPIURL(t *testing.T) {
142141
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
143142
}
144143

145-
func TestTransferOwnership(t *testing.T) {
146-
assert.NoError(t, PrepareTestDatabase())
147-
148-
doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
149-
repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
150-
repo.Owner = AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
151-
assert.NoError(t, TransferOwnership(doer, "user2", repo))
152-
153-
transferredRepo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
154-
assert.EqualValues(t, 2, transferredRepo.OwnerID)
155-
156-
assert.False(t, com.IsExist(RepoPath("user3", "repo3")))
157-
assert.True(t, com.IsExist(RepoPath("user2", "repo3")))
158-
AssertExistsAndLoadBean(t, &Action{
159-
OpType: ActionTransferRepo,
160-
ActUserID: 2,
161-
RepoID: 3,
162-
Content: "user3/repo3",
163-
})
164-
165-
CheckConsistencyFor(t, &Repository{}, &User{}, &Team{})
166-
}
167-
168144
func TestUploadAvatar(t *testing.T) {
169145

170146
// Generate image

modules/notification/action/action.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package action
66

77
import (
88
"fmt"
9+
"path"
910
"strings"
1011

1112
"code.gitea.io/gitea/models"
@@ -77,19 +78,33 @@ func (a *actionNotifier) NotifyNewPullRequest(pull *models.PullRequest) {
7778
}
7879
}
7980

80-
func (a *actionNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string) {
81+
func (a *actionNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repository, oldRepoName string) {
82+
log.Trace("action.ChangeRepositoryName: %s/%s", doer.Name, repo.Name)
83+
8184
if err := models.NotifyWatchers(&models.Action{
8285
ActUserID: doer.ID,
8386
ActUser: doer,
8487
OpType: models.ActionRenameRepo,
8588
RepoID: repo.ID,
8689
Repo: repo,
8790
IsPrivate: repo.IsPrivate,
88-
Content: oldName,
91+
Content: oldRepoName,
8992
}); err != nil {
90-
log.Error("notify watchers: %v", err)
91-
} else {
92-
log.Trace("action.renameRepoAction: %s/%s", doer.Name, repo.Name)
93+
log.Error("NotifyWatchers: %v", err)
94+
}
95+
}
96+
97+
func (a *actionNotifier) NotifyTransferRepository(doer *models.User, repo *models.Repository, oldOwnerName string) {
98+
if err := models.NotifyWatchers(&models.Action{
99+
ActUserID: doer.ID,
100+
ActUser: doer,
101+
OpType: models.ActionTransferRepo,
102+
RepoID: repo.ID,
103+
Repo: repo,
104+
IsPrivate: repo.IsPrivate,
105+
Content: path.Join(oldOwnerName, repo.Name),
106+
}); err != nil {
107+
log.Error("NotifyWatchers: %v", err)
93108
}
94109
}
95110

modules/notification/base/notifier.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ type Notifier interface {
1717
NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository)
1818
NotifyDeleteRepository(doer *models.User, repo *models.Repository)
1919
NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository)
20-
NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string)
20+
NotifyRenameRepository(doer *models.User, repo *models.Repository, oldRepoName string)
21+
NotifyTransferRepository(doer *models.User, repo *models.Repository, oldOwnerName string)
2122

2223
NotifyNewIssue(*models.Issue)
2324
NotifyIssueChangeStatus(*models.User, *models.Issue, bool)

modules/notification/base/null.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,6 @@ func (*NullNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, o
5858
func (*NullNotifier) NotifyDeleteComment(doer *models.User, c *models.Comment) {
5959
}
6060

61-
// NotifyDeleteRepository places a place holder function
62-
func (*NullNotifier) NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
63-
}
64-
65-
// NotifyForkRepository places a place holder function
66-
func (*NullNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
67-
}
68-
69-
// NotifyRenameRepository places a place holder function
70-
func (*NullNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string) {
71-
}
72-
7361
// NotifyNewRelease places a place holder function
7462
func (*NullNotifier) NotifyNewRelease(rel *models.Release) {
7563
}
@@ -111,6 +99,14 @@ func (*NullNotifier) NotifyIssueChangeLabels(doer *models.User, issue *models.Is
11199
func (*NullNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
112100
}
113101

102+
// NotifyDeleteRepository places a place holder function
103+
func (*NullNotifier) NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
104+
}
105+
106+
// NotifyForkRepository places a place holder function
107+
func (*NullNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
108+
}
109+
114110
// NotifyMigrateRepository places a place holder function
115111
func (*NullNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
116112
}
@@ -126,3 +122,11 @@ func (*NullNotifier) NotifyCreateRef(doer *models.User, repo *models.Repository,
126122
// NotifyDeleteRef notifies branch or tag deleteion to notifiers
127123
func (*NullNotifier) NotifyDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
128124
}
125+
126+
// NotifyRenameRepository places a place holder function
127+
func (*NullNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repository, oldRepoName string) {
128+
}
129+
130+
// NotifyTransferRepository places a place holder function
131+
func (*NullNotifier) NotifyTransferRepository(doer *models.User, repo *models.Repository, oldOwnerName string) {
132+
}

modules/notification/notification.go

+28-21
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,6 @@ func NotifyDeleteComment(doer *models.User, c *models.Comment) {
101101
}
102102
}
103103

104-
// NotifyDeleteRepository notifies delete repository to notifiers
105-
func NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
106-
for _, notifier := range notifiers {
107-
notifier.NotifyDeleteRepository(doer, repo)
108-
}
109-
}
110-
111-
// NotifyForkRepository notifies fork repository to notifiers
112-
func NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
113-
for _, notifier := range notifiers {
114-
notifier.NotifyForkRepository(doer, oldRepo, repo)
115-
}
116-
}
117-
118-
// NotifyRenameRepository notifies repository renamed
119-
func NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string) {
120-
for _, notifier := range notifiers {
121-
notifier.NotifyRenameRepository(doer, repo, oldName)
122-
}
123-
}
124-
125104
// NotifyNewRelease notifies new release to notifiers
126105
func NotifyNewRelease(rel *models.Release) {
127106
for _, notifier := range notifiers {
@@ -200,6 +179,34 @@ func NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Rep
200179
}
201180
}
202181

182+
// NotifyTransferRepository notifies create repository to notifiers
183+
func NotifyTransferRepository(doer *models.User, repo *models.Repository, newOwnerName string) {
184+
for _, notifier := range notifiers {
185+
notifier.NotifyTransferRepository(doer, repo, newOwnerName)
186+
}
187+
}
188+
189+
// NotifyDeleteRepository notifies delete repository to notifiers
190+
func NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
191+
for _, notifier := range notifiers {
192+
notifier.NotifyDeleteRepository(doer, repo)
193+
}
194+
}
195+
196+
// NotifyForkRepository notifies fork repository to notifiers
197+
func NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
198+
for _, notifier := range notifiers {
199+
notifier.NotifyForkRepository(doer, oldRepo, repo)
200+
}
201+
}
202+
203+
// NotifyRenameRepository notifies repository renamed
204+
func NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string) {
205+
for _, notifier := range notifiers {
206+
notifier.NotifyRenameRepository(doer, repo, oldName)
207+
}
208+
}
209+
203210
// NotifyPushCommits notifies commits pushed to notifiers
204211
func NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
205212
for _, notifier := range notifiers {

0 commit comments

Comments
 (0)