forked from chroe/gitlink-cli
134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
package label
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
func v1RepoPath(ctx *common.RuntimeContext) string {
|
|
return fmt.Sprintf("/v1/%s/%s", ctx.Owner, ctx.Repo)
|
|
}
|
|
|
|
func Shortcuts() []*common.Shortcut {
|
|
return []*common.Shortcut{
|
|
{
|
|
Name: "list",
|
|
Description: "List issue labels",
|
|
Flags: []common.Flag{
|
|
{Name: "keyword", Short: "k", Usage: "Search keyword"},
|
|
{Name: "page", Short: "p", Usage: "Page number", Default: "1"},
|
|
{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return fmt.Errorf("解析仓库信息失败: %w", err)
|
|
}
|
|
q := url.Values{}
|
|
q.Set("page", ctx.Arg("page"))
|
|
q.Set("limit", ctx.Arg("limit"))
|
|
if k := ctx.Arg("keyword"); k != "" {
|
|
q.Set("keyword", k)
|
|
}
|
|
env, err := ctx.CallAPIWithQuery("GET", v1RepoPath(ctx)+"/issue_tags", q)
|
|
if err != nil {
|
|
return fmt.Errorf("获取标签列表失败: %w", err)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "create",
|
|
Description: "Create an issue label",
|
|
Flags: []common.Flag{
|
|
{Name: "name", Short: "n", Usage: "Label name", Required: true},
|
|
{Name: "color", Short: "c", Usage: "Label color (hex, e.g. #FF0000)", Default: "#F17013"},
|
|
{Name: "description", Short: "d", Usage: "Label description"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return fmt.Errorf("解析仓库信息失败: %w", err)
|
|
}
|
|
name, err := ctx.RequireArg("name")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body := map[string]interface{}{
|
|
"name": name,
|
|
"color": ctx.Arg("color"),
|
|
}
|
|
if desc := ctx.Arg("description"); desc != "" {
|
|
body["description"] = desc
|
|
}
|
|
env, err := ctx.CallAPI("POST", v1RepoPath(ctx)+"/issue_tags", body)
|
|
if err != nil {
|
|
return fmt.Errorf("创建标签失败: %w", err)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "update",
|
|
Description: "Update an issue label",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "Label ID", Required: true},
|
|
{Name: "name", Short: "n", Usage: "New name"},
|
|
{Name: "color", Short: "c", Usage: "New color (hex, e.g. #FF0000)"},
|
|
{Name: "description", Short: "d", Usage: "New description"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return fmt.Errorf("解析仓库信息失败: %w", err)
|
|
}
|
|
id, err := ctx.RequireArg("id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
name := ctx.Arg("name")
|
|
color := ctx.Arg("color")
|
|
desc := ctx.Arg("description")
|
|
if name == "" && color == "" && desc == "" {
|
|
return fmt.Errorf("至少需要指定 --name、--color 或 --description 中的一个")
|
|
}
|
|
body := map[string]interface{}{}
|
|
if name != "" {
|
|
body["name"] = name
|
|
}
|
|
if color != "" {
|
|
body["color"] = color
|
|
}
|
|
if desc != "" {
|
|
body["description"] = desc
|
|
}
|
|
env, err := ctx.CallAPI("PATCH", fmt.Sprintf("%s/issue_tags/%s", v1RepoPath(ctx), id), body)
|
|
if err != nil {
|
|
return fmt.Errorf("更新标签失败: %w", err)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "delete",
|
|
Description: "Delete an issue label",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "Label ID", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return fmt.Errorf("解析仓库信息失败: %w", err)
|
|
}
|
|
id, err := ctx.RequireArg("id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("DELETE", fmt.Sprintf("%s/issue_tags/%s", v1RepoPath(ctx), id), nil)
|
|
if err != nil {
|
|
return fmt.Errorf("删除标签失败: %w", err)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
}
|
|
}
|