Skip to content

Commit

Permalink
refactor: 使用 rule 而不是 depends 来实现这个功能
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Nov 4, 2024
1 parent d70c38c commit 17b750d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
33 changes: 26 additions & 7 deletions src/plugins/github/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@
PullRequestReviewSubmitted,
)
from nonebot.params import Arg, Depends
from nonebot.rule import Rule
from nonebot.typing import T_State

from src.plugins.github.constants import BRANCH_NAME_PREFIX, TITLE_MAX_LENGTH
from src.plugins.github.depends import (
bypass_git,
get_github_handler,
get_installation_id,
get_labels_name,
get_related_issue_number,
get_repo_info,
install_pre_commit_hooks,
is_bot_triggered_workflow,
)
from src.plugins.github.models import GithubHandler, IssueHandler, RepoInfo
from src.plugins.github.plugins.publish.render import render_comment
from src.plugins.github.plugins.remove.constants import REMOVE_LABEL
from src.providers.validation.models import PublishType, ValidationDict

from .depends import (
get_issue_handler,
get_related_issue_handler,
get_type_by_labels_name,
is_publish_related_workflow,
)
from .utils import (
ensure_issue_content,
Expand All @@ -47,10 +49,24 @@
)


async def publish_related_rule(
labels: list[str] = Depends(get_labels_name),
publish_type: PublishType = Depends(get_type_by_labels_name),
) -> bool:
"""确保与发布相关
通过标签判断
仅包含发布相关标签,不包含 remove 标签
"""
for label in labels:
if label == REMOVE_LABEL:
return False
return True


async def pr_close_rule(
publish_type: PublishType | None = Depends(get_type_by_labels_name),
related_issue_number: int | None = Depends(get_related_issue_number),
is_publish_related_workflow: Literal[True] = Depends(is_publish_related_workflow),
) -> bool:
if publish_type is None:
logger.info("拉取请求与发布无关,已跳过")
Expand All @@ -63,7 +79,9 @@ async def pr_close_rule(
return True


pr_close_matcher = on_type(PullRequestClosed, rule=pr_close_rule)
pr_close_matcher = on_type(
PullRequestClosed, rule=Rule(pr_close_rule, publish_related_rule)
)


@pr_close_matcher.handle(
Expand Down Expand Up @@ -106,7 +124,6 @@ async def check_rule(
event: IssuesOpened | IssuesReopened | IssuesEdited | IssueCommentCreated,
publish_type: PublishType | None = Depends(get_type_by_labels_name),
is_bot: bool = Depends(is_bot_triggered_workflow),
is_publish_related_workflow: Literal[True] = Depends(is_publish_related_workflow),
) -> bool:
if is_bot:
logger.info("机器人触发的工作流,已跳过")
Expand All @@ -122,7 +139,8 @@ async def check_rule(


publish_check_matcher = on_type(
(IssuesOpened, IssuesReopened, IssuesEdited, IssueCommentCreated), rule=check_rule
(IssuesOpened, IssuesReopened, IssuesEdited, IssueCommentCreated),
rule=Rule(check_rule, publish_related_rule),
)


Expand Down Expand Up @@ -235,7 +253,6 @@ async def handle_pull_request_and_update_issue(
async def review_submiited_rule(
event: PullRequestReviewSubmitted,
publish_type: PublishType | None = Depends(get_type_by_labels_name),
is_publish_related_workflow: Literal[True] = Depends(is_publish_related_workflow),
) -> bool:
if publish_type is None:
logger.info("拉取请求与发布无关,已跳过")
Expand All @@ -250,7 +267,9 @@ async def review_submiited_rule(
return True


auto_merge_matcher = on_type(PullRequestReviewSubmitted, rule=review_submiited_rule)
auto_merge_matcher = on_type(
PullRequestReviewSubmitted, rule=Rule(review_submiited_rule, publish_related_rule)
)


@auto_merge_matcher.handle(
Expand Down
17 changes: 0 additions & 17 deletions src/plugins/github/plugins/publish/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
get_installation_id,
get_issue_number,
get_issue_title,
get_labels_name,
get_related_issue_number,
get_repo_info,
get_type_by_labels_name,
)
from src.plugins.github.models import IssueHandler, RepoInfo
from src.plugins.github.plugins.publish import utils
from src.plugins.github.plugins.remove.constants import REMOVE_LABEL
from src.providers.validation.models import PublishType


Expand Down Expand Up @@ -64,18 +62,3 @@ async def get_related_issue_handler(
return await get_issue_handler(
bot, installation_id, repo_info, related_issue_number
)


async def is_publish_related_workflow(
labels: list[str] = Depends(get_labels_name),
publish_type: PublishType = Depends(get_type_by_labels_name),
) -> bool:
"""是否是发布相关的工作流
通过标签判断
仅包含发布相关标签,不包含 remove 标签
"""
for label in labels:
if label == REMOVE_LABEL:
return False
return True

0 comments on commit 17b750d

Please sign in to comment.