2019-10-15 14:03:05 +09: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 (
|
2022-11-19 17:12:33 +09:00
|
|
|
"context"
|
2022-01-20 08:26:57 +09:00
|
|
|
|
2022-06-13 18:37:59 +09:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-03-30 17:42:47 +09:00
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2021-11-28 20:58:28 +09:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 19:09:36 +09:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 10:27:50 +09:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 04:57:58 +09:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 18:49:20 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-10 14:13:16 +09:00
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2020-01-10 16:53:53 +09:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-11-06 15:43:03 +09:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-10-15 14:03:05 +09:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/notification/base"
|
2020-01-10 18:34:21 +09:00
|
|
|
"code.gitea.io/gitea/modules/repository"
|
2019-11-03 15:59:26 +09:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-10-15 14:03:05 +09:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-12-08 19:41:14 +09:00
|
|
|
webhook_services "code.gitea.io/gitea/services/webhook"
|
2019-10-15 14:03:05 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
type webhookNotifier struct {
|
|
|
|
base.NullNotifier
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
var _ base.Notifier = &webhookNotifier{}
|
2019-10-15 14:03:05 +09:00
|
|
|
|
|
|
|
// NewNotifier create a new webhookNotifier notifier
|
|
|
|
func NewNotifier() base.Notifier {
|
|
|
|
return &webhookNotifier{}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
|
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
|
|
|
log.Error("LoadPoster: %v", err)
|
2019-10-15 14:03:05 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 18:11:15 +09:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2019-10-15 14:03:05 +09:00
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-10-15 14:03:05 +09:00
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-15 14:03:05 +09:00
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{
|
2019-10-15 14:03:05 +09:00
|
|
|
Action: api.HookIssueLabelCleared,
|
|
|
|
Index: issue.Index,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-15 14:03:05 +09:00
|
|
|
})
|
|
|
|
} else {
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssueLabel, &api.IssuePayload{
|
2019-10-15 14:03:05 +09:00
|
|
|
Action: api.HookIssueLabelCleared,
|
|
|
|
Index: issue.Index,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-15 14:03:05 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 15:54:11 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
|
|
|
|
oldMode, _ := access_model.AccessLevel(ctx, doer, oldRepo)
|
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, repo)
|
2019-10-26 15:54:11 +09:00
|
|
|
|
|
|
|
// forked webhook
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: oldRepo}, webhook.HookEventFork, &api.ForkPayload{
|
2020-12-03 06:38:30 +09:00
|
|
|
Forkee: convert.ToRepo(oldRepo, oldMode),
|
|
|
|
Repo: convert.ToRepo(repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-26 15:54:11 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err)
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
u := repo.MustOwner(ctx)
|
2019-10-26 15:54:11 +09:00
|
|
|
|
|
|
|
// Add to hook queue for created repo after session commit.
|
|
|
|
if u.IsOrganization() {
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{
|
2019-10-26 15:54:11 +09:00
|
|
|
Action: api.HookRepoCreated,
|
2021-11-28 20:58:28 +09:00
|
|
|
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
|
2021-03-28 01:45:26 +09:00
|
|
|
Organization: convert.ToUser(u, nil),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-26 15:54:11 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
2019-10-26 15:54:11 +09:00
|
|
|
// Add to hook queue for created repo after session commit.
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{
|
2020-10-02 18:37:46 +09:00
|
|
|
Action: api.HookRepoCreated,
|
2021-11-28 20:58:28 +09:00
|
|
|
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
|
2021-03-28 01:45:26 +09:00
|
|
|
Organization: convert.ToUser(u, nil),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-10-02 18:37:46 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
2019-10-26 15:54:11 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
|
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{
|
2020-10-02 18:37:46 +09:00
|
|
|
Action: api.HookRepoDeleted,
|
2021-11-28 20:58:28 +09:00
|
|
|
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
|
2022-11-19 17:12:33 +09:00
|
|
|
Organization: convert.ToUser(repo.MustOwner(ctx), nil),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-10-02 18:37:46 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
2019-10-26 15:54:11 +09:00
|
|
|
}
|
|
|
|
}
|
2019-10-28 11:11:50 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
2020-12-17 21:26:22 +09:00
|
|
|
// Add to hook queue for created repo after session commit.
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{
|
2020-12-17 21:26:22 +09:00
|
|
|
Action: api.HookRepoCreated,
|
2021-11-28 20:58:28 +09:00
|
|
|
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
|
2021-03-28 01:45:26 +09:00
|
|
|
Organization: convert.ToUser(u, nil),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
2020-12-17 21:26:22 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) {
|
2019-10-28 11:11:50 +09:00
|
|
|
if issue.IsPull {
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevelUnit(ctx, doer, issue.Repo, unit.TypePullRequests)
|
2019-10-28 11:11:50 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 11:11:50 +09:00
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
issue.PullRequest.Issue = issue
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 11:11:50 +09:00
|
|
|
}
|
|
|
|
if removed {
|
|
|
|
apiPullRequest.Action = api.HookIssueUnassigned
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueAssigned
|
|
|
|
}
|
|
|
|
// Assignee comment triggers a webhook
|
2022-10-22 01:21:56 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequestAssign, apiPullRequest); err != nil {
|
2019-10-28 11:11:50 +09:00
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevelUnit(ctx, doer, issue.Repo, unit.TypeIssues)
|
2019-10-28 11:11:50 +09:00
|
|
|
apiIssue := &api.IssuePayload{
|
|
|
|
Index: issue.Index,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 11:11:50 +09:00
|
|
|
}
|
|
|
|
if removed {
|
|
|
|
apiIssue.Action = api.HookIssueUnassigned
|
|
|
|
} else {
|
|
|
|
apiIssue.Action = api.HookIssueAssigned
|
|
|
|
}
|
|
|
|
// Assignee comment triggers a webhook
|
2022-10-22 01:21:56 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssueAssign, apiIssue); err != nil {
|
2019-10-28 11:11:50 +09:00
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) {
|
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-10-28 11:11:50 +09:00
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 11:11:50 +09:00
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
issue.PullRequest.Issue = issue
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequest, &api.PullRequestPayload{
|
2019-10-28 11:11:50 +09:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Title: &api.ChangesFromPayload{
|
|
|
|
From: oldTitle,
|
|
|
|
},
|
|
|
|
},
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 11:11:50 +09:00
|
|
|
})
|
|
|
|
} else {
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssues, &api.IssuePayload{
|
2019-10-28 11:11:50 +09:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Title: &api.ChangesFromPayload{
|
|
|
|
From: oldTitle,
|
|
|
|
},
|
|
|
|
},
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-11-24 11:47:03 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 11:11:50 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 14:26:46 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, actionComment *issues_model.Comment, isClosed bool) {
|
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-10-28 14:26:46 +09:00
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 14:26:46 +09:00
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 14:26:46 +09:00
|
|
|
}
|
|
|
|
if isClosed {
|
|
|
|
apiPullRequest.Action = api.HookIssueClosed
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueReOpened
|
|
|
|
}
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequest, apiPullRequest)
|
2019-10-28 14:26:46 +09:00
|
|
|
} else {
|
|
|
|
apiIssue := &api.IssuePayload{
|
|
|
|
Index: issue.Index,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-28 14:26:46 +09:00
|
|
|
}
|
|
|
|
if isClosed {
|
|
|
|
apiIssue.Action = api.HookIssueClosed
|
|
|
|
} else {
|
|
|
|
apiIssue.Action = api.HookIssueReOpened
|
|
|
|
}
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssues, apiIssue)
|
2019-10-28 14:26:46 +09:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 01:45:43 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyNewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
|
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2019-11-04 05:59:09 +09:00
|
|
|
log.Error("issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
2019-11-04 05:59:09 +09:00
|
|
|
log.Error("issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssues, &api.IssuePayload{
|
2019-10-29 01:45:43 +09:00
|
|
|
Action: api.HookIssueOpened,
|
|
|
|
Index: issue.Index,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(issue.Poster, nil),
|
2019-10-29 01:45:43 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 17:36:25 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues_model.PullRequest, mentions []*user_model.User) {
|
|
|
|
if err := pull.LoadIssue(ctx); err != nil {
|
2019-11-04 05:59:09 +09:00
|
|
|
log.Error("pull.LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-04-08 18:11:15 +09:00
|
|
|
if err := pull.Issue.LoadRepo(ctx); err != nil {
|
2019-11-04 05:59:09 +09:00
|
|
|
log.Error("pull.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := pull.Issue.LoadPoster(ctx); err != nil {
|
2019-11-04 05:59:09 +09:00
|
|
|
log.Error("pull.Issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, pull.Issue.Poster, pull.Issue.Repo)
|
2022-10-22 01:21:56 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: pull.Issue.Repo}, webhook.HookEventPullRequest, &api.PullRequestPayload{
|
2019-11-04 05:59:09 +09:00
|
|
|
Action: api.HookIssueOpened,
|
|
|
|
Index: pull.Issue.Index,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pull, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(pull.Issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(pull.Issue.Poster, nil),
|
2019-11-04 05:59:09 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
|
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-10-30 17:36:25 +09:00
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
|
|
|
issue.PullRequest.Issue = issue
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequest, &api.PullRequestPayload{
|
2019-10-30 17:36:25 +09:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-30 17:36:25 +09:00
|
|
|
})
|
|
|
|
} else {
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssues, &api.IssuePayload{
|
2019-10-30 17:36:25 +09:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-10-30 17:36:25 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 19:02:46 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyUpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
|
|
|
|
if err := c.LoadPoster(ctx); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := c.LoadIssue(ctx); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := c.Issue.LoadAttributes(ctx); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:21:56 +09:00
|
|
|
var eventType webhook.HookEventType
|
2020-03-06 14:10:48 +09:00
|
|
|
if c.Issue.IsPull {
|
2022-10-22 01:21:56 +09:00
|
|
|
eventType = webhook.HookEventPullRequestComment
|
2020-03-06 14:10:48 +09:00
|
|
|
} else {
|
2022-10-22 01:21:56 +09:00
|
|
|
eventType = webhook.HookEventIssueComment
|
2020-03-06 14:10:48 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, c.Issue.Repo)
|
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: c.Issue.Repo}, eventType, &api.IssueCommentPayload{
|
2022-10-22 01:21:56 +09:00
|
|
|
Action: api.HookIssueCommentEdited,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, c.Issue),
|
2022-10-22 01:21:56 +09:00
|
|
|
Comment: convert.ToComment(c),
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Repository: convert.ToRepo(c.Issue.Repo, mode),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
|
|
|
IsPull: c.Issue.IsPull,
|
|
|
|
}); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
|
2022-06-13 18:37:59 +09:00
|
|
|
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
|
2022-02-24 05:16:07 +09:00
|
|
|
) {
|
2022-10-22 01:21:56 +09:00
|
|
|
var eventType webhook.HookEventType
|
2020-03-06 14:10:48 +09:00
|
|
|
if issue.IsPull {
|
2022-10-22 01:21:56 +09:00
|
|
|
eventType = webhook.HookEventPullRequestComment
|
2020-03-06 14:10:48 +09:00
|
|
|
} else {
|
2022-10-22 01:21:56 +09:00
|
|
|
eventType = webhook.HookEventIssueComment
|
2020-03-06 14:10:48 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, repo)
|
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, eventType, &api.IssueCommentPayload{
|
2022-10-22 01:21:56 +09:00
|
|
|
Action: api.HookIssueCommentCreated,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-10-22 01:21:56 +09:00
|
|
|
Comment: convert.ToComment(comment),
|
|
|
|
Repository: convert.ToRepo(repo, mode),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
|
|
|
IsPull: issue.IsPull,
|
|
|
|
}); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyDeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) {
|
2020-03-06 14:10:48 +09:00
|
|
|
var err error
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = comment.LoadPoster(ctx); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = comment.LoadIssue(ctx); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = comment.Issue.LoadAttributes(ctx); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:21:56 +09:00
|
|
|
var eventType webhook.HookEventType
|
2020-03-06 14:10:48 +09:00
|
|
|
if comment.Issue.IsPull {
|
2022-10-22 01:21:56 +09:00
|
|
|
eventType = webhook.HookEventPullRequestComment
|
2020-03-06 14:10:48 +09:00
|
|
|
} else {
|
2022-10-22 01:21:56 +09:00
|
|
|
eventType = webhook.HookEventIssueComment
|
2020-03-06 14:10:48 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, comment.Issue.Repo)
|
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: comment.Issue.Repo}, eventType, &api.IssueCommentPayload{
|
2022-10-22 01:21:56 +09:00
|
|
|
Action: api.HookIssueCommentDeleted,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, comment.Issue),
|
2022-10-22 01:21:56 +09:00
|
|
|
Comment: convert.ToComment(comment),
|
|
|
|
Repository: convert.ToRepo(comment.Issue.Repo, mode),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
|
|
|
IsPull: comment.Issue.IsPull,
|
|
|
|
}); err != nil {
|
2019-10-30 19:02:46 +09:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 10:49:57 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
|
2022-09-05 04:54:23 +09:00
|
|
|
// Add to hook queue for created wiki page.
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{
|
2022-09-05 04:54:23 +09:00
|
|
|
Action: api.HookWikiCreated,
|
|
|
|
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
|
|
|
Page: page,
|
|
|
|
Comment: comment,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
|
2022-09-05 04:54:23 +09:00
|
|
|
// Add to hook queue for edit wiki page.
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{
|
2022-09-05 04:54:23 +09:00
|
|
|
Action: api.HookWikiEdited,
|
|
|
|
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
|
|
|
Page: page,
|
|
|
|
Comment: comment,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) {
|
2022-09-05 04:54:23 +09:00
|
|
|
// Add to hook queue for edit wiki page.
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{
|
2022-09-05 04:54:23 +09:00
|
|
|
Action: api.HookWikiDeleted,
|
|
|
|
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
|
|
|
|
Sender: convert.ToUser(doer, nil),
|
|
|
|
Page: page,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
|
2022-06-13 18:37:59 +09:00
|
|
|
addedLabels, removedLabels []*issues_model.Label,
|
2022-02-24 05:16:07 +09:00
|
|
|
) {
|
2019-11-02 10:49:57 +09:00
|
|
|
var err error
|
|
|
|
|
2022-04-08 18:11:15 +09:00
|
|
|
if err = issue.LoadRepo(ctx); err != nil {
|
2019-11-02 10:49:57 +09:00
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = issue.LoadPoster(ctx); err != nil {
|
2019-11-02 10:49:57 +09:00
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-11-02 10:49:57 +09:00
|
|
|
if issue.IsPull {
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-11-02 10:49:57 +09:00
|
|
|
log.Error("loadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = issue.PullRequest.LoadIssue(ctx); err != nil {
|
2019-11-02 10:49:57 +09:00
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{
|
2019-11-02 10:49:57 +09:00
|
|
|
Action: api.HookIssueLabelUpdated,
|
|
|
|
Index: issue.Index,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2021-11-28 20:58:28 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, perm.AccessModeNone),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-02 10:49:57 +09:00
|
|
|
})
|
|
|
|
} else {
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssueLabel, &api.IssuePayload{
|
2019-11-02 10:49:57 +09:00
|
|
|
Action: api.HookIssueLabelUpdated,
|
|
|
|
Index: issue.Index,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-02 10:49:57 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 12:33:20 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
|
2019-11-02 12:33:20 +09:00
|
|
|
var hookAction api.HookIssueAction
|
|
|
|
var err error
|
|
|
|
if issue.MilestoneID > 0 {
|
|
|
|
hookAction = api.HookIssueMilestoned
|
|
|
|
} else {
|
|
|
|
hookAction = api.HookIssueDemilestoned
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err = issue.LoadAttributes(ctx); err != nil {
|
2019-11-02 12:33:20 +09:00
|
|
|
log.Error("issue.LoadAttributes failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, issue.Repo)
|
2019-11-02 12:33:20 +09:00
|
|
|
if issue.IsPull {
|
2022-11-19 17:12:33 +09:00
|
|
|
err = issue.PullRequest.LoadIssue(ctx)
|
2019-11-02 12:33:20 +09:00
|
|
|
if err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequestMilestone, &api.PullRequestPayload{
|
2019-11-02 12:33:20 +09:00
|
|
|
Action: hookAction,
|
|
|
|
Index: issue.Index,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-02 12:33:20 +09:00
|
|
|
})
|
|
|
|
} else {
|
2022-10-22 01:21:56 +09:00
|
|
|
err = webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventIssueMilestone, &api.IssuePayload{
|
2019-11-02 12:33:20 +09:00
|
|
|
Action: hookAction,
|
|
|
|
Index: issue.Index,
|
2022-11-19 17:12:33 +09:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-02 12:33:20 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 15:59:26 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
2021-03-28 01:45:26 +09:00
|
|
|
apiPusher := convert.ToUser(pusher, nil)
|
2022-01-20 08:26:57 +09:00
|
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
|
2019-11-03 15:59:26 +09:00
|
|
|
if err != nil {
|
|
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:21:56 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventPush, &api.PushPayload{
|
2022-10-17 01:22:34 +09:00
|
|
|
Ref: opts.RefFullName,
|
|
|
|
Before: opts.OldCommitID,
|
|
|
|
After: opts.NewCommitID,
|
|
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
|
|
Commits: apiCommits,
|
|
|
|
TotalCommits: commits.Len,
|
|
|
|
HeadCommit: apiHeadCommit,
|
|
|
|
Repo: convert.ToRepo(repo, perm.AccessModeOwner),
|
|
|
|
Pusher: apiPusher,
|
|
|
|
Sender: apiPusher,
|
2019-11-03 15:59:26 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 20:04:08 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyAutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
2022-11-04 00:49:00 +09:00
|
|
|
// just redirect to the NotifyMergePullRequest
|
2022-11-19 17:12:33 +09:00
|
|
|
m.NotifyMergePullRequest(ctx, doer, pr)
|
2022-11-04 00:49:00 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (*webhookNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
2019-11-22 02:08:42 +09:00
|
|
|
// Reload pull request information.
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := pr.LoadAttributes(ctx); err != nil {
|
2019-11-22 02:08:42 +09:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-22 02:08:42 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 18:11:15 +09:00
|
|
|
if err := pr.Issue.LoadRepo(ctx); err != nil {
|
2019-11-22 02:08:42 +09:00
|
|
|
log.Error("pr.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, err := access_model.AccessLevel(ctx, doer, pr.Issue.Repo)
|
2019-11-22 02:08:42 +09:00
|
|
|
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,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(pr.Issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-22 02:08:42 +09:00
|
|
|
Action: api.HookIssueClosed,
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: pr.Issue.Repo}, webhook.HookEventPullRequest, apiPullRequest); err != nil {
|
2019-11-22 02:08:42 +09:00
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-12-16 15:20:25 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
issue := pr.Issue
|
|
|
|
|
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: issue.Repo}, webhook.HookEventPullRequest, &api.PullRequestPayload{
|
2019-12-16 15:20:25 +09:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Ref: &api.ChangesFromPayload{
|
|
|
|
From: oldBranch,
|
|
|
|
},
|
|
|
|
},
|
2022-11-19 17:12:33 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2022-11-19 17:12:33 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [pr: %d]: %v", pr.ID, err)
|
2019-12-16 15:20:25 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
|
2021-11-10 14:13:16 +09:00
|
|
|
var reviewHookType webhook.HookEventType
|
2019-11-05 20:04:08 +09:00
|
|
|
|
|
|
|
switch review.Type {
|
2022-06-13 18:37:59 +09:00
|
|
|
case issues_model.ReviewTypeApprove:
|
2021-11-10 14:13:16 +09:00
|
|
|
reviewHookType = webhook.HookEventPullRequestReviewApproved
|
2022-06-13 18:37:59 +09:00
|
|
|
case issues_model.ReviewTypeComment:
|
2021-11-10 14:13:16 +09:00
|
|
|
reviewHookType = webhook.HookEventPullRequestComment
|
2022-06-13 18:37:59 +09:00
|
|
|
case issues_model.ReviewTypeReject:
|
2021-11-10 14:13:16 +09:00
|
|
|
reviewHookType = webhook.HookEventPullRequestReviewRejected
|
2019-11-05 20:04:08 +09:00
|
|
|
default:
|
|
|
|
// unsupported review webhook type here
|
|
|
|
log.Error("Unsupported review webhook type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-05 20:04:08 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, err := access_model.AccessLevel(ctx, review.Issue.Poster, review.Issue.Repo)
|
2019-11-05 20:04:08 +09:00
|
|
|
if err != nil {
|
|
|
|
log.Error("models.AccessLevel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-22 01:21:56 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: review.Issue.Repo}, reviewHookType, &api.PullRequestPayload{
|
2020-03-06 14:10:48 +09:00
|
|
|
Action: api.HookIssueReviewed,
|
2019-11-05 20:04:08 +09:00
|
|
|
Index: review.Issue.Index,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(review.Issue.Repo, mode),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(review.Reviewer, nil),
|
2019-11-05 20:04:08 +09:00
|
|
|
Review: &api.ReviewPayload{
|
|
|
|
Type: string(reviewHookType),
|
|
|
|
Content: review.Content,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
|
2021-03-28 01:45:26 +09:00
|
|
|
apiPusher := convert.ToUser(pusher, nil)
|
2021-11-28 20:58:28 +09:00
|
|
|
apiRepo := convert.ToRepo(repo, perm.AccessModeNone)
|
2019-11-06 15:43:03 +09:00
|
|
|
refName := git.RefEndName(refFullName)
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventCreate, &api.CreatePayload{
|
2019-11-06 15:43:03 +09:00
|
|
|
Ref: refName,
|
2022-01-20 08:26:57 +09:00
|
|
|
Sha: refID,
|
2019-11-06 15:43:03 +09:00
|
|
|
RefType: refType,
|
|
|
|
Repo: apiRepo,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-05 20:04:08 +09:00
|
|
|
return
|
|
|
|
}
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := pr.Issue.LoadAttributes(ctx); err != nil {
|
2019-11-05 20:04:08 +09:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:21:56 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: pr.Issue.Repo}, webhook.HookEventPullRequestSync, &api.PullRequestPayload{
|
2019-11-05 20:04:08 +09:00
|
|
|
Action: api.HookIssueSynchronized,
|
|
|
|
Index: pr.Issue.Index,
|
2022-01-20 08:26:57 +09:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
2021-11-28 20:58:28 +09:00
|
|
|
Repository: convert.ToRepo(pr.Issue.Repo, perm.AccessModeNone),
|
2021-03-28 01:45:26 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-05 20:04:08 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 15:43:03 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
|
2021-03-28 01:45:26 +09:00
|
|
|
apiPusher := convert.ToUser(pusher, nil)
|
2021-11-28 20:58:28 +09:00
|
|
|
apiRepo := convert.ToRepo(repo, perm.AccessModeNone)
|
2019-11-06 15:43:03 +09:00
|
|
|
refName := git.RefEndName(refFullName)
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventDelete, &api.DeletePayload{
|
2019-11-06 15:43:03 +09:00
|
|
|
Ref: refName,
|
2020-02-24 03:49:40 +09:00
|
|
|
RefType: refType,
|
2019-11-06 15:43:03 +09:00
|
|
|
PusherType: api.PusherTypeUser,
|
|
|
|
Repo: apiRepo,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
2020-02-24 03:49:40 +09:00
|
|
|
log.Error("PrepareWebhooks.(delete %s): %v", refType, err)
|
2019-11-06 15:43:03 +09:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 17:25:50 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model.Release, action api.HookReleaseAction) {
|
|
|
|
if err := rel.LoadAttributes(ctx); err != nil {
|
2019-11-06 17:25:50 +09:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, rel.Repo)
|
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: rel.Repo}, webhook.HookEventRelease, &api.ReleasePayload{
|
2019-11-06 17:25:50 +09:00
|
|
|
Action: action,
|
2020-10-17 13:23:08 +09:00
|
|
|
Release: convert.ToRelease(rel),
|
2020-12-03 06:38:30 +09:00
|
|
|
Repository: convert.ToRepo(rel.Repo, mode),
|
2021-11-25 13:17:16 +09:00
|
|
|
Sender: convert.ToUser(doer, nil),
|
2019-11-06 17:25:50 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyNewRelease(ctx context.Context, rel *repo_model.Release) {
|
|
|
|
sendReleaseHook(ctx, rel.Publisher, rel, api.HookReleasePublished)
|
2019-11-06 17:25:50 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyUpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
|
|
|
sendReleaseHook(ctx, doer, rel, api.HookReleaseUpdated)
|
2019-11-06 17:25:50 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyDeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
|
|
|
sendReleaseHook(ctx, doer, rel, api.HookReleaseDeleted)
|
2019-11-06 17:25:50 +09:00
|
|
|
}
|
2019-11-24 14:16:59 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
2021-03-28 01:45:26 +09:00
|
|
|
apiPusher := convert.ToUser(pusher, nil)
|
2022-01-20 08:26:57 +09:00
|
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
|
2019-11-24 14:16:59 +09:00
|
|
|
if err != nil {
|
|
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:21:56 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventPush, &api.PushPayload{
|
2022-10-17 01:22:34 +09:00
|
|
|
Ref: opts.RefFullName,
|
|
|
|
Before: opts.OldCommitID,
|
|
|
|
After: opts.NewCommitID,
|
|
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
|
|
Commits: apiCommits,
|
|
|
|
TotalCommits: commits.Len,
|
|
|
|
HeadCommit: apiHeadCommit,
|
|
|
|
Repo: convert.ToRepo(repo, perm.AccessModeOwner),
|
|
|
|
Pusher: apiPusher,
|
|
|
|
Sender: apiPusher,
|
2019-11-24 14:16:59 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 04:12:33 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
|
|
|
|
m.NotifyCreateRef(ctx, pusher, repo, refType, refFullName, refID)
|
2020-11-14 04:12:33 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
|
|
|
|
m.NotifyDeleteRef(ctx, pusher, repo, refType, refFullName)
|
2020-11-14 04:12:33 +09:00
|
|
|
}
|
2022-03-30 17:42:47 +09:00
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyPackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
|
|
|
notifyPackage(ctx, doer, pd, api.HookPackageCreated)
|
2022-03-30 17:42:47 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func (m *webhookNotifier) NotifyPackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
|
|
|
notifyPackage(ctx, doer, pd, api.HookPackageDeleted)
|
2022-03-30 17:42:47 +09:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:12:33 +09:00
|
|
|
func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_model.PackageDescriptor, action api.HookPackageAction) {
|
2022-10-22 01:21:56 +09:00
|
|
|
source := webhook_services.EventSource{
|
|
|
|
Repository: pd.Repository,
|
|
|
|
Owner: pd.Owner,
|
2022-03-30 17:42:47 +09:00
|
|
|
}
|
|
|
|
|
2022-05-08 01:21:15 +09:00
|
|
|
apiPackage, err := convert.ToPackage(ctx, pd, sender)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error converting package: %v", err)
|
|
|
|
return
|
2022-03-30 17:42:47 +09:00
|
|
|
}
|
|
|
|
|
2022-10-22 01:21:56 +09:00
|
|
|
if err := webhook_services.PrepareWebhooks(ctx, source, webhook.HookEventPackage, &api.PackagePayload{
|
2022-05-08 01:21:15 +09:00
|
|
|
Action: action,
|
|
|
|
Package: apiPackage,
|
|
|
|
Sender: convert.ToUser(sender, nil),
|
2022-03-30 17:42:47 +09:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|