Skip to content

Commit

Permalink
refactor: 拆分 models 和 handlers 避免循环导入 (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 authored Dec 11, 2024
1 parent a02ad0b commit ce7fdc7
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/plugins/github/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pydantic import BaseModel, ConfigDict, Field, field_validator

from src.plugins.github.models import RepoInfo
from .models import RepoInfo


class PublishConfig(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/github/depends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from nonebot.params import Depends

from src.plugins.github.constants import CONFIG_LABEL, PUBLISH_LABEL, REMOVE_LABEL
from src.plugins.github.models import GithubHandler, IssueHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler, IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.typing import IssuesEvent, LabelsItems, PullRequestEvent
from src.plugins.github.utils import run_shell_command
from src.providers.validation.models import PublishType
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/github/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .git import GitHandler as GitHandler
from .github import GithubHandler as GithubHandler
from .issue import IssueHandler as IssueHandler
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from pydantic import ConfigDict

from src.plugins.github.constants import NONEFLOW_MARKER
from src.plugins.github.models import GitHandler, RepoInfo
from src.plugins.github.models import RepoInfo

from .git import GitHandler


class GithubHandler(GitHandler):
Expand Down Expand Up @@ -249,7 +251,7 @@ async def close_issue(

async def to_issue_handler(self, issue_number: int):
"""获取议题处理器"""
from src.plugins.github.models.issue import IssueHandler
from src.plugins.github.handlers import IssueHandler

issue = await self.get_issue(issue_number)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from pydantic import ConfigDict

from src.plugins.github.constants import SKIP_COMMENT
from src.plugins.github.models import AuthorInfo, GithubHandler
from src.plugins.github.models import AuthorInfo

from .github import GithubHandler


class IssueHandler(GithubHandler):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,3 @@ def from_issue(cls, issue: Issue) -> "AuthorInfo":
author=issue.user.login if issue.user else "",
author_id=issue.user.id if issue.user else 0,
)


from .git import GitHandler as GitHandler
from .github import GithubHandler as GithubHandler
from .issue import IssueHandler as IssueHandler

__all__ = ["GitHandler", "GithubHandler", "IssueHandler", "RepoInfo", "AuthorInfo"]
3 changes: 1 addition & 2 deletions src/plugins/github/plugins/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
is_bot_triggered_workflow,
is_config_workflow,
)
from src.plugins.github.models import IssueHandler
from src.plugins.github.models.github import GithubHandler
from src.plugins.github.handlers import GithubHandler, IssueHandler
from src.plugins.github.plugins.publish.render import render_comment
from src.plugins.github.plugins.publish.utils import (
ensure_issue_plugin_test_button,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from nonebot import logger

from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import AuthorInfo
from src.plugins.github.models.issue import IssueHandler
from src.plugins.github.plugins.publish.constants import (
PLUGIN_CONFIG_PATTERN,
PLUGIN_MODULE_NAME_PATTERN,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
is_bot_triggered_workflow,
is_publish_workflow,
)
from src.plugins.github.models import GithubHandler, IssueHandler
from src.plugins.github.handlers import GithubHandler, IssueHandler
from src.providers.validation.models import PublishType, ValidationDict

from .depends import (
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/publish/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from src.plugins.github import plugin_config
from src.plugins.github.constants import ISSUE_FIELD_PATTERN, ISSUE_FIELD_TEMPLATE
from src.plugins.github.depends.utils import get_type_by_labels
from src.plugins.github.models import GithubHandler, IssueHandler
from src.plugins.github.handlers import GithubHandler, IssueHandler
from src.plugins.github.typing import PullRequestList
from src.plugins.github.utils import commit_message as _commit_message
from src.providers.models import RegistryUpdatePayload, to_store
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/publish/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from nonebot import logger

from src.plugins.github import plugin_config
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import AuthorInfo
from src.plugins.github.models.issue import IssueHandler
from src.plugins.github.utils import extract_issue_info_from_issue
from src.providers.docker_test import DockerPluginTest, Metadata
from src.providers.utils import get_latest_version, load_json_from_file
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/github/plugins/remove/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
is_bot_triggered_workflow,
is_remove_workflow,
)
from src.plugins.github.models import IssueHandler
from src.plugins.github.models.github import GithubHandler
from src.plugins.github.handlers import GithubHandler, IssueHandler
from src.plugins.github.typing import IssuesEvent
from src.providers.validation.models import PublishType

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/remove/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extract_issue_number_from_ref,
get_type_by_labels,
)
from src.plugins.github.models import GithubHandler, IssueHandler
from src.plugins.github.handlers import GithubHandler, IssueHandler
from src.plugins.github.typing import PullRequestList
from src.plugins.github.utils import commit_message
from src.providers.utils import dump_json5
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/github/plugins/resolve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
get_related_issue_number,
get_type_by_labels_name,
)
from src.plugins.github.models import IssueHandler
from src.plugins.github.models.github import GithubHandler
from src.plugins.github.handlers import GithubHandler, IssueHandler
from src.plugins.github.plugins.publish.utils import (
resolve_conflict_pull_requests as resolve_conflict_publish_pull_requests,
)
Expand Down
9 changes: 6 additions & 3 deletions tests/plugins/github/publish/utils/test_comment_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


async def test_comment_issue(app: App, mocker: MockerFixture):
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo

render_comment = "test"

Expand Down Expand Up @@ -40,7 +41,8 @@ async def test_comment_issue(app: App, mocker: MockerFixture):

async def test_comment_issue_reuse(app: App, mocker: MockerFixture):
from src.plugins.github.constants import NONEFLOW_MARKER
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo

render_comment = "test"

Expand Down Expand Up @@ -76,7 +78,8 @@ async def test_comment_issue_reuse(app: App, mocker: MockerFixture):

async def test_comment_issue_reuse_same(app: App, mocker: MockerFixture):
"""测试评论内容相同时不会更新评论"""
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo

render_comment = "test\n<!-- NONEFLOW -->\n"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

async def test_ensure_issue_content(app: App, mocker: MockerFixture):
"""确保议题内容完整"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import ensure_issue_content

async with app.test_api() as ctx:
Expand Down Expand Up @@ -49,7 +50,8 @@ async def test_ensure_issue_content(app: App, mocker: MockerFixture):

async def test_ensure_issue_content_partial(app: App, mocker: MockerFixture):
"""确保议题内容被补全"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import ensure_issue_content

async with app.test_api() as ctx:
Expand Down Expand Up @@ -78,7 +80,8 @@ async def test_ensure_issue_content_partial(app: App, mocker: MockerFixture):

async def test_ensure_issue_content_complete(app: App, mocker: MockerFixture):
"""确保议题内容已经补全之后不会再次补全"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import ensure_issue_content

async with app.test_api() as ctx:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

async def test_ensure_issue_plugin_test_button(app: App, mocker: MockerFixture):
"""确保添加插件测试按钮"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import (
ensure_issue_plugin_test_button,
)
Expand Down Expand Up @@ -66,7 +67,8 @@ async def test_ensure_issue_plugin_test_button(app: App, mocker: MockerFixture):

async def test_ensure_issue_plugin_test_button_checked(app: App, mocker: MockerFixture):
"""如果测试按钮勾选,则自动取消勾选"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import (
ensure_issue_plugin_test_button,
)
Expand Down Expand Up @@ -127,7 +129,8 @@ async def test_ensure_issue_plugin_test_button_unchecked(
app: App, mocker: MockerFixture
):
"""如果测试按钮未勾选,则不进行操作"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import (
ensure_issue_plugin_test_button,
)
Expand All @@ -153,7 +156,8 @@ async def test_ensure_issue_plugin_test_button_in_progress(
app: App, mocker: MockerFixture
):
"""确保添加插件测试按钮"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import (
ensure_issue_plugin_test_button_in_progress,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ async def test_resolve_conflict_pull_requests_adapter(
app: App, mocker: MockerFixture, mocked_api: MockRouter, tmp_path: Path, mock_pull
) -> None:
from src.plugins.github import plugin_config
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import resolve_conflict_pull_requests
from src.providers.utils import dump_json5

Expand Down Expand Up @@ -158,7 +159,8 @@ async def test_resolve_conflict_pull_requests_bot(
app: App, mocker: MockerFixture, mocked_api: MockRouter, tmp_path: Path, mock_pull
) -> None:
from src.plugins.github import plugin_config
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import resolve_conflict_pull_requests
from src.providers.utils import dump_json5

Expand Down Expand Up @@ -283,7 +285,8 @@ async def test_resolve_conflict_pull_requests_plugin(
app: App, mocker: MockerFixture, mocked_api: MockRouter, tmp_path: Path, mock_pull
) -> None:
from src.plugins.github import plugin_config
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import resolve_conflict_pull_requests
from src.providers.docker_test import Metadata
from src.providers.utils import dump_json5
Expand Down Expand Up @@ -432,7 +435,8 @@ async def test_resolve_conflict_pull_requests_plugin_not_valid(
) -> None:
"""测试插件信息不合法的情况"""
from src.plugins.github import plugin_config
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import resolve_conflict_pull_requests
from src.providers.utils import dump_json5

Expand Down Expand Up @@ -524,7 +528,8 @@ async def test_resolve_conflict_pull_requests_draft(
app: App, mocker: MockerFixture, mocked_api: MockRouter, tmp_path: Path
) -> None:
from src.plugins.github import plugin_config
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import resolve_conflict_pull_requests
from src.providers.utils import dump_json5

Expand Down Expand Up @@ -586,7 +591,8 @@ async def test_resolve_conflict_pull_requests_ref(
app: App, mocker: MockerFixture, mocked_api: MockRouter, tmp_path: Path
) -> None:
from src.plugins.github import plugin_config
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.handlers import GithubHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import resolve_conflict_pull_requests
from src.providers.utils import dump_json5

Expand Down
15 changes: 10 additions & 5 deletions tests/plugins/github/publish/utils/test_trigger_registry_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
async def test_trigger_registry_update(
app: App, mocker: MockerFixture, mocked_api: MockRouter
):
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import trigger_registry_update
from src.providers.docker_test import Metadata
from src.providers.validation import PublishType
Expand Down Expand Up @@ -126,7 +127,8 @@ async def test_trigger_registry_update_skip_test(
app: App, mocker: MockerFixture, mocked_api: MockRouter
):
"""跳过插件加载测试的情况"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import trigger_registry_update
from src.providers.validation import PublishType

Expand Down Expand Up @@ -225,7 +227,8 @@ async def test_trigger_registry_update_bot(
已经有相同机器人的时候,registry_update 不会影响到机器人的测试
"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import trigger_registry_update
from src.providers.validation import PublishType

Expand Down Expand Up @@ -278,7 +281,8 @@ async def test_trigger_registry_update_plugins_issue_body_info_missing(
"""如果议题信息不全,应该不会触发更新"""
from githubkit.rest import Issue

from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import trigger_registry_update
from src.providers.docker_test import Metadata
from src.providers.validation import PublishType
Expand Down Expand Up @@ -336,7 +340,8 @@ async def test_trigger_registry_update_validation_failed(
app: App, mocker: MockerFixture, mocked_api: MockRouter
):
"""验证失败时也不会触发更新"""
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish.utils import trigger_registry_update
from src.providers.validation import PublishType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async def test_validate_info_from_issue_plugin(
app: App, mocker: MockerFixture, mocked_api: MockRouter
):
from src.plugins.github import plugin_config
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.models.issue import IssueHandler
from src.plugins.github.plugins.publish.validation import (
validate_plugin_info_from_issue,
)
Expand Down Expand Up @@ -135,8 +135,8 @@ async def test_validate_info_from_issue_plugin_skip_test(
app: App, mocker: MockerFixture, mocked_api: MockRouter
):
"""跳过插件测试的情况"""
from src.plugins.github.handlers import IssueHandler
from src.plugins.github.models import RepoInfo
from src.plugins.github.models.issue import IssueHandler
from src.plugins.github.plugins.publish.validation import (
validate_plugin_info_from_issue,
)
Expand Down
Loading

0 comments on commit ce7fdc7

Please sign in to comment.