Repository navigation
Go: add show users plan quota - #16818
Conversation
Signed-off-by: Jin Hai <[email protected]>
📝 WalkthroughWalkthroughThe admin CLI now separates users plan summary and quota commands. Quota values are parsed and range-validated, dispatched to a dedicated handler, and served through a protected admin endpoint backed by a new enterprise service method. ChangesUsers plan quota flow
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant AdminCLI
participant AdminRouter
participant ShowUsersPlan
participant EnterpriseService
AdminCLI->>AdminRouter: GET /admin/users/plan?quota=quota
AdminRouter->>ShowUsersPlan: route request
ShowUsersPlan->>EnterpriseService: ShowUsersPlanQuota(quota)
EnterpriseService-->>ShowUsersPlan: quota command data
ShowUsersPlan-->>AdminCLI: response data or error
Possibly related PRs
Suggested labels: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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 |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/cli/admin_command.go (1)
1691-1691: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winCopy-paste error in failure message.
The error message says
"failed to get users activity"but this handler fetches the users plan summary. This is a copy-paste artifact fromAdminShowUsersActivityCommand.🔧 Proposed fix
- return nil, fmt.Errorf("failed to get users activity: %w", err) + return nil, fmt.Errorf("failed to get users plan summary: %w", err)🤖 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 `@internal/cli/admin_command.go` at line 1691, In the users plan summary handler, update the error returned at the existing fmt.Errorf call to say “failed to get users plan summary” instead of “failed to get users activity,” matching the handler’s operation and distinguishing it from AdminShowUsersActivityCommand.
🤖 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 `@internal/admin/enterprise_handler.go`:
- Around line 1566-1590: ShowUsersPlan parses quota but does not enforce the
valid 0–100 range. After converting quotaStr in ShowUsersPlan, reject values
below 0 or above 100 with CodeBadRequest and the same quota validation message
used by the CLI, before calling ShowUsersPlanQuota.
In `@internal/cli/admin_parser.go`:
- Around line 2617-2618: Update the validation after consuming the QUOTA token
in the parser to return an error stating that a number is expected instead of
“expected QUOTA”; locate the relevant token parsing logic in the admin parser
and change only this misleading message.
---
Outside diff comments:
In `@internal/cli/admin_command.go`:
- Line 1691: In the users plan summary handler, update the error returned at the
existing fmt.Errorf call to say “failed to get users plan summary” instead of
“failed to get users activity,” matching the handler’s operation and
distinguishing it from AdminShowUsersActivityCommand.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 0e7e183c-3edd-4fe6-8f6e-875d69cf75e2
📒 Files selected for processing (6)
internal/admin/enterprise_handler.gointernal/admin/enterprise_service.gointernal/admin/router.gointernal/cli/admin_command.gointernal/cli/admin_parser.gointernal/cli/cli_http.go
| // ShowUsersPlan handle show users plan | ||
| func (h *Handler) ShowUsersPlan(c *gin.Context) { | ||
| var quota int | ||
| quotaStr := c.Query("quota") | ||
| if quotaStr != "" { | ||
| var err error | ||
| quota, err = strconv.Atoi(quotaStr) | ||
| if err != nil { | ||
| common.ErrorWithCode(c, common.CodeBadRequest, "Quota must be an integer") | ||
| return | ||
| } | ||
| } | ||
| usersPlanQuota, err := h.service.ShowUsersPlanQuota(quota) | ||
| if err != nil { | ||
| if errors.Is(err, common.ErrUserNotFound) { | ||
| common.ErrorWithCode(c, common.CodeNotFound, "User not found") | ||
| return | ||
| } | ||
| common.ErrorWithCode(c, common.CodeServerError, err.Error()) | ||
| return | ||
| } | ||
|
|
||
| common.SuccessWithData(c, usersPlanQuota, "") | ||
| } | ||
|
|
There was a problem hiding this comment.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
Missing server-side quota range validation.
The CLI parser validates quota is within 0–100, but the API handler accepts any integer from the query string. A direct API caller could bypass the CLI and send arbitrary quota values. Consider adding range validation server-side to keep the contract consistent.
🛡️ Proposed fix
quotaStr := c.Query("quota")
if quotaStr != "" {
var err error
quota, err = strconv.Atoi(quotaStr)
if err != nil {
common.ErrorWithCode(c, common.CodeBadRequest, "Quota must be an integer")
return
}
+ if quota < 0 || quota > 100 {
+ common.ErrorWithCode(c, common.CodeBadRequest, "Quota must be between 0 and 100")
+ return
+ }
}📝 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.
| // ShowUsersPlan handle show users plan | |
| func (h *Handler) ShowUsersPlan(c *gin.Context) { | |
| var quota int | |
| quotaStr := c.Query("quota") | |
| if quotaStr != "" { | |
| var err error | |
| quota, err = strconv.Atoi(quotaStr) | |
| if err != nil { | |
| common.ErrorWithCode(c, common.CodeBadRequest, "Quota must be an integer") | |
| return | |
| } | |
| } | |
| usersPlanQuota, err := h.service.ShowUsersPlanQuota(quota) | |
| if err != nil { | |
| if errors.Is(err, common.ErrUserNotFound) { | |
| common.ErrorWithCode(c, common.CodeNotFound, "User not found") | |
| return | |
| } | |
| common.ErrorWithCode(c, common.CodeServerError, err.Error()) | |
| return | |
| } | |
| common.SuccessWithData(c, usersPlanQuota, "") | |
| } | |
| // ShowUsersPlan handle show users plan | |
| func (h *Handler) ShowUsersPlan(c *gin.Context) { | |
| var quota int | |
| quotaStr := c.Query("quota") | |
| if quotaStr != "" { | |
| var err error | |
| quota, err = strconv.Atoi(quotaStr) | |
| if err != nil { | |
| common.ErrorWithCode(c, common.CodeBadRequest, "Quota must be an integer") | |
| return | |
| } | |
| if quota < 0 || quota > 100 { | |
| common.ErrorWithCode(c, common.CodeBadRequest, "Quota must be between 0 and 100") | |
| return | |
| } | |
| } | |
| usersPlanQuota, err := h.service.ShowUsersPlanQuota(quota) | |
| if err != nil { | |
| if errors.Is(err, common.ErrUserNotFound) { | |
| common.ErrorWithCode(c, common.CodeNotFound, "User not found") | |
| return | |
| } | |
| common.ErrorWithCode(c, common.CodeServerError, err.Error()) | |
| return | |
| } | |
| common.SuccessWithData(c, usersPlanQuota, "") | |
| } |
🤖 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 `@internal/admin/enterprise_handler.go` around lines 1566 - 1590, ShowUsersPlan
parses quota but does not enforce the valid 0–100 range. After converting
quotaStr in ShowUsersPlan, reject values below 0 or above 100 with
CodeBadRequest and the same quota validation message used by the CLI, before
calling ShowUsersPlanQuota.
| if p.curToken.Type != TokenNumber { | ||
| return nil, fmt.Errorf("expected QUOTA") |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Misleading error message after QUOTA token.
After consuming the QUOTA token, the parser expects a number. The error message "expected QUOTA" is confusing since QUOTA was already consumed. It should indicate a number was expected.
🔧 Proposed fix
if p.curToken.Type != TokenNumber {
- return nil, fmt.Errorf("expected QUOTA")
+ return nil, fmt.Errorf("expected number after QUOTA")
}📝 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.
| if p.curToken.Type != TokenNumber { | |
| return nil, fmt.Errorf("expected QUOTA") | |
| if p.curToken.Type != TokenNumber { | |
| return nil, fmt.Errorf("expected number after QUOTA") |
🤖 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 `@internal/cli/admin_parser.go` around lines 2617 - 2618, Update the validation
after consuming the QUOTA token in the parser to return an error stating that a
number is expected instead of “expected QUOTA”; locate the relevant token
parsing logic in the admin parser and change only this misleading message.
Summary