Skip to content

Commit

Permalink
gitlab: prefix outputs with action's name
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Dec 25, 2024
1 parent 02e5011 commit 0b962e3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
20 changes: 12 additions & 8 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_<ACTION>_<OUTPUT>="<value>"
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.
Expand Down
5 changes: 2 additions & 3 deletions atlasaction/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions atlasaction/circleci_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 2 additions & 5 deletions atlasaction/gitlab_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"fmt"
"io"
"os"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions atlasaction/testdata/gitlab/schema-plan-approve.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ 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
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

Expand Down
10 changes: 5 additions & 5 deletions atlasaction/testdata/gitlab/schema-plan.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0b962e3

Please sign in to comment.