Skip to content

Commit

Permalink
fix: multiple pipelines execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ferreiratiago committed May 31, 2023
1 parent 2100d93 commit a6d9086
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 55 deletions.
50 changes: 33 additions & 17 deletions engine/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ func ExecConfigurationFile(env *Env, file *ReviewpadFile) (ExitStatus, *Program,

// pipelines should only run on pull requests
if env.TargetEntity.Kind == entities.PullRequest {
// process pipelines
for _, pipeline := range file.Pipelines {
log.Infof("evaluating pipeline '%v':", pipeline.Name)
pipelineLog := log.WithField("pipeline", pipeline.Name)

pipelineLog.Infof("processing pipeline '%v':", pipeline.Name)

var err error
activated := pipeline.Trigger == ""
if !activated {
Expand All @@ -373,24 +373,40 @@ func ExecConfigurationFile(env *Env, file *ReviewpadFile) (ExitStatus, *Program,
}
}

if activated {
for num, stage := range pipeline.Stages {
pipelineLog.Infof("evaluating pipeline stage '%v'", num)
if stage.Until == "" {
program.append(stage.Actions)
retStatus, err := execActions(interpreter, stage.Actions)
return retStatus, program, err
}
if !activated {
pipelineLog.Info("skipping pipeline because the trigger condition was not met")
continue
}

isDone, err := interpreter.EvalExpr("patch", stage.Until)
if err != nil {
return ExitStatusFailure, nil, err
for num, stage := range pipeline.Stages {
pipelineLog.Infof("processing pipeline stage %v", num)

if stage.Until == "" {
pipelineLog.Info("pipeline stage 'until' condition not specified, executing actions")

program.append(stage.Actions)
retStatus, err := execActions(interpreter, stage.Actions)
if err != nil || retStatus == ExitStatusFailure {
return retStatus, nil, err
}

if !isDone {
program.append(stage.Actions)
retStatus, err := execActions(interpreter, stage.Actions)
return retStatus, program, err
break
}

isStageCompleted, err := interpreter.EvalExpr("patch", stage.Until)
if err != nil {
return ExitStatusFailure, nil, err
}

if isStageCompleted {
pipelineLog.Info("pipeline stage 'until' condition was met, skipping stage")
} else {
pipelineLog.Info("pipeline stage 'until' condition was not met, executing actions")

program.append(stage.Actions)
retStatus, err := execActions(interpreter, stage.Actions)
if err != nil || retStatus == ExitStatusFailure {
return retStatus, nil, err
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions engine/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,25 @@ func TestExecConfigurationFile(t *testing.T) {
targetEntity: engine.DefaultMockTargetEntity,
wantExitStatus: engine.ExitStatusSuccess,
},
"reviewpad with pipelines": {
inputReviewpadFilePath: "testdata/exec/reviewpad_with_pipelines.yml",
wantProgram: engine.BuildProgram(
[]*engine.Statement{
engine.BuildStatement(`$addLabel("pipeline with no trigger")`),
engine.BuildStatement(`$addLabel("pipeline with trigger")`),
engine.BuildStatement(`$addLabel("pipeline with single stage and single action")`),
engine.BuildStatement(`$addLabel("pipeline with single stage and multiple actions - 1/2")`),
engine.BuildStatement(`$addLabel("pipeline with single stage and multiple actions - 2/2")`),
engine.BuildStatement(`$addLabel("pipeline with multiple stages - stage 2")`),
engine.BuildStatement(`$addLabel("pipeline with multiple stages and multiple actions - stage 2 - 1/2")`),
engine.BuildStatement(`$addLabel("pipeline with multiple stages and multiple actions - stage 2 - 2/2")`),
engine.BuildStatement(`$addLabel("pipeline with single stage and concise actions")`),
engine.BuildStatement(`$addLabel("pipeline with multiple stages and concise actions - stage 2")`),
},
),
targetEntity: engine.DefaultMockTargetEntity,
wantExitStatus: engine.ExitStatusSuccess,
},
}

codehostClient := aladino.GetDefaultCodeHostClient(t, aladino.GetDefaultPullRequestDetails(), aladino.GetDefaultPullRequestFileList(), nil, nil)
Expand Down
65 changes: 65 additions & 0 deletions engine/testdata/exec/reviewpad_with_pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
# Use of this source code is governed by a license that can be
# found in the LICENSE file.

pipelines:
- name: pipeline with no trigger
stages:
- actions:
- $addLabel("pipeline with no trigger")

- name: pipeline with trigger
trigger: 1 == 1
stages:
- actions:
- $addLabel("pipeline with trigger")

- name: pipeline with failing trigger
trigger: 1 == 2
stages:
- actions:
- $addLabel("pipeline with failing trigger")

- name: pipeline with single stage and single action
trigger: 1 == 1
stages:
- actions:
- $addLabel("pipeline with single stage and single action")

- name: pipeline with single stage and multiple actions
trigger: 1 == 1
stages:
- actions:
- $addLabel("pipeline with single stage and multiple actions - 1/2")
- $addLabel("pipeline with single stage and multiple actions - 2/2")

- name: pipeline with multiple stages
trigger: 1 == 1
stages:
- actions:
- $addLabel("pipeline with multiple stages - stage 1")
until: 1 == 1
- actions:
- $addLabel("pipeline with multiple stages - stage 2")

- name: pipeline with multiple stages and multiple actions
trigger: 1 == 1
stages:
- actions:
- $addLabel("pipeline with multiple stages and multiple actions - stage 1")
until: 1 == 1
- actions:
- $addLabel("pipeline with multiple stages and multiple actions - stage 2 - 1/2")
- $addLabel("pipeline with multiple stages and multiple actions - stage 2 - 2/2")

- name: pipeline with single stage and concise actions
trigger: 1 == 1
stages:
- actions: $addLabel("pipeline with single stage and concise actions")

- name: pipeline with multiple stages and concise actions
trigger: 1 == 1
stages:
- actions: $addLabel("pipeline with multiple stages and concise actions - stage 1")
until: 1 == 1
- actions: $addLabel("pipeline with multiple stages and concise actions - stage 2")
Loading

0 comments on commit a6d9086

Please sign in to comment.