From ad906e212c07052c58458945295533aef9281596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E5=A4=A9=E7=BF=94?= Date: Wed, 3 Jun 2026 08:13:33 +0800 Subject: [PATCH 1/3] fix: handle XML declaration in detectHTMLResponse and remove .devops/ci.yml - Strip declaration before checking HTML prefixes - Remove .devops/ci.yml containing personal DevCloud config Co-Authored-By: Claude Opus 4.7 --- .devops/ci.yml | 41 --------------------------------------- internal/client/client.go | 6 ++++++ 2 files changed, 6 insertions(+), 41 deletions(-) delete mode 100644 .devops/ci.yml diff --git a/.devops/ci.yml b/.devops/ci.yml deleted file mode 100644 index 7fdd6d7..0000000 --- a/.devops/ci.yml +++ /dev/null @@ -1,41 +0,0 @@ -version: 2 -name: gitlink_cli_ci -description: "gitlink-cli 代码提交时自动执行 CI 检查(构建、测试、格式化)" -trigger: - webhook: gitlink@1.0.0 - event: - - ref: push - ruleset-operator: AND -global: - concurrent: 1 -workflow: - - ref: start - name: 开始 - task: start - - ref: git_clone_0 - name: 拉取代码 - task: git_clone@1.2.9 - input: - remote_url: '"https://gitlink.org.cn/jiangtx/gitlink-cli.git"' - ref: '"refs/heads/jtx_branch"' - commit_id: '""' - depth: 1 - needs: - - start - - ref: ssh_cmd_0 - name: CI 检查 - task: ssh_cmd@1.1.1 - input: - ssh_pass: ((gitlink_cli_ci.ssh_pass)) - ssh_ip: '"121.41.212.97"' - ssh_port: '"22"' - ssh_user: '"root"' - ssh_cmd: >- - "cd /root && rm -rf gitlink-cli && git clone --depth=1 -b jtx_branch https://gitlink.org.cn/jiangtx/gitlink-cli.git && cd gitlink-cli && export PATH=$PATH:/usr/local/go/bin && export GOPROXY=https://goproxy.cn,direct && go version && go build ./... && go vet ./... && go test -race ./... && output=$(gofmt -s -l .) && if [ -n \"$output\" ]; then echo '格式化检查失败:' && echo \"$output\" && exit 1; fi && echo '所有 CI 检查通过'" - needs: - - git_clone_0 - - ref: end - name: 结束 - task: end - needs: - - ssh_cmd_0 diff --git a/internal/client/client.go b/internal/client/client.go index 94f0cb1..511c5d3 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -232,6 +232,12 @@ func detectHTMLResponse(data []byte) bool { if len(trimmed) == 0 { return false } + // Skip XML declaration (e.g. ) before checking HTML prefixes + if bytes.HasPrefix(trimmed, []byte("")); idx != -1 { + trimmed = bytes.TrimSpace(trimmed[idx+2:]) + } + } prefixes := []string{" Date: Wed, 3 Jun 2026 20:34:30 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Dockerfile=20?= =?UTF-8?q?=E5=92=8C=E5=BB=BA=E6=9C=A8=20DevOps=20=E6=B5=81=E6=B0=B4?= =?UTF-8?q?=E7=BA=BF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 多阶段 Docker 构建(golang:1.26-alpine + alpine:3.20) - 流水线:push 到 master 自动测试、构建、部署到 ECS Co-Authored-By: Claude Opus 4.7 --- .devops/gitlink-cli.yml | 41 +++++++++++++++++++++++++++++++++++++++++ Dockerfile | 20 ++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 .devops/gitlink-cli.yml create mode 100644 Dockerfile diff --git a/.devops/gitlink-cli.yml b/.devops/gitlink-cli.yml new file mode 100644 index 0000000..aa9a60c --- /dev/null +++ b/.devops/gitlink-cli.yml @@ -0,0 +1,41 @@ +version: 2 +name: jtx_gitlink_cli +description: "gitlink-cli 项目:代码提交时自动测试、构建并部署到服务器" +trigger: + webhook: gitlink@1.0.0 + event: + - ref: push + ruleset-operator: AND +global: + concurrent: 1 +workflow: + - ref: start + name: 开始 + task: start + - ref: git_clone_0 + name: git clone + task: git_clone@1.2.9 + input: + remote_url: '"https://gitlink.org.cn/jiangtx/gitlink-cli.git"' + ref: '"refs/heads/master"' + commit_id: '""' + depth: 1 + needs: + - start + - ref: ssh_cmd_0 + name: 测试并部署到服务器 + task: ssh_cmd@1.1.1 + input: + ssh_pass: ((jtx_gitlink_cli.jtx_gitlink_cli_ssh)) + ssh_ip: '"121.41.212.97"' + ssh_port: '"22"' + ssh_user: '"root"' + ssh_cmd: >- + "cd /root && rm -rf gitlink-cli && git clone --depth=1 https://gitlink.org.cn/jiangtx/gitlink-cli.git && cd gitlink-cli && docker run --rm -v $(pwd):/build -w /build docker.1ms.run/library/golang:1.26-alpine go test -race ./... && docker build --no-cache -t gitlink-cli . && docker stop gitlink-cli || true && docker rm gitlink-cli || true && docker run -d --restart=always --name gitlink-cli gitlink-cli version" + needs: + - git_clone_0 + - ref: end + name: 结束 + task: end + needs: + - ssh_cmd_0 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b8240b7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM docker.1ms.run/library/golang:1.26-alpine AS builder + +WORKDIR /build + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o gitlink-cli . + +FROM docker.1ms.run/library/alpine:3.20 + +RUN apk add --no-cache ca-certificates git + +COPY --from=builder /build/gitlink-cli /usr/local/bin/gitlink-cli + +RUN chmod +x /usr/local/bin/gitlink-cli + +ENTRYPOINT ["gitlink-cli"] -- 2.34.1 From 7e7e2ccb47e1d866912368a90b7620f5d711f72e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E5=A4=A9=E7=BF=94?= Date: Wed, 3 Jun 2026 20:45:15 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E7=AE=A1=E7=90=86=E3=80=81=E6=A0=87=E7=AD=BE=E5=85=8B?= =?UTF-8?q?=E9=9A=86=E3=80=81=E4=BB=93=E5=BA=93=E6=96=87=E4=BB=B6=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=AD=89=208=20=E9=A1=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增功能: - notification: list/read/read-all/watch 快捷命令 - repo: raw/activity/compare/create-file/update-file 快捷命令 - label: clone 快捷命令(从其他仓库克隆标签) - alias: +list/+set/+delete/+expand 子命令 修复: - auth.go: 修复 Windows 下 syscall.Stdin 类型兼容问题 - register_test: 更新分组计数以包含 notify Co-Authored-By: Claude Opus 4.7 --- cmd/alias/alias.go | 139 ++++++++++++++ cmd/auth/auth.go | 2 +- cmd/root.go | 2 + shortcuts/label/label.go | 100 +++++++++++ shortcuts/notification/notification.go | 97 ++++++++++ shortcuts/notification/notification_test.go | 189 ++++++++++++++++++++ shortcuts/register.go | 3 + shortcuts/register_test.go | 1 + shortcuts/repo/repo.go | 158 ++++++++++++++++ 9 files changed, 690 insertions(+), 1 deletion(-) create mode 100644 cmd/alias/alias.go create mode 100644 shortcuts/notification/notification.go create mode 100644 shortcuts/notification/notification_test.go diff --git a/cmd/alias/alias.go b/cmd/alias/alias.go new file mode 100644 index 0000000..bd2132a --- /dev/null +++ b/cmd/alias/alias.go @@ -0,0 +1,139 @@ +package alias + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/spf13/cobra" + "gopkg.in/yaml.v3" + + "github.com/gitlink-org/gitlink-cli/internal/config" +) + +func NewAliasCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "alias", + Short: "Manage command aliases", + } + cmd.AddCommand(newListCmd()) + cmd.AddCommand(newSetCmd()) + cmd.AddCommand(newDeleteCmd()) + cmd.AddCommand(newExpandCmd()) + return cmd +} + +func aliasPath() string { + return filepath.Join(config.ConfigDir(), "aliases.yaml") +} + +func loadAliases() (map[string]string, error) { + data, err := os.ReadFile(aliasPath()) + if err != nil { + if os.IsNotExist(err) { + return map[string]string{}, nil + } + return nil, err + } + aliases := make(map[string]string) + if err := yaml.Unmarshal(data, &aliases); err != nil { + return nil, err + } + return aliases, nil +} + +func saveAliases(aliases map[string]string) error { + dir := config.ConfigDir() + if err := os.MkdirAll(dir, 0700); err != nil { + return err + } + data, err := yaml.Marshal(aliases) + if err != nil { + return err + } + return os.WriteFile(aliasPath(), data, 0600) +} + +func newListCmd() *cobra.Command { + return &cobra.Command{ + Use: "+list", + Short: "List all aliases", + RunE: func(cmd *cobra.Command, args []string) error { + aliases, err := loadAliases() + if err != nil { + return err + } + if len(aliases) == 0 { + fmt.Println("No aliases defined.") + return nil + } + for name, expanded := range aliases { + fmt.Printf("%s = %s\n", name, expanded) + } + return nil + }, + } +} + +func newSetCmd() *cobra.Command { + return &cobra.Command{ + Use: "+set ", + Short: "Set an alias", + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + aliases, err := loadAliases() + if err != nil { + return err + } + aliases[args[0]] = args[1] + if err := saveAliases(aliases); err != nil { + return err + } + fmt.Printf("Alias %s = %s\n", args[0], args[1]) + return nil + }, + } +} + +func newDeleteCmd() *cobra.Command { + return &cobra.Command{ + Use: "+delete ", + Short: "Delete an alias", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + aliases, err := loadAliases() + if err != nil { + return err + } + if _, ok := aliases[args[0]]; !ok { + return fmt.Errorf("alias %q not found", args[0]) + } + delete(aliases, args[0]) + if err := saveAliases(aliases); err != nil { + return err + } + fmt.Printf("Deleted alias %s\n", args[0]) + return nil + }, + } +} + +func newExpandCmd() *cobra.Command { + return &cobra.Command{ + Use: "+expand ", + Short: "Expand an alias to its full command", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + aliases, err := loadAliases() + if err != nil { + return err + } + expanded, ok := aliases[args[0]] + if !ok { + return fmt.Errorf("alias %q not found", args[0]) + } + fmt.Println(expanded) + return nil + }, + } +} diff --git a/cmd/auth/auth.go b/cmd/auth/auth.go index e9af757..ed9dc22 100644 --- a/cmd/auth/auth.go +++ b/cmd/auth/auth.go @@ -51,7 +51,7 @@ func loginWithPassword() error { username = strings.TrimSpace(username) fmt.Print("Password: ") - passwordBytes, err := term.ReadPassword(syscall.Stdin) + passwordBytes, err := term.ReadPassword(int(syscall.Stdin)) if err != nil { return fmt.Errorf("failed to read password: %w", err) } diff --git a/cmd/root.go b/cmd/root.go index 441a8ed..1bde0f4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" + aliasCmd "github.com/gitlink-org/gitlink-cli/cmd/alias" apiCmd "github.com/gitlink-org/gitlink-cli/cmd/api" authCmd "github.com/gitlink-org/gitlink-cli/cmd/auth" "github.com/gitlink-org/gitlink-cli/cmd/cmdutil" @@ -32,6 +33,7 @@ func init() { rootCmd.AddCommand(authCmd.NewAuthCmd()) rootCmd.AddCommand(apiCmd.NewAPICmd()) rootCmd.AddCommand(configCmd.NewConfigCmd()) + rootCmd.AddCommand(aliasCmd.NewAliasCmd()) rootCmd.AddCommand(versionCmd) shortcuts.RegisterAll(rootCmd) diff --git a/shortcuts/label/label.go b/shortcuts/label/label.go index 2b6a298..6dd847e 100644 --- a/shortcuts/label/label.go +++ b/shortcuts/label/label.go @@ -91,6 +91,16 @@ func Shortcuts() []*common.Shortcut { return ctx.Output(env) }, }, + { + Name: "clone", + Description: "Clone labels from another repository", + Flags: []common.Flag{ + {Name: "source-owner", Short: "o", Usage: "Source repository owner", Required: true}, + {Name: "source-repo", Short: "r", Usage: "Source repository name", Required: true}, + {Name: "overwrite", Usage: "Overwrite existing labels with same name (true/false)", Default: "false"}, + }, + Run: runClone, + }, } } @@ -242,3 +252,93 @@ func firstNonEmpty(values ...string) string { } return "" } + +func runClone(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + sourceOwner, err := ctx.RequireArg("source-owner") + if err != nil { + return err + } + sourceRepo, err := ctx.RequireArg("source-repo") + if err != nil { + return err + } + + sourcePath := fmt.Sprintf("/v1/%s/%s/issue_tags", sourceOwner, sourceRepo) + env, err := ctx.CallAPI("GET", sourcePath, nil) + if err != nil { + return fmt.Errorf("failed to fetch source labels: %w", err) + } + data, ok := env.Data.(map[string]interface{}) + if !ok { + return fmt.Errorf("unexpected response format from source repository") + } + rawTags, ok := data["issue_tags"].([]interface{}) + if !ok { + return fmt.Errorf("no issue_tags found in source repository") + } + + overwrite := ctx.Arg("overwrite") == "true" + created := 0 + skipped := 0 + + for _, raw := range rawTags { + tag, ok := raw.(map[string]interface{}) + if !ok { + continue + } + name := stringFromMap(tag, "name") + if name == "" { + continue + } + if !overwrite { + if existing, _ := fetchLabelByName(ctx, name); existing != nil { + skipped++ + continue + } + } + color := stringFromMap(tag, "color") + if color == "" { + color = defaultLabelColor + } + payload := map[string]interface{}{ + "name": name, + "description": stringFromMap(tag, "description"), + "color": color, + } + if _, err := ctx.CallAPI("POST", labelPath(ctx), payload); err != nil { + return fmt.Errorf("failed to create label %q: %w", name, err) + } + created++ + } + + fmt.Printf("Cloned %d labels from %s/%s (skipped %d existing)\n", created, sourceOwner, sourceRepo, skipped) + return nil +} + +func fetchLabelByName(ctx *common.RuntimeContext, name string) (map[string]interface{}, error) { + env, err := ctx.CallAPI("GET", labelPath(ctx), nil) + if err != nil { + return nil, err + } + data, ok := env.Data.(map[string]interface{}) + if !ok { + return nil, nil + } + rawTags, ok := data["issue_tags"].([]interface{}) + if !ok { + return nil, nil + } + for _, raw := range rawTags { + tag, ok := raw.(map[string]interface{}) + if !ok { + continue + } + if stringFromMap(tag, "name") == name { + return tag, nil + } + } + return nil, nil +} diff --git a/shortcuts/notification/notification.go b/shortcuts/notification/notification.go new file mode 100644 index 0000000..ac58be8 --- /dev/null +++ b/shortcuts/notification/notification.go @@ -0,0 +1,97 @@ +package notification + +import ( + "fmt" + "net/url" + + "github.com/gitlink-org/gitlink-cli/shortcuts/common" +) + +func Shortcuts() []*common.Shortcut { + return []*common.Shortcut{ + { + Name: "list", + Description: "列出通知", + Flags: []common.Flag{ + {Name: "all", Usage: "显示所有通知(含已读)", Bool: true, Default: "false"}, + {Name: "participating", Usage: "仅显示参与的通知", Bool: true, Default: "false"}, + {Name: "page", Short: "p", Usage: "页码", Default: "1"}, + {Name: "limit", Short: "l", Usage: "每页数量", Default: "20"}, + }, + Run: func(ctx *common.RuntimeContext) error { + q := url.Values{} + q.Set("page", ctx.Arg("page")) + q.Set("limit", ctx.Arg("limit")) + if ctx.Arg("all") == "true" { + q.Set("all", "true") + } + if ctx.Arg("participating") == "true" { + q.Set("participating", "true") + } + env, err := ctx.CallAPIWithQuery("GET", "/notifications", q) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "read", + Description: "标记单条通知为已读", + Flags: []common.Flag{ + {Name: "id", Short: "i", Usage: "通知 ID", Required: true}, + }, + Run: func(ctx *common.RuntimeContext) error { + id, err := ctx.RequireArg("id") + if err != nil { + return err + } + env, err := ctx.CallAPI("PUT", fmt.Sprintf("/notifications/%s", id), nil) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "read-all", + Description: "标记所有通知为已读", + Run: func(ctx *common.RuntimeContext) error { + env, err := ctx.CallAPI("PUT", "/notifications", nil) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "watch", + Description: "关注或取消关注仓库的通知", + Flags: []common.Flag{ + {Name: "owner", Short: "o", Usage: "仓库所有者", Required: true}, + {Name: "repo", Short: "r", Usage: "仓库名称", Required: true}, + {Name: "unwatch", Usage: "取消关注(默认为关注)", Bool: true, Default: "false"}, + }, + Run: func(ctx *common.RuntimeContext) error { + owner, err := ctx.RequireArg("owner") + if err != nil { + return err + } + repo, err := ctx.RequireArg("repo") + if err != nil { + return err + } + path := fmt.Sprintf("/watchers/%s/%s.json", owner, repo) + method := "POST" + if ctx.Arg("unwatch") == "true" { + method = "DELETE" + } + env, err := ctx.CallAPI(method, path, nil) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + } +} diff --git a/shortcuts/notification/notification_test.go b/shortcuts/notification/notification_test.go new file mode 100644 index 0000000..034b28f --- /dev/null +++ b/shortcuts/notification/notification_test.go @@ -0,0 +1,189 @@ +package notification + +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 runNotifShortcut(t *testing.T, server *httptest.Server, name string, args map[string]string) error { + t.Helper() + s := findNotifShortcut(t, name) + ctx := &common.RuntimeContext{ + Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL}, + Owner: "owner", + Repo: "repo", + Format: "json", + Args: args, + } + return s.Run(ctx) +} + +func findNotifShortcut(t *testing.T, name string) *common.Shortcut { + t.Helper() + for _, s := range Shortcuts() { + if s.Name == name { + return s + } + } + t.Fatalf("shortcut %q not found", name) + return nil +} + +func writeNotifJSON(w http.ResponseWriter, v interface{}) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(v) +} + +// --- list --- + +func TestNotifListBasic(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + t.Fatalf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/notifications.json" { + t.Fatalf("unexpected path: %s", r.URL.Path) + } + if got := r.URL.Query().Get("page"); got != "1" { + t.Fatalf("got page %q, want %q", got, "1") + } + if got := r.URL.Query().Get("limit"); got != "20" { + t.Fatalf("got limit %q, want %q", got, "20") + } + writeNotifJSON(w, map[string]interface{}{ + "total_count": float64(1), + "notifications": []interface{}{ + map[string]interface{}{"id": float64(1), "unread": true}, + }, + }) + })) + defer server.Close() + + err := runNotifShortcut(t, server, "list", map[string]string{ + "page": "1", "limit": "20", "all": "false", "participating": "false", + }) + if err != nil { + t.Fatalf("list failed: %v", err) + } +} + +func TestNotifListWithAll(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if got := r.URL.Query().Get("all"); got != "true" { + t.Fatalf("expected all=true, got %q", got) + } + writeNotifJSON(w, map[string]interface{}{"total_count": float64(0), "notifications": []interface{}{}}) + })) + defer server.Close() + + err := runNotifShortcut(t, server, "list", map[string]string{ + "page": "1", "limit": "20", "all": "true", "participating": "false", + }) + if err != nil { + t.Fatalf("list with all failed: %v", err) + } +} + +func TestNotifListWithParticipating(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if got := r.URL.Query().Get("participating"); got != "true" { + t.Fatalf("expected participating=true, got %q", got) + } + writeNotifJSON(w, map[string]interface{}{"total_count": float64(0), "notifications": []interface{}{}}) + })) + defer server.Close() + + err := runNotifShortcut(t, server, "list", map[string]string{ + "page": "1", "limit": "20", "all": "false", "participating": "true", + }) + if err != nil { + t.Fatalf("list with participating failed: %v", err) + } +} + +// --- read --- + +func TestNotifRead(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "PUT" { + t.Fatalf("expected PUT, got %s", r.Method) + } + if r.URL.Path != "/notifications/42.json" { + t.Fatalf("unexpected path: %s", r.URL.Path) + } + writeNotifJSON(w, map[string]interface{}{"status": 0, "message": "success"}) + })) + defer server.Close() + + err := runNotifShortcut(t, server, "read", map[string]string{"id": "42"}) + if err != nil { + t.Fatalf("read failed: %v", err) + } +} + +// --- read-all --- + +func TestNotifReadAll(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "PUT" { + t.Fatalf("expected PUT, got %s", r.Method) + } + if r.URL.Path != "/notifications.json" { + t.Fatalf("unexpected path: %s", r.URL.Path) + } + writeNotifJSON(w, map[string]interface{}{"status": 0, "message": "success"}) + })) + defer server.Close() + + err := runNotifShortcut(t, server, "read-all", map[string]string{}) + if err != nil { + t.Fatalf("read-all failed: %v", err) + } +} + +// --- watch --- + +func TestNotifWatch(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + t.Fatalf("expected POST, got %s", r.Method) + } + if r.URL.Path != "/watchers/alice/repo.json" { + t.Fatalf("unexpected path: %s", r.URL.Path) + } + writeNotifJSON(w, map[string]interface{}{"status": 0, "message": "success"}) + })) + defer server.Close() + + err := runNotifShortcut(t, server, "watch", map[string]string{ + "owner": "alice", "repo": "repo", "unwatch": "false", + }) + if err != nil { + t.Fatalf("watch failed: %v", err) + } +} + +func TestNotifUnwatch(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "DELETE" { + t.Fatalf("expected DELETE, got %s", r.Method) + } + if r.URL.Path != "/watchers/bob/project.json" { + t.Fatalf("unexpected path: %s", r.URL.Path) + } + writeNotifJSON(w, map[string]interface{}{"status": 0, "message": "success"}) + })) + defer server.Close() + + err := runNotifShortcut(t, server, "watch", map[string]string{ + "owner": "bob", "repo": "project", "unwatch": "true", + }) + if err != nil { + t.Fatalf("unwatch failed: %v", err) + } +} diff --git a/shortcuts/register.go b/shortcuts/register.go index 50fa41e..66d613e 100644 --- a/shortcuts/register.go +++ b/shortcuts/register.go @@ -11,6 +11,7 @@ import ( "github.com/gitlink-org/gitlink-cli/shortcuts/label" "github.com/gitlink-org/gitlink-cli/shortcuts/member" "github.com/gitlink-org/gitlink-cli/shortcuts/milestone" + "github.com/gitlink-org/gitlink-cli/shortcuts/notification" "github.com/gitlink-org/gitlink-cli/shortcuts/org" "github.com/gitlink-org/gitlink-cli/shortcuts/pipeline" "github.com/gitlink-org/gitlink-cli/shortcuts/pr" @@ -35,6 +36,7 @@ func RegisterAll(root *cobra.Command) { "release": release.Shortcuts(), "branch": branch.Shortcuts(), "org": org.Shortcuts(), + "notify": notification.Shortcuts(), "user": user.Shortcuts(), "search": search.Shortcuts(), "ci": ci.Shortcuts(), @@ -54,6 +56,7 @@ func RegisterAll(root *cobra.Command) { "release": "Release operations", "branch": "Branch operations", "org": "Organization operations", + "notify": "Notification operations", "user": "User operations", "search": "Search operations", "ci": "CI/CD operations", diff --git a/shortcuts/register_test.go b/shortcuts/register_test.go index e3f1351..22a19d9 100644 --- a/shortcuts/register_test.go +++ b/shortcuts/register_test.go @@ -14,6 +14,7 @@ func TestRegisterAll(t *testing.T) { "repo", "issue", "label", "pr", "release", "branch", "org", "user", "search", "ci", "workflow", "compare", "member", "milestone", "pipeline", "webhook", + "notify", } groupSet := map[string]bool{} diff --git a/shortcuts/repo/repo.go b/shortcuts/repo/repo.go index 24f467c..e66b33a 100644 --- a/shortcuts/repo/repo.go +++ b/shortcuts/repo/repo.go @@ -257,5 +257,163 @@ func Shortcuts() []*common.Shortcut { return ctx.Output(env) }, }, + { + Name: "raw", + Description: "Get raw file content from a repository", + Flags: []common.Flag{ + {Name: "filepath", Short: "f", Usage: "Path to the file", Required: true}, + {Name: "ref", Short: "r", Usage: "Branch, tag, or commit SHA (default: default branch)"}, + }, + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + filepath, err := ctx.RequireArg("filepath") + if err != nil { + return err + } + q := url.Values{} + q.Set("filepath", filepath) + if ref := ctx.Arg("ref"); ref != "" { + q.Set("ref", ref) + } + env, err := ctx.CallAPIWithQuery("GET", ctx.RepoPath()+"/raw", q) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "activity", + Description: "List recent activity/events of a repository", + Flags: []common.Flag{ + {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 err + } + q := url.Values{} + q.Set("page", ctx.Arg("page")) + q.Set("limit", ctx.Arg("limit")) + env, err := ctx.CallAPIWithQuery("GET", ctx.RepoPath()+"/activity", q) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "compare", + Description: "Compare two commits, branches, or tags", + Flags: []common.Flag{ + {Name: "from", Short: "f", Usage: "Base ref (branch/tag/SHA)", Required: true}, + {Name: "to", Short: "t", Usage: "Head ref (branch/tag/SHA)", Required: true}, + }, + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + from, err := ctx.RequireArg("from") + if err != nil { + return err + } + to, err := ctx.RequireArg("to") + if err != nil { + return err + } + env, err := ctx.CallAPI("GET", fmt.Sprintf("%s/compare/%s...%s", ctx.RepoPath(), from, to), nil) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "create-file", + Description: "Create a new file in a repository", + Flags: []common.Flag{ + {Name: "filepath", Short: "f", Usage: "Path for the new file", Required: true}, + {Name: "content", Short: "c", Usage: "File content (base64 or plain text)", Required: true}, + {Name: "message", Short: "m", Usage: "Commit message", Required: true}, + {Name: "branch", Short: "b", Usage: "Target branch (default: default branch)"}, + }, + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + filepath, err := ctx.RequireArg("filepath") + if err != nil { + return err + } + content, err := ctx.RequireArg("content") + if err != nil { + return err + } + message, err := ctx.RequireArg("message") + if err != nil { + return err + } + body := map[string]interface{}{ + "filepath": filepath, + "content": content, + "message": message, + } + if branch := ctx.Arg("branch"); branch != "" { + body["branch"] = branch + } + env, err := ctx.CallAPI("POST", ctx.RepoPath()+"/contents", body) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "update-file", + Description: "Update an existing file in a repository", + Flags: []common.Flag{ + {Name: "filepath", Short: "f", Usage: "Path of the file to update", Required: true}, + {Name: "content", Short: "c", Usage: "New file content (base64 or plain text)", Required: true}, + {Name: "message", Short: "m", Usage: "Commit message", Required: true}, + {Name: "sha", Short: "s", Usage: "SHA of the file being replaced"}, + {Name: "branch", Short: "b", Usage: "Target branch (default: default branch)"}, + }, + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + filepath, err := ctx.RequireArg("filepath") + if err != nil { + return err + } + content, err := ctx.RequireArg("content") + if err != nil { + return err + } + message, err := ctx.RequireArg("message") + if err != nil { + return err + } + body := map[string]interface{}{ + "filepath": filepath, + "content": content, + "message": message, + } + if sha := ctx.Arg("sha"); sha != "" { + body["sha"] = sha + } + if branch := ctx.Arg("branch"); branch != "" { + body["branch"] = branch + } + env, err := ctx.CallAPI("PUT", ctx.RepoPath()+"/contents", body) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, } } -- 2.34.1