Borg/pkg/github/github.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

124 lines
3.2 KiB
Go

package github
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/Snider/Borg/pkg/datanode"
"golang.org/x/oauth2"
)
type Repo struct {
CloneURL string `json:"clone_url"`
}
// GithubClient is an interface for interacting with the Github API.
type GithubClient interface {
GetPublicRepos(ctx context.Context, userOrOrg string) ([]string, error)
GetIssues(ctx context.Context, owner, repo string) (*datanode.DataNode, error)
GetPullRequests(ctx context.Context, owner, repo string) (*datanode.DataNode, error)
}
// NewGithubClient creates a new GithubClient.
func NewGithubClient() GithubClient {
return &githubClient{}
}
type githubClient struct {
apiURL string
}
// NewAuthenticatedClient creates a new authenticated http client.
var NewAuthenticatedClient = func(ctx context.Context) *http.Client {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return http.DefaultClient
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
return oauth2.NewClient(ctx, ts)
}
func (g *githubClient) GetPublicRepos(ctx context.Context, userOrOrg string) ([]string, error) {
return g.getPublicReposWithAPIURL(ctx, "https://api.github.com", userOrOrg)
}
func (g *githubClient) getPublicReposWithAPIURL(ctx context.Context, apiURL, userOrOrg string) ([]string, error) {
client := NewAuthenticatedClient(ctx)
var allCloneURLs []string
url := fmt.Sprintf("%s/users/%s/repos", apiURL, userOrOrg)
isFirstRequest := true
for {
if err := ctx.Err(); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Borg-Data-Collector")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
// If it's the first request for a user and it's a 404, we can try the org endpoint.
if isFirstRequest && strings.Contains(url, "/users/") && resp.StatusCode == http.StatusNotFound {
resp.Body.Close()
url = fmt.Sprintf("%s/orgs/%s/repos", apiURL, userOrOrg)
isFirstRequest = false // We are now trying the org endpoint.
continue // Re-run the loop with the org URL.
}
status := resp.Status
resp.Body.Close()
return nil, fmt.Errorf("failed to fetch repos: %s", status)
}
isFirstRequest = false // Subsequent requests are for pagination.
var repos []Repo
if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil {
resp.Body.Close()
return nil, err
}
resp.Body.Close()
for _, repo := range repos {
allCloneURLs = append(allCloneURLs, repo.CloneURL)
}
linkHeader := resp.Header.Get("Link")
nextURL := g.findNextURL(linkHeader)
if nextURL == "" {
break
}
url = nextURL
}
return allCloneURLs, nil
}
func (g *githubClient) findNextURL(linkHeader string) string {
links := strings.Split(linkHeader, ",")
for _, link := range links {
parts := strings.Split(link, ";")
if len(parts) < 2 {
continue
}
if strings.TrimSpace(parts[1]) == `rel="next"` {
urlPart := strings.TrimSpace(parts[0])
if strings.HasPrefix(urlPart, "<") && strings.HasSuffix(urlPart, ">") {
return urlPart[1 : len(urlPart)-1]
}
}
}
return ""
}