fix(labels): add org label for ls and pr (#1017)

Fix https://gitea.com/gitea/tea/issues/634, https://gitea.com/gitea/tea/issues/669

Reviewed-on: https://gitea.com/gitea/tea/pulls/1017
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Minjie Fang <wingsallen@gmail.com>
Co-committed-by: Minjie Fang <wingsallen@gmail.com>
This commit is contained in:
Minjie Fang
2026-06-24 17:46:12 +00:00
committed by Lunny Xiao
parent 5cfee362c8
commit 88f5cdcafa
8 changed files with 371 additions and 13 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"errors"
"fmt"
"gitea.dev/sdk"
gitea "gitea.dev/sdk"
"github.com/urfave/cli/v3"
)
+53 -6
View File
@@ -5,8 +5,9 @@ package labels
import (
stdctx "context"
"log"
"gitea.dev/sdk"
gitea "gitea.dev/sdk"
"gitea.dev/tea/cmd/flags"
"gitea.dev/tea/modules/context"
@@ -29,6 +30,14 @@ var CmdLabelsList = cli.Command{
Aliases: []string{"s"},
Usage: "Save all the labels as a file",
},
&cli.StringFlag{
Name: "org",
Usage: "List organization labels",
},
&cli.BoolFlag{
Name: "exclude-org",
Usage: "Exclude organization labels from the list",
},
&flags.PaginationPageFlag,
&flags.PaginationLimitFlag,
}, flags.AllDefaultFlags...),
@@ -40,14 +49,18 @@ func RunLabelsList(requestCtx stdctx.Context, cmd *cli.Command) error {
if err != nil {
return err
}
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
return err
if cmd.String("org") == "" && cmd.String("repo") == "" {
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
return err
}
}
if cmd.String("org") != "" && cmd.String("repo") != "" {
log.Printf("Warning: Both --org and --repo flags are set. Ignoring --org and using --repo value as owner.\n")
}
client := ctx.Login.Client()
labels, _, err := client.Repositories.ListRepoLabels(requestCtx, ctx.Owner, ctx.Repo, gitea.ListLabelsOptions{
ListOptions: flags.GetListOptions(cmd),
})
labels, err := collectLabels(requestCtx, client, ctx, cmd)
if err != nil {
return err
}
@@ -58,3 +71,37 @@ func RunLabelsList(requestCtx stdctx.Context, cmd *cli.Command) error {
return print.LabelsList(labels, ctx.Output)
}
// CollectLabels collects labels based on the provided command flags and context.
func collectLabels(requestCtx stdctx.Context, client *gitea.Client, ctx *context.TeaContext, cmd *cli.Command) ([]*gitea.Label, error) {
var labels []*gitea.Label
var err error
owner := ctx.Owner
// If --org is set but --repo is not, list organization labels only and skip repository labels.
if cmd.String("org") != "" && cmd.String("repo") == "" {
owner = ctx.Org
} else {
labels, _, err = client.Repositories.ListRepoLabels(requestCtx, owner, ctx.Repo, gitea.ListLabelsOptions{
ListOptions: flags.GetListOptions(cmd),
})
if err != nil {
log.Printf("Failed to list repository labels: %v", err)
}
}
// If --exclude is not set and the owner is an organization, also list organization labels and append them to the list.
if !ctx.IsSet("exclude-org") {
if _, _, err = client.Organizations.GetOrg(requestCtx, owner); err == nil {
orgLabels, _, err := client.Organizations.ListOrgLabels(requestCtx, owner, gitea.ListOrgLabelsOptions{
ListOptions: flags.GetListOptions(cmd),
})
if err != nil {
return nil, err
}
labels = append(labels, orgLabels...)
}
}
return labels, nil
}