Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

修改插件商店下载逻辑 #1851

Closed
wants to merge 11 commits into from
Closed

修改插件商店下载逻辑 #1851

wants to merge 11 commits into from

Conversation

Copaan
Copy link
Contributor

@Copaan Copaan commented Feb 14, 2025

No description provided.

Copy link
Contributor

sourcery-ai bot commented Feb 14, 2025

## Sourcery 评审者指南

此拉取请求修改了插件商店的下载逻辑,以便在安装插件之前从 GitHub 仓库获取最新的 commit hash。这确保始终安装最新版本的插件,即使未明确指定插件的版本。它还添加了实用程序函数来确定分支是否为 commit hash。

#### 包含最新 Commit 的插件安装顺序图

```mermaid
sequenceDiagram
  participant PluginStore
  participant GithubUtils
  participant GithubAPI
  participant Github

  PluginStore->GithubUtils: parse_github_url(github_url)
  activate GithubUtils
  GithubUtils-->PluginStore: repo_info
  deactivate GithubUtils
  PluginStore->repo_info: is_commit_hash()
  alt not repo_info.is_commit_hash()
    PluginStore->repo_info: get_latest_commit()
    activate repo_info
    repo_info->Github: GET /repos/{owner}/{repo}/branches/{branch}
    activate Github
    Github-->repo_info: commit SHA
    deactivate Github
    repo_info-->PluginStore: latest_commit
    deactivate repo_info
    PluginStore->repo_info: branch = latest_commit
  end
  PluginStore->GithubUtils: iter_api_strategies()
  loop For each repo_api in strategies
    PluginStore->repo_api: parse_repo_info(repo_info)
    activate repo_api
    repo_api->GithubAPI: Fetch repo information
    activate GithubAPI
    GithubAPI-->repo_api: Repo information
    deactivate GithubAPI
    repo_api-->PluginStore: Success or Failure
    deactivate repo_api
  end

RepoInfo 的更新类图

classDiagram
    class RepoInfo {
        -owner: str
        -repo: str
        -branch: str
        +__init__(owner: str, repo: str, branch: str)
        +to_dict(**kwargs)
        +get_latest_commit() str
        +is_commit_hash() bool
    }
    note for RepoInfo "Added get_latest_commit() and is_commit_hash() methods"
Loading

文件级别变更

变更 详情 文件
添加了从 GitHub 仓库检索最新 commit hash 并确定分支是否为 commit hash 的功能。
  • 添加了 get_latest_commit 方法,用于从 GitHub 分支检索最新的 commit SHA。
  • 添加了 is_commit_hash 方法,用于检查给定的分支是否为 commit hash。
zhenxun/utils/github_utils/models.py
修改了插件安装过程,以获取最新的 commit hash 并在安装期间将其用作分支。
  • 尝试在安装插件之前从仓库获取最新的 commit hash。
  • 如果分支还不是 commit hash,则使用最新的 commit hash 更新分支信息。
  • 添加了无法检索最新 commit 时的错误处理。
  • 删除了基于插件版本修改 github URL 的代码。
zhenxun/builtin_plugins/plugin_store/data_source.py

提示和命令

与 Sourcery 互动

  • 触发新的审查: 在拉取请求上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 通过回复审查评论,要求 Sourcery 从审查评论创建一个 issue。您也可以回复审查评论并使用 @sourcery-ai issue 从中创建一个 issue。
  • 生成拉取请求标题: 在拉取请求标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在拉取请求上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在拉取请求上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成评审者指南: 在拉取请求上评论 @sourcery-ai guide 以随时(重新)生成评审者指南。
  • 解决所有 Sourcery 评论: 在拉取请求上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在拉取请求上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想从新的审查开始,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!
  • 为 issue 生成行动计划: 在 issue 上评论 @sourcery-ai plan 以为其生成行动计划。

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的拉取请求摘要、评审者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获得帮助

```
Original review guide in English

Reviewer's Guide by Sourcery

This pull request modifies the plugin store download logic to fetch the latest commit hash from the GitHub repository before installing a plugin. This ensures that the latest version of the plugin is always installed, even if the plugin's version is not explicitly specified. It also adds utility functions to determine if a branch is a commit hash.

Sequence diagram for plugin installation with latest commit

