Skip to content
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

CLI: make the output of create commands machine-readable #8833

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

SpecLad
Copy link
Contributor

@SpecLad SpecLad commented Dec 16, 2024

Motivation and context

The underlying SDK functions already emit human-friendly log messages with the ID of the created resource. Instead of printing largely the same message twice, we can just print the ID. That way, the CLI can be more easily integrated into other software.

How has this been tested?

CLI tests.

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • [ ] I have updated the documentation accordingly
  • I have added tests to cover my changes
  • [ ] I have linked related issues (see GitHub docs)
  • [ ] I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • New Features

    • Streamlined command-line output for project and task creation commands, now displaying only the resource ID.
  • Bug Fixes

    • Improved robustness in extracting project and task IDs from command-line output by addressing trailing whitespace issues.
  • Tests

    • Updated test methods to enhance ID extraction reliability, ensuring consistent handling of output formatting.

Copy link
Contributor

coderabbitai bot commented Dec 16, 2024

Walkthrough

The changes focus on enhancing the machine-readability of CLI outputs for task and project creation commands. The modifications simplify the output to return only the resource ID when creating tasks or projects, removing additional descriptive text. This change is implemented across multiple files in the CVAT CLI, including command implementations and corresponding test files, with updates to how task and project IDs are extracted and displayed.

Changes

File Change Summary
cvat-cli/src/cvat_cli/_internal/commands_projects.py Modified ProjectCreate.execute() to print only project ID
cvat-cli/src/cvat_cli/_internal/commands_tasks.py Updated TaskCreate and TaskCreateFromBackup to print only task ID
tests/python/cli/test_cli_projects.py Updated ID extraction to use rstrip("\n") for robustness
tests/python/cli/test_cli_tasks.py Modified task ID extraction to handle trailing newlines
changelog.d/20241216_144316_roman_machine_readable_create.md Added changelog entry for the modifications

Poem

🐰 A CLI so clean and bright,
Just IDs now, no extra might!
No words, no fluff, just numbers pure,
Machine-readable, that's for sure!
Hop hop hooray for simple print! 🚀

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c729f18 and 67ed89e.

