Skip to content

Commit

Permalink
Revert "Substitute env vars in Docker Images and change_in expressions."
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyasha authored Apr 6, 2021
1 parent 2030ea2 commit 78fa02c
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 335 deletions.
9 changes: 3 additions & 6 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ blocks:
- test/e2e/change_in_excluded_paths.rb
- test/e2e/change_in_glob.rb
- test/e2e/change_in_invalid_when.rb
- test/e2e/change_in_java_vs_javascript_clash.rb
- test/e2e/change_in_large_commit_diff.rb
- test/e2e/change_in_large_commit_diff_on_default_branch.rb
- test/e2e/change_in_missing_branch.rb
- test/e2e/change_in_multiple_functions_in_one_when.rb
- test/e2e/change_in_multiple_paths.rb
- test/e2e/change_in_on_forked_prs.rb
- test/e2e/change_in_on_prs.rb
Expand All @@ -72,8 +68,9 @@ blocks:
- test/e2e/change_in_relative_paths.rb
- test/e2e/change_in_simple.rb
- test/e2e/change_in_with_default_branch.rb
- test/e2e/env_simple.rb
- test/e2e/env_vars_in_docker_images.rb
- test/e2e/change_in_java_vs_javascript_clash.rb
- test/e2e/change_in_large_commit_diff.rb
- test/e2e/change_in_large_commit_diff_on_default_branch.rb
- test/e2e/when_conditions_without_change_in.rb

commands:
Expand Down
3 changes: 0 additions & 3 deletions pkg/cli/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ var evaluateChangeInCmd = &cobra.Command{
ppl, err := pipelines.LoadFromFile(input)
check(err)

err = ppl.SubstituteEnvVarsInDockerImages()
check(err)

err = ppl.EvaluateChangeIns()
check(err)

Expand Down
50 changes: 1 addition & 49 deletions pkg/pipelines/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package pipelines

import (
"encoding/json"
"os"
"strconv"
"time"

gabs "github.com/Jeffail/gabs/v2"
"github.com/ghodss/yaml"
consolelogger "github.com/semaphoreci/spc/pkg/consolelogger"
)

type Pipeline struct {
Expand All @@ -20,61 +17,16 @@ func n() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}

func (p *Pipeline) UpdateString(path []string, value string) error {
func (p *Pipeline) UpdateWhenExpression(path []string, value string) error {
_, err := p.raw.Set(value, path...)

return err
}

func (p *Pipeline) GetStringValueFrom(path []string) (string, bool) {
val, ok := p.raw.Search(path...).Data().(string)
return val, ok
}

func (p *Pipeline) EvaluateChangeIns() error {
return newWhenEvaluator(p).Run()
}

func (p *Pipeline) SubstituteEnvVarsInDockerImages() error {
consolelogger.Info("Expanding environment variables in YAML file")
consolelogger.EmptyLine()

containers := p.raw.Search("agent", "containers").Children()

for containerIndex := range containers {
path := []string{"agent", "containers", strconv.Itoa(containerIndex), "image"}

p.expandEnvIfExists(path)
}

for blockIndex := range p.Blocks() {
path := []string{"blocks", strconv.Itoa(blockIndex), "agent", "containers"}

containers := p.raw.Search(path...).Children()

for containerIndex := range containers {
path := append(path, []string{strconv.Itoa(containerIndex), "image"}...)

p.expandEnvIfExists(path)
}
}

return nil
}

func (p *Pipeline) expandEnvIfExists(path []string) {
if value, ok := p.GetStringValueFrom(path); ok {
newValue := os.ExpandEnv(value)

consolelogger.Infof("Expanding env vars in %+v\n", path)
consolelogger.Infof("Original: '%s'\n", value)
consolelogger.Infof("Expanded: '%s'\n", newValue)
consolelogger.EmptyLine()

p.UpdateString(path, newValue)
}
}

func (p *Pipeline) Blocks() []*gabs.Container {
return p.raw.Search("blocks").Children()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/pipelines/when_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (e *whenEvaluator) Run() error {

func (e *whenEvaluator) updatePipeline() error {
for index := range e.results {
err := e.pipeline.UpdateString(e.list[index].Path, e.results[index])
err := e.pipeline.UpdateWhenExpression(e.list[index].Path, e.results[index])

if err != nil {
return err
Expand Down
36 changes: 0 additions & 36 deletions pkg/when/env/env.go

This file was deleted.

58 changes: 2 additions & 56 deletions pkg/when/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
gabs "github.com/Jeffail/gabs/v2"
consolelogger "github.com/semaphoreci/spc/pkg/consolelogger"
changein "github.com/semaphoreci/spc/pkg/when/changein"
env "github.com/semaphoreci/spc/pkg/when/env"
whencli "github.com/semaphoreci/spc/pkg/when/whencli"
)

Expand All @@ -18,22 +17,7 @@ type WhenExpression struct {

func (w *WhenExpression) Eval() error {
for _, requirment := range w.ListChangeInFunctions(w.Requirments) {
result, err := w.EvaluateChangeInFunction(requirment)
if err != nil {
return err
}

input := map[string]interface{}{}
input["name"] = w.functionName(requirment)
input["params"] = w.functionParams(requirment)
input["result"] = result

w.ReduceInputs.Keywords = map[string]interface{}{}
w.ReduceInputs.Functions = append(w.ReduceInputs.Functions, input)
}

for _, requirment := range w.ListEnvFunctions(w.Requirments) {
result, err := w.EvaluateEnvFunction(requirment)
result, err := w.EvalFunction(requirment)
if err != nil {
return err
}
Expand Down Expand Up @@ -62,18 +46,6 @@ func (w *WhenExpression) ListChangeInFunctions(requirments *gabs.Container) []*g
return result
}

func (w *WhenExpression) ListEnvFunctions(requirments *gabs.Container) []*gabs.Container {
result := []*gabs.Container{}

for _, input := range requirments.Children() {
if w.IsEnvFunction(input) {
result = append(result, input)
}
}

return result
}

func (w *WhenExpression) IsChangeInFunction(input *gabs.Container) bool {
elType := input.Search("type").Data().(string)
if elType != "fun" {
Expand All @@ -87,20 +59,7 @@ func (w *WhenExpression) IsChangeInFunction(input *gabs.Container) bool {
return true
}

func (w *WhenExpression) IsEnvFunction(input *gabs.Container) bool {
elType := input.Search("type").Data().(string)
if elType != "fun" {
return false
}

if w.functionName(input) != "env" {
return false
}

return true
}

func (w *WhenExpression) EvaluateChangeInFunction(input *gabs.Container) (bool, error) {
func (w *WhenExpression) EvalFunction(input *gabs.Container) (bool, error) {
consolelogger.EmptyLine()

consolelogger.Infof("%s(%+v)\n", w.functionName(input), w.functionParams(input))
Expand All @@ -113,19 +72,6 @@ func (w *WhenExpression) EvaluateChangeInFunction(input *gabs.Container) (bool,
return changein.Eval(fun)
}

func (w *WhenExpression) EvaluateEnvFunction(input *gabs.Container) (string, error) {
consolelogger.EmptyLine()

consolelogger.Infof("%s(%+v)\n", w.functionName(input), w.functionParams(input))

fun, err := env.Parse(input)
if err != nil {
return "", err
}

return env.Eval(fun)
}

func (w *WhenExpression) functionName(input *gabs.Container) string {
return input.Search("name").Data().(string)
}
Expand Down
88 changes: 0 additions & 88 deletions test/e2e/env_simple.rb

This file was deleted.

Loading

0 comments on commit 78fa02c

Please sign in to comment.