Skip to content

Commit

Permalink
circleci: rename inputs variable
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Dec 25, 2024
1 parent 0b962e3 commit 9835c48
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
11 changes: 8 additions & 3 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,18 +1240,23 @@ func RenderTemplate(name string, data any) (string, error) {
return buf.String(), nil
}

// toEnvVar converts the given string to an environment variable name.
func toEnvVar(s string) string {
// toEnvName converts the given string to an environment variable name.
func toEnvName(s string) string {
return strings.ToUpper(strings.NewReplacer(
" ", "_", "-", "_", "/", "_",
).Replace(s))
}

// toInputVarName converts the given string to an input variable name.
func toInputVarName(input string) string {
return fmt.Sprintf("ATLAS_INPUT_%s", toEnvName(input))
}

// toOutputVar converts the given values to an output variable.
// The action and output are used to create the output variable name with the format:
// ATLAS_OUTPUT_<ACTION>_<OUTPUT>="<value>"
func toOutputVar(action, output, value string) string {
return fmt.Sprintf("ATLAS_OUTPUT_%s=%q", toEnvVar(action+"_"+output), value)
return fmt.Sprintf("ATLAS_OUTPUT_%s=%q", toEnvName(action+"_"+output), value)
}

// fprintln writes the given values to the file using fmt.Fprintln.
Expand Down
7 changes: 4 additions & 3 deletions atlasaction/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (a *bbPipe) GetTriggerContext(context.Context) (*TriggerContext, error) {

// GetInput implements the Action interface.
func (a *bbPipe) GetInput(name string) string {
return strings.TrimSpace(a.getenv("ATLAS_INPUT_" + toEnvVar(name)))
return strings.TrimSpace(a.getenv(toInputVarName(name)))
}

// SetOutput implements Action.
Expand All @@ -104,10 +104,11 @@ func (a *bbPipe) SetOutput(name, value string) {
a.Errorf("failed to create output directory %s: %v", dir, err)
return
}
err := fprintln(filepath.Join(dir, "outputs.sh"),
outputs := filepath.Join(dir, "outputs.sh")
err := fprintln(outputs,
"export", toOutputVar(a.getenv("ATLAS_ACTION_COMMAND"), name, value))
if err != nil {
a.Errorf("failed to write output to file %s: %v", dir, err)
a.Errorf("failed to write output to file %s: %v", outputs, err)
}
}

Expand Down
10 changes: 7 additions & 3 deletions atlasaction/circleci_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func (a *circleCIOrb) Getenv(key string) string {

// GetInput implements the Action interface.
func (a *circleCIOrb) GetInput(name string) string {
return strings.TrimSpace(a.getenv(toEnvVar("INPUT_" + name)))
v := a.getenv(toInputVarName(name))
if v == "" {
// TODO: Remove this fallback once all the actions are updated.
v = a.getenv(toEnvName("INPUT_" + name))
}
return strings.TrimSpace(v)
}

// SetOutput implements the Action interface.
Expand All @@ -47,9 +52,8 @@ func (a *circleCIOrb) SetOutput(name, value string) {
err := fprintln(bashEnv,
"export", toOutputVar(a.getenv("ATLAS_ACTION_COMMAND"), name, value))
if err != nil {
a.Fatalf("failed to write env to file %s: %v", bashEnv, err)
a.Fatalf("failed to write output to file %s: %v", bashEnv, err)
}
return
}
}

Expand Down
4 changes: 2 additions & 2 deletions atlasaction/circleci_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Test_circleCIOrb_GetTriggerContext(t *testing.T) {
func TestCircleCI(t *testing.T) {
var (
actions = "actions"
output = filepath.Join(actions, "output.txt")
output = filepath.Join(actions, "output.sh")
)
wd, err := os.Getwd()
require.NoError(t, err)
Expand All @@ -75,7 +75,7 @@ func TestCircleCI(t *testing.T) {
e.Setenv("CIRCLECI", "true")
e.Setenv("CIRCLE_PROJECT_REPONAME", "atlas-orb")
e.Setenv("CIRCLE_SHA1", "1234567890")
e.Setenv("BASH_ENV", filepath.Join(dir, "output.txt"))
e.Setenv("BASH_ENV", filepath.Join(dir, "output.sh"))
return nil
},
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
Expand Down
2 changes: 1 addition & 1 deletion atlasaction/gitlab_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (a *gitlabCI) Getenv(key string) string {

// GetInput implements the Action interface.
func (a *gitlabCI) GetInput(name string) string {
return strings.TrimSpace(a.getenv(toEnvVar("ATLAS_INPUT_" + name)))
return strings.TrimSpace(a.getenv(toInputVarName(name)))
}

// SetOutput implements the Action interface.
Expand Down
24 changes: 12 additions & 12 deletions atlasaction/testdata/circleci/migrate-lint.txtar
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Mock the atlas command outputs
env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./migrate-lint
# Setup the action input variables
env INPUT_CONFIG=file://testdata/config/atlas.hcl
env INPUT_ENV=test
env INPUT_DIR_NAME=pupisu
env INPUT_TAG=staging
env INPUT_VARS='{"var1":"value1","var2":"value2"}'
env INPUT_DIR=file://testdata/migrations
env INPUT_DEV_URL=sqlite://file?mode=memory
env INPUT_RUN=example
env ATLAS_INPUT_CONFIG=file://testdata/config/atlas.hcl
env ATLAS_INPUT_ENV=test
env ATLAS_INPUT_DIR_NAME=pupisu
env ATLAS_INPUT_TAG=staging
env ATLAS_INPUT_VARS='{"var1":"value1","var2":"value2"}'
env ATLAS_INPUT_DIR=file://testdata/migrations
env ATLAS_INPUT_DEV_URL=sqlite://file?mode=memory
env ATLAS_INPUT_RUN=example

# The action's output should append the existing outputs
cp output-pre.txt actions/output.txt
cp output-pre.sh actions/output.sh
atlas-action --action=migrate/lint
output output.txt
output output.sh

-- migrate-lint/1/args --
migrate lint -w --context {"repo":"atlas-orb","path":"file://testdata/migrations","commit":"1234567890"} --env test --config file://testdata/config/atlas.hcl --dev-url sqlite://file?mode=memory --dir file://testdata/migrations --base atlas://pupisu?tag=staging --var var1=value1 --var var2=value2 --format {{ json . }}
-- migrate-lint/1/stdout --
{"URL":"https://migration-lint-report-url"}
-- output-pre.txt --
-- output-pre.sh --
export FOO=bar
-- output.txt --
-- output.sh --
export FOO=bar
export ATLAS_OUTPUT_MIGRATE_LINT_REPORT_URL="https://migration-lint-report-url"

0 comments on commit 9835c48

Please sign in to comment.