Skip to content

Commit

Permalink
Extract job priorities
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyasha committed Mar 29, 2021
1 parent 6ae569b commit a4fdb99
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
4 changes: 0 additions & 4 deletions pkg/pipelines/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
28 changes: 27 additions & 1 deletion pkg/pipelines/when_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
}
Expand Down
30 changes: 28 additions & 2 deletions pkg/pipelines/when_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
})
}

0 comments on commit a4fdb99

Please sign in to comment.