|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/m7medvision/lazycommit/internal/config" |
| 9 | + "github.com/m7medvision/lazycommit/internal/git" |
| 10 | + "github.com/m7medvision/lazycommit/internal/provider" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +// PrProvider defines the interface for generating pull request titles |
| 15 | +type PrProvider interface { |
| 16 | + GeneratePRTitle(ctx context.Context, diff string) (string, error) |
| 17 | + GeneratePRTitles(ctx context.Context, diff string) ([]string, error) |
| 18 | +} |
| 19 | + |
| 20 | +// prCmd represents the pr command |
| 21 | +var prCmd = &cobra.Command{ |
| 22 | + Use: "pr", |
| 23 | + Short: "Generate pull request title suggestions", |
| 24 | + Long: `Analyzes the diff of the current branch compared to a target branch, and generates a list of 10 suggested pull request titles. |
| 25 | +
|
| 26 | + Arguments: |
| 27 | + <target-branch> The branch to compare against (e.g., main, develop)`, |
| 28 | + Args: func(cmd *cobra.Command, args []string) error { |
| 29 | + if len(args) < 1 { |
| 30 | + return fmt.Errorf("missing required argument: <target-branch>") |
| 31 | + } |
| 32 | + if len(args) > 1 { |
| 33 | + return fmt.Errorf("too many arguments, expected 1 but got %d", len(args)) |
| 34 | + } |
| 35 | + return nil |
| 36 | + }, |
| 37 | + Example: "lazycommit pr main\n lazycommit pr develop", |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + diff, err := git.GetDiffAgainstBranch(args[0]) |
| 40 | + if err != nil { |
| 41 | + fmt.Fprintf(os.Stderr, "Error getting branch comparison diff: %v\n", err) |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + if diff == "" { |
| 46 | + fmt.Println("No changes compared to base branch.") |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + var aiProvider PrProvider |
| 51 | + |
| 52 | + providerName := config.GetProvider() |
| 53 | + |
| 54 | + // API key is not needed for anthropic provider (uses CLI) |
| 55 | + var apiKey string |
| 56 | + if providerName != "anthropic" { |
| 57 | + var err error |
| 58 | + apiKey, err = config.GetAPIKey() |
| 59 | + if err != nil { |
| 60 | + fmt.Fprintf(os.Stderr, "Error getting API key: %v\n", err) |
| 61 | + os.Exit(1) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + var model string |
| 66 | + if providerName == "copilot" || providerName == "openai" || providerName == "anthropic" { |
| 67 | + var err error |
| 68 | + model, err = config.GetModel() |
| 69 | + if err != nil { |
| 70 | + fmt.Fprintf(os.Stderr, "Error getting model: %v\n", err) |
| 71 | + os.Exit(1) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + endpoint, err := config.GetEndpoint() |
| 76 | + if err != nil { |
| 77 | + fmt.Fprintf(os.Stderr, "Error getting endpoint: %v\n", err) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + |
| 81 | + switch providerName { |
| 82 | + case "copilot": |
| 83 | + aiProvider = provider.NewCopilotProviderWithModel(apiKey, model, endpoint) |
| 84 | + case "openai": |
| 85 | + aiProvider = provider.NewOpenAIProvider(apiKey, model, endpoint) |
| 86 | + case "anthropic": |
| 87 | + // Get num_suggestions from config |
| 88 | + numSuggestions := config.GetNumSuggestions() |
| 89 | + aiProvider = provider.NewAnthropicProvider(model, numSuggestions) |
| 90 | + default: |
| 91 | + // Default to copilot if provider is not set or unknown |
| 92 | + aiProvider = provider.NewCopilotProvider(apiKey, endpoint) |
| 93 | + } |
| 94 | + |
| 95 | + prTitles, err := aiProvider.GeneratePRTitles(context.Background(), diff) |
| 96 | + if err != nil { |
| 97 | + fmt.Fprintf(os.Stderr, "Error generating pull request titles %v\n", err) |
| 98 | + os.Exit(1) |
| 99 | + } |
| 100 | + |
| 101 | + if len(prTitles) == 0 { |
| 102 | + fmt.Println("No PR titles generated.") |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + for _, title := range prTitles { |
| 107 | + fmt.Println(title) |
| 108 | + } |
| 109 | + |
| 110 | + }, |
| 111 | +} |
| 112 | + |
| 113 | +func init() { |
| 114 | + RootCmd.AddCommand(prCmd) |
| 115 | +} |
0 commit comments