Skip to content

Commit

Permalink
fix: rule 新增 is_publish_related_workflow (#255)
Browse files Browse the repository at this point in the history
* fix: rule 新增 is_publish_related_workflow

只有当没有 Remove 标签时,才会走发布流程
同时修改了判断 Bot 的方式,仅通过 sender.type 判断

* refactor: 调整函数位置并用上常量

* fix: 完善机器人判断规则

* refactor: 使用 rule 而不是 depends 来实现这个功能
  • Loading branch information
he0119 authored Nov 5, 2024
1 parent 698c87b commit 1a5df57
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 103 deletions.
15 changes: 8 additions & 7 deletions src/plugins/github/depends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
GitHubBot,
IssueCommentCreated,
IssuesEdited,
IssuesOpened,
IssuesReopened,
PullRequestClosed,
PullRequestReviewSubmitted,
)
Expand Down Expand Up @@ -82,21 +84,20 @@ def get_related_issue_number(event: PullRequestClosed) -> int | None:


def is_bot_triggered_workflow(event: IssuesEvent):
"""触发议题相关的工作流"""

"""是否是机器人触发的工作流"""
if (
isinstance(event, IssueCommentCreated)
and event.payload.comment.user
and event.payload.comment.user.type == "Bot"
):
logger.info("评论来自机器人,已跳过")
logger.info("议题评论来自机器人,已跳过")
return True
if (
isinstance(event, IssuesEdited)
and event.payload.sender
and event.payload.sender.type == "Bot"
isinstance(event, IssuesOpened | IssuesReopened | IssuesEdited)
and event.payload.issue.user
and event.payload.issue.user.type == "Bot"
):
logger.info("议题修改来自机器人,已跳过")
logger.info("议题操作来自机器人,已跳过")
return True
return False

Expand Down
29 changes: 26 additions & 3 deletions src/plugins/github/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@
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 (
Expand All @@ -46,6 +49,21 @@
)


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),
Expand All @@ -61,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 @@ -119,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 @@ -246,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
19 changes: 5 additions & 14 deletions tests/github/publish/process/test_auto_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ async def test_auto_merge(app: App, mocker: MockerFixture, mock_installation) ->
可直接合并的情况
"""
from src.plugins.github.plugins.publish import auto_merge_matcher

mock_subprocess_run = mocker.patch("subprocess.run")

mock_pull = mocker.MagicMock()
mock_pull.mergeable = True
mock_pull_resp = mocker.MagicMock()
mock_pull_resp.parsed_data = mock_pull

async with app.test_matcher(auto_merge_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent.parent
Expand Down Expand Up @@ -82,7 +80,6 @@ async def test_auto_merge_need_rebase(
需要 rebase 的情况
"""
from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.plugins.publish import auto_merge_matcher

