You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过GitHub API创建Pull Request验证失败求助

GitHub API创建Pull Request报错及添加审批者问题

问题描述

我通过GitHub API创建Pull Request时遇到困难,已完成身份验证(其他API端点测试均返回预期结果)。调用创建PR的API时返回“Validation Failed”错误,提示head字段无效。尝试过两种写法:直接填分支名test,以及my_user:test,均报错。同时想了解能否通过API为PR添加审批者。

代码示例1

$Body = @{
           title = "Test";
           body  = "This is a test Pull Request";
           head  = "test";
           base = "master"
         }
Invoke-RestMethod -Method Post -Headers $Headers -Uri "https://api.github.com/repos/owner/my_repo/pulls" -Body ($Body | ConvertTo-Json)

错误信息

Invoke-RestMethod : {"message":"Validation Failed","errors":[{"resource":"PullRequest","field":"head","code":"invalid"}],"documentation_url":"https://docs.github.com/rest/reference/pulls#create-a-pull-request"}

代码示例2

$Body = @{
           title = "Test";
           body  = "This is a test Pull Request";
           head  = "my_user:test";
           base = "master"
         }
Invoke-RestMethod -Method Post -Headers $Headers -Uri "https://api.github.com/repos/owner/my_repo/pulls" -Body ($Body | ConvertTo-Json)

问题解答

1. head字段无效的可能原因

  • 分支未推送到远程仓库:确保test分支已经推送到GitHub远程仓库,本地未推送的分支API无法识别。
  • 权限或归属问题:如果操作的是组织仓库,确认你的账号拥有仓库写入权限,且分支确实属于你指定的用户/组织。
  • 格式拼写错误:若为复刻仓库的分支,head格式应为你的GitHub用户名:分支名,检查用户名拼写、大小写是否正确。
  • base分支名称错误:虽然报错指向head,但也可能是base分支不存在(比如当前多数仓库默认分支为main而非master),先确认目标仓库的默认分支名称。

2. 通过API添加PR审批者

GitHub API没有直接添加审批者的端点,但可通过以下两种方式实现:

  • 请求特定用户/团队审查:调用POST /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers端点,在请求体中指定审查者。示例PowerShell代码:
$ReviewBody = @{
    reviewers = @("reviewer_user1", "reviewer_user2")
}
Invoke-RestMethod -Method Post -Headers $Headers -Uri "https://api.github.com/repos/owner/my_repo/pulls/{pr_number}/requested_reviewers" -Body ($ReviewBody | ConvertTo-Json)
  • 设置仓库级必需审查者:调用仓库分支保护规则API,配置required_pull_request_reviews参数,指定必需的审查人数或特定团队。

内容的提问来源于stack exchange,提问作者delucaezequiel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 07:01:16