Skip to content

feat: auto-complete mail rule reorder ids#1623

Open
jqmseu wants to merge 2 commits into
larksuite:mainfrom
jqmseu:feat/01kw3e0
Open

feat: auto-complete mail rule reorder ids#1623
jqmseu wants to merge 2 commits into
larksuite:mainfrom
jqmseu:feat/01kw3e0

Conversation

@jqmseu

@jqmseu jqmseu commented Jun 27, 2026

Copy link
Copy Markdown

Summary

  • add mail +rule-reorder shortcut for partial rule id reorder requests
  • fetch current mailbox rules and append omitted ids before calling reorder
  • add unit coverage and mail skill reference for the new shortcut

Testing

  • go test ./shortcuts/mail -run "Test(ParseRuleIDsInput|BuildCompletedRuleOrder|MailRuleReorder)"

Summary by CodeRabbit

  • New Features

    • Added a new mail shortcut: mail +rule-reorder to reorder inbox rules using either a partial --rule-ids list or move-style placement (--move with --before / --after / --to-top).
    • Supports --dry-run previews and automatically completes omitted rule IDs while preserving existing relative order.
  • Bug Fixes

    • Improved input validation (required flags, numeric-only IDs, duplicate detection, and placement constraint checks), and added clearer errors for missing/unknown rules.
  • Documentation

    • Updated mail shortcut guides and added a dedicated rule reordering reference with examples and preview output details.
  • Tests

    • Expanded unit and E2E coverage for reorder and move semantics, dry-run request sequencing, and ordering correctness.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions github-actions Bot added domain/mail PR touches the mail domain size/M Single-domain feat or fix with limited business impact labels Jun 27, 2026
@coderabbitai

coderabbitai Bot commented Jun 27, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds mail +rule-reorder, which loads current mailbox rules, completes omitted IDs or applies a move placement, and then previews or posts the reordered rule list. The shortcut is wired into mail registration, tested, and documented.

Changes

Mail rule reorder shortcut

Layer / File(s) Summary
Shortcut entry and wiring
shortcuts/mail/mail_rule_reorder.go, shortcuts/mail/shortcuts.go
MailRuleReorder is defined, validated, and added to the mail shortcut list; the shortcut resolves mailbox rules, handles dry-run versus execute, and emits the reorder preview.
Rule parsing and reorder helpers
shortcuts/mail/mail_rule_reorder.go
--rule-ids and move-mode parsing, mailbox rule listing, completed-order construction, move ordering, preview building, and JSON scalar normalization are added for the reorder flow.
Parsing and ordering tests
shortcuts/mail/mail_rule_reorder_test.go
Unit tests cover rule ID parsing, reorder-input validation, and completion or move-based rule ordering.
Dry-run and execute tests
shortcuts/mail/mail_rule_reorder_test.go
Dry-run, execute, and unknown-rule paths are covered with runtime helpers, dry-run payload decoding, and mailbox rule GET/POST stubs.
Docs and coverage references
skills/lark-mail/SKILL.md, skills/lark-mail/references/lark-mail-rule-reorder.md, tests/cli_e2e/mail/coverage.md
The mail skill guide, new rule-reorder reference doc, and CLI E2E coverage table are updated for +rule-reorder.

Sequence Diagram(s)

sequenceDiagram
  participant MailRuleReorder
  participant listMailboxRules
  participant buildRuleReorderPlan
  participant RulesReorderAPI

  MailRuleReorder->>listMailboxRules: GET current mailbox rules
  listMailboxRules-->>MailRuleReorder: rule items
  MailRuleReorder->>buildRuleReorderPlan: complete IDs or apply move
  alt dry-run
    MailRuleReorder-->>MailRuleReorder: print preview
  else execute
    MailRuleReorder->>RulesReorderAPI: POST completed_rule_ids
    RulesReorderAPI-->>MailRuleReorder: reorder response
    MailRuleReorder-->>MailRuleReorder: print preview
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • larksuite/cli#1017: Shares the mail shortcut registration path in shortcuts/mail/shortcuts.go.

Suggested reviewers

  • chanthuang
  • infeng

Poem

🐰 I hopped through rules in a tidy row,
Partial IDs and moves now know where to go.
A twitch of nose, a reorder gleam,
Then preview trails the rabbit stream.
Hop hop! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers summary and testing, but it omits the required Changes and Related Issues sections from the template. Add the required ## Changes and ## Related Issues sections, and rename Testing to ## Test Plan with a brief validation checklist.
Docstring Coverage ⚠️ Warning Docstring coverage is 3.13% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the new mail rule reorder shortcut and its auto-complete behavior.
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
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@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: 4

