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
11 changes: 2 additions & 9 deletions .github/workflows/daily-copilot-token-report.lock.yml

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

7 changes: 0 additions & 7 deletions .github/workflows/unbloat-docs.lock.yml

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

52 changes: 51 additions & 1 deletion pkg/parser/yaml_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func processYAMLWorkflowImport(filePath string) (jobs string, services string, e

// extractStepsFromCopilotSetup extracts steps from the copilot-setup-steps job
// Returns the steps in YAML array format for merging into the agent job
// Ensures a checkout step is always included at the beginning
func extractStepsFromCopilotSetup(workflow map[string]any) (string, error) {
jobsValue, ok := workflow["jobs"]
if !ok {
Expand Down Expand Up @@ -197,13 +198,62 @@ func extractStepsFromCopilotSetup(workflow map[string]any) (string, error) {
return "", fmt.Errorf("steps field is not a list in copilot-setup-steps job")
}

// Ensure checkout step is always included and placed first
stepsSlice = ensureCheckoutStepFirst(stepsSlice)

// Marshal steps array directly to YAML format (without "steps:" wrapper)
// This matches the format expected by the compiler which unmarshals into []any
stepsYAML, err := yaml.Marshal(stepsSlice)
if err != nil {
return "", fmt.Errorf("failed to marshal steps to YAML: %w", err)
}

yamlImportLog.Printf("Extracted steps from copilot-setup-steps job (YAML array format)")
yamlImportLog.Printf("Extracted steps from copilot-setup-steps job (YAML array format) with checkout step ensured")
return string(stepsYAML), nil
}

// ensureCheckoutStepFirst ensures a checkout step exists and is placed first in the steps list
// If a checkout step exists, it's moved to the beginning. If not, one is added.
func ensureCheckoutStepFirst(steps []any) []any {
// Find existing checkout step index
checkoutIndex := -1
for i, step := range steps {
if stepMap, ok := step.(map[string]any); ok {
if uses, hasUses := stepMap["uses"]; hasUses {
if usesStr, ok := uses.(string); ok {
// Check if this is a checkout action (actions/checkout@... or exactly "actions/checkout")
if strings.HasPrefix(usesStr, "actions/checkout@") || usesStr == "actions/checkout" {
checkoutIndex = i
break
}
}
}
}
}

// If checkout step exists and is already first, no changes needed
if checkoutIndex == 0 {
yamlImportLog.Print("Checkout step already at beginning of copilot-setup-steps")
return steps
}

// If checkout step exists but not first, move it to the beginning
if checkoutIndex > 0 {
yamlImportLog.Printf("Moving existing checkout step from position %d to beginning", checkoutIndex)
checkoutStep := steps[checkoutIndex]
// Remove from current position
steps = append(steps[:checkoutIndex], steps[checkoutIndex+1:]...)
// Prepend to beginning
steps = append([]any{checkoutStep}, steps...)
return steps
}

// No checkout step found, add a default one at the beginning
yamlImportLog.Print("No checkout step found in copilot-setup-steps, adding default checkout step at beginning")
defaultCheckoutStep := map[string]any{
"name": "Checkout code",
"uses": "actions/checkout@v4",
}
steps = append([]any{defaultCheckoutStep}, steps...)
return steps
}
164 changes: 164 additions & 0 deletions pkg/parser/yaml_import_copilot_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,170 @@ func TestExtractStepsFromCopilotSetup_NoSteps(t *testing.T) {
assert.Contains(t, err.Error(), "no steps found", "Error should mention missing steps")
}

func TestExtractStepsFromCopilotSetup_EnsuresCheckoutFirst(t *testing.T) {
// Test workflow with checkout step NOT first
workflow := map[string]any{
"name": "Copilot Setup Steps",
"on": "workflow_dispatch",
"jobs": map[string]any{
"copilot-setup-steps": map[string]any{
"runs-on": "ubuntu-latest",
"steps": []any{
map[string]any{
"name": "Install gh-aw extension",
"run": "curl -fsSL https://raw.githubusercontent.com/github/gh-aw/refs/heads/main/install-gh-aw.sh | bash",
},
map[string]any{
"name": "Checkout code",
"uses": "actions/checkout@v4",
},
map[string]any{
"name": "Set up Node.js",
"uses": "actions/setup-node@v4",
"with": map[string]any{
"node-version": "20",
},
},
},
},
},
}

stepsYAML, err := extractStepsFromCopilotSetup(workflow)
require.NoError(t, err, "Should extract steps without error")
require.NotEmpty(t, stepsYAML, "Should return non-empty steps YAML")

// Verify checkout step is first
lines := strings.Split(stepsYAML, "\n")
var firstStepName string
for _, line := range lines {
if strings.Contains(line, "name:") {
firstStepName = line
break
}
}
assert.Contains(t, firstStepName, "Checkout code", "First step should be the checkout step")
Comment thread
pelikhan marked this conversation as resolved.
Resolved

// Verify all steps are present
assert.Contains(t, stepsYAML, "Install gh-aw extension", "Should contain install step")
assert.Contains(t, stepsYAML, "Checkout code", "Should contain checkout step")
assert.Contains(t, stepsYAML, "Set up Node.js", "Should contain Node.js setup step")

// Verify checkout comes before install (order should be: checkout, install, node)
checkoutIndex := strings.Index(stepsYAML, "Checkout code")
installIndex := strings.Index(stepsYAML, "Install gh-aw extension")
nodeIndex := strings.Index(stepsYAML, "Set up Node.js")
assert.Less(t, checkoutIndex, installIndex, "Checkout should come before install")
assert.Less(t, installIndex, nodeIndex, "Install should come before Node.js setup")
}

func TestExtractStepsFromCopilotSetup_AddsCheckoutIfMissing(t *testing.T) {
// Test workflow without any checkout step
workflow := map[string]any{
"name": "Copilot Setup Steps",
"on": "workflow_dispatch",
"jobs": map[string]any{
"copilot-setup-steps": map[string]any{
"runs-on": "ubuntu-latest",
"steps": []any{
map[string]any{
"name": "Install dependencies",
"run": "npm install",
},
map[string]any{
"name": "Run linter",
"run": "npm run lint",
},
},
},
},
}

stepsYAML, err := extractStepsFromCopilotSetup(workflow)
require.NoError(t, err, "Should extract steps without error")
require.NotEmpty(t, stepsYAML, "Should return non-empty steps YAML")

// Verify checkout step was added
assert.Contains(t, stepsYAML, "Checkout code", "Should contain added checkout step")
assert.Contains(t, stepsYAML, "actions/checkout@v4", "Should contain checkout action")

// Verify checkout step is first
lines := strings.Split(stepsYAML, "\n")
var firstStepName string
for _, line := range lines {
if strings.Contains(line, "name:") {
firstStepName = line
break
}
}
assert.Contains(t, firstStepName, "Checkout code", "First step should be the checkout step")

// Verify original steps are still present
assert.Contains(t, stepsYAML, "Install dependencies", "Should contain original install step")
assert.Contains(t, stepsYAML, "Run linter", "Should contain original linter step")

// Verify checkout comes before other steps
checkoutIndex := strings.Index(stepsYAML, "Checkout code")
installIndex := strings.Index(stepsYAML, "Install dependencies")
lintIndex := strings.Index(stepsYAML, "Run linter")
assert.Less(t, checkoutIndex, installIndex, "Checkout should come before install")
assert.Less(t, checkoutIndex, lintIndex, "Checkout should come before lint")
}

func TestExtractStepsFromCopilotSetup_CheckoutAlreadyFirst(t *testing.T) {
// Test workflow with checkout step already first
workflow := map[string]any{
"name": "Copilot Setup Steps",
"on": "workflow_dispatch",
"jobs": map[string]any{
"copilot-setup-steps": map[string]any{
"runs-on": "ubuntu-latest",
"steps": []any{
map[string]any{
"name": "Checkout code",
"uses": "actions/checkout@v4",
},
map[string]any{
"name": "Install dependencies",
"run": "npm install",
},
map[string]any{
"name": "Run tests",
"run": "npm test",
},
},
},
},
}

stepsYAML, err := extractStepsFromCopilotSetup(workflow)
require.NoError(t, err, "Should extract steps without error")
require.NotEmpty(t, stepsYAML, "Should return non-empty steps YAML")

// Verify checkout step is first
lines := strings.Split(stepsYAML, "\n")
var firstStepName string
for _, line := range lines {
if strings.Contains(line, "name:") {
firstStepName = line
break
}
}
assert.Contains(t, firstStepName, "Checkout code", "First step should be the checkout step")

// Verify all steps are present
assert.Contains(t, stepsYAML, "Checkout code", "Should contain checkout step")
assert.Contains(t, stepsYAML, "Install dependencies", "Should contain install step")
assert.Contains(t, stepsYAML, "Run tests", "Should contain test step")

// Verify order is maintained
checkoutIndex := strings.Index(stepsYAML, "Checkout code")
installIndex := strings.Index(stepsYAML, "Install dependencies")
testIndex := strings.Index(stepsYAML, "Run tests")
assert.Less(t, checkoutIndex, installIndex, "Checkout should come before install")
assert.Less(t, installIndex, testIndex, "Install should come before tests")
}

func TestProcessYAMLWorkflowImport_CopilotSetupSteps(t *testing.T) {
// Create a temporary copilot-setup-steps.yml file
tmpDir, err := os.MkdirTemp("", "copilot-setup-test*")
Expand Down
37 changes: 20 additions & 17 deletions pkg/workflow/checkout_runtime_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ steps:

t.Logf("Found %d steps: %v", len(stepNames), stepNames)

if len(stepNames) < 6 {
t.Fatalf("Expected at least 6 steps, got %d: %v", len(stepNames), stepNames)
if len(stepNames) < 5 {
t.Fatalf("Expected at least 5 steps, got %d: %v", len(stepNames), stepNames)
}

// Verify the order in dev mode (when local actions are used):
// 1. First step should be "Checkout actions folder" (checkout local actions)
// 2. Second step should be "Setup Scripts" (use the checked out action)
// 3. Third step should be "Checkout .github folder" (checkout .github for agent job)
// 4. Fourth step should be "Create gh-aw temp directory" (before custom steps)
// 5. Fifth step should be "Checkout code" (from custom steps)
// 6. Sixth step should be "Setup Node.js" (runtime setup, inserted after checkout)
// 7. Seventh step should be "Use Node" (from custom steps)
// 3. Third step should be "Create gh-aw temp directory" (before custom steps)
// 4. Fourth step should be "Checkout code" (from custom steps - full checkout, no separate .github checkout needed)
// 5. Fifth step should be "Setup Node.js" (runtime setup, inserted after checkout)
// 6. Sixth step should be "Use Node" (from custom steps)
// NOTE: The .github sparse checkout is skipped because custom steps contain a full checkout

if stepNames[0] != "Checkout actions folder" {
t.Errorf("First step should be 'Checkout actions folder', got '%s'", stepNames[0])
Expand All @@ -140,24 +140,27 @@ steps:
t.Errorf("Second step should be 'Setup Scripts', got '%s'", stepNames[1])
}

if stepNames[2] != "Checkout .github folder" {
t.Errorf("Third step should be 'Checkout .github folder', got '%s'", stepNames[2])
if stepNames[2] != "Create gh-aw temp directory" {
t.Errorf("Third step should be 'Create gh-aw temp directory', got '%s'", stepNames[2])
}

if stepNames[3] != "Create gh-aw temp directory" {
t.Errorf("Fourth step should be 'Create gh-aw temp directory', got '%s'", stepNames[3])
if stepNames[3] != "Checkout code" {
t.Errorf("Fourth step should be 'Checkout code', got '%s'", stepNames[3])
}

if stepNames[4] != "Checkout code" {
t.Errorf("Fifth step should be 'Checkout code', got '%s'", stepNames[4])
if stepNames[4] != "Setup Node.js" {
t.Errorf("Fifth step should be 'Setup Node.js' (runtime setup after checkout), got '%s'", stepNames[4])
}

if stepNames[5] != "Setup Node.js" {
t.Errorf("Sixth step should be 'Setup Node.js' (runtime setup after checkout), got '%s'", stepNames[5])
if stepNames[5] != "Use Node" {
t.Errorf("Sixth step should be 'Use Node', got '%s'", stepNames[5])
}

if stepNames[6] != "Use Node" {
t.Errorf("Seventh step should be 'Use Node', got '%s'", stepNames[6])
// Verify that .github checkout is NOT present (redundant with full checkout in custom steps)
for _, name := range stepNames {
if name == "Checkout .github folder" {
t.Error("Checkout .github folder should not be present when custom steps contain full repository checkout")
}
}

// Additional check: verify temp directory creation is first
Expand Down
11 changes: 9 additions & 2 deletions pkg/workflow/compiler_yaml_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,17 @@ func (c *Compiler) generateCheckoutGitHubFolder(data *WorkflowData) []string {
return nil
}

// Skip .github checkout if full repository checkout will be performed
// Skip .github checkout if custom steps already contain a full repository checkout
// The full checkout already includes the .github folder, making sparse checkout redundant
if data.CustomSteps != "" && ContainsCheckout(data.CustomSteps) {
compilerYamlLog.Print("Skipping .github sparse checkout: custom steps contain full repository checkout")
return nil
}

// Skip .github checkout if an automatic full repository checkout will be added
// The shouldAddCheckoutStep function returns true when a checkout step will be automatically added
if c.shouldAddCheckoutStep(data) {
compilerYamlLog.Print("Skipping .github sparse checkout: full repository checkout will be performed")
compilerYamlLog.Print("Skipping .github sparse checkout: full repository checkout will be added automatically")
return nil
}

Expand Down
Loading