more events

This commit is contained in:
Lunny Xiao 2018-07-09 23:35:26 +08:00
parent 2563e6e8ef
commit 5a61d42a64
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
10 changed files with 81 additions and 18 deletions

View File

@ -1380,15 +1380,7 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
return fmt.Errorf("getOwnerTeam: %v", err)
} else if err = t.addRepository(e, repo); err != nil {
return fmt.Errorf("addRepository: %v", err)
} else if err = prepareWebhooks(e, repo, HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: repo.APIFormat(AccessModeOwner),
Organization: u.APIFormat(),
Sender: doer.APIFormat(),
}); err != nil {
return fmt.Errorf("prepareWebhooks: %v", err)
}
go HookQueue.Add(repo.ID)
} else {
// Organization automatically called this in addRepository method.
if err = repo.recalculateAccesses(e); err != nil {
@ -1398,8 +1390,6 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
if err = watchRepo(e, doer.ID, repo.ID, true); err != nil {
return fmt.Errorf("watchRepo: %v", err)
} else if err = newRepoAction(e, u, repo); err != nil {
return fmt.Errorf("newRepoAction: %v", err)
}
return nil

View File

@ -110,3 +110,15 @@ func (r *actionNotifier) NotifyIssueChangeTitle(doer *models.User, issue *models
func (r *actionNotifier) NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
addedLabels []*models.Label, removedLabels []*models.Label) {
}
func (w *actionNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
if err := models.NewRepoAction(doer, repo); err != nil {
log.Error(4, "NewRepoAction [%d]: %v", repo.ID, err)
}
}
func (w *actionNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
if err := models.NewRepoAction(doer, repo); err != nil {
log.Error(4, "NewRepoAction [%d]: %v", repo.ID, err)
}
}

View File

@ -31,4 +31,6 @@ type Notifier interface {
NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string)
NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
addedLabels []*models.Label, removedLabels []*models.Label)
NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository)
NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository)
}

View File

@ -88,3 +88,9 @@ func (r *indexerNotifier) NotifyIssueChangeTitle(doer *models.User, issue *model
func (r *indexerNotifier) NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
addedLabels []*models.Label, removedLabels []*models.Label) {
}
func (r *indexerNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
}
func (r *indexerNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
}

View File

@ -87,3 +87,9 @@ func (m *mailNotifier) NotifyIssueChangeTitle(doer *models.User, issue *models.I
func (m *mailNotifier) NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
addedLabels []*models.Label, removedLabels []*models.Label) {
}
func (m *mailNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
}
func (m *mailNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
}

View File

@ -153,3 +153,17 @@ func NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
notifier.NotifyIssueChangeLabels(doer, issue, addedLabels, removedLabels)
}
}
// NotifyCreateRepository notifies create repository to notifiers
func NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
for _, notifier := range notifiers {
notifier.NotifyCreateRepository(doer, u, repo)
}
}
// NotifyMigrateRepository notifies create repository to notifiers
func NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
for _, notifier := range notifiers {
notifier.NotifyMigrateRepository(doer, u, repo)
}
}

View File

@ -112,3 +112,9 @@ func (ns *notificationService) NotifyIssueChangeTitle(doer *models.User, issue *
func (ns *notificationService) NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
addedLabels []*models.Label, removedLabels []*models.Label) {
}
func (ns *notificationService) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
}
func (ns *notificationService) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
}

View File

@ -496,3 +496,29 @@ func (w *webhookNotifier) NotifyIssueChangeLabels(doer *models.User, issue *mode
go models.HookQueue.Add(issue.RepoID)
}
}
func (w *webhookNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
if err := models.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: repo.APIFormat(models.AccessModeOwner),
Organization: u.APIFormat(),
Sender: doer.APIFormat(),
}); err != nil {
log.Error(4, "PrepareWebhooks: %v", err)
} else {
go models.HookQueue.Add(repo.ID)
}
}
func (w *webhookNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
if err := models.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: repo.APIFormat(models.AccessModeOwner),
Organization: u.APIFormat(),
Sender: doer.APIFormat(),
}); err != nil {
log.Error(4, "PrepareWebhooks: %v", err)
} else {
go models.HookQueue.Add(repo.ID)
}
}

View File

@ -187,8 +187,6 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
if repo != nil {
if err = models.DeleteRepository(ctx.User, ctx.User.ID, repo.ID); err != nil {
log.Error(4, "DeleteRepository: %v", err)
} else {
notification.NotifyDeleteRepository(ctx.User, repo)
}
}
ctx.Error(500, "CreateRepository", err)
@ -196,6 +194,8 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
return
}
notification.NotifyCreateRepository(ctx.User, owner, repo)
ctx.JSON(201, repo.APIFormat(models.AccessModeOwner))
}
@ -362,8 +362,6 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
if repo != nil {
if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
log.Error(4, "DeleteRepository: %v", errDelete)
} else {
notification.NotifyDeleteRepository(ctx.User, repo)
}
}
ctx.Error(500, "MigrateRepository", err)
@ -371,6 +369,8 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
}
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
notification.NotifyMigrateRepository(ctx.User, ctxUser, repo)
ctx.JSON(201, repo.APIFormat(models.AccessModeAdmin))
}

View File

@ -176,6 +176,8 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
AutoInit: form.AutoInit,
})
if err == nil {
notification.NotifyCreateRepository(ctx.User, ctxUser, repo)
log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
return
@ -184,8 +186,6 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
if repo != nil {
if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
log.Error(4, "DeleteRepository: %v", errDelete)
} else {
notification.NotifyDeleteRepository(ctx.User, repo)
}
}
@ -254,6 +254,9 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) {
})
if err == nil {
log.Trace("Repository migrated [%d]: %s/%s", repo.ID, ctxUser.Name, form.RepoName)
notification.NotifyMigrateRepository(ctx.User, ctxUser, repo)
ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + form.RepoName)
return
}
@ -264,8 +267,6 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) {
if repo != nil {
if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
log.Error(4, "DeleteRepository: %v", errDelete)
} else {
notification.NotifyDeleteRepository(ctx.User, repo)
}
}