🤖 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/mail/mail_rule_reorder_test.go`:
- Around line 58-73: The dry-run test for MailRuleReorder only verifies the GET
and POST calls, but it does not assert the reorder payload body. Update
TestMailRuleReorderDryRunListsAndReorders to check apis[1].Body["rule_ids"] for
the expected rule ID list so the POST contract in MailRuleReorder.DryRun remains
covered if the payload shape changes.
- Around line 21-37: Add a non-numeric failure test for parseRuleIDsInput to
cover the strconv.ParseInt error path. In
shortcuts/mail/mail_rule_reorder_test.go, add a case like "3,abc" and assert the
validation error carries the expected typed metadata from parseRuleIDsInput as
well as the preserved underlying cause via the wrapped error. Reuse the existing
test pattern around parseRuleIDsInput and the duplicate-id test, but extend it
to verify both the validation type and cause for the non-numeric input.
- Around line 32-37: The rule ID parsing tests are only asserting error text, so
they can miss regressions in typed validation metadata. Update
TestParseRuleIDsInputRejectsDuplicate and the related tests in the same area to
assert the returned error with errs.ProblemOf for category/subtype and use
errors.As against *errs.ValidationError to check Param, instead of relying on
substring matching. Keep the focus on parseRuleIDsInput and the
duplicate/invalid input cases so the tests verify the structured error fields
directly.

In `@shortcuts/mail/mail_rule_reorder.go`:
- Around line 81-90: The single-rule branch in mail_rule_reorder.Execute is
returning a success preview before unknown rule IDs are validated. Move the
buildCompletedRuleOrder call ahead of the len(rules) == 1 short-circuit so the
same existence check runs for all inputs, then keep the preview/no-op return
path using the validated reorderedRules and completedIDs. Ensure the behavior in
buildRuleReorderPreview and buildCompletedRuleOrder stays consistent with the
existing not-found handling.
🪄 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

Run ID: 69a8bef4-fc67-41f6-a126-f8d8de743b61

📥 Commits

Reviewing files that changed from the base of the PR and between 8a268aa and 678ba3e.

📒 Files selected for processing (6)
  • shortcuts/mail/mail_rule_reorder.go
  • shortcuts/mail/mail_rule_reorder_test.go
  • shortcuts/mail/shortcuts.go
  • skills/lark-mail/SKILL.md
  • skills/lark-mail/references/lark-mail-rule-reorder.md
  • tests/cli_e2e/mail/coverage.md

Comment thread shortcuts/mail/mail_rule_reorder_test.go
Comment on lines +32 to +37
func TestParseRuleIDsInputRejectsDuplicate(t *testing.T) {
_, err := parseRuleIDsInput("3,1,3")
if err == nil || !strings.Contains(err.Error(), "duplicate") {
t.Fatalf("expected duplicate validation error, got %v", err)
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Assert typed error metadata here.

These tests only match error text, so they will miss regressions in category, subtype, and param. Use errs.ProblemOf for problem fields and errors.As(..., *errs.ValidationError) for Param instead of substring checks. As per coding guidelines, error-path tests must assert typed metadata, and based on learnings errs.ProblemOf does not expose Param directly.

Also applies to: 112-121

🤖 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/mail/mail_rule_reorder_test.go` around lines 32 - 37, The rule ID
parsing tests are only asserting error text, so they can miss regressions in
typed validation metadata. Update TestParseRuleIDsInputRejectsDuplicate and the
related tests in the same area to assert the returned error with errs.ProblemOf
for category/subtype and use errors.As against *errs.ValidationError to check
Param, instead of relying on substring matching. Keep the focus on
parseRuleIDsInput and the duplicate/invalid input cases so the tests verify the
structured error fields directly.

Sources: Coding guidelines, Learnings

