Added bento :)

This commit is contained in:
2026-05-01 14:10:22 -06:00
parent fec14022cd
commit 9ea2cf8c34
17 changed files with 1304 additions and 253 deletions
+50 -10
View File
@@ -6,6 +6,7 @@ import (
"io"
"net/http"
"strings"
"sync"
"time"
)
@@ -17,18 +18,18 @@ type Client struct {
func NewClient(baseURL, token string) *Client {
baseURL = strings.TrimRight(baseURL, "/")
if !strings.HasPrefix(baseURL, "http://") && !strings.HasPrefix(baseURL, "https://") {
baseURL = "https://" + baseURL
}
if strings.HasSuffix(baseURL, "/api/v1") {
baseURL = strings.TrimSuffix(baseURL, "/api/v1")
}
if strings.HasSuffix(baseURL, "/api") {
baseURL = strings.TrimSuffix(baseURL, "/api")
}
return &Client{
BaseURL: baseURL,
Token: token,
@@ -46,8 +47,9 @@ type GiteaRepo struct {
}
type GiteaCommit struct {
SHA string `json:"sha"`
Commit struct {
SHA string `json:"sha"`
RepoName string `json:"-"`
Commit struct {
Author struct {
Name string `json:"name"`
Email string `json:"email"`
@@ -57,10 +59,14 @@ type GiteaCommit struct {
} `json:"commit"`
}
// do makes an authenticated HTTP request to Gitea API
type RepoRef struct {
Owner string
Name string
}
func (c *Client) do(method, path string) ([]byte, error) {
url := fmt.Sprintf("%s/api/v1%s", c.BaseURL, path)
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request for %s: %w", url, err)
@@ -68,7 +74,7 @@ func (c *Client) do(method, path string) ([]byte, error) {
req.Header.Set("Authorization", fmt.Sprintf("token %s", c.Token))
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "gitea-wrapped/1.0")
req.Header.Set("User-Agent", "wrapped-cli/1.0")
resp, err := c.Client.Do(req)
if err != nil {
@@ -96,7 +102,7 @@ func (c *Client) GetUserRepos() ([]GiteaRepo, error) {
var repos []GiteaRepo
page := 1
pageSize := 50
maxPages := 100 // Safety limit to prevent infinite loops
maxPages := 100
for page <= maxPages {
path := fmt.Sprintf("/user/repos?page=%d&limit=%d", page, pageSize)
@@ -133,7 +139,7 @@ func (c *Client) GetRepoCommits(owner, repo string) ([]GiteaCommit, error) {
var commits []GiteaCommit
page := 1
pageSize := 50
maxPages := 1000 // Safety limit to prevent infinite loops
maxPages := 1000
for page <= maxPages {
path := fmt.Sprintf("/repos/%s/%s/commits?page=%d&limit=%d", owner, repo, page, pageSize)
@@ -166,6 +172,40 @@ func (c *Client) GetRepoCommits(owner, repo string) ([]GiteaCommit, error) {
return commits, nil
}
func (c *Client) GetReposCommitsParallel(repos []RepoRef) ([]GiteaCommit, error) {
var allCommits []GiteaCommit
var mu sync.Mutex
var wg sync.WaitGroup
semaphore := make(chan struct{}, 5)
for _, repo := range repos {
wg.Add(1)
go func(owner, repoName string) {
defer wg.Done()
semaphore <- struct{}{}
defer func() { <-semaphore }()
commits, err := c.GetRepoCommits(owner, repoName)
if err != nil {
return
}
for i := range commits {
commits[i].RepoName = repoName
}
mu.Lock()
allCommits = append(allCommits, commits...)
mu.Unlock()
}(repo.Owner, repo.Name)
}
wg.Wait()
return allCommits, nil
}
func (c *Client) TestConnection() error {
_, err := c.do("GET", "/user")
if err != nil {