cli/pkg/gitea/repos.go
Snider a24242ab70
feat(gitea): add Gitea Go SDK integration and CLI commands (#324)
* feat(gitea): add Gitea Go SDK integration and CLI commands

Add `code.gitea.io/sdk/gitea` and create `pkg/gitea/` package for
connecting to self-hosted Gitea instances. Wire into CLI as `core gitea`
command group with repo, issue, PR, mirror, and sync subcommands.

pkg/gitea/:
- client.go: thin wrapper around SDK with config-based auth
- config.go: env → config file → flags resolution
- repos.go: list/get/create/delete repos, create mirrors
- issues.go: list/get/create issues and pull requests
- meta.go: pipeline MetaReader for structural + content signals

internal/cmd/gitea/:
- config: set URL/token, test connection
- repos: list repos with table output
- issues: list/create issues
- prs: list pull requests
- mirror: create GitHub→Gitea mirrors with auth
- sync: upstream/main branch strategy (--setup + ongoing sync)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* style(gitea): fix gofmt formatting

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(gitea): address Copilot review feedback

- Use os.UserHomeDir() instead of sh -c "echo $HOME" for home dir expansion
- Distinguish "already exists" from real errors in createMainFromUpstream
- Fix package docs to match actual config resolution order
- Guard token masking against short tokens (< 8 chars)
- Paginate ListIssueComments in GetPRMeta and GetCommentBodies
- Rename loop variable to avoid shadowing receiver in GetCommentBodies
- Move gitea SDK to direct require block in go.mod

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 21:12:12 +00:00

110 lines
2.7 KiB
Go

package gitea
import (
"code.gitea.io/sdk/gitea"
"github.com/host-uk/core/pkg/log"
)
// ListOrgRepos returns all repositories for the given organisation.
func (c *Client) ListOrgRepos(org string) ([]*gitea.Repository, error) {
var all []*gitea.Repository
page := 1
for {
repos, resp, err := c.api.ListOrgRepos(org, gitea.ListOrgReposOptions{
ListOptions: gitea.ListOptions{Page: page, PageSize: 50},
})
if err != nil {
return nil, log.E("gitea.ListOrgRepos", "failed to list org repos", err)
}
all = append(all, repos...)
if resp == nil || page >= resp.LastPage {
break
}
page++
}
return all, nil
}
// ListUserRepos returns all repositories for the authenticated user.
func (c *Client) ListUserRepos() ([]*gitea.Repository, error) {
var all []*gitea.Repository
page := 1
for {
repos, resp, err := c.api.ListMyRepos(gitea.ListReposOptions{
ListOptions: gitea.ListOptions{Page: page, PageSize: 50},
})
if err != nil {
return nil, log.E("gitea.ListUserRepos", "failed to list user repos", err)
}
all = append(all, repos...)
if resp == nil || page >= resp.LastPage {
break
}
page++
}
return all, nil
}
// GetRepo returns a single repository by owner and name.
func (c *Client) GetRepo(owner, name string) (*gitea.Repository, error) {
repo, _, err := c.api.GetRepo(owner, name)
if err != nil {
return nil, log.E("gitea.GetRepo", "failed to get repo", err)
}
return repo, nil
}
// CreateMirror creates a mirror repository on Gitea from a GitHub clone URL.
// This uses the Gitea migration API to set up a pull mirror.
// If authToken is provided, it is used to authenticate against the source (e.g. for private GitHub repos).
func (c *Client) CreateMirror(owner, name, cloneURL, authToken string) (*gitea.Repository, error) {
opts := gitea.MigrateRepoOption{
RepoName: name,
RepoOwner: owner,
CloneAddr: cloneURL,
Service: gitea.GitServiceGithub,
Mirror: true,
Description: "Mirror of " + cloneURL,
}
if authToken != "" {
opts.AuthToken = authToken
}
repo, _, err := c.api.MigrateRepo(opts)
if err != nil {
return nil, log.E("gitea.CreateMirror", "failed to create mirror", err)
}
return repo, nil
}
// DeleteRepo deletes a repository from Gitea.
func (c *Client) DeleteRepo(owner, name string) error {
_, err := c.api.DeleteRepo(owner, name)
if err != nil {
return log.E("gitea.DeleteRepo", "failed to delete repo", err)
}
return nil
}
// CreateOrgRepo creates a new empty repository under an organisation.
func (c *Client) CreateOrgRepo(org string, opts gitea.CreateRepoOption) (*gitea.Repository, error) {
repo, _, err := c.api.CreateOrgRepo(org, opts)
if err != nil {
return nil, log.E("gitea.CreateOrgRepo", "failed to create org repo", err)
}
return repo, nil
}