Skip to content

Commit

Permalink
fix(api,vcs): multiple vcs notifications (#6390)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Dec 15, 2022
1 parent b1d7fe1 commit 6a166b7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion engine/api/repositoriesmanager/repositories_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func (c *vcsClient) Branch(ctx context.Context, fullname string, filters sdk.VCS
branch := sdk.VCSBranch{}
path := fmt.Sprintf("/vcs/%s/repos/%s/branches/?branch=%s&default=%v", c.name, fullname, url.QueryEscape(filters.BranchName), filters.Default)
if _, err := c.doJSONRequest(ctx, "GET", path, nil, &branch); err != nil {
return nil, sdk.NewErrorFrom(err, "unable to find branch %s/%v on repository %s from %s", filters.BranchName, filters.Default, fullname, c.name)
return nil, sdk.NewErrorFrom(err, "unable to find branch %q (default: %t) on repository %s from %s", filters.BranchName, filters.Default, fullname, c.name)
}
return &branch, nil
}
Expand Down
39 changes: 22 additions & 17 deletions engine/api/workflow/workflow_run_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,27 @@ func (e *VCSEventMessenger) SendVCSEvent(ctx context.Context, db *gorp.DbMap, st
return nil
}

var notif *sdk.WorkflowNotification
var notifs []sdk.WorkflowNotification
// browse notification to find vcs one
loopNotif:
for _, n := range wr.Workflow.Notifications {
if n.Type != sdk.VCSUserNotification {
continue
}
// If list of node is nill, send notification to all of them
if len(n.NodeIDs) == 0 {
notif = &n
break
notifs = append(notifs, n)
continue
}
// browser source node id
for _, src := range n.NodeIDs {
if src == node.ID {
notif = &n
break loopNotif
notifs = append(notifs, n)
break
}
}
}

if notif == nil {
if len(notifs) == 0 {
return nil
}

Expand Down Expand Up @@ -134,8 +133,10 @@ loopNotif:
}

if statusFound == nil || statusFound.State == "" {
if err := e.sendVCSEventStatus(ctx, tx, store, proj.Key, wr, &nodeRun, notif, vcsServerName); err != nil {
return sdk.WrapError(err, "can't sendVCSEventStatus vcs %v/%v", proj.Key, vcsServerName)
for i := range notifs {
if err := e.sendVCSEventStatus(ctx, tx, store, proj.Key, wr, &nodeRun, notifs[i], vcsServerName); err != nil {
return sdk.WrapError(err, "can't sendVCSEventStatus vcs %v/%v", proj.Key, vcsServerName)
}
}
} else {
skipStatus := false
Expand All @@ -159,17 +160,21 @@ loopNotif:
}

if !skipStatus {
if err := e.sendVCSEventStatus(ctx, tx, store, proj.Key, wr, &nodeRun, notif, vcsServerName); err != nil {
return sdk.WrapError(err, "can't sendVCSEventStatus vcs %v/%v", proj.Key, vcsServerName)
for i := range notifs {
if err := e.sendVCSEventStatus(ctx, tx, store, proj.Key, wr, &nodeRun, notifs[i], vcsServerName); err != nil {
return sdk.WrapError(err, "can't sendVCSEventStatus vcs %v/%v", proj.Key, vcsServerName)
}
}
}
}

if !sdk.StatusIsTerminated(nodeRun.Status) {
return nil
}
if err := e.sendVCSPullRequestComment(ctx, tx, wr, &nodeRun, notif, vcsServerName); err != nil {
return sdk.WrapError(err, "can't sendVCSPullRequestComment vcs %v/%v", proj.Key, vcsServerName)
for i := range notifs {
if err := e.sendVCSPullRequestComment(ctx, tx, wr, &nodeRun, notifs[i], vcsServerName); err != nil {
return sdk.WrapError(err, "can't sendVCSPullRequestComment vcs %v/%v", proj.Key, vcsServerName)
}
}

if err := tx.Commit(); err != nil {
Expand All @@ -180,8 +185,8 @@ loopNotif:
}

// sendVCSEventStatus send status
func (e *VCSEventMessenger) sendVCSEventStatus(ctx context.Context, db gorp.SqlExecutor, store cache.Store, projectKey string, wr sdk.WorkflowRun, nodeRun *sdk.WorkflowNodeRun, notif *sdk.WorkflowNotification, vcsServerName string) error {
if notif == nil || notif.Settings.Template == nil || (notif.Settings.Template.DisableStatus != nil && *notif.Settings.Template.DisableStatus) {
func (e *VCSEventMessenger) sendVCSEventStatus(ctx context.Context, db gorp.SqlExecutor, store cache.Store, projectKey string, wr sdk.WorkflowRun, nodeRun *sdk.WorkflowNodeRun, notif sdk.WorkflowNotification, vcsServerName string) error {
if notif.Settings.Template == nil || (notif.Settings.Template.DisableStatus != nil && *notif.Settings.Template.DisableStatus) {
return nil
}

Expand Down Expand Up @@ -305,8 +310,8 @@ func (e *VCSEventMessenger) sendVCSEventStatus(ctx context.Context, db gorp.SqlE
return nil
}

func (e *VCSEventMessenger) sendVCSPullRequestComment(ctx context.Context, db gorp.SqlExecutor, wr sdk.WorkflowRun, nodeRun *sdk.WorkflowNodeRun, notif *sdk.WorkflowNotification, vcsServerName string) error {
if notif == nil || notif.Settings.Template == nil || (notif.Settings.Template.DisableComment != nil && *notif.Settings.Template.DisableComment) {
func (e *VCSEventMessenger) sendVCSPullRequestComment(ctx context.Context, db gorp.SqlExecutor, wr sdk.WorkflowRun, nodeRun *sdk.WorkflowNodeRun, notif sdk.WorkflowNotification, vcsServerName string) error {
if notif.Settings.Template == nil || (notif.Settings.Template.DisableComment != nil && *notif.Settings.Template.DisableComment) {
return nil
}

Expand Down
21 changes: 18 additions & 3 deletions engine/vcs/gitlab/client_pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package gitlab

import (
"context"
"fmt"
"strconv"

"github.com/rockbears/log"
"github.com/xanzy/go-gitlab"

"github.com/ovh/cds/sdk"
Expand Down Expand Up @@ -41,13 +43,27 @@ func (c *gitlabClient) PullRequests(ctx context.Context, repo string, opts sdk.V
}
res := make([]sdk.VCSPullRequest, 0, len(mrs))
for i := range mrs {
res = append(res, toSDKPullRequest(repo, *mrs[i]))
mr, err := c.PullRequest(ctx, repo, fmt.Sprintf("%d", mrs[i].IID))
if err != nil {
return nil, sdk.WithStack(err)
}
res = append(res, mr)
}
return res, nil
}

// PullRequestComment push a new comment on a pull request
func (c *gitlabClient) PullRequestComment(context.Context, string, sdk.VCSPullRequestCommentRequest) error {
func (c *gitlabClient) PullRequestComment(ctx context.Context, repo string, prReq sdk.VCSPullRequestCommentRequest) error {
if c.disableStatus {
log.Warn(ctx, "gitlab.PullRequestComment> ⚠ Gitlab statuses are disabled")
return nil
}
_, _, err := c.client.Notes.CreateMergeRequestNote(repo, prReq.ID, &gitlab.CreateMergeRequestNoteOptions{
Body: gitlab.String(prReq.Message),
})
if err != nil {
return sdk.WithStack(err)
}
return nil
}

Expand Down Expand Up @@ -93,5 +109,4 @@ func toSDKPullRequest(repo string, mr gitlab.MergeRequest) sdk.VCSPullRequest {
pr.Updated = *mr.UpdatedAt
}
return pr

}
8 changes: 8 additions & 0 deletions ui/proxy.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
"^/cdscdn": ""
}
},
"/cdshooks": {
"target": "http://localhost:8083",
"secure": false,
"ws": true,
"pathRewrite": {
"^/cdshooks": ""
}
},
"/mon/version": {
"target": "http://localhost:8080",
"secure": false,
Expand Down

0 comments on commit 6a166b7

Please sign in to comment.