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