jenkins 使用bitbucket API向pull-request添加审阅者

ssgvzors  于 2023-04-11  发布在  Jenkins
关注(0)|答案(1)|浏览(198)

我试图通过使用powershell在jenkins工作中为我们的bitbucket仓库自动化合并分支的过程,但是它需要在合并之前获得一个批准。我成功地做了检查分支是否不同的代码,以检查是否需要合并使用get方法和创建pull请求也可以使用post方法,但是当我试图添加审阅者时,我得到错误“调用-网络请求:远程服务器返回错误:(400)请求错误。”
我希望我所面临的问题是明确的。如果有任何更多的信息,我应该提供我会很乐意这样做。
我尝试了很多方法,直接在Create-PullRequest函数中添加review:

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
  }
}

或者通过单独的功能只是为了把reviver到pr,但这是返回相同的错误(虽然分离的funcntions pr成功地创建,但当我结合这些函数pr没有创建或创建,但withour review):

$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)
}
uqdfh47h

uqdfh47h1#

由于GDPR,您不能再使用用户名添加审阅者。您必须使用uuid:

"reviewers": [{ "uuid": "{replace_me_with_an_actual_uuid}"}]

要获取uuid,您需要使用用户名调用此端点,并从响应中获取UUID。
我认为最好的选择是首先使用Postman这样的工具来调用update/create url,这会容易得多。然后你可以让Postman生成Powershell代码,然后你可以修改它以匹配你的代码。

相关问题