使用Bitbucket API为Pull Request添加审核人时遇400错误求助
解决Bitbucket API添加PR审核人时的400 Bad Request错误
问题分析
你遇到的400错误主要源于JSON格式无效、HTTP方法错误或请求头缺失,以下是针对两种实现方式的具体修复方案:
1. 创建PR时直接添加审核人的修复
你的第一个函数中,ConvertTo-Json默认仅处理2层嵌套结构,reviewers下的user对象属于第3层,会被截断为字符串导致JSON格式失效;另外部分Bitbucket Server版本要求审核人对象需包含slug或id提升识别准确率。
修复后的代码:
function Create-PullRequest([string]$Url, [string]$User, [string]$Pass, [string]$Branch, [string]$MergeBranch, [string]$ReviewerUsername) { $Headers = @{ "Content-Type" = "application/json" "Authorization" = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Pass))) } $Body = @{ title = "Automated Pull Request - $($Branch) to $($MergeBranch)" description = "This is an automated pull request created by Merge-Branches-MF Jenkins Job." fromRef = @{ id = "refs/heads/$Branch" } toRef = @{ id = "refs/heads/$MergeBranch" } reviewers = @( @{ user = @{ name = $ReviewerUsername # 可选:若知道用户slug,添加后可避免重名识别错误 # slug = "techjenkinsuser" } } ) } # 增加Depth参数确保嵌套结构被正确序列化 $JsonBody = $Body | ConvertTo-Json -Depth 3 try { $Response = Invoke-WebRequest -Uri $Url -Headers $Headers -Method Post -Body $JsonBody $PullRequest = $Response.Content | ConvertFrom-Json Write-Output "Pull request created: $($PullRequest.links.self[0].href)" return $PullRequest } catch { # 捕获详细错误响应,便于排查具体问题 if ($_.Exception.Response) { $errorContent = Get-Content $_.Exception.Response.GetResponseStream() -Raw Write-Output "Detailed error: $errorContent" } Write-Output "Error: $($_.Exception.Message)" return $null } }
2. 单独更新PR添加审核人的修复
第二个函数存在三个核心问题:未在函数内定义请求头、使用错误的HTTP方法、手动拼接JSON易出格式问题。
修复后的代码:
$ReviewerJson = @" { "user": { "name": "techJenkinsUser", "emailAddress": "exampleEmail@example.com", "id": 1212, "displayName": "Tech. Jenkins User", "active": true, "slug": "techjenkinsuser", "type": "NORMAL" }, "role": "REVIEWER", "approved": false, "status": "UNAPPROVED" } "@ $Reviewer = $ReviewerJson | ConvertFrom-Json function Update-PullRequestWithReviewer([string]$Url, [string]$User, [string]$Pass, $Reviewer) { # 函数内独立定义认证和内容类型头 $Headers = @{ "Content-Type" = "application/json" "Authorization" = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Pass))) } $Body = @{ reviewers = @( @{ user = @{ name = $Reviewer.user.name emailAddress = $Reviewer.user.emailAddress slug = $Reviewer.user.slug id = $Reviewer.user.id } } ) } $JsonBody = $Body | ConvertTo-Json -Depth 3 try { # 使用PUT方法更新PR(Bitbucket API要求更新操作使用PUT) $Response = Invoke-WebRequest -Uri $Url -Headers $Headers -Method Put -Body $JsonBody return ($Response.Content | ConvertFrom-Json) } catch { if ($_.Exception.Response) { $errorContent = Get-Content $_.Exception.Response.GetResponseStream() -Raw Write-Output "Detailed error: $errorContent" } Write-Output "Error: $($_.Exception.Message)" return $null } }
额外排查建议
- 查看详细错误:catch块中新增的响应流内容会返回Bitbucket给出的具体错误原因(如字段缺失、权限不足)
- 验证权限:确保Jenkins使用的账号拥有创建PR和添加审核人的权限
- 核对API版本:Bitbucket Cloud与Server的API格式有差异,Cloud版本需使用用户
uuid而非name指定审核人
内容的提问来源于stack exchange,提问作者Wiktor Kot
相关产品推荐
相关产品推荐

