Skip to content

Commit

Permalink
feat: 增加了 Publish 标签简化判断
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Dec 8, 2024
1 parent 3bb1ab7 commit 0718058
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 31 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
17 changes: 9 additions & 8 deletions src/plugins/github/plugins/resolve/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from nonebot import logger, on_type
from nonebot.adapters.github import GitHubBot, PullRequestClosed
from nonebot.params import Depends
from nonebot.typing import T_State

from src.plugins.github.constants import PUBLISH_LABEL, REMOVE_LABEL
from src.plugins.github.depends import (
bypass_git,
get_installation_id,
Expand All @@ -18,19 +18,21 @@
from src.plugins.github.plugins.remove.utils import (
resolve_conflict_pull_requests as resolve_conflict_remove_pull_requests,
)
from src.plugins.github.typing import PullRequestList
from src.plugins.github.typing import PullRequestLabels, PullRequestList
from src.providers.validation.models import PublishType


def is_publish(labels: list) -> bool:
return all(label.name != "Remove" or label.name != "Config" for label in labels)
def is_publish(labels: PullRequestLabels) -> bool:
return any(label.name == PUBLISH_LABEL for label in labels)


def is_remove(labels: list) -> bool:
return any(label.name == "Remove" 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):
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])
Expand Down Expand Up @@ -60,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 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
30 changes: 24 additions & 6 deletions tests/plugins/github/resolve/test_resolve_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@
)


async def test_resolve_pull_request(app: App, mocker: MockerFixture, mock_installation: MagicMock) -> None:
async def test_resolve_pull_request(
app: App, mocker: MockerFixture, mock_installation: MagicMock
) -> None:
"""测试能正确处理拉取请求关闭后其他拉取请求的冲突问题"""
mock_subprocess_run = mocker.patch("subprocess.run")

mock_issue = MockIssue(body=generate_issue_body_remove(type="Bot"), number=76).as_mock(mocker)
mock_issue = MockIssue(
body=generate_issue_body_remove(type="Bot"), number=76
).as_mock(mocker)
mock_issues_resp = mocker.MagicMock()
mock_issues_resp.parsed_data = mock_issue

mock_publish_issue = MockIssue(body=generate_issue_body_bot(), number=100).as_mock(mocker)
mock_publish_issue = MockIssue(body=generate_issue_body_bot(), number=100).as_mock(
mocker
)
mock_publish_issue_resp = mocker.MagicMock()
mock_publish_issue_resp.parsed_data = mock_publish_issue
mock_publish_pull = mocker.MagicMock()
mock_publish_pull.title = "Bot: test"
mock_publish_pull.draft = False
mock_publish_pull.head.ref = "publish/issue100"
mock_publish_pull.labels = get_pr_labels(["Bot"])
mock_publish_pull.labels = get_pr_labels(["Bot", "Publish"])
mock_remove_issue = MockIssue(
body=generate_issue_body_remove(type="Bot", key="name:https://v2.nonebot.dev"),
number=101,
Expand Down Expand Up @@ -112,7 +118,13 @@ async def test_resolve_pull_request(app: App, mocker: MockerFixture, mock_instal
["git", "checkout", "master"],
["git", "switch", "-C", "publish/issue100"],
["git", "config", "--global", "user.name", "test"],
["git", "config", "--global", "user.email", "[email protected]"],
[
"git",
"config",
"--global",
"user.email",
"[email protected]",
],
["git", "add", "-A"],
["git", "commit", "-m", ":beers: publish bot name (#100)"],
["git", "fetch", "origin"],
Expand All @@ -122,7 +134,13 @@ async def test_resolve_pull_request(app: App, mocker: MockerFixture, mock_instal
["git", "checkout", "master"],
["git", "switch", "-C", "remove/issue101"],
["git", "config", "--global", "user.name", "test"],
["git", "config", "--global", "user.email", "[email protected]"],
[
"git",
"config",
"--global",
"user.email",
"[email protected]",
],
["git", "add", "-A"],
["git", "commit", "-m", ":hammer: remove name (#101)"],
["git", "fetch", "origin"],
Expand Down

0 comments on commit 0718058

Please sign in to comment.