|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/cli/go-gh/v2/pkg/browser" |
| 7 | + "github.com/cli/go-gh/v2/pkg/prompter" |
4 | 8 | "github.com/github/gh-stack/internal/config" |
| 9 | + "github.com/github/gh-stack/internal/stack" |
5 | 10 | "github.com/spf13/cobra" |
6 | 11 | ) |
7 | 12 |
|
8 | 13 | func MergeCmd(cfg *config.Config) *cobra.Command { |
9 | | - opts := struct{}{} |
10 | | - |
11 | 14 | cmd := &cobra.Command{ |
12 | | - Use: "merge <pr>", |
| 15 | + Use: "merge [<pr-or-branch>]", |
13 | 16 | Short: "Merge a stack of PRs", |
14 | | - Long: "Merges the specified PR and all PRs below it in the stack.", |
15 | | - Args: cobra.ExactArgs(1), |
| 17 | + Long: `Merges the specified PR and all PRs below it in the stack. |
| 18 | +
|
| 19 | +Accepts a PR URL, PR number, or branch name. When run without |
| 20 | +arguments, operates on the current branch's PR.`, |
| 21 | + Args: cobra.MaximumNArgs(1), |
16 | 22 | RunE: func(cmd *cobra.Command, args []string) error { |
17 | | - return runMerge(cfg, opts) |
| 23 | + var target string |
| 24 | + if len(args) > 0 { |
| 25 | + target = args[0] |
| 26 | + } |
| 27 | + return runMerge(cfg, target) |
18 | 28 | }, |
19 | 29 | } |
20 | 30 |
|
21 | 31 | return cmd |
22 | 32 | } |
23 | 33 |
|
24 | | -// runMerge is a placeholder for the stack merge workflow. |
25 | | -// |
26 | | -// We need a mergeability check for the entire stack |
27 | | -// and an endpoint for merging an entire stack |
28 | | -func runMerge(cfg *config.Config, opts struct{}) error { |
29 | | - cfg.Warningf("`%s` is not yet implemented", cfg.ColorCyan("gh stack merge")) |
| 34 | +func runMerge(cfg *config.Config, target string) error { |
| 35 | + // Standard stack loading and validation. |
| 36 | + result, err := loadStack(cfg, "") |
| 37 | + if err != nil { |
| 38 | + return nil |
| 39 | + } |
| 40 | + s := result.Stack |
| 41 | + currentBranch := result.CurrentBranch |
| 42 | + |
| 43 | + // Sync PR state from GitHub so merge status is up to date. |
| 44 | + syncStackPRs(cfg, s) |
| 45 | + |
| 46 | + // Persist the refreshed PR state. |
| 47 | + _ = stack.Save(result.GitDir, result.StackFile) |
| 48 | + |
| 49 | + // Resolve which branch to operate on. |
| 50 | + var br *stack.BranchRef |
| 51 | + if target != "" { |
| 52 | + _, br, err = resolvePR(result.StackFile, target) |
| 53 | + if err != nil { |
| 54 | + cfg.Errorf("%s", err) |
| 55 | + return nil |
| 56 | + } |
| 57 | + } else { |
| 58 | + idx := s.IndexOf(currentBranch) |
| 59 | + if idx < 0 { |
| 60 | + if s.IsFullyMerged() { |
| 61 | + cfg.Successf("All PRs in this stack have already been merged") |
| 62 | + return nil |
| 63 | + } |
| 64 | + cfg.Errorf("current branch %q is not a stack branch (it may be the trunk)", currentBranch) |
| 65 | + return nil |
| 66 | + } |
| 67 | + br = &s.Branches[idx] |
| 68 | + } |
| 69 | + |
| 70 | + if br.PullRequest == nil { |
| 71 | + cfg.Errorf("no pull request found for branch %q", currentBranch) |
| 72 | + cfg.Printf(" Run %s to create PRs for this stack.", cfg.ColorCyan("gh stack push")) |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + if br.IsMerged() { |
| 77 | + cfg.Successf("PR %s has already been merged", cfg.PRLink(br.PullRequest.Number, br.PullRequest.URL)) |
| 78 | + cfg.Printf(" %s", br.PullRequest.URL) |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + prURL := br.PullRequest.URL |
| 83 | + prLink := cfg.PRLink(br.PullRequest.Number, prURL) |
| 84 | + |
| 85 | + cfg.Warningf("Merging stacked PRs from the CLI is not yet supported") |
| 86 | + |
| 87 | + if cfg.IsInteractive() { |
| 88 | + p := prompter.New(cfg.In, cfg.Out, cfg.Err) |
| 89 | + openWeb, promptErr := p.Confirm( |
| 90 | + fmt.Sprintf("Open %s in your browser?", prLink), true) |
| 91 | + if promptErr != nil { |
| 92 | + if isInterruptError(promptErr) { |
| 93 | + printInterrupt(cfg) |
| 94 | + return nil |
| 95 | + } |
| 96 | + cfg.Errorf("prompt failed: %s", promptErr) |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + if openWeb { |
| 101 | + b := browser.New("", cfg.Out, cfg.Err) |
| 102 | + if err := b.Browse(prURL); err != nil { |
| 103 | + cfg.Warningf("failed to open browser: %s", err) |
| 104 | + } else { |
| 105 | + cfg.Successf("Opened %s in your browser", prLink) |
| 106 | + return nil |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + cfg.Printf(" You can merge this PR at: %s", prURL) |
30 | 112 | return nil |
31 | 113 | } |
0 commit comments