Skip to content

Commit 59d071d

Browse files
CopilotdsymeCopilot
authored
fix: switch to default branch before pulling after add-wizard PR merge (#20094)
* Initial plan * fix: pull changes to default branch after PR merge in add-wizard (#issue) After `gh aw add-wizard` merges the PR, `updateLocalBranch()` now switches to the default branch before pulling, ensuring the working tree contains the merged workflow files. Previously, if the user was on a feature branch, `git pull origin <defaultBranch>` would merge the remote branch into the wrong local branch and the workflow files would not be in the working tree, causing "workflow file not found" when trying to run immediately after setup. Also surface the pull failure warning to users in non-verbose mode with a hint to run `git pull` manually. Co-authored-by: dsyme <7204669+dsyme@users.noreply.github.com> * Update pkg/cli/add_interactive_git.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dsyme <7204669+dsyme@users.noreply.github.com> Co-authored-by: Don Syme <dsyme@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent e43b634 commit 59d071d

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

pkg/cli/add_interactive_git.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,23 @@ func (c *AddInteractiveConfig) applyChanges(ctx context.Context, workflowFiles,
157157
fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("Secret '%s' added", secretName)))
158158
}
159159

160-
// Step 8d: Update local branch with merged changes from GitHub
160+
// Step 8d: Update local branch with merged changes from GitHub.
161+
// Switch to the default branch and pull so that workflow files are available
162+
// locally for the subsequent "run workflow" step.
161163
if err := c.updateLocalBranch(); err != nil {
162-
// Non-fatal - warn but continue, workflow can still run on GitHub
164+
// Non-fatal - warn the user and continue; the workflow exists on GitHub
165+
// even if we can't update the local branch.
163166
addInteractiveLog.Printf("Failed to update local branch: %v", err)
164-
if c.Verbose {
165-
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not update local branch: %v", err)))
166-
}
167+
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not update local branch: %v", err)))
168+
fmt.Fprintln(os.Stderr, "You may need to switch to your repository's default branch (for example 'main') and run 'git pull' manually before running the workflow.")
167169
}
168170

169171
return nil
170172
}
171173

172-
// updateLocalBranch fetches and pulls the latest changes from GitHub after PR merge
174+
// updateLocalBranch fetches and pulls the latest changes from GitHub after PR merge.
175+
// It switches to the default branch before pulling so that the working tree contains
176+
// the merged workflow files, which are required when offering to run the workflow.
173177
func (c *AddInteractiveConfig) updateLocalBranch() error {
174178
addInteractiveLog.Print("Updating local branch with merged changes")
175179

@@ -186,13 +190,28 @@ func (c *AddInteractiveConfig) updateLocalBranch() error {
186190
fmt.Fprintln(os.Stderr, console.FormatProgressMessage("Fetching latest changes from GitHub..."))
187191
}
188192

189-
// Use git fetch followed by git pull
190193
fetchCmd := exec.Command("git", "fetch", "origin", defaultBranch)
191194
fetchOutput, err := fetchCmd.CombinedOutput()
192195
if err != nil {
193196
return fmt.Errorf("git fetch failed: %w (output: %s)", err, string(fetchOutput))
194197
}
195198

199+
// Switch to the default branch so the working tree contains the merged workflow
200+
// files. Without this, users on a feature branch won't have the files locally and
201+
// the subsequent "run workflow" step will fail with "workflow file not found".
202+
currentBranch, err := getCurrentBranch()
203+
if err != nil {
204+
addInteractiveLog.Printf("Could not determine current branch: %v", err)
205+
currentBranch = ""
206+
}
207+
208+
if currentBranch != defaultBranch {
209+
addInteractiveLog.Printf("Switching from %q to default branch %q", currentBranch, defaultBranch)
210+
if err := switchBranch(defaultBranch, c.Verbose); err != nil {
211+
return fmt.Errorf("failed to switch to default branch %s: %w", defaultBranch, err)
212+
}
213+
}
214+
196215
pullCmd := exec.Command("git", "pull", "origin", defaultBranch)
197216
pullOutput, err := pullCmd.CombinedOutput()
198217
if err != nil {

0 commit comments

Comments
 (0)