Skip to content

Commit

Permalink
fix: 修复解决冲突时未能正确分类的问题 (#319)
Browse files Browse the repository at this point in the history
* test: 将 pull_request 相关测试放入 resolve 文件夹下

* test: 尝试简化 should_call_api

* test: 优化测试运行命令的写法

* refactor: 将函数放置在 utils 中

* fix: 修复没有正确按照拉取请求种类处理的问题

* test: 重新移动回去

* feat: 增加了 Publish 标签简化判断

* style: auto fix by pre-commit hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
he0119 and pre-commit-ci[bot] authored Dec 8, 2024
1 parent 5ccc47d commit 3e747c5
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 92 deletions.
1 change: 1 addition & 0 deletions src/plugins/github/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@

SKIP_COMMENT = "/skip"

PUBLISH_LABEL = "Publish"
REMOVE_LABEL = "Remove"
CONFIG_LABEL = "Config"
18 changes: 7 additions & 11 deletions src/plugins/github/depends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from nonebot.params import Depends

from src.plugins.github.constants import CONFIG_LABEL, REMOVE_LABEL
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.typing import IssuesEvent, LabelsItems, PullRequestEvent
from src.plugins.github.utils import run_shell_command
Expand Down Expand Up @@ -152,23 +152,19 @@ async def is_publish_workflow(
labels: list[str] = Depends(get_labels_name),
publish_type: PublishType | None = Depends(get_type_by_labels_name),
) -> bool:
"""是否是发布工作流
通过标签判断
仅包含发布相关标签,不包含 remove/config 标签
"""
if publish_type is None or REMOVE_LABEL in labels or CONFIG_LABEL in labels:
logger.debug("与发布无关,已跳过")
"""是否是发布工作流"""
if publish_type is None:
logger.debug("无法获取到发布类型,已跳过")
return False

return True
return PUBLISH_LABEL in labels


async def is_remove_workflow(
labels: list[str] = Depends(get_labels_name),
publish_type: PublishType | None = Depends(get_type_by_labels_name),
) -> bool:
"""是否是 Remove 工作流"""
"""是否是删除工作流"""
if publish_type is None:
logger.debug("无法获取到发布类型,已跳过")
return False
Expand All @@ -180,7 +176,7 @@ async def is_config_workflow(
labels: list[str] = Depends(get_labels_name),
publish_type: PublishType | None = Depends(get_type_by_labels_name),
) -> bool:
"""是否是 Config 工作流
"""是否是配置工作流
仅支持插件发布
"""
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/github/plugins/remove/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@
BRANCH_NAME_PREFIX = "remove/issue"

COMMIT_MESSAGE_PREFIX = ":hammer: remove"

REMOVE_LABEL = "Remove"
3 changes: 2 additions & 1 deletion src/plugins/github/plugins/remove/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pydantic_core import PydanticCustomError

from src.plugins.github import plugin_config
from src.plugins.github.constants import REMOVE_LABEL
from src.plugins.github.depends.utils import (
extract_issue_number_from_ref,
get_type_by_labels,
Expand All @@ -13,7 +14,7 @@
from src.providers.utils import dump_json5
from src.providers.validation.models import PublishType

from .constants import COMMIT_MESSAGE_PREFIX, REMOVE_LABEL
from .constants import COMMIT_MESSAGE_PREFIX
from .validation import RemoveInfo, load_publish_data, validate_author_info


Expand Down
59 changes: 24 additions & 35 deletions src/plugins/github/plugins/resolve/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
from typing import Literal

from nonebot import logger, on_type
from nonebot.adapters.github import GitHubBot, PullRequestClosed
from nonebot.params import Arg, Depends
from nonebot.typing import T_State
from nonebot.params import Depends

from src.plugins.github.constants import PUBLISH_LABEL, REMOVE_LABEL
from src.plugins.github.depends import (
bypass_git,
get_installation_id,
get_related_issue_handler,
get_related_issue_number,
get_type_by_labels_name,
is_publish_workflow,
is_remove_workflow,
)
from src.plugins.github.models import IssueHandler
from src.plugins.github.models.github import GithubHandler
from src.plugins.github.plugins.publish.utils import (
resolve_conflict_pull_requests as resolve_conflict_publish_pull_requests,
)
from src.plugins.github.plugins.remove.utils import (
resolve_conflict_pull_requests as resolve_conflict_remove_pull_requests,
)
from src.plugins.github.typing import PullRequestLabels, PullRequestList
from src.providers.validation.models import PublishType


def is_publish(labels: PullRequestLabels) -> bool:
return any(label.name == PUBLISH_LABEL for label in labels)


def is_remove(labels: PullRequestLabels) -> bool:
return any(label.name == REMOVE_LABEL for label in labels)


async def resolve_conflict_pull_requests(
handler: GithubHandler, pull_requests: PullRequestList
):
for pull_request in pull_requests:
if is_remove(pull_request.labels):
await resolve_conflict_remove_pull_requests(handler, [pull_request])
elif is_publish(pull_request.labels):
await resolve_conflict_publish_pull_requests(handler, [pull_request])


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 @@ -46,7 +62,6 @@ async def pr_close_rule(
async def handle_pr_close(
event: PullRequestClosed,
bot: GitHubBot,
state: T_State,
installation_id: int = Depends(get_installation_id),
publish_type: PublishType = Depends(get_type_by_labels_name),
handler: IssueHandler = Depends(get_related_issue_handler),
Expand All @@ -67,32 +82,6 @@ async def handle_pr_close(
logger.info("发布的拉取请求未合并,已跳过")
await pr_close_matcher.finish()

state["pull_requests"] = await handler.get_pull_requests_by_label(
publish_type.value
)


@pr_close_matcher.handle()
async def handle_pr_close_resolve_conflict_publish(
bot: GitHubBot,
pull_requests: list = Arg(),
installation_id: int = Depends(get_installation_id),
handler: IssueHandler = Depends(get_related_issue_handler),
is_publish: Literal[True] = Depends(is_publish_workflow),
) -> None:
async with bot.as_installation(installation_id):
logger.info("发布的拉取请求已合并,准备更新拉取请求的提交")
await resolve_conflict_publish_pull_requests(handler, pull_requests)

pull_requests = await handler.get_pull_requests_by_label(publish_type.value)

@pr_close_matcher.handle()
async def handle_pr_close_resolve_conflict_remove(
bot: GitHubBot,
pull_requests: list = Arg(),
installation_id: int = Depends(get_installation_id),
handler: IssueHandler = Depends(get_related_issue_handler),
is_remove: Literal[True] = Depends(is_remove_workflow),
) -> None:
async with bot.as_installation(installation_id):
logger.info("发布的拉取请求已合并,准备更新拉取请求的提交")
await resolve_conflict_remove_pull_requests(handler, pull_requests)
await resolve_conflict_pull_requests(handler, pull_requests)
20 changes: 1 addition & 19 deletions tests/plugins/github/config/process/test_config_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,13 @@

from tests.plugins.github.config.utils import generate_issue_body
from tests.plugins.github.event import get_mock_event
from tests.plugins.github.resolve.utils import get_pr_labels
from tests.plugins.github.utils import (
MockIssue,
get_github_bot,
)


def get_pr_labels(labels: list[str]):
from githubkit.rest import PullRequestPropLabelsItems as Label

return [
Label.model_construct(
**{
"color": "2A2219",
"default": False,
"description": "",
"id": 2798075966,
"name": label,
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Remove",
}
)
for label in labels
]


async def test_config_process_pull_request(
app: App, mocker: MockerFixture, mock_installation: MagicMock
) -> None:
Expand Down
9 changes: 9 additions & 0 deletions tests/plugins/github/events/issue-comment-bot.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
"name": "Plugin",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Plugin"
},
{
"color": "2A2219",
"default": false,
"description": "",
"id": 2798075967,
"name": "Publish",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Publish"
}
],
"labels_url": "https://api.github.com/repos/he0119/action-test/issues/101/labels{/name}",
Expand Down
9 changes: 9 additions & 0 deletions tests/plugins/github/events/issue-comment-skip.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@
"name": "Plugin",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Plugin"
},
{
"color": "2A2219",
"default": false,
"description": "",
"id": 2798075967,
"name": "Publish",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Publish"
}
],
"labels_url": "https://api.github.com/repos/he0119/action-test/issues/70/labels{/name}",
Expand Down
9 changes: 9 additions & 0 deletions tests/plugins/github/events/issue-comment.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@
"name": "Plugin",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Plugin"
},
{
"color": "2A2219",
"default": false,
"description": "",
"id": 2798075967,
"name": "Publish",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Publish"
}
],
"labels_url": "https://api.github.com/repos/he0119/action-test/issues/70/labels{/name}",
Expand Down
9 changes: 9 additions & 0 deletions tests/plugins/github/events/issue-open.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
"name": "Bot",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Bot"
},
{
"color": "2A2219",
"default": false,
"description": "",
"id": 2798075967,
"name": "Publish",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Publish"
}
],
"labels_url": "https://api.github.com/repos/he0119/action-test/issues/80/labels{/name}",
Expand Down
9 changes: 9 additions & 0 deletions tests/plugins/github/events/pr-close.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@
"name": "Plugin",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Plugin"
},
{
"color": "2A2219",
"default": false,
"description": "",
"id": 2798075967,
"name": "Publish",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Publish"
}
],
"locked": false,
Expand Down
9 changes: 9 additions & 0 deletions tests/plugins/github/events/pr-comment.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@
"name": "Plugin",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Plugin"
},
{
"color": "2A2219",
"default": false,
"description": "",
"id": 2798075967,
"name": "Publish",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Publish"
}
],
"labels_url": "https://api.github.com/repos/he0119/action-test/issues/75/labels{/name}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@
"name": "Plugin",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Plugin"
},
{
"color": "2A2219",
"default": false,
"description": "",
"id": 2798075967,
"name": "Publish",
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Publish"
}
],
"locked": false,
Expand Down
6 changes: 3 additions & 3 deletions tests/plugins/github/publish/process/test_publish_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ async def test_adapter_process_publish_check(
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event = get_mock_event(IssuesOpened)
event.payload.issue.labels = get_issue_labels(["Adapter"])
event.payload.issue.labels = get_issue_labels(["Adapter", "Publish"])

ctx.should_call_api(
"rest.apps.async_get_repo_installation",
Expand Down Expand Up @@ -461,7 +461,7 @@ async def test_plugin_process_publish_check(
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event = get_mock_event(IssuesOpened)
event.payload.issue.labels = get_issue_labels(["Plugin"])
event.payload.issue.labels = get_issue_labels(["Plugin", "Publish"])

ctx.should_call_api(
"rest.apps.async_get_repo_installation",
Expand Down Expand Up @@ -752,7 +752,7 @@ async def test_plugin_process_publish_check_re_run(
async with app.test_matcher() as ctx:
adapter, bot = get_github_bot(ctx)
event = get_mock_event(IssuesOpened)
event.payload.issue.labels = get_issue_labels(["Plugin"])
event.payload.issue.labels = get_issue_labels(["Plugin", "Publish"])

ctx.should_call_api(
"rest.apps.async_get_repo_installation",
Expand Down
20 changes: 1 addition & 19 deletions tests/plugins/github/remove/process/test_remove_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,14 @@
from pytest_mock import MockerFixture

from tests.plugins.github.event import get_mock_event
from tests.plugins.github.resolve.utils import get_pr_labels
from tests.plugins.github.utils import (
MockIssue,
generate_issue_body_remove,
get_github_bot,
)


def get_pr_labels(labels: list[str]):
from githubkit.rest import PullRequestPropLabelsItems as Label

return [
Label.model_construct(
**{
"color": "2A2219",
"default": False,
"description": "",
"id": 2798075966,
"name": label,
"node_id": "MDU6TGFiZWwyNzk4MDc1OTY2",
"url": "https://api.github.com/repos/he0119/action-test/labels/Remove",
}
)
for label in labels
]


async def test_remove_process_pull_request(
app: App, mocker: MockerFixture, mock_installation: MagicMock
) -> None:
Expand Down
Loading

0 comments on commit 3e747c5

Please sign in to comment.