cli/internal/cmd/gitea/cmd_mirror.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.4 KiB
Go

package gitea
import (
"fmt"
"os/exec"
"strings"
"github.com/host-uk/core/pkg/cli"
gt "github.com/host-uk/core/pkg/gitea"
)
// Mirror command flags.
var (
mirrorOrg string
mirrorGHToken string
)
// addMirrorCommand adds the 'mirror' subcommand for creating GitHub-to-Gitea mirrors.
func addMirrorCommand(parent *cli.Command) {
cmd := &cli.Command{
Use: "mirror <github-owner/repo>",
Short: "Mirror a GitHub repo to Gitea",
Long: `Create a pull mirror of a GitHub repository on your Gitea instance.
The mirror will be created under the specified Gitea organisation (or your user account).
Gitea will periodically sync changes from GitHub.
For private repos, a GitHub token is needed. By default it uses 'gh auth token'.`,
Args: cli.ExactArgs(1),
RunE: func(cmd *cli.Command, args []string) error {
owner, repo, err := splitOwnerRepo(args[0])
if err != nil {
return err
}
return runMirror(owner, repo)
},
}
cmd.Flags().StringVar(&mirrorOrg, "org", "", "Gitea organisation to mirror into (default: your user account)")
cmd.Flags().StringVar(&mirrorGHToken, "github-token", "", "GitHub token for private repos (default: from gh auth token)")
parent.AddCommand(cmd)
}
func runMirror(githubOwner, githubRepo string) error {
client, err := gt.NewFromConfig("", "")
if err != nil {
return err
}
cloneURL := fmt.Sprintf("https://github.com/%s/%s.git", githubOwner, githubRepo)
// Determine target owner on Gitea
targetOwner := mirrorOrg
if targetOwner == "" {
user, _, err := client.API().GetMyUserInfo()
if err != nil {
return cli.WrapVerb(err, "get", "current user")
}
targetOwner = user.UserName
}
// Resolve GitHub token for source auth
ghToken := mirrorGHToken
if ghToken == "" {
ghToken = resolveGHToken()
}
cli.Print(" Mirroring %s/%s -> %s/%s on Gitea...\n", githubOwner, githubRepo, targetOwner, githubRepo)
repo, err := client.CreateMirror(targetOwner, githubRepo, cloneURL, ghToken)
if err != nil {
return err
}
cli.Blank()
cli.Success(fmt.Sprintf("Mirror created: %s", repo.FullName))
cli.Print(" %s %s\n", dimStyle.Render("URL:"), valueStyle.Render(repo.HTMLURL))
cli.Print(" %s %s\n", dimStyle.Render("Clone:"), valueStyle.Render(repo.CloneURL))
cli.Blank()
return nil
}
// resolveGHToken tries to get a GitHub token from the gh CLI.
func resolveGHToken() string {
out, err := exec.Command("gh", "auth", "token").Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}