Add Adopt repository event and handler (#25497)

Fix #14304

---------

Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
Lunny Xiao 2023-06-26 14:59:15 +08:00 committed by GitHub
parent 48e5a74f21
commit d44a415bf0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 11 deletions

View file

@ -17,6 +17,7 @@ import (
// Notifier defines an interface to notify receiver // Notifier defines an interface to notify receiver
type Notifier interface { type Notifier interface {
Run() Run()
NotifyAdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository)
NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository)
NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository)
NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository)

View file

@ -145,6 +145,10 @@ func (*NullNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *user_mod
func (*NullNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) { func (*NullNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
} }
// NotifyAdoptRepository places a place holder function
func (*NullNotifier) NotifyAdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
}
// NotifyDeleteRepository places a place holder function // NotifyDeleteRepository places a place holder function
func (*NullNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) { func (*NullNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
} }

View file

@ -29,6 +29,10 @@ func NewNotifier() base.Notifier {
return &indexerNotifier{} return &indexerNotifier{}
} }
func (r *indexerNotifier) NotifyAdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
r.NotifyMigrateRepository(ctx, doer, u, repo)
}
func (r *indexerNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, func (r *indexerNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User, issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
) { ) {

View file

@ -274,6 +274,13 @@ func NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo
} }
} }
// NotifyAdoptRepository notifies the adoption of a repository to notifiers
func NotifyAdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
for _, notifier := range notifiers {
notifier.NotifyAdoptRepository(ctx, doer, u, repo)
}
}
// NotifyMigrateRepository notifies create repository to notifiers // NotifyMigrateRepository notifies create repository to notifiers
func NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) { func NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
for _, notifier := range notifiers { for _, notifier := range notifiers {

View file

@ -70,9 +70,17 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts repo_mo
if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true, false); err != nil { if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true, false); err != nil {
return err return err
} }
if err := adoptRepository(ctx, repoPath, doer, repo, opts); err != nil {
// Re-fetch the repository from database before updating it (else it would
// override changes that were done earlier with sql)
if repo, err = repo_model.GetRepositoryByID(ctx, repo.ID); err != nil {
return fmt.Errorf("getRepositoryByID: %w", err)
}
if err := adoptRepository(ctx, repoPath, doer, repo, opts.DefaultBranch); err != nil {
return fmt.Errorf("createDelegateHooks: %w", err) return fmt.Errorf("createDelegateHooks: %w", err)
} }
if err := repo_module.CheckDaemonExportOK(ctx, repo); err != nil { if err := repo_module.CheckDaemonExportOK(ctx, repo); err != nil {
return fmt.Errorf("checkDaemonExportOK: %w", err) return fmt.Errorf("checkDaemonExportOK: %w", err)
} }
@ -95,12 +103,12 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts repo_mo
return nil, err return nil, err
} }
notification.NotifyCreateRepository(ctx, doer, u, repo) notification.NotifyAdoptRepository(ctx, doer, u, repo)
return repo, nil return repo, nil
} }
func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts repo_module.CreateRepoOptions) (err error) { func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, defaultBranch string) (err error) {
isExist, err := util.IsExist(repoPath) isExist, err := util.IsExist(repoPath)
if err != nil { if err != nil {
log.Error("Unable to check if %s exists. Error: %v", repoPath, err) 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
return fmt.Errorf("createDelegateHooks: %w", err) return fmt.Errorf("createDelegateHooks: %w", err)
} }
// Re-fetch the repository from database before updating it (else it would
// override changes that were done earlier with sql)
if repo, err = repo_model.GetRepositoryByID(ctx, repo.ID); err != nil {
return fmt.Errorf("getRepositoryByID: %w", err)
}
repo.IsEmpty = false repo.IsEmpty = false
// Don't bother looking this repo in the context it won't be there // 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
} }
defer gitRepo.Close() defer gitRepo.Close()
if len(opts.DefaultBranch) > 0 { if len(defaultBranch) > 0 {
repo.DefaultBranch = opts.DefaultBranch repo.DefaultBranch = defaultBranch
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
return fmt.Errorf("setDefaultBranch: %w", err) return fmt.Errorf("setDefaultBranch: %w", err)