Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 95 additions & 13 deletions internal/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,13 +1208,45 @@ func newFilesUpdateCmd(project *string) *cobra.Command {
Short: "Update a document, vault, or upload",
Long: `Update a document, vault, or upload.

For documents, updating only --title or only --content preserves the untouched field.

You can pass either an item ID or a Basecamp URL:
basecamp files update 789 --title "new title" --in my-project
basecamp files update 789 --content "new content" --in my-project`,
Args: cobra.ExactArgs(1),
Annotations: map[string]string{"agent_notes": "Document updates preserve untouched title/content by fetching current state first because BC3 rebuilds documents from permitted params on PUT; explicit clears via --title \"\"/--content \"\" work because the SDK strips empty strings to absent fields, which the controller then nulls. Upload/vault updates do not clear by omission, so empty-valued flags are rejected CLI-side."},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if strings.TrimSpace(title) == "" && strings.TrimSpace(content) == "" && itemType == "" {
return noChanges(cmd)
titleChanged := cmd.Flags().Changed("title")
contentChanged := cmd.Flags().Changed("content")
titleTrimmed := strings.TrimSpace(title)
contentTrimmed := strings.TrimSpace(content)
itemType = strings.ToLower(strings.TrimSpace(itemType))
switch itemType {
case "", "document", "doc":
Comment thread
robzolkos marked this conversation as resolved.
// Explicit "" clears are valid for documents; whitespace-only is not.
hasTitle := titleChanged && (title == "" || titleTrimmed != "")
hasContent := contentChanged && (content == "" || contentTrimmed != "")
if !hasTitle && !hasContent {
return noChanges(cmd)
}
case "vault", "folder":
if contentChanged {
return output.ErrUsage("--content can only be used with --type document or upload")
}
if !titleChanged || titleTrimmed == "" {
return noChanges(cmd)
}
case "upload", "file":
hasTitle := titleChanged && titleTrimmed != ""
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Whitespace-only --title/--content can still be written when another field is valid because trimmed checks are only used for no-op gating, not when building update requests.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At internal/commands/files.go, line 1240:

<comment>Whitespace-only `--title/--content` can still be written when another field is valid because trimmed checks are only used for no-op gating, not when building update requests.</comment>

<file context>
@@ -1218,22 +1218,27 @@ You can pass either an item ID or a Basecamp URL:
 			case "upload", "file":
-				hasTitle := titleChanged && title != ""
-				hasContent := contentChanged && content != ""
+				hasTitle := titleChanged && titleTrimmed != ""
+				hasContent := contentChanged && contentTrimmed != ""
 				if !hasTitle && !hasContent {
</file context>
Fix with Cubic

hasContent := contentChanged && contentTrimmed != ""
if !hasTitle && !hasContent {
return noChanges(cmd)
}
default:
return output.ErrUsageHint(
fmt.Sprintf("Invalid type: %s", itemType),
"Use: vault, document, or upload",
)
}

app := appctx.FromContext(cmd.Context())
Expand Down Expand Up @@ -1269,12 +1301,10 @@ You can pass either an item ID or a Basecamp URL:
result = vault
detectedType = "vault"
case "document", "doc":
docHTML := richtext.MarkdownToHTML(content)
docHTML, err = resolveLocalImages(cmd, app, docHTML)
req, err := buildDocumentUpdateRequest(cmd, app, itemID, nil, titleChanged, contentChanged, title, content)
if err != nil {
return err
return convertSDKError(err)
}
req := &basecamp.UpdateDocumentRequest{Title: title, Content: docHTML}
doc, err := app.Account().Documents().Update(cmd.Context(), itemID, req)
if err != nil {
return convertSDKError(err)
Expand Down Expand Up @@ -1304,14 +1334,12 @@ You can pass either an item ID or a Basecamp URL:
var firstErr error

// Try document first (most common update case)
_, err := app.Account().Documents().Get(cmd.Context(), itemID)
existingDoc, err := app.Account().Documents().Get(cmd.Context(), itemID)
if err == nil {
docHTML := richtext.MarkdownToHTML(content)
docHTML, resolveErr := resolveLocalImages(cmd, app, docHTML)
if resolveErr != nil {
return resolveErr
req, buildErr := buildDocumentUpdateRequest(cmd, app, itemID, existingDoc, titleChanged, contentChanged, title, content)
if buildErr != nil {
return convertSDKError(buildErr)
}
req := &basecamp.UpdateDocumentRequest{Title: title, Content: docHTML}
doc, err := app.Account().Documents().Update(cmd.Context(), itemID, req)
if err != nil {
return convertSDKError(err)
Expand All @@ -1323,6 +1351,12 @@ You can pass either an item ID or a Basecamp URL:
// Try vault
_, err = app.Account().Vaults().Get(cmd.Context(), itemID)
if err == nil {
if contentChanged {
return output.ErrUsage("detected a folder/vault; use --title to rename it")
}
if !titleChanged || titleTrimmed == "" {
return noChanges(cmd)
}
req := &basecamp.UpdateVaultRequest{Title: title}
vault, err := app.Account().Vaults().Update(cmd.Context(), itemID, req)
if err != nil {
Expand All @@ -1334,6 +1368,11 @@ You can pass either an item ID or a Basecamp URL:
// Try upload
_, err = app.Account().Uploads().Get(cmd.Context(), itemID)
if err == nil {
hasTitle := titleChanged && titleTrimmed != ""
hasContent := contentChanged && contentTrimmed != ""
if !hasTitle && !hasContent {
return noChanges(cmd)
}
req := &basecamp.UpdateUploadRequest{Description: content}
if title != "" {
req.BaseName = title
Expand Down Expand Up @@ -1380,6 +1419,49 @@ You can pass either an item ID or a Basecamp URL:
return cmd
}

func buildDocumentUpdateRequest(cmd *cobra.Command, app *appctx.App, itemID int64, existingDoc *basecamp.Document, titleChanged, contentChanged bool, title, content string) (*basecamp.UpdateDocumentRequest, error) {
// BC3 rebuilds documents from permitted params on PUT, so omitted
// title/content fields are replaced with empty values. Fetch and merge when
// the caller updates only one field so the untouched field is preserved.
//
// Explicit clears via --title "" or --content "" work by composition: the
// SDK strips empty strings to absent JSON fields, and the controller then
// nulls those absent fields during rebuild. The wire-shape assertion in
// TestFilesUpdateDocumentEmptyTitleClearsWhilePreservingContent pins this.
if existingDoc == nil && (!titleChanged || !contentChanged) {
var err error
existingDoc, err = app.Account().Documents().Get(cmd.Context(), itemID)
if err != nil {
return nil, err
}
}

req := &basecamp.UpdateDocumentRequest{}
if existingDoc != nil {
req.Title = existingDoc.Title
req.Content = existingDoc.Content
}

if titleChanged {
req.Title = title
}
if contentChanged {
if content == "" {
req.Content = ""
return req, nil
}
docHTML := richtext.MarkdownToHTML(content)
var err error
docHTML, err = resolveLocalImages(cmd, app, docHTML)
if err != nil {
return nil, err
}
req.Content = docHTML
}

return req, nil
}

func newFilesDownloadCmd(project *string) *cobra.Command {
var outDir string

Expand Down
Loading
Loading