|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/github/gh-stack/internal/config" |
| 7 | + "github.com/github/gh-stack/internal/git" |
| 8 | + "github.com/github/gh-stack/internal/stack" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +type pushOptions struct { |
| 13 | + force bool |
| 14 | + draft bool |
| 15 | + dryRun bool |
| 16 | +} |
| 17 | + |
| 18 | +func PushCmd(cfg *config.Config) *cobra.Command { |
| 19 | + opts := &pushOptions{} |
| 20 | + |
| 21 | + cmd := &cobra.Command{ |
| 22 | + Use: "push", |
| 23 | + Short: "Push all branches in the current stack and create/update PRs", |
| 24 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 25 | + return runPush(cfg, opts) |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Force-push branches") |
| 30 | + cmd.Flags().BoolVar(&opts.draft, "draft", false, "Create PRs as drafts") |
| 31 | + cmd.Flags().BoolVar(&opts.dryRun, "dry-run", false, "Show what would be pushed without pushing") |
| 32 | + |
| 33 | + return cmd |
| 34 | +} |
| 35 | + |
| 36 | +func runPush(cfg *config.Config, opts *pushOptions) error { |
| 37 | + gitDir, err := git.GitDir() |
| 38 | + if err != nil { |
| 39 | + cfg.Errorf("not a git repository") |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + sf, err := stack.Load(gitDir) |
| 44 | + if err != nil { |
| 45 | + cfg.Errorf("failed to load stack state: %s", err) |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + currentBranch, err := git.CurrentBranch() |
| 50 | + if err != nil { |
| 51 | + cfg.Errorf("failed to get current branch: %s", err) |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + s := sf.FindStackForBranch(currentBranch) |
| 56 | + if s == nil { |
| 57 | + cfg.Errorf("current branch %q is not part of a stack", currentBranch) |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + client, err := cfg.GitHubClient() |
| 62 | + if err != nil { |
| 63 | + cfg.Errorf("failed to create GitHub client: %s", err) |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + // Push all branches |
| 68 | + for _, b := range s.Branches { |
| 69 | + if opts.dryRun { |
| 70 | + cfg.Printf("Would push %s\n", b.Branch) |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + cfg.Printf("Pushing %s...\n", b.Branch) |
| 75 | + if err := git.Push("origin", []string{b.Branch}, opts.force, false); err != nil { |
| 76 | + cfg.Errorf("failed to push %s: %s", b.Branch, err) |
| 77 | + return nil |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if opts.dryRun { |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + // Create or update PRs |
| 86 | + for i, b := range s.Branches { |
| 87 | + baseBranch := s.BaseBranch(b.Branch) |
| 88 | + |
| 89 | + pr, err := client.FindPRForBranch(b.Branch) |
| 90 | + if err != nil { |
| 91 | + cfg.Warningf("failed to check PR for %s: %v\n", b.Branch, err) |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + if pr == nil { |
| 96 | + // Create new PR |
| 97 | + title := b.Branch |
| 98 | + body := fmt.Sprintf("Part %d of stack.\n\nBase: `%s`", i+1, baseBranch) |
| 99 | + |
| 100 | + newPR, createErr := client.CreatePR(baseBranch, b.Branch, title, body, opts.draft) |
| 101 | + if createErr != nil { |
| 102 | + cfg.Warningf("failed to create PR for %s: %v\n", b.Branch, createErr) |
| 103 | + continue |
| 104 | + } |
| 105 | + cfg.Successf("Created PR #%d for %s\n", newPR.Number, b.Branch) |
| 106 | + } else { |
| 107 | + // Update base if needed |
| 108 | + if pr.BaseRefName != baseBranch { |
| 109 | + if err := client.UpdatePRBase(pr.ID, baseBranch); err != nil { |
| 110 | + cfg.Warningf("failed to update PR #%d base: %v\n", pr.Number, err) |
| 111 | + } else { |
| 112 | + cfg.Successf("Updated PR #%d base to %s\n", pr.Number, baseBranch) |
| 113 | + } |
| 114 | + } else { |
| 115 | + cfg.Printf("PR #%d for %s is up to date\n", pr.Number, b.Branch) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // TODO: Add PRs to a stack |
| 121 | + // |
| 122 | + // We can call an API after all the individual PRs are created/updated to create the stack at once, |
| 123 | + // or we can add a flag to the existing PR API to incrementally build the stack. |
| 124 | + // |
| 125 | + // For now, the PRs are pushed and created individually but are NOT linked as a formal stack on GitHub. |
| 126 | + cfg.Warningf("Stacked PRs is not yet implemented — PRs were created individually.\n") |
| 127 | + fmt.Fprintf(cfg.Err, " Once the GitHub Stacks API is available, PRs will be automatically\n") |
| 128 | + fmt.Fprintf(cfg.Err, " grouped into a Stack.\n") |
| 129 | + |
| 130 | + // Update head SHAs |
| 131 | + for i, b := range s.Branches { |
| 132 | + if sha, err := git.HeadSHA(b.Branch); err == nil { |
| 133 | + s.Branches[i].Head = sha |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if err := stack.Save(gitDir, sf); err != nil { |
| 138 | + cfg.Errorf("failed to save stack state: %s", err) |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + cfg.Successf("Pushed and synced %d branches\n", len(s.Branches)) |
| 143 | + return nil |
| 144 | +} |
0 commit comments