Skip to content

Commit 32af040

Browse files
authored
change: 提前判断议题是否与发布有关 (#127)
因为就算延后执行,议题的名称也不会变
1 parent 7faced2 commit 32af040

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
1111

1212
- 跳过机器人的评论
1313

14+
### Changed
15+
16+
- 提前判断议题是否与发布有关
17+
1418
## [2.1.0] - 2023-04-08
1519

1620
### Added

src/plugins/publish/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
get_related_issue_number,
2020
get_repo_info,
2121
get_type_by_labels,
22+
get_type_by_title,
2223
)
2324
from .models import PublishType, RepoInfo
2425
from .utils import (
2526
comment_issue,
2627
commit_and_push,
2728
create_pull_request,
2829
extract_publish_info_from_issue,
29-
get_type_by_title,
3030
resolve_conflict_pull_requests,
3131
run_shell_command,
3232
should_skip_plugin_test,
@@ -111,6 +111,7 @@ async def handle_pr_close(
111111

112112
async def check_rule(
113113
event: IssuesOpened | IssuesReopened | IssuesEdited | IssueCommentCreated,
114+
publish_type: PublishType | None = Depends(get_type_by_title),
114115
) -> bool:
115116
if isinstance(
116117
event, IssueCommentCreated
@@ -120,6 +121,9 @@ async def check_rule(
120121
if event.payload.issue.pull_request:
121122
logger.info("评论在拉取请求下,已跳过")
122123
return False
124+
if not publish_type:
125+
logger.info("议题与发布无关,已跳过")
126+
await publish_check_matcher.finish()
123127

124128
return True
125129

@@ -136,6 +140,7 @@ async def handle_publish_check(
136140
installation_id: int = Depends(get_installation_id),
137141
repo_info: RepoInfo = Depends(get_repo_info),
138142
issue_number: int = Depends(get_issue_number),
143+
publish_type: PublishType = Depends(get_type_by_title),
139144
) -> None:
140145
async with bot.as_installation(installation_id):
141146
# 因为 Actions 会排队,触发事件相关的议题在 Actions 执行时可能已经被关闭
@@ -150,11 +155,6 @@ async def handle_publish_check(
150155
logger.info("议题未开启,已跳过")
151156
await publish_check_matcher.finish()
152157

153-
publish_type = get_type_by_title(issue.title)
154-
if not publish_type:
155-
logger.info("议题与发布无关,已跳过")
156-
await publish_check_matcher.finish()
157-
158158
# 自动给议题添加标签
159159
await bot.rest.issues.async_add_labels(
160160
**repo_info.dict(),

src/plugins/publish/depends.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,21 @@ def get_labels(
3737
| IssuesEdited
3838
| IssueCommentCreated,
3939
):
40-
"""获取标签"""
40+
"""获取议题或拉取请求的标签"""
4141
if isinstance(event, (PullRequestClosed, PullRequestReviewSubmitted)):
4242
labels = event.payload.pull_request.labels
4343
else:
4444
labels = event.payload.issue.labels
4545
return labels
4646

4747

48+
def get_issue_title(
49+
event: IssuesOpened | IssuesReopened | IssuesEdited | IssueCommentCreated,
50+
):
51+
"""获取议题标题"""
52+
return event.payload.issue.title
53+
54+
4855
def get_type_by_labels(
4956
labels: list[Label]
5057
| list[WebhookLabel]
@@ -54,6 +61,11 @@ def get_type_by_labels(
5461
return utils.get_type_by_labels(labels)
5562

5663

64+
def get_type_by_title(title: str = Depends(get_issue_title)) -> PublishType | None:
65+
"""通过标题获取类型"""
66+
return utils.get_type_by_title(title)
67+
68+
5769
async def get_pull_requests_by_label(
5870
bot: Bot,
5971
repo_info: RepoInfo = Depends(get_repo_info),

tests/publish/process/test_publish_check.py

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ async def test_process_publish_check(
8787
event_path = Path(__file__).parent.parent / "plugin-test" / "issue-open.json"
8888
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
8989
assert isinstance(event, IssuesOpened)
90+
event.payload.issue.title = "Bot: test"
9091

9192
ctx.should_call_api(
9293
"rest.apps.async_get_repo_installation",
@@ -276,6 +277,7 @@ async def test_edit_title(app: App, mocker: MockerFixture, tmp_path: Path) -> No
276277
event_path = Path(__file__).parent.parent / "plugin-test" / "issue-open.json"
277278
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
278279
assert isinstance(event, IssuesOpened)
280+
event.payload.issue.title = "Bot: test"
279281

280282
ctx.should_call_api(
281283
"rest.apps.async_get_repo_installation",
@@ -469,6 +471,7 @@ async def test_process_publish_check_not_pass(
469471
event_path = Path(__file__).parent.parent / "plugin-test" / "issue-open.json"
470472
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
471473
assert isinstance(event, IssuesOpened)
474+
event.payload.issue.title = "Bot: test"
472475

473476
ctx.should_call_api(
474477
"rest.apps.async_get_repo_installation",
@@ -625,18 +628,6 @@ async def test_not_publish_issue(app: App, mocker: MockerFixture) -> None:
625628
"subprocess.run", side_effect=lambda *args, **kwargs: mocker.MagicMock()
626629
)
627630

628-
mock_installation = mocker.MagicMock()
629-
mock_installation.id = 123
630-
mock_installation_resp = mocker.MagicMock()
631-
mock_installation_resp.parsed_data = mock_installation
632-
633-
mock_issue = mocker.MagicMock()
634-
mock_issue.pull_request = None
635-
mock_issue.state = "open"
636-
mock_issue.title = "test"
637-
mock_issues_resp = mocker.MagicMock()
638-
mock_issues_resp.parsed_data = mock_issue
639-
640631
async with app.test_matcher(publish_check_matcher) as ctx:
641632
adapter = get_adapter(Adapter)
642633
bot = ctx.create_bot(
@@ -648,26 +639,12 @@ async def test_not_publish_issue(app: App, mocker: MockerFixture) -> None:
648639
event_path = Path(__file__).parent.parent / "plugin-test" / "issue-open.json"
649640
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
650641
assert isinstance(event, IssuesOpened)
651-
652-
ctx.should_call_api(
653-
"rest.apps.async_get_repo_installation",
654-
{"owner": "he0119", "repo": "action-test"},
655-
mock_installation_resp,
656-
)
657-
ctx.should_call_api(
658-
"rest.issues.async_get",
659-
{"owner": "he0119", "repo": "action-test", "issue_number": 80},
660-
mock_issues_resp,
661-
)
642+
event.payload.issue.title = "test"
662643

663644
ctx.receive_event(bot, event)
664645

665646
mock_httpx.assert_not_called()
666-
mock_subprocess_run.assert_called_once_with(
667-
["git", "config", "--global", "safe.directory", "*"],
668-
check=True,
669-
capture_output=True,
670-
)
647+
mock_subprocess_run.assert_not_called()
671648

672649

673650
async def test_comment_by_self(app: App, mocker: MockerFixture) -> None:

0 commit comments

Comments
 (0)