Skip to content

Commit 61ad5cb

Browse files
committed
Added support for fetching branch name from app definition
1 parent 003f5df commit 61ad5cb

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

internal/app/apptype/builtins.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,19 +444,36 @@ func createAPIBuiltin(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tup
444444
}
445445

446446
func CreateConfigBuiltin(nodeConfig types.NodeConfig, allowedEnv []string) func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
447-
return func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
448-
var input starlark.String
447+
return func(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
448+
var key starlark.String
449449
var defaultVal starlark.Value
450-
if err := starlark.UnpackArgs(CONFIG, args, kwargs, "input", &input, "default?", &defaultVal); err != nil {
450+
if err := starlark.UnpackArgs(CONFIG, args, kwargs, "key", &key, "default", &defaultVal); err != nil {
451451
return nil, fmt.Errorf("error unpacking config args: %w", err)
452452
}
453453
var err error
454454
var outVal starlark.Value = defaultVal
455-
localVal, ok := nodeConfig[string(input)]
455+
456+
if key == "_branch" {
457+
// _branch is a special input that returns the current branch name
458+
branch := thread.Local(types.TL_BRANCH)
459+
if branch == nil {
460+
return defaultVal, nil
461+
}
462+
branchStr, ok := branch.(string)
463+
if !ok {
464+
return nil, fmt.Errorf("branch is not a string %v", branch)
465+
}
466+
if branchStr == "" {
467+
return defaultVal, nil
468+
}
469+
return starlark.String(branchStr), nil
470+
}
471+
472+
localVal, ok := nodeConfig[string(key)]
456473
if ok {
457474
outVal, err = starlark_type.MarshalStarlark(localVal)
458475
if err != nil {
459-
return nil, fmt.Errorf("error marshalling config value for %s: %w", input, err)
476+
return nil, fmt.Errorf("error marshalling config value for %s: %w", key, err)
460477
}
461478
}
462479
if strVal, ok := outVal.(starlark.String); ok {

internal/server/apply.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
APP = "app"
3030
)
3131

32-
func (s *Server) loadApplyInfo(fileName string, data []byte) ([]*types.CreateAppRequest, error) {
32+
func (s *Server) loadApplyInfo(fileName string, data []byte, branch string) ([]*types.CreateAppRequest, error) {
3333
appDefs := make([]*starlarkstruct.Struct, 0)
3434

3535
createAppBuiltin := func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
@@ -81,6 +81,8 @@ func (s *Server) loadApplyInfo(fileName string, data []byte) ([]*types.CreateApp
8181
Print: func(_ *starlark.Thread, msg string) { s.Info().Msg(msg) },
8282
}
8383

84+
thread.SetLocal(types.TL_BRANCH, branch)
85+
8486
options := syntax.FileOptions{}
8587
_, err := starlark.ExecFileOptions(&options, thread, fileName, data, builtins)
8688
if err != nil {
@@ -273,6 +275,10 @@ func (s *Server) Apply(ctx context.Context, inputTx types.Transaction, applyPath
273275
return nil, nil, err
274276
}
275277

278+
if !system.IsGit(applyPath) {
279+
branch = ""
280+
}
281+
276282
if len(globFiles) == 0 {
277283
return nil, nil, fmt.Errorf("no matching files found in %s", applyPath)
278284
}
@@ -283,7 +289,7 @@ func (s *Server) Apply(ctx context.Context, inputTx types.Transaction, applyPath
283289
return nil, nil, fmt.Errorf("error reading file %s: %w", f, err)
284290
}
285291

286-
fileConfig, err := s.loadApplyInfo(f, fileBytes)
292+
fileConfig, err := s.loadApplyInfo(f, fileBytes, branch)
287293
if err != nil {
288294
return nil, nil, err
289295
}

internal/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const (
4949
TL_AUDIT_TARGET = "TL_audit_target"
5050
TL_AUDIT_DETAIL = "TL_audit_detail"
5151
TL_CONTAINER_MANAGER = "TL_container_manager"
52+
TL_BRANCH = "TL_branch"
5253
)
5354

5455
const (

tests/commander/test_apply_git.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ tests:
4444
command: ../clace apply github.com/claceio/clace/tests/apply_files/apply_invalid.ace /applytest/test2
4545
stderr: "no matching files found in"
4646
exit-code: 1
47+
48+
apply_git0060: ## create apps without branch
49+
command: ../clace apply github.com/claceio/clace/tests/apply_files/apply_git_config.ace
50+
stdout: "1 app(s) created, 0 app(s) updated, 0 app(s) reloaded, 0 app(s) skipped, 0 app(s) approved, 0 app(s) promoted."
51+
apply_git0061: ## create apps with branch
52+
command: ../clace apply --branch main github.com/claceio/clace/tests/apply_files/apply_git_config.ace
53+
stdout: "0 app(s) created, 0 app(s) updated, 0 app(s) reloaded, 1 app(s) skipped, 0 app(s) approved, 0 app(s) promoted."
54+
apply_git0062: ## create apps with branch which is invalid for app
55+
command: ../clace apply --branch test github.com/claceio/clace/tests/apply_files/apply_git_config.ace
56+
stderr: "error checking out branch test"
57+
exit-code: 1
58+
apply_git0063: ## create apps without git
59+
command: ../clace apply ./apply_files/apply_git_config.ace
60+
stdout: "0 app(s) created, 0 app(s) updated, 0 app(s) reloaded, 1 app(s) skipped, 0 app(s) approved, 0 app(s) promoted."

0 commit comments

Comments
 (0)