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

refactor: 修改部分类型与依赖,并且重构 remove 插件传入信息。 #241

Merged
merged 8 commits into from
Nov 1, 2024
25 changes: 24 additions & 1 deletion src/plugins/github/depends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from nonebot.params import Depends

from src.plugins.github.models import GithubHandler, RepoInfo
from src.plugins.github.typing import IssuesEvent, PullRequestEvent
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

from .utils import extract_issue_number_from_ref

Expand All @@ -35,6 +36,18 @@ def get_labels(event: PullRequestEvent | IssuesEvent):
return labels


def get_labels_name(labels: LabelsItems = Depends(get_labels)) -> list[str]:
"""通过标签获取名称"""
label_names: list[str] = []
if not labels:
return label_names

for label in labels:
if label.name:
label_names.append(label.name)
return label_names


def get_issue_title(event: IssuesEvent):
"""获取议题标题"""
return event.payload.issue.title
Expand Down Expand Up @@ -91,3 +104,13 @@ def is_bot_triggered_workflow(event: IssuesEvent):
def get_github_handler(bot: GitHubBot, repo_info: RepoInfo = Depends(get_repo_info)):
"""获取 GitHub 处理器"""
return GithubHandler(bot=bot, repo_info=repo_info)


def get_type_by_labels_name(
labels: list[str] = Depends(get_labels_name),
) -> PublishType | None:
"""通过标签的名称获取类型"""
for type in PublishType:
if type.value in labels:
return type
return None
14 changes: 14 additions & 0 deletions src/plugins/github/depends/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import re

from src.plugins.github.typing import PullRequestLabels
from src.providers.validation.models import PublishType


def extract_issue_number_from_ref(ref: str) -> int | None:
"""从 Ref 中提取议题号"""
match = re.search(r"(\w{4,10})\/issue(\d+)", ref)
if match:
return int(match.group(2))


def get_type_by_labels(labels: PullRequestLabels) -> PublishType | None:
"""通过拉取请求的标签获取发布类型"""
for label in labels:
if isinstance(label, str):
continue
for type in PublishType:
if label.name == type.value:
return type
return None
16 changes: 8 additions & 8 deletions src/plugins/github/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from src.plugins.github.plugins.publish.render import render_comment
from src.providers.validation.models import PublishType, ValidationDict

