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>
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
package github
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io/fs"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetIssues(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/repos/owner/repo/issues" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
issues := []Issue{
|
|
{Number: 1, Title: "Issue 1", CommentsURL: "http://" + r.Host + "/repos/owner/repo/issues/1/comments"},
|
|
{Number: 2, Title: "Issue 2", CommentsURL: "http://" + r.Host + "/repos/owner/repo/issues/2/comments"},
|
|
}
|
|
json.NewEncoder(w).Encode(issues)
|
|
} else if r.URL.Path == "/repos/owner/repo/issues/1/comments" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
comments := []Comment{
|
|
{Body: "Comment 1"},
|
|
}
|
|
json.NewEncoder(w).Encode(comments)
|
|
} else if r.URL.Path == "/repos/owner/repo/issues/2/comments" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte("[]"))
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
originalNewAuthenticatedClient := NewAuthenticatedClient
|
|
NewAuthenticatedClient = func(ctx context.Context) *http.Client {
|
|
return server.Client()
|
|
}
|
|
defer func() {
|
|
NewAuthenticatedClient = originalNewAuthenticatedClient
|
|
}()
|
|
|
|
client := &githubClient{apiURL: server.URL}
|
|
dn, err := client.GetIssues(context.Background(), "owner", "repo")
|
|
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, dn)
|
|
|
|
expectedFiles := []string{
|
|
"issues/1.md",
|
|
"issues/2.md",
|
|
"issues/INDEX.json",
|
|
}
|
|
|
|
actualFiles := []string{}
|
|
dn.Walk(".", func(path string, de fs.DirEntry, err error) error {
|
|
if !de.IsDir() {
|
|
actualFiles = append(actualFiles, path)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
assert.ElementsMatch(t, expectedFiles, actualFiles)
|
|
}
|