2019-10-15 13:03:05 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-10 13:13:16 +08:00
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2020-01-10 15:53:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-11-06 14:43:03 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-10-15 13:03:05 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/notification/base"
|
2020-01-10 17:34:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/repository"
|
2019-11-03 14:59:26 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-10-15 13:03:05 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-12-08 18:41:14 +08:00
|
|
|
webhook_services "code.gitea.io/gitea/services/webhook"
|
2019-10-15 13:03:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type webhookNotifier struct {
|
|
|
|
base.NullNotifier
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ base.Notifier = &webhookNotifier{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewNotifier create a new webhookNotifier notifier
|
|
|
|
func NewNotifier() base.Notifier {
|
|
|
|
return &webhookNotifier{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *models.Issue) {
|
|
|
|
if err := issue.LoadPoster(); err != nil {
|
|
|
|
log.Error("loadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := issue.LoadRepo(); err != nil {
|
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
|
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
|
|
|
if err = issue.LoadPullRequest(); err != nil {
|
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{
|
2019-10-15 13:03:05 +08:00
|
|
|
Action: api.HookIssueLabelCleared,
|
|
|
|
Index: issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-15 13:03:05 +08:00
|
|
|
})
|
|
|
|
} else {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssueLabel, &api.IssuePayload{
|
2019-10-15 13:03:05 +08:00
|
|
|
Action: api.HookIssueLabelCleared,
|
|
|
|
Index: issue.Index,
|
2020-02-29 10:49:50 +08:00
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-15 13:03:05 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 14:54:11 +08:00
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
|
|
|
|
oldMode, _ := models.AccessLevel(doer, oldRepo)
|
|
|
|
mode, _ := models.AccessLevel(doer, repo)
|
|
|
|
|
|
|
|
// forked webhook
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(oldRepo, webhook.HookEventFork, &api.ForkPayload{
|
2020-12-03 05:38:30 +08:00
|
|
|
Forkee: convert.ToRepo(oldRepo, oldMode),
|
|
|
|
Repo: convert.ToRepo(repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-26 14:54:11 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
u := repo.MustOwner()
|
|
|
|
|
|
|
|
// Add to hook queue for created repo after session commit.
|
|
|
|
if u.IsOrganization() {
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventRepository, &api.RepositoryPayload{
|
2019-10-26 14:54:11 +08:00
|
|
|
Action: api.HookRepoCreated,
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(repo, models.AccessModeOwner),
|
2021-03-28 00:45:26 +08:00
|
|
|
Organization: convert.ToUser(u, nil),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-26 14:54:11 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
|
|
|
|
// Add to hook queue for created repo after session commit.
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventRepository, &api.RepositoryPayload{
|
2020-10-02 17:37:46 +08:00
|
|
|
Action: api.HookRepoCreated,
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(repo, models.AccessModeOwner),
|
2021-03-28 00:45:26 +08:00
|
|
|
Organization: convert.ToUser(u, nil),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-10-02 17:37:46 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
2019-10-26 14:54:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
|
|
|
|
u := repo.MustOwner()
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventRepository, &api.RepositoryPayload{
|
2020-10-02 17:37:46 +08:00
|
|
|
Action: api.HookRepoDeleted,
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(repo, models.AccessModeOwner),
|
2021-03-28 00:45:26 +08:00
|
|
|
Organization: convert.ToUser(u, nil),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-10-02 17:37:46 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
2019-10-26 14:54:11 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-28 10:11:50 +08:00
|
|
|
|
2020-12-17 20:26:22 +08:00
|
|
|
func (m *webhookNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
|
|
|
|
// Add to hook queue for created repo after session commit.
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventRepository, &api.RepositoryPayload{
|
2020-12-17 20:26:22 +08:00
|
|
|
Action: api.HookRepoCreated,
|
|
|
|
Repository: convert.ToRepo(repo, models.AccessModeOwner),
|
2021-03-28 00:45:26 +08:00
|
|
|
Organization: convert.ToUser(u, nil),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-12-17 20:26:22 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 10:11:50 +08:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, assignee *models.User, removed bool, comment *models.Comment) {
|
|
|
|
if issue.IsPull {
|
2021-11-10 03:57:58 +08:00
|
|
|
mode, _ := models.AccessLevelUnit(doer, issue.Repo, unit.TypePullRequests)
|
2019-10-28 10:11:50 +08:00
|
|
|
|
|
|
|
if err := issue.LoadPullRequest(); err != nil {
|
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
issue.PullRequest.Issue = issue
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 10:11:50 +08:00
|
|
|
}
|
|
|
|
if removed {
|
|
|
|
apiPullRequest.Action = api.HookIssueUnassigned
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueAssigned
|
|
|
|
}
|
|
|
|
// Assignee comment triggers a webhook
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequestAssign, apiPullRequest); err != nil {
|
2019-10-28 10:11:50 +08:00
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-10 03:57:58 +08:00
|
|
|
mode, _ := models.AccessLevelUnit(doer, issue.Repo, unit.TypeIssues)
|
2019-10-28 10:11:50 +08:00
|
|
|
apiIssue := &api.IssuePayload{
|
|
|
|
Index: issue.Index,
|
2020-02-29 10:49:50 +08:00
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 10:11:50 +08:00
|
|
|
}
|
|
|
|
if removed {
|
|
|
|
apiIssue.Action = api.HookIssueUnassigned
|
|
|
|
} else {
|
|
|
|
apiIssue.Action = api.HookIssueAssigned
|
|
|
|
}
|
|
|
|
// Assignee comment triggers a webhook
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssueAssign, apiIssue); err != nil {
|
2019-10-28 10:11:50 +08:00
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) {
|
|
|
|
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
|
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
|
|
|
if err = issue.LoadPullRequest(); err != nil {
|
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
issue.PullRequest.Issue = issue
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequest, &api.PullRequestPayload{
|
2019-10-28 10:11:50 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Title: &api.ChangesFromPayload{
|
|
|
|
From: oldTitle,
|
|
|
|
},
|
|
|
|
},
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 10:11:50 +08:00
|
|
|
})
|
|
|
|
} else {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssues, &api.IssuePayload{
|
2019-10-28 10:11:50 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Title: &api.ChangesFromPayload{
|
|
|
|
From: oldTitle,
|
|
|
|
},
|
|
|
|
},
|
2020-02-29 10:49:50 +08:00
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(issue.Poster, nil),
|
2019-10-28 10:11:50 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 13:26:46 +08:00
|
|
|
|
2019-12-16 05:57:34 +08:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
|
2019-10-28 13:26:46 +08:00
|
|
|
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
|
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
|
|
|
if err = issue.LoadPullRequest(); err != nil {
|
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 13:26:46 +08:00
|
|
|
}
|
|
|
|
if isClosed {
|
|
|
|
apiPullRequest.Action = api.HookIssueClosed
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueReOpened
|
|
|
|
}
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequest, apiPullRequest)
|
2019-10-28 13:26:46 +08:00
|
|
|
} else {
|
|
|
|
apiIssue := &api.IssuePayload{
|
|
|
|
Index: issue.Index,
|
2020-02-29 10:49:50 +08:00
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 13:26:46 +08:00
|
|
|
}
|
|
|
|
if isClosed {
|
|
|
|
apiIssue.Action = api.HookIssueClosed
|
|
|
|
} else {
|
|
|
|
apiIssue.Action = api.HookIssueReOpened
|
|
|
|
}
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssues, apiIssue)
|
2019-10-28 13:26:46 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 00:45:43 +08:00
|
|
|
|
2021-01-03 01:04:02 +08:00
|
|
|
func (m *webhookNotifier) NotifyNewIssue(issue *models.Issue, mentions []*models.User) {
|
2019-11-04 04:59:09 +08:00
|
|
|
if err := issue.LoadRepo(); err != nil {
|
|
|
|
log.Error("issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := issue.LoadPoster(); err != nil {
|
|
|
|
log.Error("issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-29 00:45:43 +08:00
|
|
|
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssues, &api.IssuePayload{
|
2019-10-29 00:45:43 +08:00
|
|
|
Action: api.HookIssueOpened,
|
|
|
|
Index: issue.Index,
|
2020-02-29 10:49:50 +08:00
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(issue.Poster, nil),
|
2019-10-29 00:45:43 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 16:36:25 +08:00
|
|
|
|
2021-01-03 01:04:02 +08:00
|
|
|
func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest, mentions []*models.User) {
|
2019-11-04 04:59:09 +08:00
|
|
|
if err := pull.LoadIssue(); err != nil {
|
|
|
|
log.Error("pull.LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := pull.Issue.LoadRepo(); err != nil {
|
|
|
|
log.Error("pull.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := pull.Issue.LoadPoster(); err != nil {
|
|
|
|
log.Error("pull.Issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, _ := models.AccessLevel(pull.Issue.Poster, pull.Issue.Repo)
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(pull.Issue.Repo, webhook.HookEventPullRequest, &api.PullRequestPayload{
|
2019-11-04 04:59:09 +08:00
|
|
|
Action: api.HookIssueOpened,
|
|
|
|
Index: pull.Issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(pull, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(pull.Issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(pull.Issue.Poster, nil),
|
2019-11-04 04:59:09 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:36:25 +08:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) {
|
|
|
|
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
|
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
|
|
|
issue.PullRequest.Issue = issue
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequest, &api.PullRequestPayload{
|
2019-10-30 16:36:25 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-30 16:36:25 +08:00
|
|
|
})
|
|
|
|
} else {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssues, &api.IssuePayload{
|
2019-10-30 16:36:25 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2020-02-29 10:49:50 +08:00
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-30 16:36:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 18:02:46 +08:00
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
|
2020-03-06 13:10:48 +08:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = c.LoadPoster(); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2020-03-06 13:10:48 +08:00
|
|
|
if err = c.LoadIssue(); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-06 13:10:48 +08:00
|
|
|
if err = c.Issue.LoadAttributes(); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, _ := models.AccessLevel(doer, c.Issue.Repo)
|
2020-03-06 13:10:48 +08:00
|
|
|
if c.Issue.IsPull {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(c.Issue.Repo, webhook.HookEventPullRequestComment, &api.IssueCommentPayload{
|
2020-03-06 13:10:48 +08:00
|
|
|
Action: api.HookIssueCommentEdited,
|
|
|
|
Issue: convert.ToAPIIssue(c.Issue),
|
2020-10-17 12:23:08 +08:00
|
|
|
Comment: convert.ToComment(c),
|
2020-03-06 13:10:48 +08:00
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
2019-10-30 18:02:46 +08:00
|
|
|
},
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(c.Issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-03-06 13:10:48 +08:00
|
|
|
IsPull: true,
|
|
|
|
})
|
|
|
|
} else {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(c.Issue.Repo, webhook.HookEventIssueComment, &api.IssueCommentPayload{
|
2020-03-06 13:10:48 +08:00
|
|
|
Action: api.HookIssueCommentEdited,
|
|
|
|
Issue: convert.ToAPIIssue(c.Issue),
|
2020-10-17 12:23:08 +08:00
|
|
|
Comment: convert.ToComment(c),
|
2020-03-06 13:10:48 +08:00
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(c.Issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-03-06 13:10:48 +08:00
|
|
|
IsPull: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
|
2021-01-03 01:04:02 +08:00
|
|
|
issue *models.Issue, comment *models.Comment, mentions []*models.User) {
|
2019-10-30 18:02:46 +08:00
|
|
|
mode, _ := models.AccessLevel(doer, repo)
|
2020-03-06 13:10:48 +08:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequestComment, &api.IssueCommentPayload{
|
2020-03-06 13:10:48 +08:00
|
|
|
Action: api.HookIssueCommentCreated,
|
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-10-17 12:23:08 +08:00
|
|
|
Comment: convert.ToComment(comment),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-03-06 13:10:48 +08:00
|
|
|
IsPull: true,
|
|
|
|
})
|
|
|
|
} else {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssueComment, &api.IssueCommentPayload{
|
2020-03-06 13:10:48 +08:00
|
|
|
Action: api.HookIssueCommentCreated,
|
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-10-17 12:23:08 +08:00
|
|
|
Comment: convert.ToComment(comment),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-03-06 13:10:48 +08:00
|
|
|
IsPull: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) {
|
2020-03-06 13:10:48 +08:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = comment.LoadPoster(); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2020-03-06 13:10:48 +08:00
|
|
|
if err = comment.LoadIssue(); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-06 13:10:48 +08:00
|
|
|
if err = comment.Issue.LoadAttributes(); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, _ := models.AccessLevel(doer, comment.Issue.Repo)
|
|
|
|
|
2020-03-06 13:10:48 +08:00
|
|
|
if comment.Issue.IsPull {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(comment.Issue.Repo, webhook.HookEventPullRequestComment, &api.IssueCommentPayload{
|
2020-03-06 13:10:48 +08:00
|
|
|
Action: api.HookIssueCommentDeleted,
|
|
|
|
Issue: convert.ToAPIIssue(comment.Issue),
|
2020-10-17 12:23:08 +08:00
|
|
|
Comment: convert.ToComment(comment),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(comment.Issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-03-06 13:10:48 +08:00
|
|
|
IsPull: true,
|
|
|
|
})
|
|
|
|
} else {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(comment.Issue.Repo, webhook.HookEventIssueComment, &api.IssueCommentPayload{
|
2020-03-06 13:10:48 +08:00
|
|
|
Action: api.HookIssueCommentDeleted,
|
|
|
|
Issue: convert.ToAPIIssue(comment.Issue),
|
2020-10-17 12:23:08 +08:00
|
|
|
Comment: convert.ToComment(comment),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(comment.Issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-03-06 13:10:48 +08:00
|
|
|
IsPull: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
|
|
|
}
|
2020-03-06 13:10:48 +08:00
|
|
|
|
2019-10-30 18:02:46 +08:00
|
|
|
}
|
2019-11-02 09:49:57 +08:00
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
|
|
|
|
addedLabels []*models.Label, removedLabels []*models.Label) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = issue.LoadRepo(); err != nil {
|
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = issue.LoadPoster(); err != nil {
|
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
|
|
|
|
if issue.IsPull {
|
|
|
|
if err = issue.LoadPullRequest(); err != nil {
|
|
|
|
log.Error("loadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = issue.PullRequest.LoadIssue(); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{
|
2019-11-02 09:49:57 +08:00
|
|
|
Action: api.HookIssueLabelUpdated,
|
|
|
|
Index: issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, models.AccessModeNone),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-02 09:49:57 +08:00
|
|
|
})
|
|
|
|
} else {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssueLabel, &api.IssuePayload{
|
2019-11-02 09:49:57 +08:00
|
|
|
Action: api.HookIssueLabelUpdated,
|
|
|
|
Index: issue.Index,
|
2020-02-29 10:49:50 +08:00
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-02 09:49:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 11:33:20 +08:00
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *models.Issue, oldMilestoneID int64) {
|
|
|
|
var hookAction api.HookIssueAction
|
|
|
|
var err error
|
|
|
|
if issue.MilestoneID > 0 {
|
|
|
|
hookAction = api.HookIssueMilestoned
|
|
|
|
} else {
|
|
|
|
hookAction = api.HookIssueDemilestoned
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = issue.LoadAttributes(); err != nil {
|
|
|
|
log.Error("issue.LoadAttributes failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, _ := models.AccessLevel(doer, issue.Repo)
|
|
|
|
if issue.IsPull {
|
|
|
|
err = issue.PullRequest.LoadIssue()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequestMilestone, &api.PullRequestPayload{
|
2019-11-02 11:33:20 +08:00
|
|
|
Action: hookAction,
|
|
|
|
Index: issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-02 11:33:20 +08:00
|
|
|
})
|
|
|
|
} else {
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventIssueMilestone, &api.IssuePayload{
|
2019-11-02 11:33:20 +08:00
|
|
|
Action: hookAction,
|
|
|
|
Index: issue.Index,
|
2020-02-29 10:49:50 +08:00
|
|
|
Issue: convert.ToAPIIssue(issue),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-02 11:33:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 14:59:26 +08:00
|
|
|
|
2020-10-31 05:59:02 +08:00
|
|
|
func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
2021-03-28 00:45:26 +08:00
|
|
|
apiPusher := convert.ToUser(pusher, nil)
|
2021-06-29 21:34:03 +08:00
|
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
|
2019-11-03 14:59:26 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{
|
2020-10-31 05:59:02 +08:00
|
|
|
Ref: opts.RefFullName,
|
|
|
|
Before: opts.OldCommitID,
|
|
|
|
After: opts.NewCommitID,
|
2019-11-03 14:59:26 +08:00
|
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
|
|
Commits: apiCommits,
|
2021-06-29 21:34:03 +08:00
|
|
|
HeadCommit: apiHeadCommit,
|
2020-12-03 05:38:30 +08:00
|
|
|
Repo: convert.ToRepo(repo, models.AccessModeOwner),
|
2019-11-03 14:59:26 +08:00
|
|
|
Pusher: apiPusher,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 19:04:08 +08:00
|
|
|
|
2020-01-17 00:24:20 +08:00
|
|
|
func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User) {
|
2019-11-22 01:08:42 +08:00
|
|
|
// Reload pull request information.
|
|
|
|
if err := pr.LoadAttributes(); err != nil {
|
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pr.LoadIssue(); err != nil {
|
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pr.Issue.LoadRepo(); err != nil {
|
|
|
|
log.Error("pr.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, err := models.AccessLevel(doer, pr.Issue.Repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("models.AccessLevel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: pr.Issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(pr, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(pr.Issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-22 01:08:42 +08:00
|
|
|
Action: api.HookIssueClosed,
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(pr.Issue.Repo, webhook.HookEventPullRequest, apiPullRequest)
|
2019-11-22 01:08:42 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 14:20:25 +08:00
|
|
|
func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(doer *models.User, pr *models.PullRequest, oldBranch string) {
|
|
|
|
issue := pr.Issue
|
|
|
|
if !issue.IsPull {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = issue.LoadPullRequest(); err != nil {
|
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
issue.PullRequest.Issue = issue
|
|
|
|
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
|
2021-11-10 13:13:16 +08:00
|
|
|
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequest, &api.PullRequestPayload{
|
2019-12-16 14:20:25 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Ref: &api.ChangesFromPayload{
|
|
|
|
From: oldBranch,
|
|
|
|
},
|
|
|
|
},
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-12-16 14:20:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 01:04:02 +08:00
|
|
|
func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment, mentions []*models.User) {
|
2021-11-10 13:13:16 +08:00
|
|
|
var reviewHookType webhook.HookEventType
|
2019-11-05 19:04:08 +08:00
|
|
|
|
|
|
|
switch review.Type {
|
|
|
|
case models.ReviewTypeApprove:
|
2021-11-10 13:13:16 +08:00
|
|
|
reviewHookType = webhook.HookEventPullRequestReviewApproved
|
2019-11-05 19:04:08 +08:00
|
|
|
case models.ReviewTypeComment:
|
2021-11-10 13:13:16 +08:00
|
|
|
reviewHookType = webhook.HookEventPullRequestComment
|
2019-11-05 19:04:08 +08:00
|
|
|
case models.ReviewTypeReject:
|
2021-11-10 13:13:16 +08:00
|
|
|
reviewHookType = webhook.HookEventPullRequestReviewRejected
|
2019-11-05 19:04:08 +08:00
|
|
|
default:
|
|
|
|
// unsupported review webhook type here
|
|
|
|
log.Error("Unsupported review webhook type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pr.LoadIssue(); err != nil {
|
|
|
|
log.Error("pr.LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, err := models.AccessLevel(review.Issue.Poster, review.Issue.Repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("models.AccessLevel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2020-12-08 18:41:14 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
|
2020-03-06 13:10:48 +08:00
|
|
|
Action: api.HookIssueReviewed,
|
2019-11-05 19:04:08 +08:00
|
|
|
Index: review.Issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(pr, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(review.Issue.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(review.Reviewer, nil),
|
2019-11-05 19:04:08 +08:00
|
|
|
Review: &api.ReviewPayload{
|
|
|
|
Type: string(reviewHookType),
|
|
|
|
Content: review.Content,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:43:03 +08:00
|
|
|
func (m *webhookNotifier) NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
|
2021-03-28 00:45:26 +08:00
|
|
|
apiPusher := convert.ToUser(pusher, nil)
|
2020-12-03 05:38:30 +08:00
|
|
|
apiRepo := convert.ToRepo(repo, models.AccessModeNone)
|
2019-11-06 14:43:03 +08:00
|
|
|
refName := git.RefEndName(refFullName)
|
|
|
|
|
|
|
|
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 07:49:40 +08:00
|
|
|
shaSum, err := gitRepo.GetRefCommitID(refFullName)
|
2019-11-06 14:43:03 +08:00
|
|
|
if err != nil {
|
2019-11-13 15:01:19 +08:00
|
|
|
gitRepo.Close()
|
2019-11-13 07:49:40 +08:00
|
|
|
log.Error("GetRefCommitID[%s]: %v", refFullName, err)
|
2019-11-06 14:43:03 +08:00
|
|
|
return
|
|
|
|
}
|
2019-11-13 15:01:19 +08:00
|
|
|
gitRepo.Close()
|
2019-11-06 14:43:03 +08:00
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
if err = webhook_services.PrepareWebhooks(repo, webhook.HookEventCreate, &api.CreatePayload{
|
2019-11-06 14:43:03 +08:00
|
|
|
Ref: refName,
|
|
|
|
Sha: shaSum,
|
|
|
|
RefType: refType,
|
|
|
|
Repo: apiRepo,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 19:04:08 +08:00
|
|
|
func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *models.User, pr *models.PullRequest) {
|
|
|
|
if err := pr.LoadIssue(); err != nil {
|
|
|
|
log.Error("pr.LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := pr.Issue.LoadAttributes(); err != nil {
|
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(pr.Issue.Repo, webhook.HookEventPullRequestSync, &api.PullRequestPayload{
|
2019-11-05 19:04:08 +08:00
|
|
|
Action: api.HookIssueSynchronized,
|
|
|
|
Index: pr.Issue.Index,
|
2021-10-07 08:03:37 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(pr, nil),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(pr.Issue.Repo, models.AccessModeNone),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-05 19:04:08 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 14:43:03 +08:00
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
|
2021-03-28 00:45:26 +08:00
|
|
|
apiPusher := convert.ToUser(pusher, nil)
|
2020-12-03 05:38:30 +08:00
|
|
|
apiRepo := convert.ToRepo(repo, models.AccessModeNone)
|
2019-11-06 14:43:03 +08:00
|
|
|
refName := git.RefEndName(refFullName)
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventDelete, &api.DeletePayload{
|
2019-11-06 14:43:03 +08:00
|
|
|
Ref: refName,
|
2020-02-24 02:49:40 +08:00
|
|
|
RefType: refType,
|
2019-11-06 14:43:03 +08:00
|
|
|
PusherType: api.PusherTypeUser,
|
|
|
|
Repo: apiRepo,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
2020-02-24 02:49:40 +08:00
|
|
|
log.Error("PrepareWebhooks.(delete %s): %v", refType, err)
|
2019-11-06 14:43:03 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 16:25:50 +08:00
|
|
|
|
|
|
|
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)
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(rel.Repo, webhook.HookEventRelease, &api.ReleasePayload{
|
2019-11-06 16:25:50 +08:00
|
|
|
Action: action,
|
2020-10-17 12:23:08 +08:00
|
|
|
Release: convert.ToRelease(rel),
|
2020-12-03 05:38:30 +08:00
|
|
|
Repository: convert.ToRepo(rel.Repo, mode),
|
2021-03-28 00:45:26 +08:00
|
|
|
Sender: convert.ToUser(rel.Publisher, nil),
|
2019-11-06 16:25:50 +08:00
|
|
|
}); 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)
|
|
|
|
}
|
2019-11-24 13:16:59 +08:00
|
|
|
|
2020-10-31 05:59:02 +08:00
|
|
|
func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
2021-03-28 00:45:26 +08:00
|
|
|
apiPusher := convert.ToUser(pusher, nil)
|
2021-06-29 21:34:03 +08:00
|
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
|
2019-11-24 13:16:59 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventPush, &api.PushPayload{
|
2020-10-31 05:59:02 +08:00
|
|
|
Ref: opts.RefFullName,
|
|
|
|
Before: opts.OldCommitID,
|
|
|
|
After: opts.NewCommitID,
|
2019-11-24 13:16:59 +08:00
|
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
|
|
Commits: apiCommits,
|
2021-06-29 21:34:03 +08:00
|
|
|
HeadCommit: apiHeadCommit,
|
2020-12-03 05:38:30 +08:00
|
|
|
Repo: convert.ToRepo(repo, models.AccessModeOwner),
|
2019-11-24 13:16:59 +08:00
|
|
|
Pusher: apiPusher,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 03:12:33 +08:00
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifySyncCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
|
|
|
|
m.NotifyCreateRef(pusher, repo, refType, refFullName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *webhookNotifier) NotifySyncDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
|
|
|
|
m.NotifyDeleteRef(pusher, repo, refType, refFullName)
|
|
|
|
}
|