-
Notifications
You must be signed in to change notification settings - Fork 9
Preserve untouched document fields in files update #441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robzolkos
wants to merge
6
commits into
main
Choose a base branch
from
fix/files-update-preserve-document-fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+411
−14
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39485b9
Preserve untouched document fields in files update
robzolkos 3abd94f
Respect explicit empty document fields in files update
robzolkos c5ca84a
Validate files update fields by item type
robzolkos 657e20d
Reject content-only vault updates in autodetect mode
robzolkos e3c708c
Reject empty upload/vault fields in files update
jeremy 169f34b
Treat whitespace-only files update values as no-op
jeremy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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": | ||
| // 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 != "" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Whitespace-only Prompt for AI agents |
||
| 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()) | ||
|
|
@@ -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) | ||
|
|
@@ -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) | ||
|
|
@@ -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 { | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.