Skip to content

Commit a0e753a

Browse files
authored
fix bug where repo not checked out (#15279)
1 parent 0bdc133 commit a0e753a

File tree

5 files changed

+26
-48
lines changed

5 files changed

+26
-48
lines changed

pkg/cli/copilot_setup.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ import (
1313

1414
var copilotSetupLog = logger.New("cli:copilot_setup")
1515

16+
// getActionRef returns the action reference string based on action mode and version
17+
func getActionRef(actionMode workflow.ActionMode, version string) string {
18+
if actionMode.IsRelease() && version != "" && version != "dev" {
19+
return "@" + version
20+
}
21+
return "@main"
22+
}
23+
1624
// generateCopilotSetupStepsYAML generates the copilot-setup-steps.yml content based on action mode
1725
func generateCopilotSetupStepsYAML(actionMode workflow.ActionMode, version string) string {
1826
// Determine the action reference - use version tag in release mode, @main in dev mode
19-
actionRef := "@main"
20-
if actionMode.IsRelease() && version != "" && version != "dev" {
21-
actionRef = "@" + version
22-
}
27+
actionRef := getActionRef(actionMode, version)
2328

2429
if actionMode.IsRelease() {
2530
// Use the actions/setup-cli action in release mode
@@ -247,10 +252,7 @@ func renderCopilotSetupUpdateInstructions(filePath string, actionMode workflow.A
247252
fmt.Fprintln(os.Stderr)
248253

249254
// Determine the action reference
250-
actionRef := "@main"
251-
if actionMode.IsRelease() && version != "" && version != "dev" {
252-
actionRef = "@" + version
253-
}
255+
actionRef := getActionRef(actionMode, version)
254256

255257
if actionMode.IsRelease() {
256258
fmt.Fprintln(os.Stderr, " - name: Checkout repository")
@@ -279,10 +281,7 @@ func upgradeSetupCliVersion(workflow *Workflow, actionMode workflow.ActionMode,
279281
}
280282

281283
upgraded := false
282-
actionRef := "@main"
283-
if actionMode.IsRelease() && version != "" && version != "dev" {
284-
actionRef = "@" + version
285-
}
284+
actionRef := getActionRef(actionMode, version)
286285

287286
// Iterate through steps and update any actions/setup-cli steps
288287
for i := range job.Steps {
@@ -321,10 +320,7 @@ func injectExtensionInstallStep(workflow *Workflow, actionMode workflow.ActionMo
321320
var installStep, checkoutStep CopilotWorkflowStep
322321

323322
// Determine the action reference - use version tag in release mode, @main in dev mode
324-
actionRef := "@main"
325-
if actionMode.IsRelease() && version != "" && version != "dev" {
326-
actionRef = "@" + version
327-
}
323+
actionRef := getActionRef(actionMode, version)
328324

329325
if actionMode.IsRelease() {
330326
// In release mode, use the actions/setup-cli action

pkg/workflow/compiler_jobs.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -501,39 +501,21 @@ func (c *Compiler) buildCustomJobs(data *WorkflowData, activationJobCreated bool
501501
}
502502

503503
// shouldAddCheckoutStep returns true if the workflow requires a checkout step.
504-
// A checkout is needed when the workflow uses custom agent files, accesses local
505-
// actions in dev/script mode, or needs to reference .github directory content.
504+
// The repository checkout is needed in the agent job to access workflow files,
505+
// custom agent files, and other repository content.
506506
//
507-
// The checkout step is skipped in the following cases:
507+
// The checkout step is only skipped when:
508508
// - Custom steps already contain a checkout action
509-
// - Workflow is in release mode without a custom agent file
510509
//
511-
// The checkout step is always added when:
512-
// - A custom agent file is specified (via imports)
513-
// - Running in dev or script mode (requires .github and .actions access)
514-
// - Action mode is uninitialized (defaults to requiring checkout)
510+
// Otherwise, checkout is always added to ensure the agent has access to the repository.
515511
func (c *Compiler) shouldAddCheckoutStep(data *WorkflowData) bool {
516-
// Check condition 1: If custom steps already contain checkout, don't add another one
512+
// If custom steps already contain checkout, don't add another one
517513
if data.CustomSteps != "" && ContainsCheckout(data.CustomSteps) {
518514
log.Print("Skipping checkout step: custom steps already contain checkout")
519-
return false // Custom steps already have checkout
520-
}
521-
522-
// Check condition 2: If custom agent file is specified (via imports), checkout is required
523-
if data.AgentFile != "" {
524-
log.Printf("Adding checkout step: custom agent file specified: %s", data.AgentFile)
525-
return true // Custom agent file requires checkout to access the file
526-
}
527-
528-
// Check condition 3: Only skip checkout in explicit release mode without agent file
529-
// Dev mode, script mode, and uninitialized mode all need checkout for .github and .actions access
530-
if c.actionMode.IsRelease() {
531-
// In release mode without agent file, checkout is not needed
532-
log.Print("Skipping checkout step: release mode without agent file")
533515
return false
534516
}
535517

536-
// Default: add checkout for dev/script mode and uninitialized mode
537-
log.Printf("Adding checkout step: %s mode requires .github and .actions access", c.actionMode)
518+
// Always add checkout to ensure agent has repository access
519+
log.Print("Adding checkout step: agent job requires repository access")
538520
return true
539521
}

pkg/workflow/compiler_jobs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func TestShouldAddCheckoutStep(t *testing.T) {
343343
CustomSteps: "",
344344
},
345345
actionMode: ActionModeRelease,
346-
expected: false,
346+
expected: true, // Checkout always needed unless already in steps
347347
},
348348
{
349349
name: "dev mode without agent file",

pkg/workflow/compiler_yaml_main_job_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ func TestShouldAddCheckoutStepEdgeCases(t *testing.T) {
766766
expectCheckout: true,
767767
},
768768
{
769-
name: "no checkout in release mode without agent file",
769+
name: "checkout required in release mode without agent file",
770770
setupData: func() *WorkflowData {
771771
return &WorkflowData{
772772
Name: "Test",
@@ -775,7 +775,7 @@ func TestShouldAddCheckoutStepEdgeCases(t *testing.T) {
775775
setupCompiler: func(c *Compiler) {
776776
c.actionMode = ActionModeRelease
777777
},
778-
expectCheckout: false,
778+
expectCheckout: true, // Checkout always needed unless already in steps
779779
},
780780
{
781781
name: "checkout required when agent file specified",

pkg/workflow/engine_agent_import_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ func TestCheckoutWithAgentFromImports(t *testing.T) {
378378
}
379379
})
380380

381-
t.Run("no_checkout_without_agent_and_permissions", func(t *testing.T) {
381+
t.Run("checkout_added_without_agent", func(t *testing.T) {
382382
compiler := NewCompiler()
383-
// Set action mode to release to prevent automatic contents:read addition
383+
// Set action mode to release
384384
compiler.SetActionMode(ActionModeRelease)
385385

386386
workflowData := &WorkflowData{
@@ -391,8 +391,8 @@ func TestCheckoutWithAgentFromImports(t *testing.T) {
391391
}
392392

393393
shouldCheckout := compiler.shouldAddCheckoutStep(workflowData)
394-
if shouldCheckout {
395-
t.Error("Expected checkout NOT to be added in release mode without agent file and without contents permission")
394+
if !shouldCheckout {
395+
t.Error("Expected checkout to be added (always needed unless already in custom steps)")
396396
}
397397
})
398398

0 commit comments

Comments
 (0)