cli/pkg/gitea/config.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

92 lines
2.3 KiB
Go

package gitea
import (
"os"
"github.com/host-uk/core/pkg/config"
"github.com/host-uk/core/pkg/log"
)
const (
// ConfigKeyURL is the config key for the Gitea instance URL.
ConfigKeyURL = "gitea.url"
// ConfigKeyToken is the config key for the Gitea API token.
ConfigKeyToken = "gitea.token"
// DefaultURL is the default Gitea instance URL.
DefaultURL = "https://gitea.snider.dev"
)
// NewFromConfig creates a Gitea client using the standard config resolution:
//
// 1. ~/.core/config.yaml keys: gitea.token, gitea.url
// 2. GITEA_TOKEN + GITEA_URL environment variables (override config file)
// 3. Provided flag overrides (highest priority; pass empty to skip)
func NewFromConfig(flagURL, flagToken string) (*Client, error) {
url, token, err := ResolveConfig(flagURL, flagToken)
if err != nil {
return nil, err
}
if token == "" {
return nil, log.E("gitea.NewFromConfig", "no API token configured (set GITEA_TOKEN or run: core gitea config --token TOKEN)", nil)
}
return New(url, token)
}
// ResolveConfig resolves the Gitea URL and token from all config sources.
// Flag values take highest priority, then env vars, then config file.
func ResolveConfig(flagURL, flagToken string) (url, token string, err error) {
// Start with config file values
cfg, cfgErr := config.New()
if cfgErr == nil {
_ = cfg.Get(ConfigKeyURL, &url)
_ = cfg.Get(ConfigKeyToken, &token)
}
// Overlay environment variables
if envURL := os.Getenv("GITEA_URL"); envURL != "" {
url = envURL
}
if envToken := os.Getenv("GITEA_TOKEN"); envToken != "" {
token = envToken
}
// Overlay flag values (highest priority)
if flagURL != "" {
url = flagURL
}
if flagToken != "" {
token = flagToken
}
// Default URL if nothing configured
if url == "" {
url = DefaultURL
}
return url, token, nil
}
// SaveConfig persists the Gitea URL and/or token to the config file.
func SaveConfig(url, token string) error {
cfg, err := config.New()
if err != nil {
return log.E("gitea.SaveConfig", "failed to load config", err)
}
if url != "" {
if err := cfg.Set(ConfigKeyURL, url); err != nil {
return log.E("gitea.SaveConfig", "failed to save URL", err)
}
}
if token != "" {
if err := cfg.Set(ConfigKeyToken, token); err != nil {
return log.E("gitea.SaveConfig", "failed to save token", err)
}
}
return nil
}