mirror of
https://gitea.com/gitea/tea.git
synced 2026-03-13 09:13:30 +01:00
feat(repos): support owner-based repository listing with robust lookup (#931)
- Add an owner flag to the repos list command to list repositories for a specific user or organization - Implement owner-based repository listing by detecting whether the owner is an organization or a user and calling the appropriate API - Improve error handling for owner lookup by checking HTTP status codes instead of relying on error string matching - Align repository search logic with the updated owner lookup behavior using HTTP response validation Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/931 Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-committed-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -5,6 +5,8 @@ package repos
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
@@ -32,6 +34,12 @@ var CmdReposListFlags = append([]cli.Flag{
|
||||
Required: false,
|
||||
Usage: "List your starred repos instead",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "owner",
|
||||
Aliases: []string{"O"},
|
||||
Required: false,
|
||||
Usage: "List repos of a specific owner (org or user)",
|
||||
},
|
||||
repoFieldsFlag,
|
||||
&typeFilterFlag,
|
||||
&flags.PaginationPageFlag,
|
||||
@@ -59,7 +67,26 @@ func RunReposList(_ stdctx.Context, cmd *cli.Command) error {
|
||||
}
|
||||
|
||||
var rps []*gitea.Repository
|
||||
if teaCmd.Bool("starred") {
|
||||
if owner := teaCmd.String("owner"); owner != "" {
|
||||
var err error
|
||||
_, resp, orgErr := client.GetOrg(owner)
|
||||
if orgErr != nil {
|
||||
if resp == nil || resp.StatusCode != http.StatusNotFound {
|
||||
return fmt.Errorf("could not find owner: %w", orgErr)
|
||||
}
|
||||
// not an org, treat as user
|
||||
rps, _, err = client.ListUserRepos(owner, gitea.ListReposOptions{
|
||||
ListOptions: flags.GetListOptions(),
|
||||
})
|
||||
} else {
|
||||
rps, _, err = client.ListOrgRepos(owner, gitea.ListOrgReposOptions{
|
||||
ListOptions: flags.GetListOptions(),
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if teaCmd.Bool("starred") {
|
||||
user, _, err := client.GetMyUserInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -6,6 +6,7 @@ package repos
|
||||
import (
|
||||
stdctx "context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
@@ -63,11 +64,10 @@ func runReposSearch(_ stdctx.Context, cmd *cli.Command) error {
|
||||
var ownerID int64
|
||||
if teaCmd.IsSet("owner") {
|
||||
// test if owner is an organization
|
||||
org, _, err := client.GetOrg(teaCmd.String("owner"))
|
||||
org, resp, err := client.GetOrg(teaCmd.String("owner"))
|
||||
if err != nil {
|
||||
// HACK: the client does not return a response on 404, so we can't check res.StatusCode
|
||||
if err.Error() != "404 Not Found" {
|
||||
return fmt.Errorf("Could not find owner: %s", err)
|
||||
if resp == nil || resp.StatusCode != http.StatusNotFound {
|
||||
return fmt.Errorf("Could not find owner: %w", err)
|
||||
}
|
||||
|
||||
// if owner is no org, its a user
|
||||
|
||||
@@ -1007,6 +1007,8 @@ Show repository details
|
||||
|
||||
**--output, -o**="": Output format. (simple, table, csv, tsv, yaml, json)
|
||||
|
||||
**--owner, -O**="": List repos of a specific owner (org or user)
|
||||
|
||||
**--page, -p**="": specify page (default: 1)
|
||||
|
||||
**--starred, -s**: List your starred repos instead
|
||||
@@ -1029,6 +1031,8 @@ List repositories you have access to
|
||||
|
||||
**--output, -o**="": Output format. (simple, table, csv, tsv, yaml, json)
|
||||
|
||||
**--owner, -O**="": List repos of a specific owner (org or user)
|
||||
|
||||
**--page, -p**="": specify page (default: 1)
|
||||
|
||||
**--starred, -s**: List your starred repos instead
|
||||
|
||||
Reference in New Issue
Block a user