diff --git a/atlasaction/action.go b/atlasaction/action.go index 957d98bd..9829e550 100644 --- a/atlasaction/action.go +++ b/atlasaction/action.go @@ -1247,19 +1247,23 @@ func toEnvVar(s string) string { ).Replace(s)) } -// writeBashEnv writes the given name and value to the bash environment file. -func writeBashEnv(path, name, value string) error { +// 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__="" +func toOutputVar(action, output, value string) string { + return fmt.Sprintf("ATLAS_OUTPUT_%s=%q", toEnvVar(action+"_"+output), value) +} + +// fprintln writes the given values to the file using fmt.Fprintln. +func fprintln(name string, val ...any) error { // Write the output to a file. - f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + f, err := os.OpenFile(name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { return fmt.Errorf("failed to open file: %w", err) } defer f.Close() - _, err = fmt.Fprintf(f, "export %s=%q\n", name, value) - if err != nil { - return err - } - return nil + _, err = fmt.Fprintln(f, val...) + return err } // commentMarker creates a hidden marker to identify the comment as one created by this action. diff --git a/atlasaction/bitbucket.go b/atlasaction/bitbucket.go index 7026a91e..04b63603 100644 --- a/atlasaction/bitbucket.go +++ b/atlasaction/bitbucket.go @@ -104,9 +104,8 @@ func (a *bbPipe) SetOutput(name, value string) { a.Errorf("failed to create output directory %s: %v", dir, err) return } - cmd := a.getenv("ATLAS_ACTION_COMMAND") - err := writeBashEnv(filepath.Join(dir, "outputs.sh"), toEnvVar( - fmt.Sprintf("ATLAS_OUTPUT_%s_%s", cmd, name)), value) + err := fprintln(filepath.Join(dir, "outputs.sh"), + "export", toOutputVar(a.getenv("ATLAS_ACTION_COMMAND"), name, value)) if err != nil { a.Errorf("failed to write output to file %s: %v", dir, err) } diff --git a/atlasaction/circleci_action.go b/atlasaction/circleci_action.go index ae3e5d55..8bb63f01 100644 --- a/atlasaction/circleci_action.go +++ b/atlasaction/circleci_action.go @@ -44,9 +44,8 @@ func (a *circleCIOrb) GetInput(name string) string { // SetOutput implements the Action interface. func (a *circleCIOrb) SetOutput(name, value string) { if bashEnv := a.getenv("BASH_ENV"); bashEnv != "" { - cmd := a.getenv("ATLAS_ACTION_COMMAND") - err := writeBashEnv(bashEnv, toEnvVar( - fmt.Sprintf("ATLAS_OUTPUT_%s_%s", cmd, name)), value) + 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) } diff --git a/atlasaction/gitlab_ci.go b/atlasaction/gitlab_ci.go index 78dd4c5b..e0152c9e 100644 --- a/atlasaction/gitlab_ci.go +++ b/atlasaction/gitlab_ci.go @@ -8,7 +8,6 @@ import ( "context" "fmt" "io" - "os" "slices" "strconv" "strings" @@ -45,12 +44,10 @@ func (a *gitlabCI) GetInput(name string) string { // SetOutput implements the Action interface. func (a *gitlabCI) SetOutput(name, value string) { - f, err := os.OpenFile(".env", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) + err := fprintln(".env", toOutputVar(a.getenv("ATLAS_ACTION_COMMAND"), name, value)) if err != nil { - return + a.Errorf("failed to write output to file .env: %v", err) } - defer f.Close() - fmt.Fprintf(f, "%s=%s\n", name, value) } // GetTriggerContext implements the Action interface. diff --git a/atlasaction/testdata/gitlab/schema-plan-approve.txtar b/atlasaction/testdata/gitlab/schema-plan-approve.txtar index 7741ce1c..7d6318a4 100644 --- a/atlasaction/testdata/gitlab/schema-plan-approve.txtar +++ b/atlasaction/testdata/gitlab/schema-plan-approve.txtar @@ -7,7 +7,7 @@ stdout 'No schema plan found' # One pending plan. atlas-action --action=schema/plan/approve -output expected-output.env +output .env.expected-output # Multiple pending plans. ! atlas-action --action=schema/plan/approve @@ -15,12 +15,12 @@ stdout 'No plan URL provided, searching for the pending plan' stdout 'Found schema plan: atlas://plans/1234' stdout 'Found schema plan: atlas://plans/5678' stdout 'found multiple schema plans, please approve or delete the existing plans' -output expected-output.env +output .env.expected-output --- expected-output.env -- -link=https://test.atlasgo.cloud/schemas/123/plans/456 -plan=atlas://plans/1234 -status= +-- .env.expected-output -- +ATLAS_OUTPUT_SCHEMA_PLAN_APPROVE_LINK="https://test.atlasgo.cloud/schemas/123/plans/456" +ATLAS_OUTPUT_SCHEMA_PLAN_APPROVE_PLAN="atlas://plans/1234" +ATLAS_OUTPUT_SCHEMA_PLAN_APPROVE_STATUS="" -- schema-plan-approve/1/args -- schema plan list --format {{ json . }} --context {"scmType":"GITLAB"} --pending --auto-approve diff --git a/atlasaction/testdata/gitlab/schema-plan.txtar b/atlasaction/testdata/gitlab/schema-plan.txtar index 8d72fad2..fb8fdc81 100644 --- a/atlasaction/testdata/gitlab/schema-plan.txtar +++ b/atlasaction/testdata/gitlab/schema-plan.txtar @@ -15,12 +15,12 @@ atlas-action --action=schema/plan stdout 'Schema plan does not exist, creating a new one with name "pr-1-3RRRcLHF"' cmp comments-expected/1 comments/1 -output expected-output.env +output .env.expected-output --- expected-output.env -- -link=http://test.atlasgo.cloud/schemas/141733920769/plans/210453397511 -plan=atlas://app/plans/20241010143904 -status=PENDING +-- .env.expected-output -- +ATLAS_OUTPUT_SCHEMA_PLAN_LINK="http://test.atlasgo.cloud/schemas/141733920769/plans/210453397511" +ATLAS_OUTPUT_SCHEMA_PLAN_PLAN="atlas://app/plans/20241010143904" +ATLAS_OUTPUT_SCHEMA_PLAN_STATUS="PENDING" -- schema-plan/1/args -- schema plan list --format {{ json . }} --context {"repo":"my-project","branch":"test-branch","commit":"123","url":"https://gitlab.com/projects/my-project/merge_requests/1","username":"user","userID":"123","scmType":"GITLAB"} --pending --auto-approve