Skip to content

feat: report upload file events - #2093

Open
zhoujunteng-max wants to merge 14 commits into
mainfrom
feat/upload-file-event-report
Open

feat: report upload file events#2093
zhoujunteng-max wants to merge 14 commits into
mainfrom
feat/upload-file-event-report

Conversation

@zhoujunteng-max

@zhoujunteng-max zhoujunteng-max commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

Report best-effort upload file events from Drive file/media upload flows, including Drive import and Sheets workbook import. Preserve the original upload result while enriching tenant-capacity errors with a server-provided expansion URL when available; all reports use file_scene: lark-cli.

Changes

  • Add a shared upload file-event reporter with a bounded timeout, per-command deduplication, and typed upload-error preservation.
  • Integrate reporting into single-part and multipart Drive file/media uploads and expose the report request in dry-run plans.
  • Add unit, dry-run, and live workflow coverage for Drive upload/import behavior, including the shared Sheets workbook-import path.

Test Plan

  • Affected unit packages pass: go test ./shortcuts/common ./shortcuts/drive -count=1
  • Build, vet, formatting, module tidiness, golangci-lint, source-contract lint, quality gate, dependency licenses, and incremental deadcode checks pass.
  • Full dry-run/Regression CLI E2E passes, including Drive and Sheets workbook import.
  • Plugin, sidecar, lint-module, and script integration tests pass.
  • Live E2E was not run locally because it requires tenant credentials and creates temporary Drive resources; CI will run the credentialed workflow.

Local make unit-test on macOS can hit an unrelated internal/qualitygate/publiccontent TempDir cleanup race (.git/objects or .git/ai remains non-empty). The affected packages pass, and the latest base commit passes the complete Linux CI workflow.

Related Issues

  • None

Summary by CodeRabbit

  • New Features
    • Drive uploads now report file events for single-part and multipart transfers, including success and failure details.
    • Capacity-exceeded errors can include an expansion-link hint.
    • Drive import and upload dry runs now show upload-reporting steps and metadata.
  • Bug Fixes
    • Reporting is best-effort, occurs at most once per command, and never replaces the original upload error.
  • Tests
    • Expanded Drive upload and dry-run coverage, including a complete import workflow test.

@github-actions github-actions Bot added domain/ccm PR touches the ccm domain size/L Large or sensitive change across domains or core paths labels Jul 29, 2026
@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Drive uploads now emit structured file-event reports for success and failure paths. Shared helpers enforce one report per runtime, preserve upload errors, add validated tenant-capacity hints, and represent reports in dry-run output. Drive upload, import, typed media, unit, and end-to-end tests were updated.

Changes

Drive upload event reporting

