Skip to content

Commit 20a486f

Browse files
authored
Merge pull request #19 from hmariset/KFLUXVNGD-681
fix: ensure base branch is fetched before creating PR branches
2 parents 3b0c7c9 + c6d6496 commit 20a486f

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

internal/discover/runner.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,11 @@ func (r *Runner) createPullRequests(ctx context.Context, configs []config.Reposi
318318
return fmt.Errorf("failed to get current repository name: %w", err)
319319
}
320320

321-
// Determine base branch (main or master)
322-
baseBranch := "main"
321+
// Get default branch from GitHub API
322+
baseBranch, err := r.getDefaultBranch(ctx, currentRepo)
323+
if err != nil {
324+
return fmt.Errorf("failed to get default branch: %w", err)
325+
}
323326

324327
// Use writeClient for PR creation (may have different permissions than readClient)
325328
prCreator := pr.NewCreator(r.writeClient, workDir, r.config.Organization, currentRepo, baseBranch)
@@ -366,6 +369,21 @@ func (r *Runner) getCurrentRepoName(ctx context.Context) (string, error) {
366369
return "", fmt.Errorf("failed to parse repository name from remote URL: %s", remoteURL)
367370
}
368371

372+
func (r *Runner) getDefaultBranch(ctx context.Context, repoName string) (string, error) {
373+
repo, _, err := r.writeClient.Repositories.Get(ctx, r.config.Organization, repoName)
374+
if err != nil {
375+
return "", fmt.Errorf("failed to get repository info: %w", err)
376+
}
377+
378+
defaultBranch := repo.GetDefaultBranch()
379+
if defaultBranch == "" {
380+
// Fallback to main if default branch is not set
381+
return "main", nil
382+
}
383+
384+
return defaultBranch, nil
385+
}
386+
369387
func (r *Runner) printSummary(totalRepos, newRepos, created int) {
370388
fmt.Println("=========================================")
371389
fmt.Println("Discovery Summary")
@@ -412,14 +430,21 @@ func (r *Runner) prAlreadyExists(ctx context.Context, repoName string) bool {
412430
return false
413431
}
414432

433+
// Get default branch
434+
baseBranch, err := r.getDefaultBranch(ctx, currentRepo)
435+
if err != nil {
436+
// Fallback to main if we can't determine the default branch
437+
baseBranch = "main"
438+
}
439+
415440
// Branch name format matches pr/creator.go
416441
branchName := fmt.Sprintf("add-repo/%s", repoName)
417442

418443
// Check if PR exists with this branch as head
419444
opts := &github.PullRequestListOptions{
420445
State: "open",
421446
Head: fmt.Sprintf("%s:%s", r.config.Organization, branchName),
422-
Base: "main",
447+
Base: baseBranch,
423448
}
424449

425450
prs, _, err := r.writeClient.PullRequests.List(ctx, r.config.Organization, currentRepo, opts)

internal/pr/creator.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func (c *Creator) CreatePullRequest(ctx context.Context, cfg config.RepositoryCo
9797
return fmt.Errorf("GitHub API error: %w", err)
9898
}
9999

100-
// 5. Return to base branch for next iteration (handles CI environment)
101-
if _, err := RunGitCommand(ctx, c.workDir, "checkout", "-B", c.baseBranch, "origin/"+c.baseBranch); err != nil {
100+
// 5. Return to base branch for next iteration
101+
if _, err := RunGitCommand(ctx, c.workDir, "checkout", c.baseBranch); err != nil {
102102
fmt.Printf(" ⚠️ Warning: failed to checkout %s: %v\n", c.baseBranch, err)
103103
}
104104

@@ -113,10 +113,33 @@ func (c *Creator) createBranch(ctx context.Context, branchName string) error {
113113
}
114114
}
115115

116-
// Checkout base branch - create/reset from origin if needed (handles CI environment)
117-
// Using -B ensures the branch is created from origin/base if it doesn't exist locally
118-
if _, err := RunGitCommand(ctx, c.workDir, "checkout", "-B", c.baseBranch, "origin/"+c.baseBranch); err != nil {
119-
return err
116+
// Try to fetch the base branch from origin
117+
// If this fails, we'll try to use the local branch if it exists
118+
fetchSucceeded := false
119+
if _, err := RunGitCommand(ctx, c.workDir, "fetch", "origin", c.baseBranch); err == nil {
120+
fetchSucceeded = true
121+
}
122+
123+
// Check if base branch exists locally
124+
baseExistsLocally := c.branchExists(ctx, c.baseBranch)
125+
126+
if !baseExistsLocally && !fetchSucceeded {
127+
// Can't proceed without either local branch or successful fetch
128+
return fmt.Errorf("base branch %s does not exist locally and fetch from origin failed", c.baseBranch)
129+
}
130+
131+
if fetchSucceeded {
132+
// Create/reset local branch to match FETCH_HEAD (latest from remote)
133+
// -B creates the branch if it doesn't exist, or resets it if it does
134+
if _, err := RunGitCommand(ctx, c.workDir, "checkout", "-B", c.baseBranch, "FETCH_HEAD"); err != nil {
135+
return fmt.Errorf("failed to setup base branch %s from remote: %w", c.baseBranch, err)
136+
}
137+
} else {
138+
// Fetch failed but local branch exists - use local copy
139+
if _, err := RunGitCommand(ctx, c.workDir, "checkout", c.baseBranch); err != nil {
140+
return fmt.Errorf("failed to checkout base branch %s: %w", c.baseBranch, err)
141+
}
142+
fmt.Printf(" ⚠️ Warning: using local %s branch (fetch failed)\n", c.baseBranch)
120143
}
121144

122145
// Create and checkout new branch

0 commit comments

Comments
 (0)