Skip to content

Commit 0fbebdd

Browse files
authored
fix: 处理冲突时直接重新提交 (#170)
此时没有测试环境, 并且之前的测试结果应该依旧有效。 fix #161
1 parent e9baf90 commit 0fbebdd

File tree

5 files changed

+477
-72
lines changed

5 files changed

+477
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
1515

1616
- 商店插件列表增加插件测试时的版本号
1717
- 调整商店测试验证输出
18+
- 处理冲突时直接重新提交
1819

1920
## [2.7.3] - 2023-08-07
2021

src/plugins/publish/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async def handle_pr_close(
119119
pull_requests = await get_pull_requests_by_label(
120120
bot, repo_info, publish_type
121121
)
122-
await resolve_conflict_pull_requests(bot, repo_info, pull_requests)
122+
await resolve_conflict_pull_requests(pull_requests)
123123
else:
124124
logger.info("发布的拉取请求未合并,已跳过")
125125

@@ -258,7 +258,7 @@ async def handle_auto_merge(
258258
["git", "fetch", "origin", plugin_config.input_config.base]
259259
)
260260
# 尝试处理冲突
261-
await resolve_conflict_pull_requests(bot, repo_info, [pull_request])
261+
await resolve_conflict_pull_requests([pull_request])
262262

263263
await bot.rest.pulls.async_merge(
264264
**repo_info.dict(),

src/plugins/publish/utils.py

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@
4040
from .render import render_comment
4141

4242
if TYPE_CHECKING:
43-
from githubkit.rest.models import Issue, IssuePropLabelsItemsOneof1, Label
43+
from githubkit.rest.models import (
44+
Issue,
45+
IssuePropLabelsItemsOneof1,
46+
Label,
47+
PullRequestPropLabelsItems,
48+
PullRequestSimplePropLabelsItems,
49+
)
4450
from githubkit.webhooks.models import Issue as WebhookIssue
4551
from githubkit.webhooks.models import (
4652
IssueCommentCreatedPropIssue,
@@ -71,7 +77,9 @@ def run_shell_command(command: list[str]):
7177
def get_type_by_labels(
7278
labels: list["Label"]
7379
| list["WebhookLabel"]
74-
| list[Union[str, "IssuePropLabelsItemsOneof1"]],
80+
| list[Union[str, "IssuePropLabelsItemsOneof1"]]
81+
| list["PullRequestSimplePropLabelsItems"]
82+
| list["PullRequestPropLabelsItems"],
7583
) -> PublishType | None:
7684
"""通过标签获取类型"""
7785
for label in labels:
@@ -208,6 +216,7 @@ def validate_info_from_issue(
208216
"skip_plugin_test": plugin_config.skip_plugin_test,
209217
"plugin_test_result": plugin_config.plugin_test_result,
210218
"plugin_test_output": plugin_config.plugin_test_output,
219+
"plugin_test_metadata": plugin_config.plugin_test_metadata,
211220
"previous_data": data,
212221
}
213222
# 如果插件测试被跳过,则从议题中获取信息
@@ -247,46 +256,63 @@ def validate_info_from_issue(
247256

248257

249258
async def resolve_conflict_pull_requests(
250-
bot: Bot,
251-
repo_info: RepoInfo,
252259
pulls: list[PullRequestSimple] | list[PullRequest],
253260
):
254261
"""根据关联的议题提交来解决冲突
255262
256-
参考对应的议题重新更新对应分支
263+
直接重新提交之前分支中的内容
257264
"""
258-
# 跳过插件测试,因为这个时候插件测试任务没有运行
259-
plugin_config.skip_plugin_test = True
260-
261265
for pull in pulls:
262-
# 回到主分支
263-
run_shell_command(["git", "checkout", plugin_config.input_config.base])
264-
# 切换到对应分支
265-
run_shell_command(["git", "switch", "-C", pull.head.ref])
266-
267266
issue_number = extract_issue_number_from_ref(pull.head.ref)
268267
if not issue_number:
269-
logger.error(f"无法获取 {pull.title} 对应的议题")
270-
return
268+
logger.error(f"无法获取 {pull.title} 对应的议题编号")
269+
continue
271270

272271
logger.info(f"正在处理 {pull.title}")
273-
issue = (
274-
await bot.rest.issues.async_get(
275-
**repo_info.dict(), issue_number=issue_number
276-
)
277-
).parsed_data
272+
if pull.draft:
273+
logger.info("拉取请求为草稿,跳过处理")
274+
continue
278275

279-
publish_type = get_type_by_labels(issue.labels)
276+
publish_type = get_type_by_labels(pull.labels)
280277
if publish_type:
281-
result = validate_info_from_issue(issue, publish_type)
282-
if result["valid"]:
283-
update_file(result)
284-
commit_and_push(result, pull.head.ref, issue_number)
285-
logger.info("拉取请求更新完毕")
286-
else:
287-
comment = await render_comment(result)
288-
logger.info(comment)
289-
logger.info("发布没通过检查,已跳过")
278+
result = generate_validation_dict_from_file(publish_type)
279+
# 回到主分支
280+
run_shell_command(["git", "checkout", plugin_config.input_config.base])
281+
# 切换到对应分支
282+
run_shell_command(["git", "switch", "-C", pull.head.ref])
283+
update_file(result)
284+
commit_and_push(result, pull.head.ref, issue_number)
285+
logger.info("拉取请求更新完毕")
286+
287+
288+
def generate_validation_dict_from_file(publish_type: PublishType) -> ValidationDict:
289+
"""从文件中获取发布所需数据"""
290+
match publish_type:
291+
case PublishType.ADAPTER:
292+
with plugin_config.input_config.adapter_path.open(
293+
"r", encoding="utf-8"
294+
) as f:
295+
data: list[dict[str, str]] = json.load(f)
296+
raw_data = data[-1]
297+
case PublishType.BOT:
298+
with plugin_config.input_config.bot_path.open("r", encoding="utf-8") as f:
299+
data: list[dict[str, str]] = json.load(f)
300+
raw_data = data[-1]
301+
case PublishType.PLUGIN:
302+
with plugin_config.input_config.plugin_path.open(
303+
"r", encoding="utf-8"
304+
) as f:
305+
data: list[dict[str, str]] = json.load(f)
306+
raw_data = data[-1]
307+
308+
return ValidationDict(
309+
valid=True,
310+
type=publish_type,
311+
name=raw_data["name"],
312+
author=raw_data["author"],
313+
data=raw_data,
314+
errors=[],
315+
)
290316

291317

292318
def update_file(result: ValidationDict) -> None:

tests/publish/process/test_auto_merge.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async def test_auto_merge(app: App, mocker: MockerFixture) -> None:
8181
check=True,
8282
capture_output=True,
8383
),
84-
],
84+
], # type: ignore
8585
any_order=True,
8686
)
8787

@@ -92,7 +92,6 @@ async def test_auto_merge_need_rebase(app: App, mocker: MockerFixture) -> None:
9292
需要 rebase 的情况
9393
"""
9494
from src.plugins.publish import auto_merge_matcher
95-
from src.plugins.publish.models import RepoInfo
9695

9796
mock_subprocess_run = mocker.patch("subprocess.run")
9897
mock_resolve_conflict_pull_requests = mocker.patch(
@@ -168,9 +167,7 @@ async def test_auto_merge_need_rebase(app: App, mocker: MockerFixture) -> None:
168167
],
169168
any_order=True,
170169
)
171-
mock_resolve_conflict_pull_requests.assert_called_once_with(
172-
bot, RepoInfo(owner="he0119", repo="action-test"), [mock_pull]
173-
)
170+
mock_resolve_conflict_pull_requests.assert_called_once_with([mock_pull])
174171

175172

176173
async def test_auto_merge_not_publish(app: App, mocker: MockerFixture) -> None:

0 commit comments

Comments
 (0)