Skip to content

Commit eb2d22c

Browse files
authored
feat: 发布信息更新后如果发现不通过将拉取请求转成草稿 (#171)
1 parent 0fbebdd commit eb2d22c

File tree

4 files changed

+399
-10
lines changed

4 files changed

+399
-10
lines changed

CHANGELOG.md

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

1212
- 使用 jinja 渲染评论
13+
- 发布信息更新后如果发现不通过将拉取请求转成草稿
1314

1415
### Changed
1516

src/plugins/publish/__init__.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ async def handle_publish_check(
189189
# 限制标题长度,过长的标题不好看
190190
title = f"{publish_type.value}: {result['name'][:MAX_NAME_LENGTH]}"
191191

192+
# 分支命名示例 publish/issue123
193+
branch_name = f"{BRANCH_NAME_PREFIX}{issue_number}"
192194
if result["valid"]:
193195
# 创建新分支
194-
# 命名示例 publish/issue123
195-
branch_name = f"{BRANCH_NAME_PREFIX}{issue_number}"
196196
run_shell_command(["git", "switch", "-C", branch_name])
197197
# 更新文件并提交更改
198198
update_file(result)
@@ -202,7 +202,24 @@ async def handle_publish_check(
202202
bot, repo_info, result, branch_name, issue_number, title
203203
)
204204
else:
205-
logger.info("发布没通过检查,暂不创建拉取请求")
205+
# 如果之前已经创建了拉取请求,则将其转换为草稿
206+
pulls = (
207+
await bot.rest.pulls.async_list(
208+
**repo_info.dict(), head=f"{repo_info.owner}:{branch_name}"
209+
)
210+
).parsed_data
211+
if pulls and (pull := pulls[0]) and not pull.draft:
212+
await bot.async_graphql(
213+
query="""mutation convertPullRequestToDraft($pullRequestId: ID!) {
214+
convertPullRequestToDraft(input: {pullRequestId: $pullRequestId}) {
215+
clientMutationId
216+
}
217+
}""",
218+
variables={"pullRequestId": pull.node_id},
219+
)
220+
logger.info("发布没通过检查,已将之前的拉取请求转换为草稿")
221+
else:
222+
logger.info("发布没通过检查,暂不创建拉取请求")
206223

207224
# 修改议题标题
208225
# 需要等创建完拉取请求并打上标签后执行

src/plugins/publish/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,16 @@ async def create_pull_request(
402402
**repo_info.dict(), pull_number=pull.number, title=title
403403
)
404404
logger.info(f"拉取请求标题已修改为 {title}")
405+
if pull.draft:
406+
await bot.async_graphql(
407+
query="""mutation markPullRequestReadyForReview($pullRequestId: ID!) {
408+
markPullRequestReadyForReview(input: {pullRequestId: $pullRequestId}) {
409+
clientMutationId
410+
}
411+
}""",
412+
variables={"pullRequestId": pull.node_id},
413+
)
414+
logger.info("拉取请求已标记为可评审")
405415

406416

407417
async def comment_issue(

0 commit comments

Comments
 (0)