Skip to content

Commit

Permalink
EvaluateRequest: support new file system abstraction (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev committed Jun 8, 2023
1 parent 91e894e commit 08c69fa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 46 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/breml/rootcerts v0.2.11
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/cirruslabs/cirrus-ci-agent v1.108.0
github.com/cirruslabs/cirrus-ci-agent v1.111.0
github.com/cirruslabs/echelon v1.9.0
github.com/cirruslabs/go-java-glob v0.1.0
github.com/cirruslabs/podmanapi v0.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cirruslabs/cirrus-ci-agent v1.108.0 h1:/G4gZaHHjC13LY1EmOJBuOq2C1t7CnnMvwc2jFIbx5Q=
github.com/cirruslabs/cirrus-ci-agent v1.108.0/go.mod h1:wXwXeQknxQhVgr96wFL/m1WWeoDIItSfUM6osyuNk/c=
github.com/cirruslabs/cirrus-ci-agent v1.111.0 h1:dqNST7EG2PunDp3XGd1s0w1Kkukg8xoK0G7Mjv7YgYU=
github.com/cirruslabs/cirrus-ci-agent v1.111.0/go.mod h1:wXwXeQknxQhVgr96wFL/m1WWeoDIItSfUM6osyuNk/c=
github.com/cirruslabs/echelon v1.9.0 h1:UtHAtoc+C7KZoYtbMCOL8JYA1Ndi6/4u0+gWxw9sB8I=
github.com/cirruslabs/echelon v1.9.0/go.mod h1:Zk4IGe8upeefcwPGE143JCM7iwOkJnZ2ZFCbDWwoadA=
github.com/cirruslabs/go-java-glob v0.1.0 h1:qC4Fzrq2sXKTLLsBb7ih7BNdCjCR6ZJAAK4nOZ6d/Ak=
Expand Down
62 changes: 19 additions & 43 deletions internal/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import (
const pathYAML = ".cirrus.yml"
const pathStarlark = ".cirrus.star"

var ErrGitHubFromEnvFailed = errors.New("got cirrus.fs call, but GitHub filesystem failed " +
"to initialize from environment")
var ErrNoFS = errors.New("no filesystem available")

type ConfigurationEvaluatorServiceServer struct {
// must be embedded to have forward compatible implementations
Expand Down Expand Up @@ -78,32 +77,6 @@ func Serve(ctx context.Context, lis net.Listener) error {
}
}

func fsFromEnvironment(env map[string]string) (fs fs.FileSystem, err error) {
// Fallback to failing filesystem implementation that will fail the evaluation only
// if cirrus.fs is used
fs = failing.New(ErrGitHubFromEnvFailed)

// Use GitHub filesystem if all the required variables are present
owner, ok := env["CIRRUS_REPO_OWNER"]
if !ok {
return
}
repo, ok := env["CIRRUS_REPO_NAME"]
if !ok {
return
}
reference, ok := env["CIRRUS_CHANGE_IN_REPO"]
if !ok {
return
}
token, ok := env["CIRRUS_REPO_CLONE_TOKEN"]
if !ok {
return
}

return github.New(owner, repo, reference, token)
}

func (r *ConfigurationEvaluatorServiceServer) EvaluateConfig(
ctx context.Context,
request *api.EvaluateConfigRequest,
Expand All @@ -117,21 +90,7 @@ func (r *ConfigurationEvaluatorServiceServer) EvaluateConfig(
yamlConfigs = append(yamlConfigs, request.YamlConfig)
}

var fs fs.FileSystem
var err error

if len(request.FilesContents) != 0 {
// memory.New() expects a map with []byte
// values instead of string values
filesContents := map[string][]byte{}
for key, value := range request.FilesContents {
filesContents[key] = []byte(value)
}

fs, err = memory.New(filesContents)
} else {
fs, err = fsFromEnvironment(request.Environment)
}
fs, err := convertFS(request.Fs)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to initialize file system: %v", err)
}
Expand Down Expand Up @@ -306,3 +265,20 @@ func TransformAdditionalInstances(

return additionalInstances, nil
}

func convertFS(apiFS *api.FileSystem) (fs fs.FileSystem, err error) {
fs = failing.New(ErrNoFS)

if apiFS == nil {
return fs, err
}

switch impl := apiFS.Impl.(type) {
case *api.FileSystem_Memory_:
fs, err = memory.New(impl.Memory.FilesContents)
case *api.FileSystem_Github_:
fs, err = github.New(impl.Github.Owner, impl.Github.Repo, impl.Github.Reference, impl.Github.Token)
}

return fs, err
}

0 comments on commit 08c69fa

Please sign in to comment.