from .depends import get_type_by_labels
from .depends import get_type_by_labels_name
from .utils import (
ensure_issue_content,
ensure_issue_plugin_test_button,
Expand All @@ -44,7 +44,7 @@


async def pr_close_rule(
publish_type: PublishType | None = Depends(get_type_by_labels),
publish_type: PublishType | None = Depends(get_type_by_labels_name),
related_issue_number: int | None = Depends(get_related_issue_number),
) -> bool:
if publish_type is None:
Expand All @@ -68,7 +68,7 @@ async def handle_pr_close(
event: PullRequestClosed,
bot: GitHubBot,
installation_id: int = Depends(get_installation_id),
publish_type: PublishType = Depends(get_type_by_labels),
publish_type: PublishType = Depends(get_type_by_labels_name),
repo_info: RepoInfo = Depends(get_repo_info),
related_issue_number: int = Depends(get_related_issue_number),
) -> None:
Expand Down Expand Up @@ -108,7 +108,7 @@ async def handle_pr_close(

async def check_rule(
event: IssuesOpened | IssuesReopened | IssuesEdited | IssueCommentCreated,
publish_type: PublishType | None = Depends(get_type_by_labels),
publish_type: PublishType | None = Depends(get_type_by_labels_name),
is_bot: bool = Depends(is_bot_triggered_workflow),
) -> bool:
if is_bot:
Expand Down Expand Up @@ -138,7 +138,7 @@ async def handle_publish_plugin_check(
installation_id: int = Depends(get_installation_id),
repo_info: RepoInfo = Depends(get_repo_info),
issue_number: int = Depends(get_issue_number),
publish_type: Literal[PublishType.PLUGIN] = Depends(get_type_by_labels),
publish_type: Literal[PublishType.PLUGIN] = Depends(get_type_by_labels_name),
) -> None:
async with bot.as_installation(installation_id):
# 因为 Actions 会排队,触发事件相关的议题在 Actions 执行时可能已经被关闭
Expand Down Expand Up @@ -181,7 +181,7 @@ async def handle_adapter_publish_check(
installation_id: int = Depends(get_installation_id),
repo_info: RepoInfo = Depends(get_repo_info),
issue_number: int = Depends(get_issue_number),
publish_type: Literal[PublishType.ADAPTER] = Depends(get_type_by_labels),
publish_type: Literal[PublishType.ADAPTER] = Depends(get_type_by_labels_name),
) -> None:
async with bot.as_installation(installation_id):
# 因为 Actions 会排队,触发事件相关的议题在 Actions 执行时可能已经被关闭
Expand Down Expand Up @@ -215,7 +215,7 @@ async def handle_bot_publish_check(
installation_id: int = Depends(get_installation_id),
repo_info: RepoInfo = Depends(get_repo_info),
issue_number: int = Depends(get_issue_number),
publish_type: Literal[PublishType.BOT] = Depends(get_type_by_labels),
publish_type: Literal[PublishType.BOT] = Depends(get_type_by_labels_name),
) -> None:
async with bot.as_installation(installation_id):
# 因为 Actions 会排队,触发事件相关的议题在 Actions 执行时可能已经被关闭
Expand Down Expand Up @@ -272,7 +272,7 @@ async def handle_pull_request_and_update_issue(

async def review_submiited_rule(
event: PullRequestReviewSubmitted,
publish_type: PublishType | None = Depends(get_type_by_labels),
publish_type: PublishType | None = Depends(get_type_by_labels_name),
) -> bool:
if publish_type is None:
logger.info("拉取请求与发布无关,已跳过")
Expand Down
14 changes: 6 additions & 8 deletions src/plugins/github/plugins/publish/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
from nonebot.adapters.github import Bot
from nonebot.params import Depends

from src.plugins.github.depends import get_issue_title, get_labels, get_repo_info
from src.plugins.github.depends import (
get_issue_title,
get_repo_info,
get_type_by_labels_name,
)
from src.plugins.github.models import RepoInfo
from src.plugins.github.plugins.publish import utils
from src.plugins.github.typing import LabelsItems
from src.providers.validation.models import PublishType


def get_type_by_labels(labels: LabelsItems = Depends(get_labels)) -> PublishType | None:
"""通过标签获取类型"""
return utils.get_type_by_labels(labels)


def get_type_by_title(title: str = Depends(get_issue_title)) -> PublishType | None:
"""通过标题获取类型"""
return utils.get_type_by_title(title)
Expand All @@ -22,7 +20,7 @@ def get_type_by_title(title: str = Depends(get_issue_title)) -> PublishType | No
async def get_pull_requests_by_label(
bot: Bot,
repo_info: RepoInfo = Depends(get_repo_info),
publish_type: PublishType = Depends(get_type_by_labels),
publish_type: PublishType = Depends(get_type_by_labels_name),
) -> list[PullRequestSimple]:
pulls = (
await bot.rest.pulls.async_list(**repo_info.model_dump(), state="open")
Expand Down
21 changes: 1 addition & 20 deletions src/plugins/github/plugins/publish/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

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 IssueHandler, RepoInfo
from src.plugins.github.models.github import GithubHandler
from src.plugins.github.typing import LabelsItems
from src.plugins.github.utils import commit_message as _commit_message
from src.plugins.github.utils import dump_json, load_json, run_shell_command
from src.providers.models import RegistryUpdatePayload, to_store
Expand All @@ -32,28 +32,9 @@
from githubkit.rest import (
PullRequest,
PullRequestSimple,
PullRequestSimplePropLabelsItems,
)


def get_type_by_labels(
labels: LabelsItems | list["PullRequestSimplePropLabelsItems"],
) -> PublishType | None:
"""通过标签获取类型"""
if not labels:
return None

for label in labels:
if isinstance(label, str):
continue
if label.name == PublishType.BOT.value:
return PublishType.BOT
if label.name == PublishType.PLUGIN.value:
return PublishType.PLUGIN
if label.name == PublishType.ADAPTER.value:
return PublishType.ADAPTER


def get_type_by_title(title: str) -> PublishType | None:
"""通过标题获取类型"""
if title.startswith(f"{PublishType.BOT.value}:"):
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/github/plugins/publish/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from src.plugins.github import plugin_config
from src.plugins.github.models import AuthorInfo
from src.plugins.github.models.issue import IssueHandler
from src.plugins.github.utils import extract_publish_info_from_issue
from src.plugins.github.utils import extract_issue_info_from_issue
from src.providers.constants import DOCKER_IMAGES
from src.providers.docker_test import DockerPluginTest, Metadata
from src.providers.validation import PublishType, ValidationDict, validate_info
Expand Down Expand Up @@ -48,7 +48,7 @@ async def validate_plugin_info_from_issue(
body = issue.body if issue.body else ""

# 从议题里提取插件所需信息
raw_data: dict[str, Any] = extract_publish_info_from_issue(
raw_data: dict[str, Any] = extract_issue_info_from_issue(
{
"module_name": PLUGIN_MODULE_NAME_PATTERN,
"project_link": PROJECT_LINK_PATTERN,
Expand Down Expand Up @@ -77,7 +77,7 @@ async def validate_plugin_info_from_issue(
raw_data["skip_test"] = skip_test
if skip_test:
# 如果插件被跳过,则从议题获取插件信息
metadata = extract_publish_info_from_issue(
metadata = extract_issue_info_from_issue(
{
"name": PLUGIN_NAME_PATTERN,
"desc": PLUGIN_DESC_PATTERN,
Expand Down Expand Up @@ -135,7 +135,7 @@ async def validate_plugin_info_from_issue(
async def validate_adapter_info_from_issue(issue: Issue) -> ValidationDict:
"""从议题中提取适配器信息"""
body = issue.body if issue.body else ""
raw_data: dict[str, Any] = extract_publish_info_from_issue(
raw_data: dict[str, Any] = extract_issue_info_from_issue(
{
"module_name": ADAPTER_MODULE_NAME_PATTERN,
"project_link": PROJECT_LINK_PATTERN,
Expand All @@ -157,7 +157,7 @@ async def validate_adapter_info_from_issue(issue: Issue) -> ValidationDict:
async def validate_bot_info_from_issue(issue: Issue) -> ValidationDict:
"""从议题中提取机器人信息"""
body = issue.body if issue.body else ""
raw_data: dict[str, Any] = extract_publish_info_from_issue(
raw_data: dict[str, Any] = extract_issue_info_from_issue(
{
"name": BOT_NAME_PATTERN,
"desc": BOT_DESC_PATTERN,
Expand Down
23 changes: 14 additions & 9 deletions src/plugins/github/plugins/remove/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
get_issue_number,
get_related_issue_number,
get_repo_info,
get_type_by_labels_name,
install_pre_commit_hooks,
is_bot_triggered_workflow,
)
from src.plugins.github.models import IssueHandler
from src.plugins.github.typing import IssuesEvent
from src.providers.validation.models import PublishType

from .constants import BRANCH_NAME_PREFIX, REMOVE_LABEL
from .depends import check_labels, get_name_by_labels
from .depends import check_labels
from .render import render_comment, render_error
from .utils import process_pull_reqeusts, resolve_conflict_pull_requests
from .validation import validate_author_info
Expand Down Expand Up @@ -87,8 +90,8 @@ async def handle_pr_close(


async def check_rule(
event: IssuesOpened | IssuesReopened | IssuesEdited | IssueCommentCreated,
edit_type: list[str] = Depends(get_name_by_labels),
event: IssuesEvent,
is_remove: bool = check_labels(REMOVE_LABEL),
is_bot: bool = Depends(is_bot_triggered_workflow),
) -> bool:
if is_bot:
Expand All @@ -97,8 +100,8 @@ async def check_rule(
if event.payload.issue.pull_request:
logger.info("评论在拉取请求下,已跳过")
return False
if REMOVE_LABEL not in edit_type:
logger.info("议题与删除无关,已跳过")
if is_remove is False:
logger.info("非删除工作流,已跳过")
return False
return True

Expand All @@ -116,6 +119,7 @@ async def handle_remove_check(
installation_id: int = Depends(get_installation_id),
repo_info: RepoInfo = Depends(get_repo_info),
issue_number: int = Depends(get_issue_number),
publish_type: PublishType = Depends(get_type_by_labels_name),
):
async with bot.as_installation(installation_id):
issue = (
Expand All @@ -127,23 +131,24 @@ async def handle_remove_check(
if issue.state != "open":
logger.info("议题未开启,已跳过")
await remove_check_matcher.finish()

handler = IssueHandler(bot=bot, repo_info=repo_info, issue=issue)

try:
# 搜索包的信息和验证作者信息
result = await validate_author_info(issue)
result = await validate_author_info(issue, publish_type)
except PydanticCustomError as err:
logger.error(f"信息验证失败: {err}")
await handler.comment_issue(await render_error(err))
await remove_check_matcher.finish()

title = f"{result.type}: Remove {result.name or 'Unknown'}"[:TITLE_MAX_LENGTH]
title = f"{result.publish_type}: Remove {result.name or 'Unknown'}"[
:TITLE_MAX_LENGTH
]
branch_name = f"{BRANCH_NAME_PREFIX}{issue_number}"

# 处理拉取请求和议题标题
await process_pull_reqeusts(handler, result, branch_name, title)
# 更新议题标题

await handler.update_issue_title(title)

await handler.comment_issue(await render_comment(result))
17 changes: 9 additions & 8 deletions src/plugins/github/plugins/remove/constants.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import re

from src.plugins.github import plugin_config
from src.plugins.github.constants import ISSUE_PATTERN
from src.providers.validation.models import PublishType

# Bot
REMOVE_BOT_HOMEPAGE_PATTERN = re.compile(
he0119 marked this conversation as resolved.
Show resolved Hide resolved
ISSUE_PATTERN.format("机器人项目仓库/主页链接")
)
REMOVE_BOT_NAME_PATTERN = re.compile(ISSUE_PATTERN.format("机器人名称"))
# Plugin
REMOVE_PLUGIN_PROJECT_LINK_PATTERN = re.compile(ISSUE_PATTERN.format("PyPI 项目名"))
REMOVE_PLUGIN_MODULE_NAME_PATTERN = re.compile(ISSUE_PATTERN.format("import 包名"))
# Driver / Adapter
REMOVE_HOMEPAGE_PATTERN = re.compile(ISSUE_PATTERN.format("项目主页"))

BRANCH_NAME_PREFIX = "remove/issue"

COMMIT_MESSAGE_PREFIX = ":hammer: remove"

REMOVE_LABEL = "Remove"

PUBLISH_PATH = {
PublishType.PLUGIN: plugin_config.input_config.plugin_path,
PublishType.ADAPTER: plugin_config.input_config.adapter_path,
PublishType.BOT: plugin_config.input_config.bot_path,
}
19 changes: 3 additions & 16 deletions src/plugins/github/plugins/remove/depends.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
from nonebot.params import Depends

from src.plugins.github.depends import get_labels
from src.plugins.github.typing import LabelsItems


def get_name_by_labels(labels: LabelsItems = Depends(get_labels)) -> list[str]:
"""通过标签获取名称"""
label_names: list[str] = []
if not labels:
return label_names

for label in labels:
if label.name:
label_names.append(label.name)
return label_names
from src.plugins.github.depends import get_labels_name


def check_labels(labels: list[str] | str):
Expand All @@ -22,8 +9,8 @@ def check_labels(labels: list[str] | str):
labels = [labels]

async def _check_labels(
has_labels: list[str] = Depends(get_name_by_labels),
has_labels: list[str] = Depends(get_labels_name),
) -> bool:
return all(label in has_labels for label in labels)
return any(label in has_labels for label in labels)

return Depends(_check_labels)
Loading
Loading