cli/internal/cmd/gitea/cmd_repos.go
Snider 89461d12eb 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

125 lines
2.4 KiB
Go

package gitea
import (
"fmt"
"github.com/host-uk/core/pkg/cli"
gt "github.com/host-uk/core/pkg/gitea"
)
// Repos command flags.
var (
reposOrg string
reposMirrors bool
)
// addReposCommand adds the 'repos' subcommand for listing repositories.
func addReposCommand(parent *cli.Command) {
cmd := &cli.Command{
Use: "repos",
Short: "List repositories",
Long: "List repositories from your Gitea instance, optionally filtered by organisation or mirror status.",
RunE: func(cmd *cli.Command, args []string) error {
return runRepos()
},
}
cmd.Flags().StringVar(&reposOrg, "org", "", "Filter by organisation")
cmd.Flags().BoolVar(&reposMirrors, "mirrors", false, "Show only mirror repositories")
parent.AddCommand(cmd)
}
func runRepos() error {
client, err := gt.NewFromConfig("", "")
if err != nil {
return err
}
var repos []*giteaRepo
if reposOrg != "" {
raw, err := client.ListOrgRepos(reposOrg)
if err != nil {
return err
}
for _, r := range raw {
repos = append(repos, &giteaRepo{
Name: r.Name,
FullName: r.FullName,
Mirror: r.Mirror,
Private: r.Private,
Stars: r.Stars,
CloneURL: r.CloneURL,
})
}
} else {
raw, err := client.ListUserRepos()
if err != nil {
return err
}
for _, r := range raw {
repos = append(repos, &giteaRepo{
Name: r.Name,
FullName: r.FullName,
Mirror: r.Mirror,
Private: r.Private,
Stars: r.Stars,
CloneURL: r.CloneURL,
})
}
}
// Filter mirrors if requested
if reposMirrors {
var filtered []*giteaRepo
for _, r := range repos {
if r.Mirror {
filtered = append(filtered, r)
}
}
repos = filtered
}
if len(repos) == 0 {
cli.Text("No repositories found.")
return nil
}
// Build table
table := cli.NewTable("Name", "Type", "Visibility", "Stars")
for _, r := range repos {
repoType := "source"
if r.Mirror {
repoType = "mirror"
}
visibility := successStyle.Render("public")
if r.Private {
visibility = warningStyle.Render("private")
}
table.AddRow(
repoStyle.Render(r.FullName),
dimStyle.Render(repoType),
visibility,
fmt.Sprintf("%d", r.Stars),
)
}
cli.Blank()
cli.Print(" %s\n\n", fmt.Sprintf("%d repositories", len(repos)))
table.Render()
return nil
}
// giteaRepo is a simplified repo for display purposes.
type giteaRepo struct {
Name string
FullName string
Mirror bool
Private bool
Stars int
CloneURL string
}