Skip to content

Commit 9d69a47

Browse files
GiteaBotlunnydelvhKN4CK3R
authored
Add Adopt repository event and handler (go-gitea#25497) (go-gitea#25518)
Backport go-gitea#25497 by @lunny Fix go-gitea#14304 Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: KN4CK3R <[email protected]>
1 parent 53747a5 commit 9d69a47

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

modules/notification/base/notifier.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
// Notifier defines an interface to notify receiver
1818
type Notifier interface {
1919
Run()
20+
NotifyAdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository)
2021
NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository)
2122
NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository)
2223
NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository)

modules/notification/base/null.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ func (*NullNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *user_mod
145145
func (*NullNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
146146
}
147147

148+
// NotifyAdoptRepository places a place holder function
149+
func (*NullNotifier) NotifyAdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
150+
}
151+
148152
// NotifyDeleteRepository places a place holder function
149153
func (*NullNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
150154
}

modules/notification/indexer/indexer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func NewNotifier() base.Notifier {
2929
return &indexerNotifier{}
3030
}
3131

32+
func (r *indexerNotifier) NotifyAdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
33+
r.NotifyMigrateRepository(ctx, doer, u, repo)
34+
}
35+
3236
func (r *indexerNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
3337
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
3438
) {

modules/notification/notification.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ func NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo
274274
}
275275
}
276276

277+
// NotifyAdoptRepository notifies the adoption of a repository to notifiers
278+
func NotifyAdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
279+
for _, notifier := range notifiers {
280+
notifier.NotifyAdoptRepository(ctx, doer, u, repo)
281+
}
282+
}
283+
277284
// NotifyMigrateRepository notifies create repository to notifiers
278285
func NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
279286
for _, notifier := range notifiers {

services/repository/adopt.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,17 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts repo_mo
7070
if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true, false); err != nil {
7171
return err
7272
}
73-
if err := adoptRepository(ctx, repoPath, doer, repo, opts); err != nil {
73+
74+
// Re-fetch the repository from database before updating it (else it would
75+
// override changes that were done earlier with sql)
76+
if repo, err = repo_model.GetRepositoryByID(ctx, repo.ID); err != nil {
77+
return fmt.Errorf("getRepositoryByID: %w", err)
78+
}
79+
80+
if err := adoptRepository(ctx, repoPath, doer, repo, opts.DefaultBranch); err != nil {
7481
return fmt.Errorf("createDelegateHooks: %w", err)
7582
}
83+
7684
if err := repo_module.CheckDaemonExportOK(ctx, repo); err != nil {
7785
return fmt.Errorf("checkDaemonExportOK: %w", err)
7886
}
@@ -95,12 +103,12 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts repo_mo
95103
return nil, err
96104
}
97105

98-
notification.NotifyCreateRepository(ctx, doer, u, repo)
106+
notification.NotifyAdoptRepository(ctx, doer, u, repo)
99107

100108
return repo, nil
101109
}
102110

103-
func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts repo_module.CreateRepoOptions) (err error) {
111+
func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, defaultBranch string) (err error) {
104112
isExist, err := util.IsExist(repoPath)
105113
if err != nil {
106114
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
@@ -114,12 +122,6 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
114122
return fmt.Errorf("createDelegateHooks: %w", err)
115123
}
116124

117-
// Re-fetch the repository from database before updating it (else it would
118-
// override changes that were done earlier with sql)
119-
if repo, err = repo_model.GetRepositoryByID(ctx, repo.ID); err != nil {
120-
return fmt.Errorf("getRepositoryByID: %w", err)
121-
}
122-
123125
repo.IsEmpty = false
124126

125127
// Don't bother looking this repo in the context it won't be there
@@ -129,8 +131,8 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
129131
}
130132
defer gitRepo.Close()
131133

132-
if len(opts.DefaultBranch) > 0 {
133-
repo.DefaultBranch = opts.DefaultBranch
134+
if len(defaultBranch) > 0 {
135+
repo.DefaultBranch = defaultBranch
134136

135137
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
136138
return fmt.Errorf("setDefaultBranch: %w", err)

0 commit comments

Comments
 (0)