Comment on lines +58 to +73
func TestMailRuleReorderDryRunListsAndReorders(t *testing.T) {
runtime := runtimeForMailRuleReorderDryRun(t, map[string]string{
"mailbox": "me",
"rule-ids": "3,1",
})
apis := dryRunAPIsForMailRuleReorderTest(t, MailRuleReorder.DryRun(context.Background(), runtime))
if len(apis) != 2 {
t.Fatalf("expected 2 API calls in dry-run, got %d", len(apis))
}
if apis[0].Method != "GET" || apis[0].URL != mailboxPath("me", "rules") {
t.Fatalf("first dry-run API = %+v, want GET %s", apis[0], mailboxPath("me", "rules"))
}
if apis[1].Method != "POST" || apis[1].URL != mailboxPath("me", "rules", "reorder") {
t.Fatalf("second dry-run API = %+v, want POST %s", apis[1], mailboxPath("me", "rules", "reorder"))
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

The dry-run test never checks the reorder payload.

A regression that drops or renames the POST rule_ids body would still pass, even though that payload is part of the new dry-run contract. Please assert apis[1].Body["rule_ids"] too. As per coding guidelines, every behavior change needs a test, and the dry-run implementation in shortcuts/mail/mail_rule_reorder.go:52-66 includes the POST body contract.

🤖 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/mail/mail_rule_reorder_test.go` around lines 58 - 73, The dry-run
test for MailRuleReorder only verifies the GET and POST calls, but it does not
assert the reorder payload body. Update
TestMailRuleReorderDryRunListsAndReorders to check apis[1].Body["rule_ids"] for
the expected rule ID list so the POST contract in MailRuleReorder.DryRun remains
covered if the payload shape changes.

Source: Coding guidelines

Comment thread shortcuts/mail/mail_rule_reorder.go Outdated
@github-actions github-actions Bot added size/L Large or sensitive change across domains or core paths and removed size/M Single-domain feat or fix with limited business impact labels Jun 27, 2026

@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

🤖 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/mail/mail_rule_reorder.go`:
- Around line 267-271: The validation metadata for the rule reorder checks is
inconsistent because the MoveRuleID/BeforeRuleID and MoveRuleID/AfterRuleID
branches pass plain labels instead of the CLI flag names. Update the error
construction in mail_rule_reorder.go within the rule reorder validation logic to
use the same "--before" and "--after" identifiers as the other validation paths,
so mailValidationParamError is called consistently for the BeforeRuleID and
AfterRuleID checks.
🪄 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

Run ID: a2a381ec-36e8-41f7-a001-60d432e54ccc

📥 Commits

Reviewing files that changed from the base of the PR and between 678ba3e and 3334132.

📒 Files selected for processing (2)
  • shortcuts/mail/mail_rule_reorder.go
  • shortcuts/mail/mail_rule_reorder_test.go

Comment on lines +267 to +271
if input.MoveRuleID == input.BeforeRuleID && input.BeforeRuleID != "" {
return ruleReorderInput{}, mailValidationParamError("before", "--before cannot reference the same rule as --move")
}
if input.MoveRuleID == input.AfterRuleID && input.AfterRuleID != "" {
return ruleReorderInput{}, mailValidationParamError("after", "--after cannot reference the same rule as --move")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Use the CLI flag name in validation metadata.

These two paths pass "before" / "after" while the other validation paths use "--before" / "--after", so typed error metadata becomes inconsistent for the same flags.

Proposed fix
-		if input.MoveRuleID == input.BeforeRuleID && input.BeforeRuleID != "" {
-			return ruleReorderInput{}, mailValidationParamError("before", "--before cannot reference the same rule as --move")
+		if input.MoveRuleID == input.BeforeRuleID && input.BeforeRuleID != "" {
+			return ruleReorderInput{}, mailValidationParamError("--before", "--before cannot reference the same rule as --move")
 		}
 		if input.MoveRuleID == input.AfterRuleID && input.AfterRuleID != "" {
-			return ruleReorderInput{}, mailValidationParamError("after", "--after cannot reference the same rule as --move")
+			return ruleReorderInput{}, mailValidationParamError("--after", "--after cannot reference the same rule as --move")
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if input.MoveRuleID == input.BeforeRuleID && input.BeforeRuleID != "" {
return ruleReorderInput{}, mailValidationParamError("before", "--before cannot reference the same rule as --move")
}
if input.MoveRuleID == input.AfterRuleID && input.AfterRuleID != "" {
return ruleReorderInput{}, mailValidationParamError("after", "--after cannot reference the same rule as --move")
if input.MoveRuleID == input.BeforeRuleID && input.BeforeRuleID != "" {
return ruleReorderInput{}, mailValidationParamError("--before", "--before cannot reference the same rule as --move")
}
if input.MoveRuleID == input.AfterRuleID && input.AfterRuleID != "" {
return ruleReorderInput{}, mailValidationParamError("--after", "--after cannot reference the same rule as --move")
}
🤖 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/mail/mail_rule_reorder.go` around lines 267 - 271, The validation
metadata for the rule reorder checks is inconsistent because the
MoveRuleID/BeforeRuleID and MoveRuleID/AfterRuleID branches pass plain labels
instead of the CLI flag names. Update the error construction in
mail_rule_reorder.go within the rule reorder validation logic to use the same
"--before" and "--after" identifiers as the other validation paths, so
mailValidationParamError is called consistently for the BeforeRuleID and
AfterRuleID checks.

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

Labels

domain/mail PR touches the mail domain size/L Large or sensitive change across domains or core paths

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants