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

🐛 修复获取群组时会修改群组插件关闭状态 #1869

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HibiKier
Copy link
Owner

@HibiKier HibiKier commented Mar 8, 2025

No description provided.

Copy link
Contributor

sourcery-ai bot commented Mar 8, 2025

## Sourcery 评审者指南

此拉取请求解决了一个 bug,该 bug 导致检索组信息时意外修改了禁用的插件状态。它还改进了帮助图片的删除,并更新了用于设置插件状态的消息。此修复包括修改插件查询以包含 `load_status=True`,并更新帮助图片删除逻辑。

#### GroupConsole.get_or_create 的序列图

```mermaid
sequenceDiagram
  participant GroupConsole
  participant TaskInfo
  participant PluginInfo

  GroupConsole->>TaskInfo: filter(default_status=False).values_list("module", flat=True)
  TaskInfo-->>GroupConsole: modules
  alt modules is not empty
    GroupConsole->>GroupConsole: convert_module_format(modules)
    GroupConsole->>GroupConsole: group.block_task = ...
  end
  GroupConsole->>PluginInfo: filter(plugin_type__in=[...], default_status=False, load_status=True).values_list("module", flat=True)
  PluginInfo-->>GroupConsole: modules
  alt modules is not empty
    GroupConsole->>GroupConsole: convert_module_format(modules)
    GroupConsole->>GroupConsole: group.block_plugin = ...
  end
  GroupConsole->>GroupConsole: group.save(update_fields=["block_plugin", "block_task"])

GroupConsole.update_or_create 的序列图

sequenceDiagram
  participant GroupConsole
  participant TaskInfo
  participant PluginInfo

  GroupConsole->>TaskInfo: filter(default_status=False).values_list("module", flat=True)
  TaskInfo-->>GroupConsole: modules
  alt modules is not empty
    GroupConsole->>GroupConsole: convert_module_format(modules)
    GroupConsole->>GroupConsole: group.block_task = ...
  end
  GroupConsole->>PluginInfo: filter(plugin_type__in=[...], default_status=False, load_status=True).values_list("module", flat=True)
  PluginInfo-->>GroupConsole: modules
  alt modules is not empty
    GroupConsole->>GroupConsole: convert_module_format(modules)
    GroupConsole->>GroupConsole: group.block_plugin = ...
  end
  GroupConsole->>GroupConsole: group.save(update_fields=["block_plugin", "block_task"])
Loading

文件级别变更

变更 详情 文件
修复了检索组时会修改组的插件禁用状态的问题。
  • get_or_createupdate_or_create 方法中的 PluginInfo 过滤器中添加了 load_status=True,以防止意外修改插件状态。
  • 在查询 TaskInfoPluginInfo 之前,删除了不必要的条件检查 if is_create and
zhenxun/models/group_console.py
通过迭代文件并删除以组 ID 开头的文件,改进了帮助图片的删除。
  • 修改了 delete_help_image 以迭代 GROUP_HELP_PATH 中的文件,并删除以组 ID 开头的文件,而不是仅删除名为 {gid}.png 的文件。
zhenxun/builtin_plugins/admin/plugin_switch/_data_source.py
更新了用于设置所有插件状态的消息。
  • 更正了 set_all_plugin_status 中的返回消息,通过在字符串中使用单引号来提高可读性。
zhenxun/builtin_plugins/admin/plugin_switch/_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 addresses a bug where retrieving group information inadvertently modified the disabled plugin state. It also improves the deletion of help images and updates messages for setting plugin statuses. The fix involves modifying the query for plugins to include load_status=True and updating the help image deletion logic.

Sequence diagram for GroupConsole.get_or_create

sequenceDiagram
  participant GroupConsole
  participant TaskInfo
  participant PluginInfo

  GroupConsole->>TaskInfo: filter(default_status=False).values_list("module", flat=True)
  TaskInfo-->>GroupConsole: modules
  alt modules is not empty
    GroupConsole->>GroupConsole: convert_module_format(modules)
    GroupConsole->>GroupConsole: group.block_task = ...
  end
  GroupConsole->>PluginInfo: filter(plugin_type__in=[...], default_status=False, load_status=True).values_list("module", flat=True)
  PluginInfo-->>GroupConsole: modules
  alt modules is not empty
    GroupConsole->>GroupConsole: convert_module_format(modules)
    GroupConsole->>GroupConsole: group.block_plugin = ...
  end
  GroupConsole->>GroupConsole: group.save(update_fields=["block_plugin", "block_task"])
Loading

Sequence diagram for GroupConsole.update_or_create

sequenceDiagram
  participant GroupConsole
  participant TaskInfo
  participant PluginInfo

  GroupConsole->>TaskInfo: filter(default_status=False).values_list("module", flat=True)
  TaskInfo-->>GroupConsole: modules
  alt modules is not empty
    GroupConsole->>GroupConsole: convert_module_format(modules)
    GroupConsole->>GroupConsole: group.block_task = ...
  end
  GroupConsole->>PluginInfo: filter(plugin_type__in=[...], default_status=False, load_status=True).values_list("module", flat=True)
  PluginInfo-->>GroupConsole: modules
  alt modules is not empty
    GroupConsole->>GroupConsole: convert_module_format(modules)
    GroupConsole->>GroupConsole: group.block_plugin = ...
  end
  GroupConsole->>GroupConsole: group.save(update_fields=["block_plugin", "block_task"])
Loading

File-Level Changes

Change Details Files
Fixes an issue where retrieving a group would modify the group's plugin disabled state.
  • Adds load_status=True to the PluginInfo filter in get_or_create and update_or_create methods to prevent unintended modification of plugin states.
  • Removes unnecessary conditional checks if is_create and before querying TaskInfo and PluginInfo.
zhenxun/models/group_console.py
Improves the deletion of help images by iterating through files and removing those that start with the group ID.
  • Modifies delete_help_image to iterate through files in GROUP_HELP_PATH and remove files that start with the group ID, instead of only deleting a file named exactly {gid}.png.
zhenxun/builtin_plugins/admin/plugin_switch/_data_source.py
Updates messages for setting all plugin statuses.
  • Corrects the return messages in set_all_plugin_status for better readability by using single quotes inside the string.
zhenxun/builtin_plugins/admin/plugin_switch/_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

@AkashiCoin AkashiCoin added the bug Something isn't working label Mar 8, 2025
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.

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

Overall Comments:

  • The logic in get_or_create and update_or_create is very similar; consider refactoring to reduce duplication.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 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 +19 to +21
for file in os.listdir(GROUP_HELP_PATH):
if file.startswith(f"{gid}"):
os.remove(GROUP_HELP_PATH / file)
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider using Pathlib methods for file operations.

Since GROUP_HELP_PATH appears to be a Path object, you could use GROUP_HELP_PATH.glob(...) and then call unlink() on each file for better consistency and readability, instead of mixing os.listdir with os.remove.

Suggested change
for file in os.listdir(GROUP_HELP_PATH):
if file.startswith(f"{gid}"):
os.remove(GROUP_HELP_PATH / file)
for file in GROUP_HELP_PATH.glob(f"{gid}*"):
file.unlink()

@@ -110,17 +110,17 @@ async def get_or_create(
group, is_create = await super().get_or_create(
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting the block-setting code into a helper function to reduce nesting and duplication in get_or_create and update_or_create methods, improving readability and maintainability.

You can reduce nesting and duplicate logic by extracting the block–setting code into a helper. This flattens the conditionals and makes both get_or_create and update_or_create easier to read. For example:

@classmethod
async def update_block_fields(cls, group, load_status: bool):
    # Retrieve and set task modules
    task_modules = await TaskInfo.filter(default_status=False).values_list("module", flat=True)
    if task_modules:
        group.block_task = cls.convert_module_format(task_modules)

    # Retrieve and set plugin modules; load_status is applied only if True.
    plugin_filter = dict(
        plugin_type__in=[PluginType.NORMAL, PluginType.DEPENDANT],
        default_status=False,
    )
    if load_status:
        plugin_filter["load_status"] = True

    plugin_modules = await PluginInfo.filter(**plugin_filter).values_list("module", flat=True)
    if plugin_modules:
        group.block_plugin = cls.convert_module_format(plugin_modules)

Then in your methods use an early check:

async def get_or_create(cls, defaults: dict | None = None, using_db: BaseDBAsyncClient | None = None, **kwargs: Any) -> tuple[Self, bool]:
    group, is_create = await super().get_or_create(defaults=defaults, using_db=using_db, **kwargs)
    if is_create:
        await cls.update_block_fields(group, load_status=True)
    await group.save(using_db=using_db, update_fields=["block_plugin", "block_task"])
    return group, is_create

@classmethod
async def update_or_create(cls, defaults: dict | None = None, using_db: BaseDBAsyncClient | None = None, **kwargs: Any) -> tuple[Self, bool]:
    group, is_create = await super().update_or_create(defaults=defaults, using_db=using_db, **kwargs)
    if is_create:
        await cls.update_block_fields(group, load_status=True)
    await group.save(using_db=using_db, update_fields=["block_plugin", "block_task"])
    return group, is_create

This approach keeps all functionality intact while reducing the complexity from nested conditionals.

Copy link
Contributor

@molanp molanp left a comment

Choose a reason for hiding this comment

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

阿咪合一下pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants