Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .github/workflows/test-safe-output-create-issue.lock.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions pkg/workflow/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,11 @@ func (c *Compiler) buildCreateOutputIssueJob(data *WorkflowData, mainJobName str
steps = append(steps, fmt.Sprintf(" GITHUB_AW_ISSUE_LABELS: %q\n", labelsStr))
}

// Pass the staged flag if it's set to true
if data.SafeOutputs.Staged != nil && *data.SafeOutputs.Staged {
steps = append(steps, " GITHUB_AW_SAFE_OUTPUTS_STAGED: \"true\"\n")
}

steps = append(steps, " with:\n")
steps = append(steps, " script: |\n")

Expand Down Expand Up @@ -2547,6 +2552,11 @@ func (c *Compiler) buildCreateOutputPullRequestJob(data *WorkflowData, mainJobNa
}
steps = append(steps, fmt.Sprintf(" GITHUB_AW_PR_IF_NO_CHANGES: %q\n", ifNoChanges))

// Pass the staged flag if it's set to true
if data.SafeOutputs.Staged != nil && *data.SafeOutputs.Staged {
steps = append(steps, " GITHUB_AW_SAFE_OUTPUTS_STAGED: \"true\"\n")
}

steps = append(steps, " with:\n")
steps = append(steps, " script: |\n")

Expand Down
5 changes: 5 additions & 0 deletions pkg/workflow/output_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func (c *Compiler) buildCreateOutputLabelJob(data *WorkflowData, mainJobName str
// Pass the max limit
steps = append(steps, fmt.Sprintf(" GITHUB_AW_LABELS_MAX_COUNT: %d\n", maxCount))

// Pass the staged flag if it's set to true
if data.SafeOutputs.Staged != nil && *data.SafeOutputs.Staged {
steps = append(steps, " GITHUB_AW_SAFE_OUTPUTS_STAGED: \"true\"\n")
}

steps = append(steps, " with:\n")
steps = append(steps, " script: |\n")

Expand Down
118 changes: 118 additions & 0 deletions pkg/workflow/staged_add_issue_labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package workflow

import (
"strings"
"testing"
)

func TestAddIssueLabelsJobWithStagedFlag(t *testing.T) {
// Create a compiler instance
c := NewCompiler(false, "", "test")

// Test with staged: true
workflowData := &WorkflowData{
Name: "test-workflow",
SafeOutputs: &SafeOutputsConfig{
AddIssueLabels: &AddIssueLabelsConfig{},
Staged: &[]bool{true}[0], // pointer to true
},
}

job, err := c.buildCreateOutputLabelJob(workflowData, "main_job")
if err != nil {
t.Fatalf("Unexpected error building add labels job: %v", err)
}

// Convert steps to a single string for testing
stepsContent := strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is included in the env section
if !strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED: \"true\"\n") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable to be set to true in add-issue-label job")
}

// Test with staged: false
workflowData.SafeOutputs.Staged = &[]bool{false}[0] // pointer to false

job, err = c.buildCreateOutputLabelJob(workflowData, "main_job")
if err != nil {
t.Fatalf("Unexpected error building add labels job: %v", err)
}

stepsContent = strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is not included in the env section when false
// We need to be specific to avoid matching the JavaScript code that references the variable
if strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED:") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable not to be set when staged is false")
}

// Test with staged: nil (not specified)
workflowData.SafeOutputs.Staged = nil

job, err = c.buildCreateOutputLabelJob(workflowData, "main_job")
if err != nil {
t.Fatalf("Unexpected error building add labels job: %v", err)
}

stepsContent = strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is not included in the env section when nil
// We need to be specific to avoid matching the JavaScript code that references the variable
if strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED:") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable not to be set when staged is nil")
}
}

func TestAddIssueLabelsJobWithNilSafeOutputs(t *testing.T) {
// Create a compiler instance
c := NewCompiler(false, "", "test")

// Test with no SafeOutputs config - this should fail
workflowData := &WorkflowData{
Name: "test-workflow",
SafeOutputs: nil,
}

_, err := c.buildCreateOutputLabelJob(workflowData, "main_job")
if err == nil {
t.Error("Expected error when SafeOutputs is nil")
}

expectedError := "safe-outputs configuration is required"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedError, err)
}
}

func TestAddIssueLabelsJobWithNilAddIssueLabelsConfig(t *testing.T) {
// Create a compiler instance
c := NewCompiler(false, "", "test")

// Test with SafeOutputs but nil AddIssueLabels config - this should work as it's a valid case
workflowData := &WorkflowData{
Name: "test-workflow",
SafeOutputs: &SafeOutputsConfig{
AddIssueLabels: nil, // This is valid - means empty configuration
Staged: &[]bool{true}[0],
},
}

job, err := c.buildCreateOutputLabelJob(workflowData, "main_job")
if err != nil {
t.Fatalf("Expected no error when AddIssueLabels is nil (should use defaults): %v", err)
}

// Convert steps to a single string for testing
stepsContent := strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is included in the env section
if !strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED: \"true\"\n") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable to be set to true even with nil AddIssueLabels config")
}

// Check that default max count is used
if !strings.Contains(stepsContent, " GITHUB_AW_LABELS_MAX_COUNT: 3\n") {
t.Error("Expected default max count of 3 when AddIssueLabels is nil")
}
}
101 changes: 101 additions & 0 deletions pkg/workflow/staged_create_issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package workflow

import (
"strings"
"testing"
)

