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