From a4fdb99250f1b172058f90b1c1a70532b1cdc94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20=C5=A0ar=C4=8Devi=C4=87?= Date: Mon, 29 Mar 2021 15:01:37 +0200 Subject: [PATCH] Extract job priorities --- pkg/pipelines/model.go | 4 ---- pkg/pipelines/when_evaluator.go | 28 +++++++++++++++++++++++++- pkg/pipelines/when_evaluator_test.go | 30 ++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/pkg/pipelines/model.go b/pkg/pipelines/model.go index 59e60fe..9465877 100644 --- a/pkg/pipelines/model.go +++ b/pkg/pipelines/model.go @@ -39,10 +39,6 @@ func (p *Pipeline) PathExists(path []string) bool { return p.raw.Exists(path...) } -func (p *Pipeline) GetStringFromPath(path []string) string { - return p.raw.Search(path...).Data().(string) -} - func (p *Pipeline) GlobalPriorityRules() []*gabs.Container { return p.raw.Search("global_job_config", "priority").Children() } diff --git a/pkg/pipelines/when_evaluator.go b/pkg/pipelines/when_evaluator.go index 780831c..bce21b7 100644 --- a/pkg/pipelines/when_evaluator.go +++ b/pkg/pipelines/when_evaluator.go @@ -201,6 +201,27 @@ func (e *whenEvaluator) ExtractFromPriority() { for index := range e.pipeline.GlobalPriorityRules() { e.tryExtractingFromPath([]string{"global_job_config", "priority", strconv.Itoa(index), "when"}) } + + for blockIndex, block := range e.pipeline.Blocks() { + jobs := block.Search("task", "jobs").Children() + + for jobIndex, job := range jobs { + priority := job.Search("priority").Children() + + for priorityIndex := range priority { + e.tryExtractingFromPath([]string{ + "blocks", + strconv.Itoa(blockIndex), + "task", + "jobs", + strconv.Itoa(jobIndex), + "priority", + strconv.Itoa(priorityIndex), + "when", + }) + } + } + } } func (e *whenEvaluator) ExtractFromQueue() { @@ -214,8 +235,13 @@ func (e *whenEvaluator) tryExtractingFromPath(path []string) { return } + value, ok := e.pipeline.raw.Search(path...).Data().(string) + if !ok { + return + } + expression := when.WhenExpression{ - Expression: e.pipeline.GetStringFromPath(path), + Expression: value, Path: path, YamlPath: e.pipeline.yamlPath, } diff --git a/pkg/pipelines/when_evaluator_test.go b/pkg/pipelines/when_evaluator_test.go index c40d034..1cfd1cd 100644 --- a/pkg/pipelines/when_evaluator_test.go +++ b/pkg/pipelines/when_evaluator_test.go @@ -12,8 +12,34 @@ func Test__WhenEvaluatorExtractAll(t *testing.T) { assert.Nil(t, err) e := newWhenEvaluator(pipeline) + e.ExtractAll() - assert.Equal(t, e, []when.WhenExpression{ - {}, + assert.Equal(t, 5, len(e.list)) + assert.Equal(t, e.list, []when.WhenExpression{ + { + Expression: "change_in('lib')", + Path: []string{"blocks", "0", "run", "when"}, + YamlPath: "../../test/fixtures/all_when_locations.yml", + }, + { + Expression: "change_in('lib')", + Path: []string{"blocks", "1", "run", "when"}, + YamlPath: "../../test/fixtures/all_when_locations.yml", + }, + { + Expression: "change_in('lib')", + Path: []string{"promotions", "0", "auto_promote", "when"}, + YamlPath: "../../test/fixtures/all_when_locations.yml", + }, + { + Expression: "branch = 'master'", + Path: []string{"global_job_config", "priority", "0", "when"}, + YamlPath: "../../test/fixtures/all_when_locations.yml", + }, + { + Expression: "branch = 'master'", + Path: []string{"blocks", "1", "task", "jobs", "0", "priority", "0", "when"}, + YamlPath: "../../test/fixtures/all_when_locations.yml", + }, }) }