@@ -34,15 +34,34 @@ func ResyncCommitStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
3434 continue
3535 }
3636
37- var vcsServerName string
38- var repoFullName string
39-
4037 node := wr .Workflow .WorkflowData .NodeByID (nodeID )
4138 if ! node .IsLinkedToRepo (& wr .Workflow ) {
4239 continue
4340 }
44- vcsServerName = wr .Workflow .Applications [node .Context .ApplicationID ].VCSServer
45- repoFullName = wr .Workflow .Applications [node .Context .ApplicationID ].RepositoryFullname
41+
42+ var notif * sdk.WorkflowNotification
43+ // browse notification to find vcs one
44+ loopNotif:
45+ for _ , n := range wr .Workflow .Notifications {
46+ if n .Type != sdk .VCSUserNotification {
47+ continue
48+ }
49+ // If list of node is nill, send notification to all of them
50+ if len (n .NodeIDs ) == 0 {
51+ notif = & n
52+ break
53+ }
54+ // browser source node id
55+ for _ , src := range n .NodeIDs {
56+ if src == node .ID {
57+ notif = & n
58+ break loopNotif
59+ }
60+ }
61+ }
62+
63+ vcsServerName := wr .Workflow .Applications [node .Context .ApplicationID ].VCSServer
64+ repoFullName := wr .Workflow .Applications [node .Context .ApplicationID ].RepositoryFullname
4665
4766 vcsServer := repositoriesmanager .GetProjectVCSServer (proj , vcsServerName )
4867 if vcsServer == nil {
@@ -80,11 +99,11 @@ func ResyncCommitStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
8099 }
81100
82101 if statusFound == nil || statusFound .State == "" {
83- if err := sendVCSEventStatus (ctx , db , store , proj , wr , & nodeRun ); err != nil {
102+ if err := sendVCSEventStatus (ctx , db , store , proj , wr , & nodeRun , notif ); err != nil {
84103 log .Error (ctx , "resyncCommitStatus> Error sending status %s err: %v" , details , err )
85104 }
86105
87- if err := sendVCSPullRequestComment (ctx , db , store , proj , wr , & nodeRun ); err != nil {
106+ if err := sendVCSPullRequestComment (ctx , db , store , proj , wr , & nodeRun , notif ); err != nil {
88107 log .Error (ctx , "resyncCommitStatus> Error sending pr comments %s %s err:%v" , statusFound .State , details , err )
89108 }
90109 continue
@@ -111,12 +130,12 @@ func ResyncCommitStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
111130 }
112131
113132 if ! skipStatus {
114- if err := sendVCSEventStatus (ctx , db , store , proj , wr , & nodeRun ); err != nil {
133+ if err := sendVCSEventStatus (ctx , db , store , proj , wr , & nodeRun , notif ); err != nil {
115134 log .Error (ctx , "resyncCommitStatus> Error sending status %s %s err:%v" , statusFound .State , details , err )
116135 }
117136 }
118137
119- if err := sendVCSPullRequestComment (ctx , db , store , proj , wr , & nodeRun ); err != nil {
138+ if err := sendVCSPullRequestComment (ctx , db , store , proj , wr , & nodeRun , notif ); err != nil {
120139 log .Error (ctx , "resyncCommitStatus> Error sending pr comments %s %s err:%v" , statusFound .State , details , err )
121140 }
122141
@@ -125,7 +144,11 @@ func ResyncCommitStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
125144}
126145
127146// sendVCSEventStatus send status
128- func sendVCSEventStatus (ctx context.Context , db gorp.SqlExecutor , store cache.Store , proj sdk.Project , wr * sdk.WorkflowRun , nodeRun * sdk.WorkflowNodeRun ) error {
147+ func sendVCSEventStatus (ctx context.Context , db gorp.SqlExecutor , store cache.Store , proj sdk.Project , wr * sdk.WorkflowRun , nodeRun * sdk.WorkflowNodeRun , notif * sdk.WorkflowNotification ) error {
148+ if notif == nil || notif .Settings .Template == nil || (notif .Settings .Template .DisableStatus != nil && * notif .Settings .Template .DisableStatus ) {
149+ return nil
150+ }
151+
129152 log .Debug ("Send status for node run %d" , nodeRun .ID )
130153 var app sdk.Application
131154 var pip sdk.Pipeline
@@ -258,23 +281,22 @@ func sendVCSEventStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
258281 return nil
259282}
260283
261- func sendVCSPullRequestComment (ctx context.Context , db gorp.SqlExecutor , store cache.Store , proj sdk.Project , wr * sdk.WorkflowRun , nodeRun * sdk.WorkflowNodeRun ) error {
284+ func sendVCSPullRequestComment (ctx context.Context , db gorp.SqlExecutor , store cache.Store , proj sdk.Project , wr * sdk.WorkflowRun , nodeRun * sdk.WorkflowNodeRun , notif * sdk.WorkflowNotification ) error {
285+ if notif == nil || notif .Settings .Template == nil || (notif .Settings .Template .DisableComment != nil && * notif .Settings .Template .DisableComment ) {
286+ return nil
287+ }
288+
289+ if nodeRun .Status != sdk .StatusFail && nodeRun .Status != sdk .StatusStopped && notif .Settings .OnSuccess != sdk .UserNotificationAlways {
290+ return nil
291+ }
292+
262293 log .Debug ("Send pull-request comment for node run %d" , nodeRun .ID )
263294
264295 var app sdk.Application
265296 node := wr .Workflow .WorkflowData .NodeByID (nodeRun .WorkflowNodeID )
266297 if ! node .IsLinkedToRepo (& wr .Workflow ) {
267298 return nil
268299 }
269- notif , errN := loadVCSNotificationWithNodeID (db , wr .WorkflowID , node .ID )
270- if errN != nil {
271- return sdk .WrapError (errN , "cannot load notification" )
272- }
273-
274- // vcs notification not enabled
275- if notif .ID == 0 {
276- return nil
277- }
278300
279301 if nodeRun .VCSReport == "" {
280302 nodeRun .VCSReport = notif .Settings .Template .Body
@@ -296,7 +318,7 @@ func sendVCSPullRequestComment(ctx context.Context, db gorp.SqlExecutor, store c
296318 //Get the RepositoriesManager Client
297319 client , errClient := repositoriesmanager .AuthorizedClient (ctx , db , store , proj .Key , vcsServer )
298320 if errClient != nil {
299- return sdk . WrapError ( errClient , "sendVCSPullRequestComment> Cannot get client" )
321+ return errClient
300322 }
301323
302324 // Check if it's a gerrit or not
@@ -305,7 +327,29 @@ func sendVCSPullRequestComment(ctx context.Context, db gorp.SqlExecutor, store c
305327 return err
306328 }
307329
308- if vcsConf .Type != "gerrit" && (notif .Settings .Template .DisableComment == nil || ! * notif .Settings .Template .DisableComment ) {
330+ var changeID string
331+ changeIDParam := sdk .ParameterFind (nodeRun .BuildParameters , "gerrit.change.id" )
332+ if changeIDParam != nil {
333+ changeID = changeIDParam .Value
334+ }
335+
336+ var revision string
337+ revisionParams := sdk .ParameterFind (nodeRun .BuildParameters , "git.hash" )
338+ if revisionParams != nil {
339+ revision = revisionParams .Value
340+ }
341+
342+ reqComment := sdk.VCSPullRequestCommentRequest {Message : report }
343+ reqComment .Revision = revision
344+
345+ // If we are on Gerrit
346+ if changeID != "" && vcsConf .Type == "gerrit" {
347+ reqComment .ChangeID = changeID
348+ if err := client .PullRequestComment (ctx , app .RepositoryFullname , reqComment ); err != nil {
349+ log .Error (ctx , "sendVCSPullRequestComment> unable to send PR report:%v" , err )
350+ return nil
351+ }
352+ } else if vcsConf .Type != "gerrit" {
309353 //Check if this branch and this commit is a pullrequest
310354 prs , err := client .PullRequests (ctx , app .RepositoryFullname )
311355 if err != nil {
@@ -314,16 +358,15 @@ func sendVCSPullRequestComment(ctx context.Context, db gorp.SqlExecutor, store c
314358 }
315359
316360 //Send comment on pull request
317- if nodeRun .Status == sdk .StatusFail || nodeRun .Status == sdk .StatusStopped || notif .Settings .OnSuccess == sdk .UserNotificationAlways {
318- for _ , pr := range prs {
319- if pr .Head .Branch .DisplayID == nodeRun .VCSBranch && pr .Head .Branch .LatestCommit == nodeRun .VCSHash && ! pr .Merged && ! pr .Closed {
320- if err := client .PullRequestComment (ctx , app .RepositoryFullname , pr .ID , report ); err != nil {
321- log .Error (ctx , "sendVCSPullRequestComment> unable to send PR report:%v" , err )
322- return nil
323- }
324- // if we found the pull request for head branch we can break (only one PR for the branch should exist)
325- break
361+ for _ , pr := range prs {
362+ if pr .Head .Branch .DisplayID == nodeRun .VCSBranch && pr .Head .Branch .LatestCommit == nodeRun .VCSHash && ! pr .Merged && ! pr .Closed {
363+ reqComment .ID = pr .ID
364+ if err := client .PullRequestComment (ctx , app .RepositoryFullname , reqComment ); err != nil {
365+ log .Error (ctx , "sendVCSPullRequestComment> unable to send PR report:%v" , err )
366+ return nil
326367 }
368+ // if we found the pull request for head branch we can break (only one PR for the branch should exist)
369+ break
327370 }
328371 }
329372 }
0 commit comments