Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fheinecke committed Dec 20, 2024
1 parent 5055ee5 commit 92bddbe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 68 deletions.
55 changes: 9 additions & 46 deletions tools/env-loader/pkg/writers/gha-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,59 +38,22 @@ func NewGHAEnvWriter() *GHAEnvWriter {
return &GHAEnvWriter{}
}

// Generates a delimiter that is guaranteed to not contain the provided string.
// This is required for writing multiline values to `GITHUB_ENV`, per
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings
func generateMultilineDelimiter(value string) string {
valueLines := strings.Split(value, "\n")

// Start with no suffix to make this a little more readable
delimiter := delimiterPrefix
for {
// Check if there are any lines that match the delimiter exactly
foundMatch := false
for _, line := range valueLines {
if line == delimiter {
foundMatch = true
break
}
}

if foundMatch {
// Add a reasonably unique value to the delimiter
delimiter = fmt.Sprintf("%s_%s", delimiterPrefix, uuid.NewString())
continue
}

// If no line matches the delimiter exactly, then the delimiter can be
// used.
return delimiter
}
}

func (ew *GHAEnvWriter) FormatEnvironmentValues(values map[string]values.Value) (string, error) {
renderedValues := make([]string, 0, len(values))
for key, value := range values {
if key == "" {
return "", trace.Errorf("found empty key for log value %q", value.String())
}

// Don't format strings without new lines as multiline. This would be valid, but a little
// less readable.
var renderedValue string
if strings.Contains(value.UnderlyingValue, "\n") {
// Match GHA docs:
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings
// Formats values like:
// {name}<<{delimiter}
// {value}
// {delimiter}
//
delimiter := generateMultilineDelimiter(value.UnderlyingValue)
renderedValue = fmt.Sprintf("%s<<%s\n%s\n%s\n", key, delimiter, value.UnderlyingValue, delimiter)
} else {
renderedValue = fmt.Sprintf("%s=%s\n", key, value.UnderlyingValue)
}
// Match GHA docs:
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings
// Formats values like:
// {name}<<{delimiter}
// {value}
// {delimiter}
//
delimiter := fmt.Sprintf("%s_%s", delimiterPrefix, uuid.NewString())
renderedValue := fmt.Sprintf("%s<<%s\n%s\n%s\n", key, delimiter, value.UnderlyingValue, delimiter)

renderedValues = append(renderedValues, renderedValue)
}
Expand Down
22 changes: 0 additions & 22 deletions tools/env-loader/pkg/writers/gha-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,12 @@
package writers

import (
"fmt"
"testing"

"github.com/google/uuid"
"github.com/gravitational/shared-workflows/tools/env-loader/pkg/values"
"github.com/stretchr/testify/require"
)

func TestGenerateMultilineDelimiter(t *testing.T) {
testCases := []string{
"value",
"multiline\nvalue",
"multiline\nvalue\nwith\nnewline\n",
"\n",
"\n\n\n",
fmt.Sprintf("\n%s", delimiterPrefix),
fmt.Sprintf("\n%s\n", delimiterPrefix),
"",
delimiterPrefix,
fmt.Sprintf("%s_%s", delimiterPrefix, uuid.NewString()),
}

for _, testCase := range testCases {
actualDelimiter := generateMultilineDelimiter(testCase)
require.NotEqual(t, testCase, actualDelimiter)
}
}

func TestGHAEnvFormat(t *testing.T) {
testCases := []struct {
desc string
Expand Down

0 comments on commit 92bddbe

Please sign in to comment.