* 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>
146 lines
3.4 KiB
Go
146 lines
3.4 KiB
Go
package gitea
|
|
|
|
import (
|
|
"time"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/host-uk/core/pkg/log"
|
|
)
|
|
|
|
// PRMeta holds structural signals from a pull request,
|
|
// used by the pipeline MetaReader for AI-driven workflows.
|
|
type PRMeta struct {
|
|
Number int64
|
|
Title string
|
|
State string
|
|
Author string
|
|
Branch string
|
|
BaseBranch string
|
|
Labels []string
|
|
Assignees []string
|
|
IsMerged bool
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
CommentCount int
|
|
}
|
|
|
|
// Comment represents a comment with metadata.
|
|
type Comment struct {
|
|
ID int64
|
|
Author string
|
|
Body string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
const commentPageSize = 50
|
|
|
|
// GetPRMeta returns structural signals for a pull request.
|
|
// This is the Gitea side of the dual MetaReader described in the pipeline design.
|
|
func (c *Client) GetPRMeta(owner, repo string, pr int64) (*PRMeta, error) {
|
|
pull, _, err := c.api.GetPullRequest(owner, repo, pr)
|
|
if err != nil {
|
|
return nil, log.E("gitea.GetPRMeta", "failed to get PR metadata", err)
|
|
}
|
|
|
|
meta := &PRMeta{
|
|
Number: pull.Index,
|
|
Title: pull.Title,
|
|
State: string(pull.State),
|
|
Branch: pull.Head.Ref,
|
|
BaseBranch: pull.Base.Ref,
|
|
IsMerged: pull.HasMerged,
|
|
}
|
|
|
|
if pull.Created != nil {
|
|
meta.CreatedAt = *pull.Created
|
|
}
|
|
if pull.Updated != nil {
|
|
meta.UpdatedAt = *pull.Updated
|
|
}
|
|
|
|
if pull.Poster != nil {
|
|
meta.Author = pull.Poster.UserName
|
|
}
|
|
|
|
for _, label := range pull.Labels {
|
|
meta.Labels = append(meta.Labels, label.Name)
|
|
}
|
|
|
|
for _, assignee := range pull.Assignees {
|
|
meta.Assignees = append(meta.Assignees, assignee.UserName)
|
|
}
|
|
|
|
// Fetch comment count from the issue side (PRs are issues in Gitea).
|
|
// Paginate to get an accurate count.
|
|
count := 0
|
|
page := 1
|
|
for {
|
|
comments, _, listErr := c.api.ListIssueComments(owner, repo, pr, gitea.ListIssueCommentOptions{
|
|
ListOptions: gitea.ListOptions{Page: page, PageSize: commentPageSize},
|
|
})
|
|
if listErr != nil {
|
|
break
|
|
}
|
|
count += len(comments)
|
|
if len(comments) < commentPageSize {
|
|
break
|
|
}
|
|
page++
|
|
}
|
|
meta.CommentCount = count
|
|
|
|
return meta, nil
|
|
}
|
|
|
|
// GetCommentBodies returns all comment bodies for a pull request.
|
|
// This reads full content, which is safe on the home lab Gitea instance.
|
|
func (c *Client) GetCommentBodies(owner, repo string, pr int64) ([]Comment, error) {
|
|
var comments []Comment
|
|
page := 1
|
|
|
|
for {
|
|
raw, _, err := c.api.ListIssueComments(owner, repo, pr, gitea.ListIssueCommentOptions{
|
|
ListOptions: gitea.ListOptions{Page: page, PageSize: commentPageSize},
|
|
})
|
|
if err != nil {
|
|
return nil, log.E("gitea.GetCommentBodies", "failed to get PR comments", err)
|
|
}
|
|
|
|
if len(raw) == 0 {
|
|
break
|
|
}
|
|
|
|
for _, rc := range raw {
|
|
comment := Comment{
|
|
ID: rc.ID,
|
|
Body: rc.Body,
|
|
CreatedAt: rc.Created,
|
|
UpdatedAt: rc.Updated,
|
|
}
|
|
if rc.Poster != nil {
|
|
comment.Author = rc.Poster.UserName
|
|
}
|
|
comments = append(comments, comment)
|
|
}
|
|
|
|
if len(raw) < commentPageSize {
|
|
break
|
|
}
|
|
page++
|
|
}
|
|
|
|
return comments, nil
|
|
}
|
|
|
|
// GetIssueBody returns the body text of an issue.
|
|
// This reads full content, which is safe on the home lab Gitea instance.
|
|
func (c *Client) GetIssueBody(owner, repo string, issue int64) (string, error) {
|
|
iss, _, err := c.api.GetIssue(owner, repo, issue)
|
|
if err != nil {
|
|
return "", log.E("gitea.GetIssueBody", "failed to get issue body", err)
|
|
}
|
|
|
|
return iss.Body, nil
|
|
}
|