Layer / File(s) Summary
Reporting contract and runtime delivery
shortcuts/common/lark_cli_file_event.go, shortcuts/common/runner.go, shortcuts/common/*file_event*_test.go
Adds upload event metadata, one-time best-effort reporting, timeout handling, URL sanitization, capacity hints, dry-run request construction, and context-aware API execution.
Typed media upload integration
shortcuts/common/drive_media_upload.go, shortcuts/common/drive_media_upload_typed_test.go, shortcuts/common/call_api_typed_test.go
Centralizes media endpoints and reports single-part and multipart typed upload results with step-specific metadata.
Drive upload flow and dry-run wiring
shortcuts/drive/drive_upload.go, shortcuts/drive/drive_import.go, shortcuts/drive/*_test.go
Adds event metadata to Drive upload paths and inserts upload report requests into import and upload dry-run plans.
End-to-end validation and coverage records
tests/cli_e2e/drive/*, tests/cli_e2e/sheets/*, tests/cli_e2e/drive/coverage.md
Validates report payloads, output-field isolation, asynchronous import completion, spreadsheet import request ordering, and updated Drive coverage status.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant DriveUpload
  participant DriveAPI
  participant FileEventReport
  CLI->>DriveUpload: execute upload
  DriveUpload->>DriveAPI: upload_all or multipart request
  DriveAPI-->>DriveUpload: upload result or typed error
  DriveUpload->>FileEventReport: POST success or error event
  FileEventReport-->>DriveUpload: report response and optional capacity URL
  DriveUpload-->>CLI: file token or original upload error
Loading

Possibly related PRs

  • larksuite/cli#194: Extends the Drive import and upload flows introduced by this PR.
  • larksuite/cli#294: Modifies the multipart and single-part upload flows integrated by this PR.
  • larksuite/cli#1921: Shares changes to the Drive media upload helpers but addresses multipart request pacing.

Suggested labels: feature

Suggested reviewers: fangshuyu-768

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 70.89% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: reporting upload file events.
Description check ✅ Passed The description follows the required template and covers scope, changes, tests, limitations, and related issues.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/upload-file-event-report

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown

🚀 PR Preview Install Guide

🧰 CLI update

npm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@109095ce7414e13943d5331502582b89ea816aaf

🧩 Skill update

npx skills add larksuite/cli#feat/upload-file-event-report -y -g

@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.39593% with 170 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.40%. Comparing base (be2a96f) to head (109095c).

Files with missing lines Patch % Lines
shortcuts/common/drive_media_upload_plan.go 62.63% 29 Missing and 5 partials ⚠️
shortcuts/drive/drive_upload.go 61.62% 30 Missing and 3 partials ⚠️
shortcuts/calendar/description_rich_images.go 6.66% 28 Missing ⚠️
internal/fileevent/report.go 85.90% 16 Missing and 5 partials ⚠️
shortcuts/mail/large_attachment.go 56.66% 11 Missing and 2 partials ⚠️
shortcuts/mail/mail_draft_edit.go 0.00% 8 Missing ⚠️
shortcuts/doc/doc_media_upload.go 75.86% 4 Missing and 3 partials ⚠️
shortcuts/base/record_upload_attachment.go 80.95% 2 Missing and 2 partials ⚠️
shortcuts/common/drive_media_upload.go 87.09% 3 Missing and 1 partial ⚠️
shortcuts/sheets/lark_sheet_object_crud.go 0.00% 4 Missing ⚠️
... and 7 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2093      +/-   ##
==========================================
+ Coverage   76.34%   76.40%   +0.05%     
==========================================
  Files         991      993       +2     
  Lines      106019   106229     +210     
==========================================
+ Hits        80940    81163     +223     
+ Misses      18944    18899      -45     
- Partials     6135     6167      +32     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (7)
tests/cli_e2e/drive/drive_import_workflow_test.go (1)

70-70: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Put ctx first in the helper signature.

waitDriveImportReady(t *testing.T, ctx context.Context, ...) inverts the idiomatic ordering (ctx first, or immediately after t per repo precedent); revive/golangci-lint context-as-argument checks commonly flag this.

🤖 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 `@tests/cli_e2e/drive/drive_import_workflow_test.go` at line 70, Update the
waitDriveImportReady helper signature to place ctx immediately after t, before
ticket and fallbackType, and adjust every call site to pass arguments in the
matching order.
shortcuts/drive/drive_io_test.go (2)

1738-1747: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Also assert Category/Subtype on these error paths.

Per repo test guidelines, error-path assertions should pin typed metadata (category and subtype) rather than only Code/Hint, so a regression in classification still fails the test.

As per coding guidelines: "Error-path tests must assert typed metadata through errs.ProblemOf (category, subtype, and param) and verify cause preservation rather than relying only on message substrings."

Also applies to: 1795-1801, 1843-1849

🤖 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 1738 - 1747, Extend the
error-path assertions around errs.ProblemOf in the affected tests to verify the
expected Category, Subtype, and param metadata, and assert that the original
cause is preserved. Apply the same typed-metadata and cause checks to the
analogous cases near the existing assertions at lines 1795-1801 and 1843-1849,
while retaining the current Code and Hint checks.

Source: Coding guidelines


1771-1776: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use the existing withDriveWorkingDir helper instead of manual Chdir.

The other tests in this file use withDriveWorkingDir(t, t.TempDir()); here the os.Getwd error is dropped and defer os.Chdir(origDir) is unchecked (likely an errcheck hit under the pinned golangci-lint config).

♻️ Proposed refactor
-	origDir, _ := os.Getwd()
-	tmpDir := t.TempDir()
-	if err := os.Chdir(tmpDir); err != nil {
-		t.Fatalf("Chdir() error: %v", err)
-	}
-	defer os.Chdir(origDir)
-
-	fh, err := os.Create("large.bin")
+	withDriveWorkingDir(t, t.TempDir())
+
+	fh, err := os.Create("large.bin")
🤖 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 1771 - 1776, Replace the
manual working-directory setup around this test with the existing
withDriveWorkingDir helper, passing t and t.TempDir(). Remove the os.Getwd,
os.Chdir, and deferred restoration logic while preserving the test’s execution
in the temporary drive directory.
shortcuts/common/lark_cli_file_event_test.go (1)

150-175: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Error-path test only checks identity, not typed metadata.

TestReportUploadFileEventOnError_ReportsAndPreservesError only asserts returned != uploadErr (reference identity) and tags["code"]; it never asserts errs.ProblemOf Category/Subtype for the returned error, as required for error-path tests.

As per coding guidelines: "Error-path tests must assert typed metadata through errs.ProblemOf (category, subtype, and param) and verify cause preservation rather than relying only on message substrings."

🤖 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/common/lark_cli_file_event_test.go` around lines 150 - 175,
Strengthen TestReportUploadFileEventOnError_ReportsAndPreservesError by applying
errs.ProblemOf to the returned error and asserting the expected category,
subtype, and param metadata, while also verifying the original cause is
preserved. Retain the existing identity, reporting-count, and tag assertions.

Source: Coding guidelines

shortcuts/common/drive_media_upload_typed_test.go (2)

1-1: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

New error-path tests don't fully assert typed error metadata. Several new tests for the upload-reporting feature assert p.Code/p.Hint (or bare error identity) via errs.ProblemOf but omit Category/Subtype assertions, as required for error-path tests.

  • shortcuts/common/drive_media_upload_typed_test.go#L399-430: in TestUploadDriveMediaAllTypedReportsFileEventOnError, add assertions on p.Category/p.Subtype alongside the existing p.Code check.
  • shortcuts/common/drive_media_upload_typed_test.go#L432-471: in TestUploadDriveMediaAllTypedReportFailureKeepsUploadError, add p.Category/p.Subtype assertions alongside p.Code/p.Hint.
  • shortcuts/common/lark_cli_file_event_test.go#L150-175: in TestReportUploadFileEventOnError_ReportsAndPreservesError, assert errs.ProblemOf(returned) Category/Subtype in addition to the existing identity check.

As per coding guidelines: "Error-path tests must assert typed metadata through errs.ProblemOf (category, subtype, and param) and verify cause preservation rather than relying only on message substrings."

🤖 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/common/drive_media_upload_typed_test.go` at line 1, Update the
error-path tests TestUploadDriveMediaAllTypedReportsFileEventOnError,
TestUploadDriveMediaAllTypedReportFailureKeepsUploadError, and
TestReportUploadFileEventOnError_ReportsAndPreservesError to assert
errs.ProblemOf metadata for Category and Subtype alongside the existing checks;
preserve the current cause or error-identity assertions and add the required
param assertion where applicable.

Source: Coding guidelines


399-430: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Error-path tests assert Code but omit Category/Subtype.

TestUploadDriveMediaAllTypedReportsFileEventOnError and TestUploadDriveMediaAllTypedReportFailureKeepsUploadError (432-471) assert p.Code/p.Hint via errs.ProblemOf but don't assert Category/Subtype, as required for error-path tests exercising typed errors.

As per coding guidelines: "Error-path tests must assert typed metadata through errs.ProblemOf (category, subtype, and param) and verify cause preservation rather than relying only on message substrings." This shares the same root cause as the gap flagged in shortcuts/common/lark_cli_file_event_test.go; see the consolidated comment.

🤖 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/common/drive_media_upload_typed_test.go` around lines 399 - 430,
Update TestUploadDriveMediaAllTypedReportsFileEventOnError and
TestUploadDriveMediaAllTypedReportFailureKeepsUploadError to fully validate
errs.ProblemOf metadata, asserting the expected category, subtype, and param
alongside the existing code or hint checks. Also verify that the typed error
preserves its underlying cause, rather than relying only on message content.

Source: Coding guidelines

shortcuts/common/lark_cli_file_event.go (1)

63-138: 🧹 Nitpick | 🔵 Trivial

Best-effort report blocks the command for up to 3s on every upload.

ReportUploadFileEvent/ReportUploadFileEventOnError call postUploadFileEvent synchronously before the upload function returns, bounded by a 3s timeout (uploadFileEventReportTimeout). On a slow/lossy network this adds up to 3 extra seconds of perceived latency to every single Drive upload, on both success and failure paths, even though the report result is discarded. This is likely an intentional tradeoff (CLI process exit would otherwise race a fire-and-forget goroutine), but worth confirming the 3s bound is acceptable UX for interactive use, and possibly worth lowering further for the common success path.

🤖 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/common/lark_cli_file_event.go` around lines 63 - 138, Make the
upload event reporting path non-blocking so ReportUploadFileEvent and
ReportUploadFileEventOnError do not add the full uploadFileEventReportTimeout to
interactive uploads, while preserving best-effort behavior,
once-per-RuntimeContext deduplication, and the original upload error. Ensure any
asynchronous report has sufficient runtime/context lifetime to complete before
process exit, or reduce the reporting timeout—especially for the successful
upload path—if the existing synchronous lifecycle must be retained.
🤖 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 `@tests/cli_e2e/drive/drive_import_workflow_test.go`:
- Around line 18-27: Add clie2e.SkipWithoutTenantAccessToken(t) as the first
operation in TestDrive_ImportWorkflow, before creating the context, fixture, or
invoking any live Drive commands, so the test skips when tenant credentials are
unavailable.

---

Nitpick comments:
In `@shortcuts/common/drive_media_upload_typed_test.go`:
- Line 1: Update the error-path tests
TestUploadDriveMediaAllTypedReportsFileEventOnError,
TestUploadDriveMediaAllTypedReportFailureKeepsUploadError, and
TestReportUploadFileEventOnError_ReportsAndPreservesError to assert
errs.ProblemOf metadata for Category and Subtype alongside the existing checks;
preserve the current cause or error-identity assertions and add the required
param assertion where applicable.
- Around line 399-430: Update
TestUploadDriveMediaAllTypedReportsFileEventOnError and
TestUploadDriveMediaAllTypedReportFailureKeepsUploadError to fully validate
errs.ProblemOf metadata, asserting the expected category, subtype, and param
alongside the existing code or hint checks. Also verify that the typed error
preserves its underlying cause, rather than relying only on message content.

In `@shortcuts/common/lark_cli_file_event_test.go`:
- Around line 150-175: Strengthen
TestReportUploadFileEventOnError_ReportsAndPreservesError by applying
errs.ProblemOf to the returned error and asserting the expected category,
subtype, and param metadata, while also verifying the original cause is
preserved. Retain the existing identity, reporting-count, and tag assertions.

In `@shortcuts/common/lark_cli_file_event.go`:
- Around line 63-138: Make the upload event reporting path non-blocking so
ReportUploadFileEvent and ReportUploadFileEventOnError do not add the full
uploadFileEventReportTimeout to interactive uploads, while preserving
best-effort behavior, once-per-RuntimeContext deduplication, and the original
upload error. Ensure any asynchronous report has sufficient runtime/context
lifetime to complete before process exit, or reduce the reporting
timeout—especially for the successful upload path—if the existing synchronous
lifecycle must be retained.

In `@shortcuts/drive/drive_io_test.go`:
- Around line 1738-1747: Extend the error-path assertions around errs.ProblemOf
in the affected tests to verify the expected Category, Subtype, and param
metadata, and assert that the original cause is preserved. Apply the same
typed-metadata and cause checks to the analogous cases near the existing
assertions at lines 1795-1801 and 1843-1849, while retaining the current Code
and Hint checks.
- Around line 1771-1776: Replace the manual working-directory setup around this
test with the existing withDriveWorkingDir helper, passing t and t.TempDir().
Remove the os.Getwd, os.Chdir, and deferred restoration logic while preserving
the test’s execution in the temporary drive directory.

In `@tests/cli_e2e/drive/drive_import_workflow_test.go`:
- Line 70: Update the waitDriveImportReady helper signature to place ctx
immediately after t, before ticket and fallbackType, and adjust every call site
to pass arguments in the matching order.
🪄 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: 3d3dfa38-e600-4491-b734-7189eb854062

📥 Commits

Reviewing files that changed from the base of the PR and between 358cd06 and 8913826.

📒 Files selected for processing (16)
  • shortcuts/common/call_api_typed_test.go
  • shortcuts/common/drive_media_upload.go
  • shortcuts/common/drive_media_upload_typed_test.go
  • shortcuts/common/lark_cli_file_event.go
  • shortcuts/common/lark_cli_file_event_test.go
  • shortcuts/common/runner.go
  • shortcuts/drive/drive_import.go
  • shortcuts/drive/drive_import_test.go
  • shortcuts/drive/drive_io_test.go
  • shortcuts/drive/drive_upload.go
  • tests/cli_e2e/drive/coverage.md
  • tests/cli_e2e/drive/drive_import_dryrun_test.go
  • tests/cli_e2e/drive/drive_import_workflow_test.go
  • tests/cli_e2e/drive/drive_upload_dryrun_test.go
  • tests/cli_e2e/drive/drive_upload_workflow_test.go
  • tests/cli_e2e/sheets/sheets_workbook_import_dryrun_test.go

Comment thread tests/cli_e2e/drive/drive_import_workflow_test.go
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@zhoujunteng-max
zhoujunteng-max force-pushed the feat/upload-file-event-report branch from 33626c5 to d38de40 Compare August 6, 2026 07:49
@github-actions github-actions Bot added domain/base PR touches the base domain domain/calendar PR touches the calendar domain domain/mail PR touches the mail domain size/XL Architecture-level or global-impact change and removed size/L Large or sensitive change across domains or core paths labels Aug 7, 2026
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown

PR Quality Summary

CI did not complete successfully. Use the failed check links below to decide whether this PR needs a code change or a rerun.

Failed checks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain/base PR touches the base domain domain/calendar PR touches the calendar domain domain/ccm PR touches the ccm domain domain/mail PR touches the mail domain size/XL Architecture-level or global-impact change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant