Skip to content

Commit 59bea31

Browse files
authored
Refactor: Move safe output functions to safe_outputs_env_helpers.go (#3837)
1 parent ddb00c1 commit 59bea31

2 files changed

Lines changed: 47 additions & 44 deletions

File tree

pkg/workflow/frontmatter_extraction.go

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,8 @@ import (
1212
var frontmatterLog = logger.New("workflow:frontmatter_extraction")
1313

1414
// Note: extractStringValue, parseIntValue, and filterMapKeys have been moved to frontmatter_helpers.go
15-
16-
// addCustomSafeOutputEnvVars adds custom environment variables to safe output job steps
17-
func (c *Compiler) addCustomSafeOutputEnvVars(steps *[]string, data *WorkflowData) {
18-
if data.SafeOutputs != nil && len(data.SafeOutputs.Env) > 0 {
19-
for key, value := range data.SafeOutputs.Env {
20-
*steps = append(*steps, fmt.Sprintf(" %s: %s\n", key, value))
21-
}
22-
}
23-
}
24-
25-
// addSafeOutputGitHubToken adds github-token to the with section of github-script actions
26-
// Uses precedence: safe-outputs global github-token > top-level github-token > default
27-
func (c *Compiler) addSafeOutputGitHubToken(steps *[]string, data *WorkflowData) {
28-
var safeOutputsToken string
29-
if data.SafeOutputs != nil {
30-
safeOutputsToken = data.SafeOutputs.GitHubToken
31-
}
32-
effectiveToken := getEffectiveGitHubToken(safeOutputsToken, data.GitHubToken)
33-
*steps = append(*steps, fmt.Sprintf(" github-token: %s\n", effectiveToken))
34-
}
35-
36-
// addSafeOutputGitHubTokenForConfig adds github-token to the with section, preferring per-config token over global
37-
// Uses precedence: config token > safe-outputs global github-token > top-level github-token > default
38-
func (c *Compiler) addSafeOutputGitHubTokenForConfig(steps *[]string, data *WorkflowData, configToken string) {
39-
var safeOutputsToken string
40-
if data.SafeOutputs != nil {
41-
safeOutputsToken = data.SafeOutputs.GitHubToken
42-
}
43-
// Get effective token using double precedence: config > safe-outputs, then > top-level > default
44-
effectiveToken := getEffectiveGitHubToken(configToken, getEffectiveGitHubToken(safeOutputsToken, data.GitHubToken))
45-
*steps = append(*steps, fmt.Sprintf(" github-token: %s\n", effectiveToken))
46-
}
47-
48-
// addSafeOutputCopilotGitHubTokenForConfig adds github-token to the with section for Copilot-related operations
49-
// Uses precedence: config token > safe-outputs global github-token > top-level github-token > COPILOT_GITHUB_TOKEN > COPILOT_CLI_TOKEN > GH_AW_COPILOT_TOKEN (legacy) > GH_AW_GITHUB_TOKEN (legacy)
50-
func (c *Compiler) addSafeOutputCopilotGitHubTokenForConfig(steps *[]string, data *WorkflowData, configToken string) {
51-
var safeOutputsToken string
52-
if data.SafeOutputs != nil {
53-
safeOutputsToken = data.SafeOutputs.GitHubToken
54-
}
55-
// Get effective token using double precedence: config > safe-outputs, then > top-level > Copilot default
56-
effectiveToken := getEffectiveCopilotGitHubToken(configToken, getEffectiveCopilotGitHubToken(safeOutputsToken, data.GitHubToken))
57-
*steps = append(*steps, fmt.Sprintf(" github-token: %s\n", effectiveToken))
58-
}
15+
// Note: addCustomSafeOutputEnvVars, addSafeOutputGitHubToken, addSafeOutputGitHubTokenForConfig,
16+
// and addSafeOutputCopilotGitHubTokenForConfig have been moved to safe_outputs_env_helpers.go
5917

6018
// extractYAMLValue extracts a scalar value from the frontmatter map
6119
func (c *Compiler) extractYAMLValue(frontmatter map[string]any, key string) string {

pkg/workflow/safe_outputs_env_helpers.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package workflow
22

33
import (
4+
"fmt"
45
"strings"
56
"testing"
67
)
@@ -16,3 +17,47 @@ func assertEnvVarsInSteps(t *testing.T, steps []string, expectedEnvVars []string
1617
}
1718
}
1819
}
20+
21+
// addCustomSafeOutputEnvVars adds custom environment variables to safe output job steps
22+
func (c *Compiler) addCustomSafeOutputEnvVars(steps *[]string, data *WorkflowData) {
23+
if data.SafeOutputs != nil && len(data.SafeOutputs.Env) > 0 {
24+
for key, value := range data.SafeOutputs.Env {
25+
*steps = append(*steps, fmt.Sprintf(" %s: %s\n", key, value))
26+
}
27+
}
28+
}
29+
30+
// addSafeOutputGitHubToken adds github-token to the with section of github-script actions
31+
// Uses precedence: safe-outputs global github-token > top-level github-token > default
32+
func (c *Compiler) addSafeOutputGitHubToken(steps *[]string, data *WorkflowData) {
33+
var safeOutputsToken string
34+
if data.SafeOutputs != nil {
35+
safeOutputsToken = data.SafeOutputs.GitHubToken
36+
}
37+
effectiveToken := getEffectiveGitHubToken(safeOutputsToken, data.GitHubToken)
38+
*steps = append(*steps, fmt.Sprintf(" github-token: %s\n", effectiveToken))
39+
}
40+
41+
// addSafeOutputGitHubTokenForConfig adds github-token to the with section, preferring per-config token over global
42+
// Uses precedence: config token > safe-outputs global github-token > top-level github-token > default
43+
func (c *Compiler) addSafeOutputGitHubTokenForConfig(steps *[]string, data *WorkflowData, configToken string) {
44+
var safeOutputsToken string
45+
if data.SafeOutputs != nil {
46+
safeOutputsToken = data.SafeOutputs.GitHubToken
47+
}
48+
// Get effective token using double precedence: config > safe-outputs, then > top-level > default
49+
effectiveToken := getEffectiveGitHubToken(configToken, getEffectiveGitHubToken(safeOutputsToken, data.GitHubToken))
50+
*steps = append(*steps, fmt.Sprintf(" github-token: %s\n", effectiveToken))
51+
}
52+
53+
// addSafeOutputCopilotGitHubTokenForConfig adds github-token to the with section for Copilot-related operations
54+
// Uses precedence: config token > safe-outputs global github-token > top-level github-token > COPILOT_GITHUB_TOKEN > COPILOT_CLI_TOKEN > GH_AW_COPILOT_TOKEN (legacy) > GH_AW_GITHUB_TOKEN (legacy)
55+
func (c *Compiler) addSafeOutputCopilotGitHubTokenForConfig(steps *[]string, data *WorkflowData, configToken string) {
56+
var safeOutputsToken string
57+
if data.SafeOutputs != nil {
58+
safeOutputsToken = data.SafeOutputs.GitHubToken
59+
}
60+
// Get effective token using double precedence: config > safe-outputs, then > top-level > Copilot default
61+
effectiveToken := getEffectiveCopilotGitHubToken(configToken, getEffectiveCopilotGitHubToken(safeOutputsToken, data.GitHubToken))
62+
*steps = append(*steps, fmt.Sprintf(" github-token: %s\n", effectiveToken))
63+
}

0 commit comments

Comments
 (0)