go-scm/cmd/forge/cmd_issues.go
Snider f3dd8ca0f0 fix(review): address CodeRabbit PR #2 findings
Critical/Major:
- Remove dead functions syncRepoNameFromArg and repoNameFromArg (used url pkg without import, would cause compile error)
- Migrate forge.lthn.ai/core/config → dappco.re/go/core/config in forge/config.go and gitea/config.go
- Propagate ListIssueCommentsIter errors in forge/meta.go and gitea/meta.go (was silently returning truncated count)
- Add RedactedToken() to gitea/client.go to avoid exposing raw API tokens
- Add 30s timeout to http.DefaultClient usage in gitea/prs.go via package-level httpClient
- Fix stringsx.Fields (bufio 64KiB limit), Repeat (wrong for negative/zero), Replace (ignored n param) to match stdlib
- Fix fmtx.Println to use fmt.Sprintln so spaces appear between operands
- Fix filepathx.Abs to use path/filepath for OS-aware path handling; wrap Getwd error
- Fix stdio.Write to return io.ErrShortWrite on partial writes
- Add mutex lock to jobrunner.Journal.Query to prevent data race with Append
- Add sync.RWMutex to ScmProvider; protect p.index reads/writes in pkg/api/provider.go
- Fix cmd/scm/cmd_index.go: append dir to repoPaths only after ReadDir confirms existence
- Fix manifest/compile.go: copy manifest before applying version override to avoid mutating caller
- Fix forge/labels.go: use ListOrgLabelsIter/ListRepoLabelsIter names in iterator error logs
- Wrap single-segment validation error in syncutil.ParseRepoName with function context

Minor:
- Fix import ordering (stdlib → forge.lthn.ai → third-party) in cmd/forge, cmd/collect, repos, cmd/gitea files
- Add t.Setenv("HOME", t.TempDir()) to gitea testhelpers and forge/labels_test.go
- Add iterator yield guard in forge/orgs_test.go
- Convert syncutil/repo_name_test.go to table-driven tests
- Use json.Marshal in pkg/api/provider_test.go instead of string concatenation
- Fix test naming (redundant/conflicting _Good/_Bad suffixes) across 10 test files

Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-07 09:25:42 +01:00

202 lines
4.4 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
package forge
import (
"forge.lthn.ai/core/cli/pkg/cli"
forgejo "codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v2"
fg "dappco.re/go/core/scm/forge"
fmt "dappco.re/go/core/scm/internal/ax/fmtx"
strings "dappco.re/go/core/scm/internal/ax/stringsx"
)
// Issues command flags.
var (
issuesState string
issuesTitle string
issuesBody string
)
// addIssuesCommand adds the 'issues' subcommand for listing and creating issues.
func addIssuesCommand(parent *cli.Command) {
cmd := &cli.Command{
Use: "issues [owner/repo]",
Short: "List and manage issues",
Long: "List issues for a repository, or list all open issues across all your repos.",
Args: cli.MaximumNArgs(1),
RunE: func(cmd *cli.Command, args []string) error {
if len(args) == 0 {
return runListAllIssues()
}
owner, repo, err := splitOwnerRepo(args[0])
if err != nil {
return err
}
// If title is set, create an issue instead
if issuesTitle != "" {
return runCreateIssue(owner, repo)
}
return runListIssues(owner, repo)
},
}
cmd.Flags().StringVar(&issuesState, "state", "open", "Filter by state (open, closed, all)")
cmd.Flags().StringVar(&issuesTitle, "title", "", "Create issue with this title")
cmd.Flags().StringVar(&issuesBody, "body", "", "Issue body (used with --title)")
parent.AddCommand(cmd)
}
func runListAllIssues() error {
client, err := fg.NewFromConfig("", "")
if err != nil {
return err
}
// Collect all repos: user repos + all org repos, deduplicated
seen := make(map[string]bool)
var allRepos []*forgejo.Repository
userRepos, err := client.ListUserRepos()
if err == nil {
for _, r := range userRepos {
if !seen[r.FullName] {
seen[r.FullName] = true
allRepos = append(allRepos, r)
}
}
}
orgs, err := client.ListMyOrgs()
if err != nil {
return err
}
for _, org := range orgs {
repos, err := client.ListOrgRepos(org.UserName)
if err != nil {
continue
}
for _, r := range repos {
if !seen[r.FullName] {
seen[r.FullName] = true
allRepos = append(allRepos, r)
}
}
}
total := 0
cli.Blank()
for _, repo := range allRepos {
if repo.OpenIssues == 0 {
continue
}
owner, name := repo.Owner.UserName, repo.Name
issues, err := client.ListIssues(owner, name, fg.ListIssuesOpts{
State: issuesState,
})
if err != nil || len(issues) == 0 {
continue
}
cli.Print(" %s %s\n", repoStyle.Render(repo.FullName), dimStyle.Render(fmt.Sprintf("(%d)", len(issues))))
for _, issue := range issues {
printForgeIssue(issue)
}
cli.Blank()
total += len(issues)
}
if total == 0 {
cli.Text(fmt.Sprintf("No %s issues found.", issuesState))
} else {
cli.Print(" %s\n", dimStyle.Render(fmt.Sprintf("%d %s issues total", total, issuesState)))
}
cli.Blank()
return nil
}
func runListIssues(owner, repo string) error {
client, err := fg.NewFromConfig("", "")
if err != nil {
return err
}
issues, err := client.ListIssues(owner, repo, fg.ListIssuesOpts{
State: issuesState,
})
if err != nil {
return err
}
if len(issues) == 0 {
cli.Text(fmt.Sprintf("No %s issues in %s/%s.", issuesState, owner, repo))
return nil
}
cli.Blank()
cli.Print(" %s\n\n", fmt.Sprintf("%d %s issues in %s/%s", len(issues), issuesState, owner, repo))
for _, issue := range issues {
printForgeIssue(issue)
}
return nil
}
func runCreateIssue(owner, repo string) error {
client, err := fg.NewFromConfig("", "")
if err != nil {
return err
}
issue, err := client.CreateIssue(owner, repo, forgejo.CreateIssueOption{
Title: issuesTitle,
Body: issuesBody,
})
if err != nil {
return err
}
cli.Blank()
cli.Success(fmt.Sprintf("Created issue #%d: %s", issue.Index, issue.Title))
cli.Print(" %s %s\n", dimStyle.Render("URL:"), valueStyle.Render(issue.HTMLURL))
cli.Blank()
return nil
}
func printForgeIssue(issue *forgejo.Issue) {
num := numberStyle.Render(fmt.Sprintf("#%d", issue.Index))
title := valueStyle.Render(cli.Truncate(issue.Title, 60))
line := fmt.Sprintf(" %s %s", num, title)
// Add labels
if len(issue.Labels) > 0 {
var labels []string
for _, l := range issue.Labels {
labels = append(labels, l.Name)
}
line += " " + warningStyle.Render("["+strings.Join(labels, ", ")+"]")
}
// Add assignees
if len(issue.Assignees) > 0 {
var assignees []string
for _, a := range issue.Assignees {
assignees = append(assignees, "@"+a.UserName)
}
line += " " + infoStyle.Render(strings.Join(assignees, ", "))
}
cli.Text(line)
}