Skip to content

Commit

Permalink
fix(api): add logs on sendVCSPullRequestComment (#6604)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored Aug 11, 2023
1 parent ef9d317 commit 02afa1b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions engine/api/workflow/workflow_run_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,25 @@ func (e *VCSEventMessenger) sendVCSEventStatus(ctx context.Context, db gorp.SqlE
}

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) {
log.Info(ctx, "Send pull-request comment for node run %d", nodeRun.ID)
if notif.Settings.Template == nil {
log.Info(ctx, "nothing to do: template is empty", nodeRun.ID)
return nil
}
if notif.Settings.Template.DisableComment != nil && *notif.Settings.Template.DisableComment {
log.Info(ctx, "nothing to do: comment are disabled")
return nil
}

if nodeRun.Status != sdk.StatusFail && nodeRun.Status != sdk.StatusStopped && notif.Settings.OnSuccess != sdk.UserNotificationAlways {
log.Info(ctx, "nothing to do: status is %v", nodeRun.Status)
return nil
}

log.Debug(ctx, "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) {
log.Info(ctx, "nothing to do: node is not linked to repo")
return nil
}

Expand All @@ -349,6 +355,7 @@ func (e *VCSEventMessenger) sendVCSPullRequestComment(ctx context.Context, db go

report, err := nodeRun.Report()
if err != nil {
log.ErrorWithStackTrace(ctx, err)
return err
}

Expand All @@ -369,29 +376,36 @@ func (e *VCSEventMessenger) sendVCSPullRequestComment(ctx context.Context, db go

isGerrit, err := e.vcsClient.IsGerrit(ctx, db)
if err != nil {
log.ErrorWithStackTrace(ctx, err)
return err
}

if changeID != "" && isGerrit {
reqComment.ChangeID = changeID
if err := e.vcsClient.PullRequestComment(ctx, app.RepositoryFullname, reqComment); err != nil {
log.ErrorWithStackTrace(ctx, err)
return err
}
} else if !isGerrit {
//Check if this branch and this commit is a pullrequest
prs, err := e.vcsClient.PullRequests(ctx, app.RepositoryFullname)
if err != nil {
log.ErrorWithStackTrace(ctx, err)
return err
}

//Send comment on pull request
for _, pr := range prs {
if pr.Head.Branch.DisplayID == nodeRun.VCSBranch && IsSameCommit(pr.Head.Branch.LatestCommit, nodeRun.VCSHash) && !pr.Merged && !pr.Closed {
reqComment.ID = pr.ID
log.Info(ctx, "send comment (revision: %v pr: %v) on repo %s", reqComment.Revision, reqComment.ID, app.RepositoryFullname)
if err := e.vcsClient.PullRequestComment(ctx, app.RepositoryFullname, reqComment); err != nil {
log.ErrorWithStackTrace(ctx, err)
return err
}
break
} else {
log.Info(ctx, "nothing to do on pr %+v for branch %s", pr, nodeRun.VCSBranch)
}
}
}
Expand Down

0 comments on commit 02afa1b

Please sign in to comment.