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