From 67f8068e3e92e74b40486ea5aae5750fbddaf1c1 Mon Sep 17 00:00:00 2001 From: ZxR <3193291180@qq.com> Date: Tue, 2 Jun 2026 11:14:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0label=20=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/changes/label-shortcut.md | 10 +++ shortcuts/label/label.go | 114 ++++++++++++++++++++++++++++++ shortcuts/label/label_test.go | 128 ++++++++++++++++++++++++++++++++++ shortcuts/register.go | 3 + 4 files changed, 255 insertions(+) create mode 100644 doc/changes/label-shortcut.md create mode 100644 shortcuts/label/label.go create mode 100644 shortcuts/label/label_test.go diff --git a/doc/changes/label-shortcut.md b/doc/changes/label-shortcut.md new file mode 100644 index 0000000..121120c --- /dev/null +++ b/doc/changes/label-shortcut.md @@ -0,0 +1,10 @@ +# label shortcut + +新增 `label` 命令组,支持仓库标签管理: + +| 命令 | 功能 | +|------|------| +| `label +list` | 列出仓库所有标签 | +| `label +create --name --color ` | 创建标签 | +| `label +update --id [--name ] [--color ]` | 更新标签 | +| `label +delete --id ` | 删除标签 | diff --git a/shortcuts/label/label.go b/shortcuts/label/label.go new file mode 100644 index 0000000..11e345f --- /dev/null +++ b/shortcuts/label/label.go @@ -0,0 +1,114 @@ +package label + +import ( + "fmt" + + "github.com/gitlink-org/gitlink-cli/shortcuts/common" +) + +func Shortcuts() []*common.Shortcut { + return []*common.Shortcut{ + { + Name: "list", + Description: "List repository labels", + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + env, err := ctx.CallAPI("GET", labelPath(ctx), nil) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "create", + Description: "Create a repository label", + Flags: []common.Flag{ + {Name: "name", Short: "n", Usage: "Label name", Required: true}, + {Name: "color", Short: "c", Usage: "Label color (hex, e.g. #ff0000)", Required: true}, + }, + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + name, err := ctx.RequireArg("name") + if err != nil { + return err + } + color, err := ctx.RequireArg("color") + if err != nil { + return err + } + env, err := ctx.CallAPI("POST", labelPath(ctx), map[string]interface{}{ + "name": name, + "color": color, + }) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "update", + Description: "Update a repository label", + Flags: []common.Flag{ + {Name: "id", Short: "i", Usage: "Label ID", Required: true}, + {Name: "name", Short: "n", Usage: "New label name"}, + {Name: "color", Short: "c", Usage: "New label color (hex)"}, + }, + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + id, err := ctx.RequireArg("id") + if err != nil { + return err + } + payload := map[string]interface{}{} + if v := ctx.Arg("name"); v != "" { + payload["name"] = v + } + if v := ctx.Arg("color"); v != "" { + payload["color"] = v + } + env, err := ctx.CallAPI("PATCH", labelItemPath(ctx, id), payload) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "delete", + Description: "Delete a repository 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 err + } + id, err := ctx.RequireArg("id") + if err != nil { + return err + } + env, err := ctx.CallAPI("DELETE", labelItemPath(ctx, id), nil) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + } +} + +func labelPath(ctx *common.RuntimeContext) string { + return fmt.Sprintf("/v1/%s/%s/labels", ctx.Owner, ctx.Repo) +} + +func labelItemPath(ctx *common.RuntimeContext, id string) string { + return fmt.Sprintf("%s/%s", labelPath(ctx), id) +} diff --git a/shortcuts/label/label_test.go b/shortcuts/label/label_test.go new file mode 100644 index 0000000..c783970 --- /dev/null +++ b/shortcuts/label/label_test.go @@ -0,0 +1,128 @@ +package label + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gitlink-org/gitlink-cli/internal/client" + "github.com/gitlink-org/gitlink-cli/shortcuts/common" +) + +func TestLabelList(t *testing.T) { + server := newTestServer(t, func(w http.ResponseWriter, r *http.Request) { + assertRequest(t, r, "GET", "/v1/owner/repo/labels.json") + writeJSON(t, w, []interface{}{}) + }) + defer server.Close() + + if err := runShortcut(t, server, "list", nil); err != nil { + t.Fatalf("list failed: %v", err) + } +} + +func TestLabelCreate(t *testing.T) { + var payload map[string]interface{} + server := newTestServer(t, func(w http.ResponseWriter, r *http.Request) { + assertRequest(t, r, "POST", "/v1/owner/repo/labels.json") + payload = decodeJSON(t, r) + writeJSON(t, w, map[string]interface{}{"id": 1}) + }) + defer server.Close() + + if err := runShortcut(t, server, "create", map[string]string{"name": "bug", "color": "#ff0000"}); err != nil { + t.Fatalf("create failed: %v", err) + } + assertEqual(t, payload["name"], "bug") + assertEqual(t, payload["color"], "#ff0000") +} + +func TestLabelUpdate(t *testing.T) { + var payload map[string]interface{} + server := newTestServer(t, func(w http.ResponseWriter, r *http.Request) { + assertRequest(t, r, "PATCH", "/v1/owner/repo/labels/3.json") + payload = decodeJSON(t, r) + writeJSON(t, w, map[string]interface{}{"id": 3}) + }) + defer server.Close() + + if err := runShortcut(t, server, "update", map[string]string{"id": "3", "name": "enhancement", "color": "#00ff00"}); err != nil { + t.Fatalf("update failed: %v", err) + } + assertEqual(t, payload["name"], "enhancement") + assertEqual(t, payload["color"], "#00ff00") +} + +func TestLabelDelete(t *testing.T) { + server := newTestServer(t, func(w http.ResponseWriter, r *http.Request) { + assertRequest(t, r, "DELETE", "/v1/owner/repo/labels/3.json") + writeJSON(t, w, map[string]interface{}{"status": 0}) + }) + defer server.Close() + + if err := runShortcut(t, server, "delete", map[string]string{"id": "3"}); err != nil { + t.Fatalf("delete failed: %v", err) + } +} + +func runShortcut(t *testing.T, server *httptest.Server, name string, args map[string]string) error { + t.Helper() + var shortcut *common.Shortcut + for _, s := range Shortcuts() { + if s.Name == name { + shortcut = s + break + } + } + if shortcut == nil { + t.Fatalf("shortcut %q not found", name) + } + if args == nil { + args = map[string]string{} + } + ctx := &common.RuntimeContext{ + Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL}, + Owner: "owner", + Repo: "repo", + Format: "json", + Args: args, + } + return shortcut.Run(ctx) +} + +func newTestServer(t *testing.T, handler http.HandlerFunc) *httptest.Server { + t.Helper() + return httptest.NewServer(handler) +} + +func assertRequest(t *testing.T, r *http.Request, method, path string) { + t.Helper() + if r.Method != method || r.URL.Path != path { + t.Fatalf("got %s %s, want %s %s", r.Method, r.URL.Path, method, path) + } +} + +func decodeJSON(t *testing.T, r *http.Request) map[string]interface{} { + t.Helper() + var payload map[string]interface{} + if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { + t.Fatalf("decode body: %v", err) + } + return payload +} + +func writeJSON(t *testing.T, w http.ResponseWriter, v interface{}) { + t.Helper() + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(v); err != nil { + t.Fatalf("write response: %v", err) + } +} + +func assertEqual(t *testing.T, got, want interface{}) { + t.Helper() + if got != want { + t.Fatalf("got %v, want %v", got, want) + } +} diff --git a/shortcuts/register.go b/shortcuts/register.go index 559ec06..3b3d8be 100644 --- a/shortcuts/register.go +++ b/shortcuts/register.go @@ -4,6 +4,7 @@ import ( "github.com/spf13/cobra" "github.com/gitlink-org/gitlink-cli/shortcuts/branch" + "github.com/gitlink-org/gitlink-cli/shortcuts/label" "github.com/gitlink-org/gitlink-cli/shortcuts/ci" "github.com/gitlink-org/gitlink-cli/shortcuts/common" "github.com/gitlink-org/gitlink-cli/shortcuts/compare" @@ -39,6 +40,7 @@ func RegisterAll(root *cobra.Command) { "webhook": webhook.Shortcuts(), "workflow": workflow.Shortcuts(), "wiki": wiki.Shortcuts(), + "label": label.Shortcuts(), } descriptions := map[string]string{ @@ -57,6 +59,7 @@ func RegisterAll(root *cobra.Command) { "webhook": "Webhook operations", "workflow": "AI agent workflow analysis", "wiki": "Wiki page operations", + "label": "Label operations", } for name, shortcuts := range groups {