我试图通过在Jenkins作业中使用Powershell为我们的bitbucket repo自动化合并分支的过程,但是在合并发生之前需要获得一个批准。我成功地制作了检查分支是否不同的代码,通过使用get方法检查是否需要合并,并通过使用post方法创建拉取请求也有效,但是当我试图添加审稿人时,我得到了错误:
我希望我所面临的问题是清楚的。如果还需要我提供更多的信息,我很乐意提供。"调用- webrequest:远程服务器返回一个错误:(400)错误请求。">
我试图通过多种方式直接在Create-PullRequest
函数中添加reviewer:
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
}
}
)
}
$JsonBody = $Body | ConvertTo-Json
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 {
Write-Output "Error: $($_.Exception.Message)"
return $null
}
}
或者通过单独的函数将接收器放到pr中,但会返回相同的错误。(虽然分离功能pr成功创建,但当我合并这些功能时,pr没有创建或已创建但没有审阅器):
$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) {
$reviewersArray = @(
@{
user = @{
name = $Reviewer.user.name
emailAddress = $Reviewer.user.emailAddress
slug = $Reviewer.user.slug
id = $Reviewer.user.id
}
}
)
$reviewersJson = $reviewersArray | ConvertTo-Json
$Body = @"
{
"reviewers": $reviewersJson
}
"@
$Response = Invoke-WebRequest -Uri $Url -Headers $Headers -Method Post -Body $Body
return ($Response.Content | ConvertFrom-Json)
}
由于GDPR,您不能再使用用户名添加审稿人。您必须使用uuid:
"reviewers": [{ "uuid": "{replace_me_with_an_actual_uuid}"}]
要获取uuid,您需要使用用户名调用该端点,并从响应中获取uuid。
我想说,最好的选择是首先使用像Postman这样的工具来调用更新/创建url,这将更容易。然后你可以让Postman生成Powershell代码,然后你可以修改它来匹配你的代码。