Skip to content

Commit

Permalink
change: 提前判断议题是否与发布有关 (#127)
Browse files Browse the repository at this point in the history
因为就算延后执行,议题的名称也不会变
  • Loading branch information
he0119 authored Apr 10, 2023
1 parent 7faced2 commit 32af040
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

- 跳过机器人的评论

### Changed

- 提前判断议题是否与发布有关

## [2.1.0] - 2023-04-08

### Added
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
get_related_issue_number,
get_repo_info,
get_type_by_labels,
get_type_by_title,
)
from .models import PublishType, RepoInfo
from .utils import (
comment_issue,
commit_and_push,
create_pull_request,
extract_publish_info_from_issue,
get_type_by_title,
resolve_conflict_pull_requests,
run_shell_command,
should_skip_plugin_test,
Expand Down Expand Up @@ -111,6 +111,7 @@ async def handle_pr_close(

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

return True

Expand All @@ -136,6 +140,7 @@ async def handle_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: PublishType = Depends(get_type_by_title),
) -> None:
async with bot.as_installation(installation_id):
# 因为 Actions 会排队,触发事件相关的议题在 Actions 执行时可能已经被关闭
Expand All @@ -150,11 +155,6 @@ async def handle_publish_check(
logger.info("议题未开启,已跳过")
await publish_check_matcher.finish()

publish_type = get_type_by_title(issue.title)
if not publish_type:
logger.info("议题与发布无关,已跳过")
await publish_check_matcher.finish()

# 自动给议题添加标签
await bot.rest.issues.async_add_labels(
**repo_info.dict(),
Expand Down
14 changes: 13 additions & 1 deletion src/plugins/publish/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@ def get_labels(
| IssuesEdited
| IssueCommentCreated,
):
"""获取标签"""
"""获取议题或拉取请求的标签"""
if isinstance(event, (PullRequestClosed, PullRequestReviewSubmitted)):
labels = event.payload.pull_request.labels
else:
labels = event.payload.issue.labels
return labels


def get_issue_title(
event: IssuesOpened | IssuesReopened | IssuesEdited | IssueCommentCreated,
):
"""获取议题标题"""
return event.payload.issue.title


def get_type_by_labels(
labels: list[Label]
| list[WebhookLabel]
Expand All @@ -54,6 +61,11 @@ def get_type_by_labels(
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)


async def get_pull_requests_by_label(
bot: Bot,
repo_info: RepoInfo = Depends(get_repo_info),
Expand Down
33 changes: 5 additions & 28 deletions tests/publish/process/test_publish_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async def test_process_publish_check(
event_path = Path(__file__).parent.parent / "plugin-test" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
assert isinstance(event, IssuesOpened)
event.payload.issue.title = "Bot: test"

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

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

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

mock_installation = mocker.MagicMock()
mock_installation.id = 123
mock_installation_resp = mocker.MagicMock()
mock_installation_resp.parsed_data = mock_installation

mock_issue = mocker.MagicMock()
mock_issue.pull_request = None
mock_issue.state = "open"
mock_issue.title = "test"
mock_issues_resp = mocker.MagicMock()
mock_issues_resp.parsed_data = mock_issue

async with app.test_matcher(publish_check_matcher) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(
Expand All @@ -648,26 +639,12 @@ async def test_not_publish_issue(app: App, mocker: MockerFixture) -> None:
event_path = Path(__file__).parent.parent / "plugin-test" / "issue-open.json"
event = Adapter.payload_to_event("1", "issues", event_path.read_bytes())
assert isinstance(event, IssuesOpened)

ctx.should_call_api(
"rest.apps.async_get_repo_installation",
{"owner": "he0119", "repo": "action-test"},
mock_installation_resp,
)
ctx.should_call_api(
"rest.issues.async_get",
{"owner": "he0119", "repo": "action-test", "issue_number": 80},
mock_issues_resp,
)
event.payload.issue.title = "test"

ctx.receive_event(bot, event)

mock_httpx.assert_not_called()
mock_subprocess_run.assert_called_once_with(
["git", "config", "--global", "safe.directory", "*"],
check=True,
capture_output=True,
)
mock_subprocess_run.assert_not_called()


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

0 comments on commit 32af040

Please sign in to comment.