Move release webhook to notification (#8817)

* Move release webhook to notification

* Extract release webhook method

* fix bug

* fix import
release/v1.15
Lunny Xiao 2019-11-06 16:25:50 +08:00 committed by GitHub
parent 6d42add37f
commit 0109229928
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 41 deletions

View File

@ -11,7 +11,6 @@ import (
"code.gitea.io/gitea/modules/notification/base" "code.gitea.io/gitea/modules/notification/base"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/webhook"
webhook_module "code.gitea.io/gitea/modules/webhook" webhook_module "code.gitea.io/gitea/modules/webhook"
) )
@ -289,7 +288,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest) {
} }
mode, _ := models.AccessLevel(pull.Issue.Poster, pull.Issue.Repo) mode, _ := models.AccessLevel(pull.Issue.Poster, pull.Issue.Repo)
if err := webhook.PrepareWebhooks(pull.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{ if err := webhook_module.PrepareWebhooks(pull.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueOpened, Action: api.HookIssueOpened,
Index: pull.Issue.Index, Index: pull.Issue.Index,
PullRequest: pull.APIFormat(), PullRequest: pull.APIFormat(),
@ -548,7 +547,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review
log.Error("models.AccessLevel: %v", err) log.Error("models.AccessLevel: %v", err)
return return
} }
if err := webhook.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{ if err := webhook_module.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
Action: api.HookIssueSynchronized, Action: api.HookIssueSynchronized,
Index: review.Issue.Index, Index: review.Issue.Index,
PullRequest: pr.APIFormat(), PullRequest: pr.APIFormat(),
@ -627,3 +626,32 @@ func (m *webhookNotifier) NotifyDeleteRef(pusher *models.User, repo *models.Repo
log.Error("PrepareWebhooks.(delete branch): %v", err) log.Error("PrepareWebhooks.(delete branch): %v", err)
} }
} }
func sendReleaseHook(doer *models.User, rel *models.Release, action api.HookReleaseAction) {
if err := rel.LoadAttributes(); err != nil {
log.Error("LoadAttributes: %v", err)
return
}
mode, _ := models.AccessLevel(rel.Publisher, rel.Repo)
if err := webhook_module.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{
Action: action,
Release: rel.APIFormat(),
Repository: rel.Repo.APIFormat(mode),
Sender: rel.Publisher.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks: %v", err)
}
}
func (m *webhookNotifier) NotifyNewRelease(rel *models.Release) {
sendReleaseHook(rel.Publisher, rel, api.HookReleasePublished)
}
func (m *webhookNotifier) NotifyUpdateRelease(doer *models.User, rel *models.Release) {
sendReleaseHook(doer, rel, api.HookReleaseUpdated)
}
func (m *webhookNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Release) {
sendReleaseHook(doer, rel, api.HookReleaseDeleted)
}

View File

@ -12,10 +12,9 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/webhook"
) )
func createTag(gitRepo *git.Repository, rel *models.Release) error { func createTag(gitRepo *git.Repository, rel *models.Release) error {
@ -81,19 +80,7 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs
} }
if !rel.IsDraft { if !rel.IsDraft {
if err := rel.LoadAttributes(); err != nil { notification.NotifyNewRelease(rel)
log.Error("LoadAttributes: %v", err)
} else {
mode, _ := models.AccessLevel(rel.Publisher, rel.Repo)
if err := webhook.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{
Action: api.HookReleasePublished,
Release: rel.APIFormat(),
Repository: rel.Repo.APIFormat(mode),
Sender: rel.Publisher.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks: %v", err)
}
}
} }
return nil return nil
@ -114,20 +101,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea
log.Error("AddReleaseAttachments: %v", err) log.Error("AddReleaseAttachments: %v", err)
} }
if err = rel.LoadAttributes(); err != nil { notification.NotifyUpdateRelease(doer, rel)
return err
}
// even if attachments added failed, hooks will be still triggered
mode, _ := models.AccessLevel(doer, rel.Repo)
if err1 := webhook.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{
Action: api.HookReleaseUpdated,
Release: rel.APIFormat(),
Repository: rel.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err1 != nil {
log.Error("PrepareWebhooks: %v", err)
}
return err return err
} }
@ -183,15 +157,7 @@ func DeleteReleaseByID(id int64, doer *models.User, delTag bool) error {
} }
} }
mode, _ := models.AccessLevel(doer, rel.Repo) notification.NotifyDeleteRelease(doer, rel)
if err := webhook.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{
Action: api.HookReleaseDeleted,
Release: rel.APIFormat(),
Repository: rel.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks: %v", err)
}
return nil return nil
} }