Skip to content

Commit

Permalink
fix(api): add conditions handling for outgoing hook (#3754)
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Coenen <benjamin.coenen@corp.ovh.com>
  • Loading branch information
bnjjj authored and yesnault committed Dec 21, 2018
1 parent 8ce1e8b commit feb6221
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion engine/api/workflow/execute_outgoing_hook_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func UpdateOutgoingHookRunStatus(ctx context.Context, db gorp.SqlExecutor, store
mapNodes := wr.Workflow.WorkflowData.Maps()
node := wr.Workflow.WorkflowData.NodeByID(nodeRun.WorkflowNodeID)

report1, err := processNodeOutGoingHook(ctx, db, store, proj, wr, mapNodes, nil, node, int(nodeRun.SubNumber))
report1, _, err := processNodeOutGoingHook(ctx, db, store, proj, wr, mapNodes, nil, node, int(nodeRun.SubNumber))
report.Merge(report1, err) //nolint
if err != nil {
return nil, sdk.WrapError(err, "Unable to processNodeOutGoingHook")
Expand Down
4 changes: 2 additions & 2 deletions engine/api/workflow/process_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ func processNodeRun(ctx context.Context, db gorp.SqlExecutor, store cache.Store,
report.Merge(r1, nil) // nolint
return report, conditionOK, nil
case sdk.NodeTypeOutGoingHook:
r1, errO := processNodeOutGoingHook(ctx, db, store, proj, wr, mapNodes, parentNodeRuns, n, subNumber)
r1, conditionOK, errO := processNodeOutGoingHook(ctx, db, store, proj, wr, mapNodes, parentNodeRuns, n, subNumber)
if errO != nil {
return nil, false, sdk.WrapError(errO, "Unable to processNodeOutGoingHook")
}
report.Merge(r1, nil) // nolint
return report, true, nil
return report, conditionOK, nil
}
return nil, false, nil
}
Expand Down
27 changes: 16 additions & 11 deletions engine/api/workflow/process_outgoinghook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/ovh/cds/sdk/log"
)

func processNodeOutGoingHook(ctx context.Context, db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, wr *sdk.WorkflowRun, mapNodes map[int64]*sdk.Node, parentNodeRun []*sdk.WorkflowNodeRun, node *sdk.Node, subNumber int) (*ProcessorReport, error) {
func processNodeOutGoingHook(ctx context.Context, db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, wr *sdk.WorkflowRun, mapNodes map[int64]*sdk.Node, parentNodeRun []*sdk.WorkflowNodeRun, node *sdk.Node, subNumber int) (*ProcessorReport, bool, error) {
ctx, end := observability.Span(ctx, "workflow.processNodeOutGoingHook")
defer end()

Expand All @@ -34,34 +34,34 @@ func processNodeOutGoingHook(ctx context.Context, db gorp.SqlExecutor, store cac
// If the hookrun is at status terminated, let's trigger outgoing children
if exitingNodeRun != nil && !sdk.StatusIsTerminated(exitingNodeRun.Status) {
log.Debug("hook %d already processed", node.ID)
return nil, nil
return nil, false, nil
} else if exitingNodeRun != nil && exitingNodeRun.Status != sdk.StatusStopped.String() {
log.Debug("hook %d is over, we have to reprocess al the things", node.ID)
for i := range node.Triggers {
t := &node.Triggers[i]
log.Debug("checking trigger %+v", t)
r1, err := processNodeTriggers(ctx, db, store, proj, wr, mapNodes, []*sdk.WorkflowNodeRun{exitingNodeRun}, node, subNumber)
if err != nil {
return nil, sdk.WrapError(err, "Unable to process outgoing hook triggers")
return nil, false, sdk.WrapError(err, "Unable to process outgoing hook triggers")
}
report.Merge(r1, nil) // nolint
}
return report, nil
return report, false, nil
} else if exitingNodeRun != nil && exitingNodeRun.Status == sdk.StatusStopped.String() {
return report, nil
return report, false, nil
}
}

//FIX: For the moment, we trigger outgoing hooks on success
for _, p := range parentNodeRun {
if p.Status != sdk.StatusSuccess.String() {
return report, nil
return report, false, nil
}
}

srvs, err := services.FindByType(db, services.TypeHooks)
if err != nil {
return nil, sdk.WrapError(err, "Cannot get hooks service")
return nil, false, sdk.WrapError(err, "Cannot get hooks service")
}

mapParams := map[string]string{}
Expand Down Expand Up @@ -101,6 +101,11 @@ func processNodeOutGoingHook(ctx context.Context, db gorp.SqlExecutor, store cac
OutgoingHook: node.OutGoingHookContext,
}

if !checkNodeRunCondition(wr, node.Context.Conditions, hookRun.BuildParameters) {
log.Debug("Condition failed %d/%d %+v", wr.ID, node.ID, hookRun.BuildParameters)
return report, false, nil
}

var task sdk.Task
if _, err := services.DoJSONRequest(ctx, srvs, "POST", "/task/execute", hookRun, &task); err != nil {
log.Warning("outgoing hook execution failed: %v", err)
Expand All @@ -113,7 +118,7 @@ func processNodeOutGoingHook(ctx context.Context, db gorp.SqlExecutor, store cac
}

if err := insertWorkflowNodeRun(db, &hookRun); err != nil {
return nil, sdk.WrapError(err, "unable to insert run (node id : %d, node name : %s, subnumber : %d)", hookRun.WorkflowNodeID, hookRun.WorkflowNodeName, hookRun.SubNumber)
return nil, true, sdk.WrapError(err, "unable to insert run (node id : %d, node name : %s, subnumber : %d)", hookRun.WorkflowNodeID, hookRun.WorkflowNodeName, hookRun.SubNumber)
}
wr.LastExecution = time.Now()

Expand All @@ -125,7 +130,7 @@ func processNodeOutGoingHook(ctx context.Context, db gorp.SqlExecutor, store cac
}

if err := UpdateNodeRunBuildParameters(db, hookRun.ID, hookRun.BuildParameters); err != nil {
return nil, sdk.WrapError(err, "unable to update workflow node run build parameters")
return nil, true, sdk.WrapError(err, "unable to update workflow node run build parameters")
}
}

Expand All @@ -147,8 +152,8 @@ func processNodeOutGoingHook(ctx context.Context, db gorp.SqlExecutor, store cac
wr.LastSubNumber = MaxSubNumber(wr.WorkflowNodeRuns)

if err := UpdateWorkflowRun(ctx, db, wr); err != nil {
return nil, sdk.WrapError(err, "unable to update workflow run")
return nil, true, sdk.WrapError(err, "unable to update workflow run")
}

return report, nil
return report, true, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class WorkflowTriggerComponent {
this.selectedType = t;
this.isParent = isP;
const config = new TemplateModalConfig<boolean, boolean, void>(this.triggerModal);
config.mustScroll = true;
this.modal = this._modalService.open(config);
}

Expand Down

0 comments on commit feb6221

Please sign in to comment.