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

Adds the file-root in the hurl cli command if there is a file or Mult… #199

Merged
merged 4 commits into from
Sep 30, 2024

Conversation

Xouzoura
Copy link
Contributor

@Xouzoura Xouzoura commented Sep 29, 2024

Simple change to allow usage of the CWD with hurl to use endpoints that require uploading files.

WHAT

Adding the functionality to use local files of the project with hurl through the --file-root command.
Example

POST {{HOST}}/upload/{{GROUP}}/{{CONVERSATION}}/ad_hoc/upload
Authorization: Bearer {{BEARER}}
[MultipartFormData]
file: file,src/tests/files/doc1.pdf;type=application/pdf
url: ""

should work now.

Link to issue

WHY

Hurl does not allow by default usage of documents if the path is not specified.

HOW

Adding current path to the hurl if there is a file uploaded (Multipart + file must exist). However modification is needed if there is a need for documents not within the project.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • Linter
  • Tests
  • Review comments
  • Security

Important

Adds --file-root option to hurl command for file uploads using CWD.

  • Behavior:
    • Adds --file-root with CWD to hurl command if file upload is detected in execute_hurl_cmd() in main.lua.
    • Uses has_file_in_opts() in utils.lua to check for file uploads in options.
  • Functions:
    • Adds has_file_in_opts() in utils.lua to determine if options include file uploads.
  • Misc:
    • Updates execute_hurl_cmd() in main.lua to extend command with --file-root when necessary.

This description was created by Ellipsis for 985ae22. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • New Features

    • Introduced a command option to specify a file root based on the current directory when a file option is included.
    • Added functionality to check for file paths in command options, enhancing error handling and validation.
    • Enhanced output handling to support JSON responses and improved logging.
  • Bug Fixes

    • Improved error messaging for missing or inaccessible file paths in command options.

Copy link

codesandbox bot commented Sep 29, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

changeset-bot bot commented Sep 29, 2024

⚠️ No Changeset found

Latest commit: 1ef08c9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Sep 29, 2024

Walkthrough

The changes introduce a new boolean variable is_file_mode in the lua/hurl/main.lua file, which checks for a file option in the command options. If a file option is present, it appends a --file-root option to the command with the current working directory. Additionally, a new function util.has_file_in_opts is added in lua/hurl/utils.lua, which verifies the presence of a file path in the options and checks its content for specific keywords.

Changes

Files Change Summary
lua/hurl/main.lua Added variable local is_file_mode to check for file options and modify the command accordingly. Updated output handling to include checks for the --json flag.
lua/hurl/utils.lua Introduced function util.has_file_in_opts(opts) to check for file paths in options and validate them.

Poem

In the burrow, changes sprout,
A file mode to check, no doubt!
With options in hand, we dance and play,
Hurling commands in a clever way.
A hop, a skip, a joyful cheer,
For every file, we hold so dear! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 47478d6 and 1ef08c9.

📒 Files selected for processing (1)
  • lua/hurl/utils.lua (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • lua/hurl/utils.lua

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 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.

Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

❌ Changes requested. Reviewed everything up to 985ae22 in 18 seconds

More details
  • Looked at 60 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. lua/hurl/utils.lua:299
  • Draft comment:
    Avoid using print for error messages in utility functions. Consider using a logging mechanism or returning an error message instead.
  • Reason this comment was not posted:
    Marked as duplicate.

Workflow ID: wflow_IR29LipByijcVc35


Want Ellipsis to fix these issues? Tag @ellipsis-dev in a comment. You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

@@ -286,5 +286,30 @@ util.find_env_files_in_folders = function()

return env_files
end
util.has_file_in_opts = function(opts)
if #opts == 0 then
print('Error: No file path provided in opts.')
Copy link

Choose a reason for hiding this comment

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

Avoid using print for error messages in utility functions. Consider using a logging mechanism or returning an error message instead.

Copy link
Owner

Choose a reason for hiding this comment

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

Great. Thanks @Xouzoura Could you fix the review from AI review bot?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
lua/hurl/main.lua (3)

195-198: Addition of --file-root option

The code now adds the --file-root option with the current working directory when is_file_mode is true. This change aligns with the PR objective of enabling the use of local project files when uploading documents.

However, there are a few points to consider:

  1. The current implementation always uses vim.fn.getcwd() as the file root. This might not always be the desired behavior, especially if the Hurl file is in a different directory.
  2. There's no error handling if vim.fn.getcwd() fails for any reason.

Consider the following improvements:

  1. Allow users to specify a custom file root:
local file_root = _HURL_GLOBAL_CONFIG.file_root or vim.fn.getcwd()
vim.list_extend(cmd, { '--file-root', file_root })
  1. Add error handling:
local cwd = vim.fn.getcwd()
if cwd then
  vim.list_extend(cmd, { '--file-root', cwd })
else
  utils.log_warn('hurl: Failed to get current working directory')
end
  1. Consider using vim.fn.expand('%:p:h') instead of vim.fn.getcwd() to use the directory of the current file as the root:
local file_dir = vim.fn.expand('%:p:h')
vim.list_extend(cmd, { '--file-root', file_dir })

Line range hint 149-198: Overall impact of changes

The introduction of the is_file_mode check and the addition of the --file-root option enhance the functionality of the Hurl CLI command within this Lua script. These changes allow for better handling of local project files during document uploads, which aligns well with the stated PR objectives.

The implementation is non-breaking and additive, maintaining compatibility with existing usage while providing new capabilities. This approach is commendable as it improves functionality without disrupting current workflows.

To further improve the robustness and flexibility of this feature:

  1. Consider adding a configuration option for users to specify a default file root.
  2. Implement error handling for edge cases, such as when the current working directory cannot be determined.
  3. Add documentation or comments explaining the purpose and usage of the new is_file_mode variable and the --file-root option for future maintainers.

Line range hint 1-598: Suggestions for overall improvements

While the changes implemented are good, there are some general improvements that could be made to enhance the overall quality of the file:

  1. Documentation: Consider adding more inline comments explaining the purpose of key functions and variables, especially for the newly added is_file_mode functionality.

  2. Error Handling: Implement more robust error handling throughout the file, not just for the new changes. This could include checking for nil values, handling potential exceptions, and providing informative error messages.

  3. Configuration: Consider centralizing configuration options, including the new file root functionality, in a dedicated configuration section or file.

  4. Testing: If not already present, add unit tests for the new functionality to ensure it works as expected and to prevent future regressions.

  5. Refactoring: Some functions in this file are quite long and complex. Consider breaking them down into smaller, more focused functions to improve readability and maintainability.

These suggestions aim to improve the overall code quality, maintainability, and robustness of the entire file, not just the changed sections.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 87ff786 and 985ae22.

📒 Files selected for processing (2)
  • lua/hurl/main.lua (2 hunks)
  • lua/hurl/utils.lua (1 hunks)
🔇 Additional comments (3)
lua/hurl/utils.lua (2)

Line range hint 1-313: Request clarification on PR objectives and implementation

The new util.has_file_in_opts function adds the ability to check for 'file' or 'multipart' keywords in a given file. However, its relationship to the PR objective of adding a --file-root command is not immediately clear from this file alone.

Could you please provide more context on how this function contributes to the implementation of the --file-root command mentioned in the PR objectives? Additionally, are there other files or changes that work in conjunction with this new function to achieve the stated goals?

To gain more context, let's search for files that might be using the new --file-root command:

rg --type lua "file-root"

This will help us understand where and how the --file-root command is being implemented or used in the project.


289-313: 🛠️ Refactor suggestion

⚠️ Potential issue

Clarify the function's purpose and improve its implementation

The new util.has_file_in_opts function seems to be checking for the presence of 'file' or 'multipart' keywords in a given file. However, its relationship to the PR objective of adding a --file-root command is not clear. Consider the following improvements:

  1. Rename the function to better reflect its purpose, e.g., util.file_contains_upload_keywords.

  2. Enhance input validation and error handling:

-util.has_file_in_opts = function(opts)
-  if #opts == 0 then
-    print('Error: No file path provided in opts.')
-    return false
-  end
-
-  local file_path = opts[1]
+util.file_contains_upload_keywords = function(file_path)
+  if type(file_path) ~= "string" or file_path == "" then
+    util.log_error('Error: Invalid file path provided.')
+    return false
+  end
  1. Use existing logging functions instead of print:
-    print('Error: Failed to open file: ' .. file_path)
+    util.log_error('Error: Failed to open file: ' .. file_path)
  1. Consider adding parameters for customizable keywords and case sensitivity:
util.file_contains_keywords = function(file_path, keywords, case_sensitive)
  -- ... (input validation)
  
  for line in file:lines() do
    for _, keyword in ipairs(keywords) do
      if (case_sensitive and line:find(keyword)) or
         (not case_sensitive and line:lower():find(keyword:lower())) then
        file:close()
        return true
      end
    end
  end
  
  -- ... (file closure)
end
  1. Explain how this function relates to the --file-root command mentioned in the PR objectives.

To understand the context better, let's search for references to this new function:

This will help clarify how the function is being used in relation to the --file-root command.

lua/hurl/main.lua (1)

149-150: New variable is_file_mode introduced

A new boolean variable is_file_mode is added to determine if the command includes a file option. This is a good addition as it allows for conditional behavior based on whether a file is being processed.

@@ -190,6 +192,10 @@ local function execute_hurl_cmd(opts, callback)

-- Include the HTTP headers in the output and do not colorize output.
local cmd = vim.list_extend({ 'hurl', '-i', '--no-color' }, opts)
if is_file_mode then
local file_root = _HURL_GLOBAL_CONFIG.file_root or vim.fn.getcwd()
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add the to global variable about the new config? (file_root) @Xouzoura

Copy link
Owner

@jellydn jellydn left a comment

Choose a reason for hiding this comment

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

Nice works. Thanks @Xouzoura

Copy link
Owner

@jellydn jellydn left a comment

Choose a reason for hiding this comment

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

LGTM!

@jellydn jellydn merged commit 36c90c3 into jellydn:main Sep 30, 2024
7 checks passed
@jellydn
Copy link
Owner

jellydn commented Sep 30, 2024

@all-contributors add @Xouzoura for code.

Copy link
Contributor

@jellydn

I've put up a pull request to add @Xouzoura! 🎉

@coderabbitai coderabbitai bot mentioned this pull request Oct 7, 2024
7 tasks
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.

2 participants