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

Handle tree paths in GitHub identifiers #5324

Merged
merged 1 commit into from
Dec 28, 2024
Merged

Conversation

brimoor
Copy link
Contributor

@brimoor brimoor commented Dec 27, 2024

Change log

The @voxel51/plugins/install_plugin operator stopped working as a side effect of #4614 because the operator tried to issue a command like this:

import fiftyone.plugins as fop

url = "https://github.com/voxel51/fiftyone-plugins/tree/main/plugins/dashboard"
fop.download_plugin(url, plugin_names=["@voxel51/dashboard"])

where the url is pulled directly from here.

When the operator was initially written, the above download_plugin() syntax worked, but only because the GitHubRepository class would automatically strip main/plugins/dashboard down to main. However, that was causing our plugins API to not support branches/tags that contain / (what #4614 solved).

This PR brings us back to the best of both worlds: GitHubRepository(repo) now supports repo strings of the form <ref>[/<path>], where both <ref> and <path> may contain /.

With this patch, the @voxel51/plugins/install_plugin operator works again.

And, as an added bonus, the download_plugin() method now supports scoping downloads by subdirectory!

fop.download_plugin("https://github.com/voxel51/fiftyone-plugins/tree/main/plugins/dashboard")

Previously the only way to install a subset of plugins from a repository was to specify them by name via the plugin_names= kwarg.

Example usages

Search for plugins in a specific tree root

import fiftyone.plugins.utils as fopu

plugins = fopu.find_plugins("https://github.com/voxel51/fiftyone-plugins/tree/main/plugins/annotation")
assert len(plugins) == 1

Download plugin from a specific tree root

import fiftyone.plugins as fop

fop.download_plugin("https://github.com/voxel51/fiftyone-plugins/tree/main/plugins/dashboard")

New GitHubRepository(..., safe=True) syntax

This new syntax is what enables the other syntaxes to work under the hood:

import fiftyone.utils.github as foug

url = "https://github.com/voxel51/fiftyone-plugins/tree/main/plugins/dashboard"

repo = foug.GitHubRepository(url)
assert repo.ref == "main/plugins/dashboard"
assert repo.safe_path == None

repo = foug.GitHubRepository(url, safe=True)
assert repo.ref == "main"
assert repo.safe_path == "plugins/dashboard"

url = "https://github.com/voxel51/fiftyone-plugins/tree/f5e1d93233caace681b427d7e13677cbcfe58a06/plugins/dashboard"

repo = foug.GitHubRepository(url, safe=True)
assert repo.ref == "f5e1d93233caace681b427d7e13677cbcfe58a06"
assert repo.safe_path == "plugins/dashboard"

@brimoor brimoor added the bug Bug fixes label Dec 27, 2024
@brimoor brimoor requested a review from a team December 27, 2024 23:04
Copy link
Contributor

coderabbitai bot commented Dec 27, 2024

Walkthrough

The pull request introduces enhancements to the plugin management system in FiftyOne, focusing on improving GitHub repository handling and plugin discovery. The changes primarily involve adding a safe parameter to the GitHubRepository class, which allows more flexible path handling when working with GitHub repositories. The modifications enable more granular control over plugin downloads and searches, with improved error handling and path resolution mechanisms.

Changes

File Change Summary
fiftyone/plugins/core.py - Updated download_plugin method to use safe=True in repository instantiation
- Refined root_dir logic for plugin downloads
- Improved logging for plugin discovery
fiftyone/plugins/utils.py - Enhanced find_plugins function to handle GitHub tree paths
- Updated documentation for repository path handling
fiftyone/utils/github.py - Added safe parameter to GitHubRepository initialization
- Implemented _handle_safe_path method
- Added safe_path property
- Refactored token retrieval method

Sequence Diagram

sequenceDiagram
    participant User
    participant GitHubRepository
    participant GitHub API
    User->>GitHubRepository: Initialize with repo and safe=True
    GitHubRepository->>GitHub API: Validate repository path
    GitHub API-->>GitHubRepository: Return path details
    GitHubRepository-->>User: Repository with safe path
Loading

Poem

🐰 A Rabbit's Ode to Safe Paths 🌿

Through GitHub's winding repository maze,
With safe as my guide, I hop and graze,
Paths now flexible, no longer constrained,
A plugin's journey, elegantly explained!
Code leaps forward with newfound grace 🚀

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. We would love to hear your feedback on Discord.


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.

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
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: 1

🧹 Nitpick comments (2)
fiftyone/plugins/utils.py (1)

119-122: Inline expression for cleaner code
Consider merging the if/else into a single ternary expression to make the logic more concise.

Proposed fix snippet:

-        if path is not None:
-            root = repo.safe_path + "/" + path
-        else:
-            root = repo.safe_path
+        root = repo.safe_path + "/" + path if path is not None else repo.safe_path
🧰 Tools
🪛 Ruff (0.8.2)

119-122: Use ternary operator root = repo.safe_path + "/" + path if path is not None else repo.safe_path instead of if-else-block

Replace if-else-block with root = repo.safe_path + "/" + path if path is not None else repo.safe_path

(SIM108)

fiftyone/plugins/core.py (1)

213-223: Consider explicitly handling multi-subdir scenarios
When repo.safe_path is used, the code assumes there is a single subdir. If multiple subdirs exist, no action is taken. Consider logging a warning or throwing an error to handle unexpected directory structures.

+            else:
+                logger.warning(
+                    "Multiple subdirectories found. Cannot reliably map "
+                    f"safe_path='{repo.safe_path}'"
+                )
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 18dde78 and 93580f7.

📒 Files selected for processing (3)
  • fiftyone/plugins/core.py (4 hunks)
  • fiftyone/plugins/utils.py (2 hunks)
  • fiftyone/utils/github.py (4 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
fiftyone/plugins/utils.py

119-122: Use ternary operator root = repo.safe_path + "/" + path if path is not None else repo.safe_path instead of if-else-block

Replace if-else-block with root = repo.safe_path + "/" + path if path is not None else repo.safe_path

(SIM108)

fiftyone/utils/github.py

84-84: Do not use bare except

(E722)

🔇 Additional comments (10)
fiftyone/utils/github.py (4)

37-40: Docstring enhancement is clear and concise
These lines cleanly document the new safe parameter and its behavior. Nicely done!


43-43: Approach for safe initialization
The safe parameter is initialized in the constructor and leads to _handle_safe_path(). This approach is clean and straightforward, ensuring all logic for path handling is encapsulated in _handle_safe_path().

Also applies to: 52-52, 55-56


102-105: New safe_path property complements the design
Exposing _safe_path via a read-only property is a good design choice, providing a clear interface for consuming code.


224-224: Improved token retrieval and enhanced error messaging
Adding _get_token() centralizes token retrieval logic. The improved error message with suggestions to set GITHUB_TOKEN is helpful for users.

Also applies to: 231-233, 239-245

fiftyone/plugins/utils.py (2)

82-86: Example usage is informative
These example lines effectively guide the user on how to discover plugins within a subdirectory.


96-104: Docstring clarification
Expanding the parameter documentation to include “GitHub tree path” usage is great for discoverability.

fiftyone/plugins/core.py (4)

25-25: Import statement
Importing fiftyone.core.storage as fos looks good and is consistent with project style.


176-177: Docstring update for tree paths
Adding tree path examples improves clarity and helps users quickly identify how to fetch subdirectory-scoped plugins.


192-196: Consistent safe=True usage
Using safe=True for both GitHub URL paths and repo identifiers ensures consistent behavior in all code paths.


229-233: User-friendly feedback when no plugins are found
Logging the source (repo.identifier or url) aids in diagnosing plugin discovery issues.

Copy link
Contributor

@kaixi-wang kaixi-wang left a comment

Choose a reason for hiding this comment

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

nice!
image

@brimoor brimoor merged commit 35c5b78 into develop Dec 28, 2024
14 checks passed
@brimoor brimoor deleted the bugfix/github-tree-path branch December 28, 2024 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants