Skip to content

Commit f346408

Browse files
authored
fix(api,vcs): notification set status + add gerrit in vcs notification (#5017)
1 parent 7555bf9 commit f346408

24 files changed

+558
-113
lines changed

engine/api/repositoriesmanager/repositories_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ func (c *vcsClient) PullRequests(ctx context.Context, fullname string) ([]sdk.VC
395395
return prs, nil
396396
}
397397

398-
func (c *vcsClient) PullRequestComment(ctx context.Context, fullname string, id int, body string) error {
399-
path := fmt.Sprintf("/vcs/%s/repos/%s/pullrequests/%d/comments", c.name, fullname, id)
398+
func (c *vcsClient) PullRequestComment(ctx context.Context, fullname string, body sdk.VCSPullRequestCommentRequest) error {
399+
path := fmt.Sprintf("/vcs/%s/repos/%s/pullrequests/comments", c.name, fullname)
400400
if _, err := c.doJSONRequest(ctx, "POST", path, body, nil); err != nil {
401401
return sdk.WrapError(err, "unable to post pullrequest comments on repository %s from %s", fullname, c.name)
402402
}

engine/api/workflow/dao_notification.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,35 +64,12 @@ func loadNotification(db gorp.SqlExecutor, w *sdk.Workflow, id int64) (sdk.Workf
6464
return n, nil
6565
}
6666

67-
func loadVCSNotificationWithNodeID(db gorp.SqlExecutor, workflowID, nodeID int64) (sdk.WorkflowNotification, error) {
68-
dbnotif := Notification{}
69-
query := `SELECT workflow_notification.*
70-
FROM workflow_notification
71-
JOIN workflow_notification_source ON workflow_notification.id = workflow_notification_source.workflow_notification_id
72-
WHERE workflow_notification.workflow_id = $1 AND workflow_notification_source.node_id = $2 AND workflow_notification.type = $3 LIMIT 1`
73-
//Load the notification
74-
if err := db.SelectOne(&dbnotif, query, workflowID, nodeID, sdk.VCSUserNotification); err != nil {
75-
if err == sql.ErrNoRows {
76-
return sdk.WorkflowNotification{}, nil
77-
}
78-
return sdk.WorkflowNotification{}, sdk.WrapError(err, "Unable to load notification for workflow id %d and node id %d", workflowID, nodeID)
79-
}
80-
dbnotif.WorkflowID = workflowID
81-
82-
return sdk.WorkflowNotification(dbnotif), nil
83-
}
84-
8567
func InsertNotification(db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.WorkflowNotification) error {
8668
n.WorkflowID = w.ID
8769
n.ID = 0
8870
n.NodeIDs = nil
8971
dbNotif := Notification(*n)
9072

91-
//Check references to sources
92-
if len(n.SourceNodeRefs) == 0 {
93-
return sdk.WrapError(sdk.ErrWorkflowNodeRef, "insertNotification> No notification references")
94-
}
95-
9673
for _, s := range n.SourceNodeRefs {
9774
nodeFoundRef := w.WorkflowData.NodeByName(s)
9875
if nodeFoundRef == nil || nodeFoundRef.ID == 0 {

engine/api/workflow/workflow_run_event.go

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)