sequenceDiagram
  participant PluginStore
  participant GithubUtils
  participant GithubAPI
  participant Github

  PluginStore->GithubUtils: parse_github_url(github_url)
  activate GithubUtils
  GithubUtils-->PluginStore: repo_info
  deactivate GithubUtils
  PluginStore->repo_info: is_commit_hash()
  alt not repo_info.is_commit_hash()
    PluginStore->repo_info: get_latest_commit()
    activate repo_info
    repo_info->Github: GET /repos/{owner}/{repo}/branches/{branch}
    activate Github
    Github-->repo_info: commit SHA
    deactivate Github
    repo_info-->PluginStore: latest_commit
    deactivate repo_info
    PluginStore->repo_info: branch = latest_commit
  end
  PluginStore->GithubUtils: iter_api_strategies()
  loop For each repo_api in strategies
    PluginStore->repo_api: parse_repo_info(repo_info)
    activate repo_api
    repo_api->GithubAPI: Fetch repo information
    activate GithubAPI
    GithubAPI-->repo_api: Repo information
    deactivate GithubAPI
    repo_api-->PluginStore: Success or Failure
    deactivate repo_api
  end
Loading

Updated class diagram for RepoInfo

classDiagram
    class RepoInfo {
        -owner: str
        -repo: str
        -branch: str
        +__init__(owner: str, repo: str, branch: str)
        +to_dict(**kwargs)
        +get_latest_commit() str
        +is_commit_hash() bool
    }
    note for RepoInfo "Added get_latest_commit() and is_commit_hash() methods"
Loading

File-Level Changes

Change Details Files
Added functionality to retrieve the latest commit hash from a GitHub repository and determine if a branch is a commit hash.
  • Added get_latest_commit method to retrieve the latest commit SHA from a GitHub branch.
  • Added is_commit_hash method to check if a given branch is a commit hash.
zhenxun/utils/github_utils/models.py
Modified the plugin installation process to fetch the latest commit hash and use it as the branch during installation.
  • Attempt to get the latest commit hash from the repository before installing the plugin.
  • Update the branch information with the latest commit hash if it's not already a commit hash.
  • Added error handling for when the latest commit cannot be retrieved.
  • Removed code that modified the github URL based on the plugin version.
zhenxun/builtin_plugins/plugin_store/data_source.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

@Copaan - 我已经查看了你的更改 - 这里有一些反馈:

总体评论

  • 考虑向 get_latest_commit 添加缓存,以避免不必要的 API 调用。
  • install_plugin_with_repo 中的 try-except 块会重新引发异常,请考虑这是否是所需的行为。
以下是我在审查期间查看的内容
  • 🟡 一般问题:发现 1 个问题
  • 🟢 安全性:一切看起来都不错
  • 🟢 测试:一切看起来都不错
  • 🟢 复杂性:一切看起来都不错
  • 🟢 文档:一切看起来都不错

Sourcery 对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我变得更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @Copaan - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a cache to get_latest_commit to avoid unnecessary API calls.
  • The try-except block in install_plugin_with_repo re-raises the exception, consider if this is the desired behavior.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +72 to +74
if response.status_code == 200:
data = response.json()
return data["commit"]["sha"]
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): 保护措施,防止出现意外的 API 响应结构。

虽然 API 调用会检查响应状态,但它假设 JSON 数据包含预期的“commit”和“sha”键。添加显式键检查或处理潜在的 KeyError 异常可以使该方法对 API 响应结构的变化更具鲁棒性。

Suggested change
if response.status_code == 200:
data = response.json()
return data["commit"]["sha"]
if response.status_code == 200:
data = response.json()
if "commit" in data and "sha" in data["commit"]:
return data["commit"]["sha"]
raise ValueError(f"Unexpected API response structure: {data}")
Original comment in English

suggestion (bug_risk): Safeguard against unexpected API response structures.

While the API call is checked for response status, it assumes the JSON data contains the expected 'commit' and 'sha' keys. Adding explicit key checks or handling potential KeyError exceptions could make the method more robust against changes in the API's response structure.

Suggested change
if response.status_code == 200:
data = response.json()
return data["commit"]["sha"]
if response.status_code == 200:
data = response.json()
if "commit" in data and "sha" in data["commit"]:
return data["commit"]["sha"]
raise ValueError(f"Unexpected API response structure: {data}")

@Copaan Copaan closed this Feb 14, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants