fix(drive): use title for default download filename - #2089
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthrough
ChangesDrive download output flow
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant User
participant DriveDownload
participant DriveMetadataAPI
participant DriveDownloadAPI
participant FileSystem
User->>DriveDownload: Run download without --output
DriveDownload->>DriveMetadataAPI: Query metadata title
DriveDownload->>DriveDownloadAPI: Request file content
DriveDownloadAPI-->>DriveDownload: Return content and filename header
DriveDownload->>FileSystem: Resolve, validate, and save output path
Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🚀 PR Preview Install Guide🧰 CLI updatenpm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@3fa8d23ad56cffdead082b9c50317dcdb45b4611🧩 Skill updatenpx skills add larksuite/cli#fix/drive-download-default-filename -y -g |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2089 +/- ##
==========================================
+ Coverage 75.39% 75.42% +0.03%
==========================================
Files 924 924
Lines 98005 98114 +109
==========================================
+ Hits 73895 74007 +112
+ Misses 18478 18474 -4
- Partials 5632 5633 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
shortcuts/drive/drive_download.go (1)
46-55: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsolidate the Drive metadata scope constant.
driveMetadataReadScopeis this file-only copy of the shared drive metadata scope already used across many Drive shortcuts. Define/export it fromshortcuts/commonor another shared location and reuse it here.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@shortcuts/drive/drive_download.go` around lines 46 - 55, Consolidate the duplicated driveMetadataReadScope constant by defining or exporting the shared Drive metadata scope from shortcuts/common (or the existing shared Drive scope location), then update DriveDownload and other references to reuse that symbol. Remove the file-local copy while preserving the existing ConditionalScopes behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@shortcuts/drive/drive_download.go`:
- Around line 21-44: Update driveDownloadFallbackFileName and
driveDownloadDefaultOutputPath to reject "/" in addition to the existing empty,
"." and ".." checks, falling back to fileToken before returning or appending an
extension.
In `@shortcuts/drive/drive_io_test.go`:
- Around line 1714-1744: Strengthen
TestDriveDownloadMetadataErrorBeforeDownloadWhenOutputOmitted by asserting
problem.Subtype alongside problem.Category and problem.Code. Verify it is the
expected authorization subtype, or at minimum non-empty if no specific subtype
constant is available, while preserving the existing typed-error validation.
In `@tests/cli_e2e/drive/drive_download_dryrun_test.go`:
- Line 1: Add live E2E coverage alongside the existing dry-run tests in
drive_download_dryrun_test.go for drive +download filename resolution:
create/upload a fixture with a known title, download without an explicit output
path against the live backend, assert the resolved filename and file contents,
and clean up all created resources using the available bot credentials. Update
the corresponding coverage.md row/notes to reflect the new live workflow and
remove the “no live file fixture workflow yet” caveat only after the test is
implemented.
---
Nitpick comments:
In `@shortcuts/drive/drive_download.go`:
- Around line 46-55: Consolidate the duplicated driveMetadataReadScope constant
by defining or exporting the shared Drive metadata scope from shortcuts/common
(or the existing shared Drive scope location), then update DriveDownload and
other references to reuse that symbol. Remove the file-local copy while
preserving the existing ConditionalScopes behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: a289514b-00f9-41e4-94fe-a2636810964e
📒 Files selected for processing (5)
shortcuts/drive/drive_download.goshortcuts/drive/drive_io_test.goskills/lark-drive/references/lark-drive-download.mdtests/cli_e2e/drive/coverage.mdtests/cli_e2e/drive/drive_download_dryrun_test.go
40fefc3 to
21daea2
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
shortcuts/drive/drive_io_test.go (1)
1808-1812: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAvoid
t.Fatalinside theOnMatchcallback.
OnMatchruns insideRoundTrip, which is not guaranteed to execute on the test goroutine;t.Fatalthere callsruntime.Goexiton the wrong goroutine and can hang or mask the failure. Record the ordering and assert after the command returns (or uset.Errorf).♻️ Suggested adjustment
+ var metaSeenBeforeDownload bool OnMatch: func(req *http.Request) { - if len(metaStub.CapturedBody) == 0 { - t.Fatal("metadata title lookup must happen before download") - } + metaSeenBeforeDownload = len(metaStub.CapturedBody) > 0 },Then assert
metaSeenBeforeDownloadaftermountAndRunDrivereturns.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@shortcuts/drive/drive_io_test.go` around lines 1808 - 1812, Replace the t.Fatal call inside the OnMatch callback with a goroutine-safe ordering record, such as setting metaSeenBeforeDownload when the metadata body is captured before download. After mountAndRunDrive returns, assert the recorded ordering on the test goroutine, preserving the requirement that metadata lookup precedes download.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@skills/lark-drive/references/lark-drive-download.md`:
- Around line 14-15: Update the token-only usage documentation for `lark-cli
drive +download` to state that omitting `--output` requires the Drive metadata
read scope, and document the automatic filename precedence as
Content-Disposition filename, then metadata title, then file token.
In `@tests/cli_e2e/drive/coverage.md`:
- Line 34: Update the Metrics block in coverage.md to reflect 15 covered out of
32 leaf commands and the corresponding percentage, then add a Summary bullet for
the newly covered drive +download command describing its dry-run
metadata-planning and explicit-output behavior.
---
Nitpick comments:
In `@shortcuts/drive/drive_io_test.go`:
- Around line 1808-1812: Replace the t.Fatal call inside the OnMatch callback
with a goroutine-safe ordering record, such as setting metaSeenBeforeDownload
when the metadata body is captured before download. After mountAndRunDrive
returns, assert the recorded ordering on the test goroutine, preserving the
requirement that metadata lookup precedes download.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: c6e193d4-30f8-452e-bcb9-be7ca01e3f4c
📒 Files selected for processing (5)
shortcuts/drive/drive_download.goshortcuts/drive/drive_io_test.goskills/lark-drive/references/lark-drive-download.mdtests/cli_e2e/drive/coverage.mdtests/cli_e2e/drive/drive_download_dryrun_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
- tests/cli_e2e/drive/drive_download_dryrun_test.go
- shortcuts/drive/drive_download.go
21daea2 to
e31740f
Compare
e31740f to
b682673
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (2)
shortcuts/drive/drive_download.go (2)
117-126: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMerge the two identical
outputPath != ""guards.♻️ Proposed simplification
- if outputPath != "" { - if _, resolveErr := runtime.ResolveSavePath(outputPath); resolveErr != nil { - return errs.NewValidationError(errs.SubtypeInvalidArgument, "unsafe output path: %s", resolveErr).WithParam("--output") - } - } - if outputPath != "" { - if _, statErr := runtime.FileIO().Stat(outputPath); statErr == nil && !overwrite { - return errs.NewValidationError(errs.SubtypeInvalidArgument, "output file already exists: %s (use --overwrite to replace)", outputPath).WithParam("--output") - } - } + if outputPath != "" { + if _, resolveErr := runtime.ResolveSavePath(outputPath); resolveErr != nil { + return errs.NewValidationError(errs.SubtypeInvalidArgument, "unsafe output path: %s", resolveErr).WithParam("--output") + } + if _, statErr := runtime.FileIO().Stat(outputPath); statErr == nil && !overwrite { + return errs.NewValidationError(errs.SubtypeInvalidArgument, "output file already exists: %s (use --overwrite to replace)", outputPath).WithParam("--output") + } + }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@shortcuts/drive/drive_download.go` around lines 117 - 126, Merge the two outputPath checks into a single outputPath != "" guard in the surrounding download validation flow, keeping both ResolveSavePath validation and existing-file/overwrite validation unchanged and in their current order.
105-109: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueStep numbering is misleading when
--outputis provided.The download step is always labelled
[2], but it is the only planned call when--outputis set. Consider deriving the prefix from whether the metadata step was planned.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@shortcuts/drive/drive_download.go` around lines 105 - 109, Update the download plan description in the drive download flow to derive its step prefix from whether the metadata step was planned, so an output-only download is numbered as the first step while retaining `[2]` when metadata is included. Use the existing metadata-planning condition or symbol rather than hardcoding `[2]`.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@shortcuts/drive/drive_download.go`:
- Around line 117-126: Merge the two outputPath checks into a single outputPath
!= "" guard in the surrounding download validation flow, keeping both
ResolveSavePath validation and existing-file/overwrite validation unchanged and
in their current order.
- Around line 105-109: Update the download plan description in the drive
download flow to derive its step prefix from whether the metadata step was
planned, so an output-only download is numbered as the first step while
retaining `[2]` when metadata is included. Use the existing metadata-planning
condition or symbol rather than hardcoding `[2]`.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 586c9a91-2742-4eba-97cd-7a29ec0e8f3e
📒 Files selected for processing (5)
shortcuts/drive/drive_download.goshortcuts/drive/drive_io_test.goskills/lark-drive/references/lark-drive-download.mdtests/cli_e2e/drive/coverage.mdtests/cli_e2e/drive/drive_download_dryrun_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
- skills/lark-drive/references/lark-drive-download.md
- tests/cli_e2e/drive/coverage.md
b682673 to
0ad1a6a
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@shortcuts/drive/drive_download.go`:
- Around line 37-45: The filename fallback in driveDownloadDefaultOutputPath
skips the metadata title when the header candidate is unusable. In
shortcuts/drive/drive_download.go lines 37-45, sanitize the metadata fallback
from driveDownloadFallbackFileName first, then use it before fileToken when
validating or replacing an invalid header filename, while preserving extension
handling. Update shortcuts/drive/drive_io_test.go lines 1605-1612 to expect
Metadata Title.txt for the invalid CON.txt header case.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 74912b24-cfc4-44da-af13-32b147d522bd
📒 Files selected for processing (7)
shortcuts/drive/drive_download.goshortcuts/drive/drive_export_common.goshortcuts/drive/drive_export_common_test.goshortcuts/drive/drive_io_test.goskills/lark-drive/references/lark-drive-download.mdtests/cli_e2e/drive/coverage.mdtests/cli_e2e/drive/drive_download_dryrun_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
- skills/lark-drive/references/lark-drive-download.md
- tests/cli_e2e/drive/coverage.md
0ad1a6a to
f4b47fa
Compare
f4b47fa to
3fa8d23
Compare
Summary
Fix
drive +downloaddefault naming when--outputis omitted. The command now resolves metadata title before starting the download and saves withContent-Dispositionfilename > metadata title > token.Changes
--output, so missing metadata permission fails before the download stream starts.--outputbehavior unchanged.Test Plan
go test ./shortcuts/drive -run 'TestDriveDownload(UsesContentDispositionWhenOutputOmitted|FallsBackToMetadataTitleWhenOutputOmitted|FallsBackToTokenWhenOutputOmittedAndMetadataEmpty|MetadataErrorBeforeDownloadWhenOutputOmitted|RejectsOverwriteWithoutFlag|AllowsOverwriteFlag)' -count=1\n- [x] Dry-run E2E passes with a locally built binary:LARK_CLI_BIN=/Users/bytedance/go/src/github.com/larksuite/cli/tmp/lark-cli-drive-download-default go test ./tests/cli_e2e/drive -run 'TestDriveDownloadDryRun_(DefaultNamePlansMetadataBeforeDownload|ExplicitOutputSkipsMetadata)' -count=1\n- [x]git diff --cached --check\n\n## Related Issues\n- None\nSummary by CodeRabbit
--outputis omitted, Drive downloads now derive a safe default filename/path (sanitizingContent-Disposition, including slash-only and Windows-reserved characters), with priority:Content-Dispositionfilename → Drive metadata title → file token.--outputskips them.--overwriteis set).drive +downloaddocs for the token-only default save location.