📒 Files selected for processing (5)
  • changelog.d/20241216_144316_roman_machine_readable_create.md (1 hunks)
  • cvat-cli/src/cvat_cli/_internal/commands_projects.py (1 hunks)
  • cvat-cli/src/cvat_cli/_internal/commands_tasks.py (2 hunks)
  • tests/python/cli/test_cli_projects.py (2 hunks)
  • tests/python/cli/test_cli_tasks.py (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • cvat-cli/src/cvat_cli/_internal/commands_tasks.py
🔇 Additional comments (7)
changelog.d/20241216_144316_roman_machine_readable_create.md (1)

1-6: LGTM! Clear and well-structured changelog entry.

The changelog entry effectively communicates the changes and their purpose, following the proper format and including all necessary details.

tests/python/cli/test_cli_projects.py (2)

38-38: LGTM! Robust handling of CLI output.

The change correctly handles the new simplified output format, properly removing trailing newlines before parsing the project ID.


55-55: LGTM! Consistent output handling.

The change maintains consistency with the previous test method in handling the simplified output format.

cvat-cli/src/cvat_cli/_internal/commands_projects.py (1)

86-86: LGTM! Simplified output for better machine readability.

The change aligns with the PR objective of making the output machine-readable.

Let's verify error handling remains intact:

✅ Verification successful

LGTM! Error handling is properly managed through the SDK layer.

The error handling for the CLI command is properly managed through the SDK layer. The code shows:

  • The command simply prints the project ID on success
  • Any errors (e.g., permission errors, validation errors) are handled by the SDK's exception system
  • The SDK tests verify proper error handling for various scenarios like:
    • Permission errors (403)
    • Not found errors (404)
    • Validation errors (400)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if error messages are properly handled and not affected by the output changes

# Search for error handling patterns in the codebase
rg -A 3 "raise|except" cvat-cli/src/cvat_cli/_internal/commands_projects.py

Length of output: 77


Script:

#!/bin/bash
# Let's try a different approach to check error handling patterns
# First, let's see the entire file content to understand the context
cat cvat-cli/src/cvat_cli/_internal/commands_projects.py

Length of output: 2756


Script:

#!/bin/bash
# Let's check the SDK implementation to understand error handling
# First, check the projects module in SDK
fd "projects.py" --exec cat {} \;

# Also check the base implementation for error handling patterns
fd "base.py" --exec cat {} \;

Length of output: 66710

tests/python/cli/test_cli_tasks.py (3)

105-105: LGTM! Consistent with previous change

The modification maintains consistency with the previous test case, using the same robust approach to extract the task ID.


194-194: LGTM! Verify backup creation output format

The change maintains consistency with other task creation commands. However, since this uses a different command path (create-from-backup), let's verify its output format.

✅ Verification successful

LGTM! The task ID output format is consistent

Looking at the implementation in cvat-cli/src/cvat_cli/_internal/commands_tasks.py, the TaskCreateFromBackup class simply prints the task ID using print(task.id), which outputs only the numeric task ID. This is consistent with other task creation commands and confirms that using rstrip("\n") is appropriate for parsing the output.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the task create-from-backup command outputs only the task ID
# Expected: Output should be a single line containing only digits

# Search for the backup creation command implementation
rg -l "def.*TaskCreateFromBackup"

# Search for print statements in backup creation
rg "print\(" --type py | rg -i "task.*backup.*create"

Length of output: 91


Script:

#!/bin/bash
# Let's try a different approach to find the implementation
# First, let's find files related to task backup
fd -e py . | rg -i "task.*backup"

# Then, let's look for CLI command registration related to task backup
rg "def.*command.*backup" --type py

# Finally, let's look for any print statements or stdout writes in task-related CLI files
rg "print|stdout.write" "cli/tasks.py" "cli/task_backup.py" 2>/dev/null

Length of output: 131


Script:

#!/bin/bash
# Let's look at the test file itself to understand the command being tested
cat "tests/python/cli/test_cli_tasks.py"

# Also search for any task-related CLI files in the codebase
fd -t f -e py | rg "task.*cli|cli.*task"

Length of output: 9261


Script:

#!/bin/bash
# Let's examine the CLI implementation for task creation from backup
cat "cvat-cli/src/cvat_cli/_internal/commands_tasks.py"

Length of output: 16507


77-77: LGTM! Verify CLI output format

The change to rstrip("\n") is more robust than splitting. However, let's verify that the CLI command outputs only the task ID.

✅ Verification successful

Based on the code found, I can now generate the final response:

The change to use rstrip("\n") is correct and matches the CLI's output format

The CLI command implementation in TaskCreateFromBackup.execute() shows that it outputs only the task ID using print(task.id), which will append a single newline. Therefore, using rstrip("\n") is the correct way to parse the output.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the task create command outputs only the task ID
# Expected: Output should be a single line containing only digits

# Search for the task create command implementation
rg -l "def.*TaskCreate" 

# Search for print statements in task creation
rg "print\(" --type py | rg -i "task.*create"

Length of output: 73


Script:

#!/bin/bash
# Let's try a different approach to find the CLI command implementation
# First, find the CLI command file
fd "cli.py|tasks.py" --type f

# Then search for task creation related code
rg "task.*create" --type py -A 5

Length of output: 65665


Script:

#!/bin/bash
# Let's check the CLI command implementation to verify the output format
rg -A 5 "def create_from_backup" cvat-cli/src/cvat_cli/_internal/commands_tasks.py

Length of output: 84


Script:

#!/bin/bash
# Let's try finding the CLI command implementation in a different way
fd "commands_tasks.py" --type f --exec cat {}

Length of output: 16499


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

The underlying SDK functions already emit human-friendly log messages with
the ID of the created resource. Instead of printing largely the same message
twice, we can just print the ID. That way, the CLI can be more easily
integrated into other software.
@SpecLad SpecLad force-pushed the machine-readable-create branch from 99799df to 67ed89e Compare December 16, 2024 12:46
@SpecLad SpecLad marked this pull request as ready for review December 16, 2024 12:54
@SpecLad SpecLad requested review from zhiltsov-max and removed request for nmanovic and azhavoro December 16, 2024 12:54
@SpecLad SpecLad changed the title CLI make the output of create commands machine-readable CLI: make the output of create commands machine-readable Dec 16, 2024
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.89%. Comparing base (289ad43) to head (67ed89e).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8833      +/-   ##
===========================================
- Coverage    73.93%   73.89%   -0.05%     
===========================================
  Files          409      409              
  Lines        43930    43930              
  Branches      3986     3986              
===========================================
- Hits         32478    32460      -18     
- Misses       11452    11470      +18     
Components Coverage Δ
cvat-ui 78.33% <ø> (ø)
cvat-server 70.07% <ø> (-0.08%) ⬇️

@SpecLad SpecLad merged commit 73458b3 into cvat-ai:develop Dec 17, 2024
43 checks passed
@SpecLad SpecLad deleted the machine-readable-create branch December 17, 2024 12:49
@cvat-bot cvat-bot bot mentioned this pull request Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants