Borg/cmd/collect_github_prs.go
google-labs-jules[bot] 3020500da5 feat: Add GitHub Issues and PRs collection
This commit introduces the ability to collect GitHub issues and pull requests.

Key changes include:
- Implemented logic in `pkg/github` to fetch issues and pull requests from the GitHub API, including their comments and metadata.
- Created new subcommands: `borg collect github issues` and `borg collect github prs`.
- Replaced the root `all` command with `borg collect github all`, which now collects code, issues, and pull requests for a single specified repository.
- Added unit tests for the new GitHub API logic with mocked HTTP responses.
- Added integration tests for the new `issues` and `prs` subcommands.

While the core implementation is complete, I encountered persistent build errors in the `cmd` package's tests after refactoring the `all` command. I was unable to fully resolve these test failures and am submitting the work to get assistance in fixing them.

Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
2026-02-02 00:44:46 +00:00

84 lines
2.5 KiB
Go

package cmd
import (
"fmt"
"os"
"strings"
"github.com/Snider/Borg/pkg/compress"
"github.com/Snider/Borg/pkg/github"
"github.com/Snider/Borg/pkg/ui"
"github.com/spf13/cobra"
)
// NewCollectGithubPrsCmd creates a new cobra command for collecting github pull requests.
func NewCollectGithubPrsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "prs <owner/repo>",
Short: "Collect pull requests from a GitHub repository",
Long: `Collect all pull requests from a GitHub repository and store them in a DataNode.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
repoPath := args[0]
parts := strings.Split(repoPath, "/")
if len(parts) != 2 {
return fmt.Errorf("invalid repository path: %s (must be in the format <owner>/<repo>)", repoPath)
}
owner, repo := parts[0], parts[1]
outputFile, _ := cmd.Flags().GetString("output")
format, _ := cmd.Flags().GetString("format")
compression, _ := cmd.Flags().GetString("compression")
if format != "datanode" {
return fmt.Errorf("invalid format: %s (must be 'datanode')", format)
}
if compression != "none" && compression != "gz" && compression != "xz" {
return fmt.Errorf("invalid compression: %s (must be 'none', 'gz', or 'xz')", compression)
}
prompter := ui.NewNonInteractivePrompter(ui.GetVCSQuote)
prompter.Start()
defer prompter.Stop()
client := github.NewGithubClient()
dn, err := client.GetPullRequests(cmd.Context(), owner, repo)
if err != nil {
return fmt.Errorf("error getting pull requests: %w", err)
}
data, err := dn.ToTar()
if err != nil {
return fmt.Errorf("error serializing DataNode: %w", err)
}
compressedData, err := compress.Compress(data, compression)
if err != nil {
return fmt.Errorf("error compressing data: %w", err)
}
if outputFile == "" {
outputFile = "prs." + format
if compression != "none" {
outputFile += "." + compression
}
}
err = os.WriteFile(outputFile, compressedData, 0644)
if err != nil {
return fmt.Errorf("error writing DataNode to file: %w", err)
}
fmt.Fprintln(cmd.OutOrStdout(), "Pull requests saved to", outputFile)
return nil
},
}
cmd.Flags().String("output", "", "Output file for the DataNode")
cmd.Flags().String("format", "datanode", "Output format (datanode)")
cmd.Flags().String("compression", "none", "Compression format (none, gz, or xz)")
return cmd
}
func init() {
GetCollectGithubCmd().AddCommand(NewCollectGithubPrsCmd())
}