Skip to content

Commit

Permalink
fix(api): send vcs comment when needed (#4649)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjjj authored and fsamin committed Oct 16, 2019
1 parent c6e9533 commit be42543
Showing 1 changed file with 70 additions and 48 deletions.
118 changes: 70 additions & 48 deletions engine/api/workflow/workflow_run_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ func ResyncCommitStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
defer end()

for nodeID, nodeRuns := range wr.WorkflowNodeRuns {

sort.Slice(nodeRuns, func(i, j int) bool {
return nodeRuns[i].SubNumber >= nodeRuns[j].SubNumber
})

nodeRun := nodeRuns[0]

if !sdk.StatusIsTerminated(nodeRun.Status) {
Expand Down Expand Up @@ -134,55 +132,43 @@ func ResyncCommitStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
if err := sendVCSEventStatus(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending status %s err: %v", details, err)
}
continue
}

if statusFound.State == sdk.StatusBuilding.String() {
if err := sendVCSEventStatus(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending status %s err: %v", details, err)
if err := sendVCSPullRequestComment(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending pr comments %s %s err:%v", statusFound.State, details, err)
}
continue
}

skipStatus := false
switch statusFound.State {
case sdk.StatusBuilding.String():
if err := sendVCSEventStatus(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending status %s %s err:%v", statusFound.State, details, err)
}
continue

case sdk.StatusSuccess.String():
switch nodeRun.Status {
case sdk.StatusSuccess.String():
continue
default:
if err := sendVCSEventStatus(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending status %s %s err:%v", statusFound.State, details, err)
}
continue
skipStatus = true
}
case sdk.StatusFail.String():
switch nodeRun.Status {
case sdk.StatusFail.String():
continue
default:
if err := sendVCSEventStatus(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending status %s %s err:%v", statusFound.State, details, err)
}
continue
skipStatus = true
}

case sdk.StatusSkipped.String():
switch nodeRun.Status {
case sdk.StatusDisabled.String(), sdk.StatusNeverBuilt.String(), sdk.StatusSkipped.String():
continue
default:
if err := sendVCSEventStatus(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending status %s %s err:%v", statusFound.State, details, err)
}
continue
skipStatus = true
}
}

if !skipStatus {
if err := sendVCSEventStatus(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending status %s %s err:%v", statusFound.State, details, err)
}
}

if err := sendVCSPullRequestComment(ctx, db, store, proj, wr, &nodeRun); err != nil {
log.Error("resyncCommitStatus> Error sending pr comments %s %s err:%v", statusFound.State, details, err)
}

}
return nil
}
Expand All @@ -198,20 +184,6 @@ func sendVCSEventStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
return nil
}

notif, errN := loadVCSNotificationWithNodeID(db, wr.WorkflowID, node.ID)
if errN != nil {
return sdk.WrapError(errN, "cannot load notification")
}

// vcs notification not enabled
if notif.ID == 0 {
return nil
}

if nodeRun.VCSReport == "" {
nodeRun.VCSReport = notif.Settings.Template.Body
}

app = wr.Workflow.Applications[node.Context.ApplicationID]
if node.Context.PipelineID > 0 {
pip = wr.Workflow.Pipelines[node.Context.PipelineID]
Expand Down Expand Up @@ -326,14 +298,64 @@ func sendVCSEventStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
if err2 := repositoriesmanager.RetryEvent(&evt, err, store); err2 != nil {
log.Error("sendEvent>processEvent> err while retry event: %v", err2)
}
return fmt.Errorf("sendEvent> err:%v", err)
log.Error("sendEvent> err:%v", err)
}

return nil
}

func sendVCSPullRequestComment(ctx context.Context, db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, wr *sdk.WorkflowRun, nodeRun *sdk.WorkflowNodeRun) error {
log.Debug("Send pull-request comment for node run %d", nodeRun.ID)

var app sdk.Application
node := wr.Workflow.WorkflowData.NodeByID(nodeRun.WorkflowNodeID)
if !node.IsLinkedToRepo(&wr.Workflow) {
return nil
}
notif, errN := loadVCSNotificationWithNodeID(db, wr.WorkflowID, node.ID)
if errN != nil {
return sdk.WrapError(errN, "cannot load notification")
}

// vcs notification not enabled
if notif.ID == 0 {
return nil
}

if nodeRun.VCSReport == "" {
nodeRun.VCSReport = notif.Settings.Template.Body
}

app = wr.Workflow.Applications[node.Context.ApplicationID]

report, err := nodeRun.Report()
if err != nil {
log.Error("sendVCSPullRequestComment> unable to compute node run report%v", err)
return nil
}

vcsServer := repositoriesmanager.GetProjectVCSServer(proj, app.VCSServer)
if vcsServer == nil {
return nil
}

//Get the RepositoriesManager Client
client, errClient := repositoriesmanager.AuthorizedClient(ctx, db, store, proj.Key, vcsServer)
if errClient != nil {
return sdk.WrapError(errClient, "sendVCSPullRequestComment> Cannot get client")
}

// Check if it's a gerrit or not
vcsConf, err := repositoriesmanager.LoadByName(ctx, db, vcsServer.Name)
if err != nil {
return err
}

if vcsConf.Type != "gerrit" && (notif.Settings.Template.DisableComment == nil || !*notif.Settings.Template.DisableComment) {
//Check if this branch and this commit is a pullrequest
prs, err := client.PullRequests(ctx, app.RepositoryFullname)
if err != nil {
log.Error("sendVCSEventStatus> unable to get pull requests on repo %s: %v", app.RepositoryFullname, err)
log.Error("sendVCSPullRequestComment> unable to get pull requests on repo %s: %v", app.RepositoryFullname, err)
return nil
}

Expand All @@ -342,7 +364,7 @@ func sendVCSEventStatus(ctx context.Context, db gorp.SqlExecutor, store cache.St
for _, pr := range prs {
if pr.Head.Branch.DisplayID == nodeRun.VCSBranch && pr.Head.Branch.LatestCommit == nodeRun.VCSHash && !pr.Merged && !pr.Closed {
if err := client.PullRequestComment(ctx, app.RepositoryFullname, pr.ID, report); err != nil {
log.Error("sendVCSEventStatus> unable to send PR report:%v", err)
log.Error("sendVCSPullRequestComment> unable to send PR report:%v", err)
return nil
}
// if we found the pull request for head branch we can break (only one PR for the branch should exist)
Expand Down

0 comments on commit be42543

Please sign in to comment.