func TestCreateIssueJobWithStagedFlag(t *testing.T) {
// Create a compiler instance
c := NewCompiler(false, "", "test")

// Test with staged: true
workflowData := &WorkflowData{
Name: "test-workflow",
SafeOutputs: &SafeOutputsConfig{
CreateIssues: &CreateIssuesConfig{},
Staged: &[]bool{true}[0], // pointer to true
},
}

job, err := c.buildCreateOutputIssueJob(workflowData, "main_job", false)
if err != nil {
t.Fatalf("Unexpected error building create issue job: %v", err)
}

// Convert steps to a single string for testing
stepsContent := strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is included in the env section
if !strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED: \"true\"\n") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable to be set to true in create-issue job")
}

// Test with staged: false
workflowData.SafeOutputs.Staged = &[]bool{false}[0] // pointer to false

job, err = c.buildCreateOutputIssueJob(workflowData, "main_job", false)
if err != nil {
t.Fatalf("Unexpected error building create issue job: %v", err)
}

stepsContent = strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is not included in the env section when false
// We need to be specific to avoid matching the JavaScript code that references the variable
if strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED:") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable not to be set when staged is false")
}

// Test with staged: nil (not specified)
workflowData.SafeOutputs.Staged = nil

job, err = c.buildCreateOutputIssueJob(workflowData, "main_job", false)
if err != nil {
t.Fatalf("Unexpected error building create issue job: %v", err)
}

stepsContent = strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is not included in the env section when nil
// We need to be specific to avoid matching the JavaScript code that references the variable
if strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED:") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable not to be set when staged is nil")
}
}

func TestCreateIssueJobWithoutSafeOutputs(t *testing.T) {
// Create a compiler instance
c := NewCompiler(false, "", "test")

// Test with no SafeOutputs config - this should fail
workflowData := &WorkflowData{
Name: "test-workflow",
SafeOutputs: nil,
}

_, err := c.buildCreateOutputIssueJob(workflowData, "main_job", false)
if err == nil {
t.Error("Expected error when SafeOutputs is nil")
}

expectedError := "safe-outputs.create-issue configuration is required"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedError, err)
}

// Test with SafeOutputs but no CreateIssues config - this should also fail
workflowData.SafeOutputs = &SafeOutputsConfig{
CreatePullRequests: &CreatePullRequestsConfig{},
Staged: &[]bool{true}[0],
}

_, err = c.buildCreateOutputIssueJob(workflowData, "main_job", false)
if err == nil {
t.Error("Expected error when CreateIssues is nil")
}

if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedError, err)
}
}
101 changes: 101 additions & 0 deletions pkg/workflow/staged_pull_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package workflow

import (
"strings"
"testing"
)

func TestCreatePullRequestJobWithStagedFlag(t *testing.T) {
// Create a compiler instance
c := NewCompiler(false, "", "test")

// Test with staged: true
workflowData := &WorkflowData{
Name: "test-workflow",
SafeOutputs: &SafeOutputsConfig{
CreatePullRequests: &CreatePullRequestsConfig{},
Staged: &[]bool{true}[0], // pointer to true
},
}

job, err := c.buildCreateOutputPullRequestJob(workflowData, "main_job")
if err != nil {
t.Fatalf("Unexpected error building create pull request job: %v", err)
}

// Convert steps to a single string for testing
stepsContent := strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is included in the env section
if !strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED: \"true\"\n") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable to be set to true in create-pull-request job")
}

// Test with staged: false
workflowData.SafeOutputs.Staged = &[]bool{false}[0] // pointer to false

job, err = c.buildCreateOutputPullRequestJob(workflowData, "main_job")
if err != nil {
t.Fatalf("Unexpected error building create pull request job: %v", err)
}

stepsContent = strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is not included in the env section when false
// We need to be specific to avoid matching the JavaScript code that references the variable
if strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED:") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable not to be set when staged is false")
}

// Test with staged: nil (not specified)
workflowData.SafeOutputs.Staged = nil

job, err = c.buildCreateOutputPullRequestJob(workflowData, "main_job")
if err != nil {
t.Fatalf("Unexpected error building create pull request job: %v", err)
}

stepsContent = strings.Join(job.Steps, "")

// Check that GITHUB_AW_SAFE_OUTPUTS_STAGED is not included in the env section when nil
// We need to be specific to avoid matching the JavaScript code that references the variable
if strings.Contains(stepsContent, " GITHUB_AW_SAFE_OUTPUTS_STAGED:") {
t.Error("Expected GITHUB_AW_SAFE_OUTPUTS_STAGED environment variable not to be set when staged is nil")
}
}

func TestCreatePullRequestJobWithoutSafeOutputs(t *testing.T) {
// Create a compiler instance
c := NewCompiler(false, "", "test")

// Test with no SafeOutputs config - this should fail
workflowData := &WorkflowData{
Name: "test-workflow",
SafeOutputs: nil,
}

_, err := c.buildCreateOutputPullRequestJob(workflowData, "main_job")
if err == nil {
t.Error("Expected error when SafeOutputs is nil")
}

expectedError := "safe-outputs.create-pull-request configuration is required"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedError, err)
}

// Test with SafeOutputs but no CreatePullRequests config - this should also fail
workflowData.SafeOutputs = &SafeOutputsConfig{
CreateIssues: &CreateIssuesConfig{},
Staged: &[]bool{true}[0],
}

_, err = c.buildCreateOutputPullRequestJob(workflowData, "main_job")
if err == nil {
t.Error("Expected error when CreatePullRequests is nil")
}

if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedError, err)
}
}