mock_subprocess_run = mocker.patch("subprocess.run")
mock_resolve_conflict_pull_requests = mocker.patch(
Expand All @@ -95,7 +92,7 @@ async def test_auto_merge_need_rebase(
mock_pull_resp = mocker.MagicMock()
mock_pull_resp.parsed_data = mock_pull

async with app.test_matcher(auto_merge_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent.parent
Expand Down Expand Up @@ -153,14 +150,12 @@ async def test_auto_merge_not_publish(app: App, mocker: MockerFixture) -> None:
和发布无关
"""
from src.plugins.github.plugins.publish import auto_merge_matcher

mock_subprocess_run = mocker.patch("subprocess.run")
mock_resolve_conflict_pull_requests = mocker.patch(
"src.plugins.github.plugins.publish.resolve_conflict_pull_requests"
)

async with app.test_matcher(auto_merge_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent.parent
Expand All @@ -185,14 +180,12 @@ async def test_auto_merge_not_member(app: App, mocker: MockerFixture) -> None:
审核者不是仓库成员
"""
from src.plugins.github.plugins.publish import auto_merge_matcher

mock_subprocess_run = mocker.patch("subprocess.run")
mock_resolve_conflict_pull_requests = mocker.patch(
"src.plugins.github.plugins.publish.resolve_conflict_pull_requests"
)

async with app.test_matcher(auto_merge_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent.parent
Expand All @@ -217,14 +210,12 @@ async def test_auto_merge_not_approve(app: App, mocker: MockerFixture) -> None:
审核未通过
"""
from src.plugins.github.plugins.publish import auto_merge_matcher

mock_subprocess_run = mocker.patch("subprocess.run")
mock_resolve_conflict_pull_requests = mocker.patch(
"src.plugins.github.plugins.publish.resolve_conflict_pull_requests"
)

async with app.test_matcher(auto_merge_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent.parent
Expand Down
40 changes: 12 additions & 28 deletions tests/github/publish/process/test_publish_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ async def test_bot_process_publish_check(
) -> None:
"""测试机器人的发布流程"""
from src.plugins.github import plugin_config
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
Expand Down Expand Up @@ -60,7 +59,7 @@ async def test_bot_process_publish_check(

check_json_data(plugin_config.input_config.bot_path, [])

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand Down Expand Up @@ -221,7 +220,6 @@ async def test_adapter_process_publish_check(
) -> None:
"""测试适配器的发布流程"""
from src.plugins.github import plugin_config
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
Expand Down Expand Up @@ -252,7 +250,7 @@ async def test_adapter_process_publish_check(

check_json_data(plugin_config.input_config.adapter_path, [])

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand Down Expand Up @@ -434,7 +432,6 @@ async def test_edit_title(
名称被修改后,标题也应该被修改
"""
from src.plugins.github import plugin_config
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
Expand Down Expand Up @@ -466,7 +463,7 @@ async def test_edit_title(

check_json_data(plugin_config.input_config.bot_path, [])

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand Down Expand Up @@ -656,7 +653,6 @@ async def test_edit_title_too_long(
标题过长的情况
"""
from src.plugins.github import plugin_config
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
Expand Down Expand Up @@ -687,7 +683,7 @@ async def test_edit_title_too_long(

check_json_data(plugin_config.input_config.bot_path, [])

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand Down Expand Up @@ -793,7 +789,6 @@ async def test_process_publish_check_not_pass(
) -> None:
"""测试发布检查不通过"""
from src.plugins.github import plugin_config
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
Expand All @@ -819,7 +814,7 @@ async def test_process_publish_check_not_pass(

check_json_data(plugin_config.input_config.bot_path, [])

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand Down Expand Up @@ -918,13 +913,11 @@ async def test_comment_at_pull_request(
event.issue.pull_request 不为空
"""
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
)

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "pr-comment.json"
event = Adapter.payload_to_event("1", "issue_comment", event_path.read_bytes())
Expand All @@ -943,8 +936,6 @@ async def test_issue_state_closed(
event.issue.state = "closed"
"""
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
)
Expand All @@ -953,7 +944,7 @@ async def test_issue_state_closed(
mock_issues_resp = mocker.MagicMock()
mock_issues_resp.parsed_data = mock_issue

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand Down Expand Up @@ -996,13 +987,11 @@ async def test_not_publish_issue(
议题的标签不是 "Bot/Adapter/Plugin"
"""
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
)

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand All @@ -1018,13 +1007,11 @@ async def test_comment_by_self(
app: App, mocker: MockerFixture, mocked_api: MockRouter
) -> None:
"""测试自己评论触发的情况"""
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
)

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent.parent / "events" / "issue-comment-bot.json"
Expand All @@ -1046,7 +1033,6 @@ async def test_skip_plugin_check(
) -> None:
"""测试手动跳过插件测试的流程"""
from src.plugins.github import plugin_config
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
Expand Down Expand Up @@ -1076,7 +1062,7 @@ async def test_skip_plugin_check(

check_json_data(plugin_config.input_config.plugin_path, [])

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent.parent / "events" / "issue-comment-skip.json"
Expand Down Expand Up @@ -1249,7 +1235,6 @@ async def test_convert_pull_request_to_draft(
) -> None:
"""未通过时将拉取请求转换为草稿"""
from src.plugins.github import plugin_config
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
Expand Down Expand Up @@ -1281,7 +1266,7 @@ async def test_convert_pull_request_to_draft(

check_json_data(plugin_config.input_config.bot_path, [])

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand Down Expand Up @@ -1391,7 +1376,6 @@ async def test_process_publish_check_ready_for_review(
) -> None:
"""当之前失败后再次通过测试时,应该将拉取请求标记为 ready for review"""
from src.plugins.github import plugin_config
from src.plugins.github.plugins.publish import publish_check_matcher

mock_subprocess_run = mocker.patch(
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
Expand Down Expand Up @@ -1425,7 +1409,7 @@ async def test_process_publish_check_ready_for_review(

check_json_data(plugin_config.input_config.bot_path, [])

async with app.test_matcher(publish_check_matcher) as ctx:
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event_path = Path(__file__).parent.parent.parent / "events" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
Expand Down
Loading

0 comments on commit 1a5df57

Please sign in to comment.