在 Azure DevOps 中自动创建从开发到主的拉取请求

问题描述 投票:0回答:1

在 AzureDevOps 中,我们有三个分支:

  • 特点
  • 发展
  • 主要

我们从要开发的功能创建一个拉取请求(PR)。 PR 完成后,我们创建一个从 develop 到 main 的 PR。当开发 PR 的功能完成时,有没有一种方法可以自动创建从 develop 到 main 的 PR?

更新:

我在YAML中添加了一个任务如下:

    - task: PowerShell@2
      displayName: 'PowerShell Script'
      inputs:
        targetType: filePath
        filePath: ./Scripts/CreatePRBuildTask.ps1
        arguments: '-token $(System.AccessToken)'


-- CreatePRBuildTask.ps1

$user = "PrivateToken"
$branchTarget = "refs/heads/main"
$branchSource = "refs/heads/develop"
$branchTragetPath = $branchTarget -replace "refs/heads/", ""
$teamProject = "team"
$repoName = "$(Build.Repository.Name)"
$organization = "org"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
 
$uriBranchStatus = "https://dev.azure.com/$organization/$teamProject/_apis/git/repositories/$repoName/stats/branches?name=$branchTragetPath&api-version=5.1"
$uriCheckActivePR = "https://dev.azure.com/$organization/$teamProject/_apis/git/repositories/$repoName/pullrequests?searchCriteria.targetRefName=$branchTarget&searchCriteria.sourceRefName=$branchSource&api-version=5.1"
$uriCreatePR = "https://dev.azure.com/$organization/$teamProject/_apis/git/repositories/$repoName/pullrequests?api-version=5.1"

$resultStatus = Invoke-RestMethod -Uri $uriBranchStatus -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

if ($resultStatus.behindCount -eq 0)
{
    Write-Host "Current branch contains last changes from master"
    Return
}

$resultActivePR = Invoke-RestMethod -Uri $uriCheckActivePR -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

if ($resultActivePR.count -gt 0) 
{
    Write-Host "PR exists already"
    Return
}

$bodyCreatePR = "{sourceRefName:'$branchSource',targetRefName:'$branchTarget',title:'Sync changes from $branchSource'}"

$result = Invoke-RestMethod -Uri $uriCreatePR -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $bodyCreatePR

Write-Host "Created PR" $result.pullRequestId

看起来它正在出错。我做错了什么?

azure-devops yaml pull-request azure-pipelines-yaml
1个回答
0
投票

开箱即用,不,我们不能。但是您可以使用 REST Api。这是 PowerShell 内联脚本的示例:CreatePRBuildTask.ps1

将此步骤添加到您的开发 CI 并更改:

$branchTarget = "$(Build.SourceBranch)"
$branchSource = "refs/heads/master"
$branchTragetPath = $branchTarget -replace "refs/heads/", ""

致:

$branchTarget = "refs/heads/master"
$branchSource = "refs/heads/develop"
$branchTragetPath = $branchTarget -replace "refs/heads/", ""

此外,在您的仓库中添加项目构建服务权限“Contribute on pull requests”。

© www.soinside.com 2019 - 2024. All rights reserved.