diff --git a/shortcuts/register.go b/shortcuts/register.go index 1fedc7e..2c1c90f 100644 --- a/shortcuts/register.go +++ b/shortcuts/register.go @@ -24,6 +24,7 @@ import ( "github.com/gitlink-org/gitlink-cli/shortcuts/repo" "github.com/gitlink-org/gitlink-cli/shortcuts/search" "github.com/gitlink-org/gitlink-cli/shortcuts/user" + "github.com/gitlink-org/gitlink-cli/shortcuts/watch" "github.com/gitlink-org/gitlink-cli/shortcuts/webhook" "github.com/gitlink-org/gitlink-cli/shortcuts/wiki" "github.com/gitlink-org/gitlink-cli/shortcuts/workflow" @@ -50,6 +51,7 @@ func RegisterAll(root *cobra.Command, translators ...*i18n.Translator) { "org": org.Shortcuts(tr), "user": user.Shortcuts(tr), "search": search.Shortcuts(tr), + "watch": watch.Shortcuts(), "ci": ci.Shortcuts(tr), "compare": compare.Shortcuts(), "dataset": dataset.Shortcuts(tr), @@ -75,6 +77,7 @@ func RegisterAll(root *cobra.Command, translators ...*i18n.Translator) { "org": tr.T("cmd.org.short"), "user": tr.T("cmd.user.short"), "search": tr.T("cmd.search.short"), + "watch": "Watch (subscribe) repository operations", "ci": tr.T("cmd.ci.short"), "compare": "Compare branches, tags, or commits", "dataset": tr.T("cmd.dataset.short"), diff --git a/shortcuts/watch/watch.go b/shortcuts/watch/watch.go new file mode 100644 index 0000000..3a25b7a --- /dev/null +++ b/shortcuts/watch/watch.go @@ -0,0 +1,91 @@ +package watch + +import ( + "fmt" + "net/url" + + "github.com/gitlink-org/gitlink-cli/shortcuts/common" +) + +func Shortcuts() []*common.Shortcut { + return []*common.Shortcut{ + { + Name: "watch", + Description: "Watch a repository", + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + projectID, err := resolveProjectID(ctx) + if err != nil { + return err + } + q := url.Values{} + q.Set("target_type", "project") + q.Set("id", fmt.Sprintf("%d", projectID)) + env, err := ctx.CallAPIWithQuery("POST", "/watchers/follow", q) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "unwatch", + Description: "Unwatch a repository", + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + projectID, err := resolveProjectID(ctx) + if err != nil { + return err + } + q := url.Values{} + q.Set("target_type", "project") + q.Set("id", fmt.Sprintf("%d", projectID)) + env, err := ctx.CallAPIWithQuery("DELETE", "/watchers/unfollow", q) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "watchers", + Description: "List watchers of a repository", + Flags: []common.Flag{ + {Name: "owner", Short: "o", Usage: "Repository owner", Required: true}, + {Name: "repo", Short: "r", Usage: "Repository name", Required: true}, + }, + Run: func(ctx *common.RuntimeContext) error { + owner, _ := ctx.RequireArg("owner") + repo, _ := ctx.RequireArg("repo") + ctx.Owner = owner + ctx.Repo = repo + env, err := ctx.CallAPI("GET", fmt.Sprintf("/%s/%s/watchers", owner, repo), nil) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + } +} + +func resolveProjectID(ctx *common.RuntimeContext) (int64, error) { + env, err := ctx.CallAPI("GET", ctx.RepoPath(), nil) + if err != nil { + return 0, fmt.Errorf("failed to get project info: %w", err) + } + data, ok := env.Data.(map[string]interface{}) + if !ok { + return 0, fmt.Errorf("unexpected project info response") + } + for _, key := range []string{"id", "project_id", "repo_id"} { + if id, ok := data[key].(float64); ok { + return int64(id), nil + } + } + return 0, fmt.Errorf("cannot find project id in response") +} diff --git a/shortcuts/watch/watch_test.go b/shortcuts/watch/watch_test.go new file mode 100644 index 0000000..6bdfc47 --- /dev/null +++ b/shortcuts/watch/watch_test.go @@ -0,0 +1,109 @@ +package watch + +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 TestWatch(t *testing.T) { + callCount := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + callCount++ + switch { + case r.Method == "GET" && r.URL.Path == "/owner/repo.json": + writeJSON(t, w, map[string]interface{}{"id": float64(100), "name": "repo"}) + case r.Method == "POST" && r.URL.Path == "/watchers/follow.json": + if r.URL.Query().Get("target_type") != "project" { + t.Fatal("expected target_type=project") + } + if r.URL.Query().Get("id") != "100" { + t.Fatalf("expected id=100, got %s", r.URL.Query().Get("id")) + } + writeJSON(t, w, map[string]interface{}{"watched": true}) + default: + t.Fatalf("unexpected request #%d: %s %s", callCount, r.Method, r.URL.Path) + } + })) + defer server.Close() + + if err := runWatchShortcut(t, server, "watch", map[string]string{}); err != nil { + t.Fatalf("watch failed: %v", err) + } + if callCount != 2 { + t.Fatalf("expected 2 calls (GET project + POST watch), got %d", callCount) + } +} + +func TestUnwatch(t *testing.T) { + callCount := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + callCount++ + switch { + case r.Method == "GET" && r.URL.Path == "/owner/repo.json": + writeJSON(t, w, map[string]interface{}{"id": float64(100)}) + case r.Method == "DELETE" && r.URL.Path == "/watchers/unfollow.json": + writeJSON(t, w, map[string]interface{}{"watched": false}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) + } + })) + defer server.Close() + + if err := runWatchShortcut(t, server, "unwatch", map[string]string{}); err != nil { + t.Fatalf("unwatch failed: %v", err) + } +} + +func TestWatchers(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" || r.URL.Path != "/owner/repo/watchers.json" { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) + } + writeJSON(t, w, map[string]interface{}{ + "count": 1, + "users": []map[string]interface{}{{"login": "alice", "is_watch": true}}, + }) + })) + defer server.Close() + + err := runWatchShortcut(t, server, "watchers", map[string]string{ + "owner": "owner", "repo": "repo", + }) + if err != nil { + t.Fatalf("watchers failed: %v", err) + } +} + +// === helpers === + +func runWatchShortcut(t *testing.T, server *httptest.Server, name string, args map[string]string) error { + t.Helper() + shortcut := findWatchShortcut(t, name) + 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 findWatchShortcut(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 writeJSON(t *testing.T, w http.ResponseWriter, payload interface{}) { + t.Helper() + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(payload) +}