diff --git a/README.md b/README.md index f4d55f2..d0bcfbb 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,10 @@ cu task create # View task details cu task view +# Archive a task (reversible; keeps history and URL) +cu task archive +cu task archive --undo + # Export tasks to CSV cu export tasks --format csv > tasks.csv ``` diff --git a/docs/site/commands/cu_task.md b/docs/site/commands/cu_task.md index a0a655b..4e4b42d 100644 --- a/docs/site/commands/cu_task.md +++ b/docs/site/commands/cu_task.md @@ -23,6 +23,7 @@ Create, view, update, and manage ClickUp tasks. ### SEE ALSO * [cu](cu.md) - A GitHub CLI-inspired command-line interface for ClickUp +* [cu task archive](cu_task_archive.md) - Archive a task * [cu task close](cu_task_close.md) - Close a task * [cu task create](cu_task_create.md) - Create a new task * [cu task interactive](cu_task_interactive.md) - Interactive task browser diff --git a/docs/site/commands/cu_task_archive.md b/docs/site/commands/cu_task_archive.md new file mode 100644 index 0000000..e572cca --- /dev/null +++ b/docs/site/commands/cu_task_archive.md @@ -0,0 +1,37 @@ +## cu task archive + +Archive a task + +### Synopsis + +Archive a task, removing it from active views while keeping its history +and URL intact. + +Archiving is reversible with --undo. Prefer it over closing a task whose work +has moved elsewhere: closing asserts a status the task never reached, and not +every list even has a status named "closed". + +``` +cu task archive [task-id] [flags] +``` + +### Options + +``` + -h, --help help for archive + --undo Unarchive instead of archiving +``` + +### Options inherited from parent commands + +``` + --config string config file (default is $HOME/.config/cu/config.yaml) + --debug enable debug mode + -o, --output string output format (table|json|yaml|csv) (default "table") +``` + +### SEE ALSO + +* [cu task](cu_task.md) - Manage tasks + +###### Auto generated by spf13/cobra on 28-Aug-2026 diff --git a/internal/api/archive.go b/internal/api/archive.go new file mode 100644 index 0000000..b634e4c --- /dev/null +++ b/internal/api/archive.go @@ -0,0 +1,42 @@ +package api + +import ( + "context" + "fmt" + "net/url" + + "github.com/raksul/go-clickup/clickup" +) + +// archiveRequest is sent directly rather than through clickup.TaskUpdateRequest, +// which has no archived field — the SDK cannot express this update. +type archiveRequest struct { + Archived bool `json:"archived"` +} + +// SetTaskArchived archives or unarchives a task. +// +// Archiving keeps the task, its history and its URL while removing it from +// active views. That makes it the right primitive for retiring a task whose +// work has moved elsewhere: closing it would assert a status the task never +// reached (and "closed" is not even a status name on every list), and deleting +// it destroys the record. +func (c *Client) SetTaskArchived(ctx context.Context, taskID string, archived bool) (*clickup.Task, error) { + if taskID == "" { + return nil, fmt.Errorf("a task id is required") + } + if err := c.rateLimiter.Wait(ctx); err != nil { + return nil, err + } + + req, err := c.client.NewRequest("PUT", "task/"+url.PathEscape(taskID), &archiveRequest{Archived: archived}) + if err != nil { + return nil, err + } + + task := new(clickup.Task) + if _, err := c.client.Do(ctx, req, task); err != nil { + return nil, c.handleError(err) + } + return task, nil +} diff --git a/internal/api/archive_test.go b/internal/api/archive_test.go new file mode 100644 index 0000000..baa73d5 --- /dev/null +++ b/internal/api/archive_test.go @@ -0,0 +1,100 @@ +package api + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "net/url" + "testing" + "time" + + "github.com/raksul/go-clickup/clickup" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// newTestClient builds a Client whose SDK client talks to server instead of +// the real ClickUp API. +func newTestClient(t *testing.T, server *httptest.Server) *Client { + t.Helper() + + sdk := clickup.NewClient(server.Client(), "test-token") + baseURL, err := url.Parse(server.URL + "/") + require.NoError(t, err) + sdk.BaseURL = baseURL + + return &Client{ + client: sdk, + rateLimiter: NewRateLimiter(100, time.Minute), + } +} + +func TestSetTaskArchived(t *testing.T) { + tests := []struct { + name string + archived bool + }{ + {name: "archive", archived: true}, + {name: "undo", archived: false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var gotMethod, gotPath string + var gotBody map[string]any + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotMethod = r.Method + gotPath = r.URL.Path + raw, _ := io.ReadAll(r.Body) + _ = json.Unmarshal(raw, &gotBody) + + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"id":"abc123","name":"Retired task"}`)) + })) + defer server.Close() + + task, err := newTestClient(t, server).SetTaskArchived(context.Background(), "abc123", tt.archived) + require.NoError(t, err) + + assert.Equal(t, http.MethodPut, gotMethod) + assert.Equal(t, "/task/abc123", gotPath) + assert.Equal(t, map[string]any{"archived": tt.archived}, gotBody) + assert.Equal(t, "abc123", task.ID) + assert.Equal(t, "Retired task", task.Name) + }) + } +} + +func TestSetTaskArchived_NotFound(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"err":"Task not found","ECODE":"ITEM_013"}`)) + })) + defer server.Close() + + task, err := newTestClient(t, server).SetTaskArchived(context.Background(), "missing", true) + assert.Error(t, err) + assert.Nil(t, task) +} + +func TestSetTaskArchived_EscapesTaskID(t *testing.T) { + var gotRawPath string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotRawPath = r.URL.EscapedPath() + _, _ = w.Write([]byte(`{"id":"a/b"}`)) + })) + defer server.Close() + + _, err := newTestClient(t, server).SetTaskArchived(context.Background(), "a/b", true) + require.NoError(t, err) + assert.Equal(t, "/task/a%2Fb", gotRawPath) +} + +func TestSetTaskArchived_RequiresTaskID(t *testing.T) { + _, err := (&Client{}).SetTaskArchived(context.Background(), "", true) + assert.Error(t, err) +} diff --git a/internal/cmd/task.go b/internal/cmd/task.go index 4a0ea6f..f802ae8 100644 --- a/internal/cmd/task.go +++ b/internal/cmd/task.go @@ -497,6 +497,49 @@ var taskReopenCmd = &cobra.Command{ }, } +var taskArchiveCmd = &cobra.Command{ + Use: "archive [task-id]", + Short: "Archive a task", + Long: `Archive a task, removing it from active views while keeping its history +and URL intact. + +Archiving is reversible with --undo. Prefer it over closing a task whose work +has moved elsewhere: closing asserts a status the task never reached, and not +every list even has a status named "closed".`, + Args: cobra.ExactArgs(1), + // API failures are not usage errors; don't bury them under the help text. + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + taskID := args[0] + undo, _ := cmd.Flags().GetBool("undo") + + verb, action := "Archived", "archive" + if undo { + verb, action = "Unarchived", "unarchive" + } + + client, err := api.NewClient() + if err != nil { + return fmt.Errorf("failed to create API client: %w", err) + } + + task, err := client.SetTaskArchived(context.Background(), taskID, !undo) + if err != nil { + return fmt.Errorf("failed to %s task: %w", action, err) + } + + if outputFormat != "table" { + return output.Format(outputFormat, task) + } + + fmt.Printf("✓ %s task %s: %s\n", verb, task.ID, task.Name) + if task.URL != "" { + fmt.Printf(" View in ClickUp: %s\n", task.URL) + } + return nil + }, +} + var taskSearchCmd = &cobra.Command{ Use: "search [query]", Short: "Search for tasks", @@ -683,6 +726,7 @@ func init() { taskCmd.AddCommand(taskCloseCmd) taskCmd.AddCommand(taskReopenCmd) taskCmd.AddCommand(taskSearchCmd) + taskCmd.AddCommand(taskArchiveCmd) // List command flags taskListCmd.Flags().StringP("list", "l", "", "List ID or name") @@ -718,6 +762,9 @@ func init() { taskUpdateCmd.Flags().StringSlice("add-assignee", []string{}, "Add assignees (username or ID)") taskUpdateCmd.Flags().StringSlice("remove-assignee", []string{}, "Remove assignees (username or ID)") + // Archive command flags + taskArchiveCmd.Flags().Bool("undo", false, "Unarchive instead of archiving") + // Reopen command flags taskCloseCmd.Flags().StringP("status", "s", "", "Status to set (default: the list's done status)") taskReopenCmd.Flags().StringP("status", "s", "", "Status to set (default: the list's first open status)") diff --git a/internal/cmd/task_test.go b/internal/cmd/task_test.go index 83d1ccd..7b5b056 100644 --- a/internal/cmd/task_test.go +++ b/internal/cmd/task_test.go @@ -8,6 +8,7 @@ import ( "github.com/raksul/go-clickup/clickup" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTaskCommands_Structure(t *testing.T) { @@ -68,6 +69,20 @@ func TestTaskCommands_Structure(t *testing.T) { assert.NotNil(t, taskReopenCmd) assert.NotNil(t, taskSearchCmd) }) + + // Test task archive command + t.Run("task archive command", func(t *testing.T) { + cmd, _, err := taskCmd.Find([]string{"archive"}) + require.NoError(t, err) + assert.Same(t, taskArchiveCmd, cmd) + assert.NotNil(t, cmd.RunE) + assert.True(t, cmd.SilenceUsage) + + undo := cmd.Flags().Lookup("undo") + require.NotNil(t, undo) + assert.Equal(t, "bool", undo.Value.Type()) + assert.Equal(t, "false", undo.DefValue) + }) } // Test helper functions diff --git a/mkdocs.yml b/mkdocs.yml index 43b4b31..c486d28 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -50,6 +50,7 @@ nav: - cu task close: commands/cu_task_close.md - cu task reopen: commands/cu_task_reopen.md - cu task search: commands/cu_task_search.md + - cu task archive: commands/cu_task_archive.md - cu task interactive: commands/cu_task_interactive.md - Lists: - cu list: commands/cu_list.md