の自動補完プル要求を作成する


以来、自動的にCI/CDパイプラインを使用してプル要求を作成し、プルリクエストの自動補完を設定するすべての必要なポリシーが満たされていることを考えた?時々、これは1つのブランチを他のブランチと同期させておくのに便利です、あるいは、あなたはどんな変更も自動的に1つの枝にされるどんなものでも引っ張りたいです.
Azure Devopsはプログラム的にdevopsサービスと対話するためにREST APIsのセットを提供します.しかし、私たちは自動完了を有効にするとPRを作成することができます単一のAPIはありません.それで、我々はこれを達成するために2つのAPI呼び出しをしなければなりません.プル要求作成のためのCreate PRおよび自動補完にプル要求をセットするUpdate PR
我々は残りのAPIを多くの方法で呼び出すことができます.私はRSET呼び出しとPowerShellパイプラインタスクをAzure devops CI/CDパイプラインと統合するためにPowerShellを使用しています.以下は私が作成したサンプルパイプラインとPowerShellパイプラインタスクで使用したPowerShellスクリプトのスクリーンショットです.


パワーシェルスクリプト
# construct base URLs
$apisUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)/$($env:SYSTEM_TEAMPROJECT)/_apis"
$projectUrl = "$apisUrl/git/repositories/<Your Repository Name>"

# create common headers
$headers = @{}
$headers.Add("Authorization", "Bearer $env:SYSTEM_ACCESSTOKEN")
$headers.Add("Content-Type", "application/json")

# Create a Pull Request
$pullRequestUrl = "$projectUrl/pullrequests?api-version=5.1"
$pullRequest = @{
        "sourceRefName" = "refs/heads/develop"
        "targetRefName" = "refs/heads/master"
        "title" = "Pull from develop to master"
        "description" = ""
    }

$pullRequestJson = ($pullRequest | ConvertTo-Json -Depth 5)

Write-Output "Sending a REST call to create a new pull request from develop to master"

# REST call to create a Pull Request
$pullRequestResult = Invoke-RestMethod -Method POST -Headers $headers -Body $pullRequestJson -Uri $pullRequestUrl;
$pullRequestId = $pullRequestResult.pullRequestId

Write-Output "Pull request created. Pull Request Id: $pullRequestId"

# Set PR to auto-complete
$setAutoComplete = @{
    "autoCompleteSetBy" = @{
        "id" = $pullRequestResult.createdBy.id
    }
    "completionOptions" = @{       
        "deleteSourceBranch" = $False
        "bypassPolicy" = $False
    }
}

$setAutoCompleteJson = ($setAutoComplete | ConvertTo-Json -Depth 5)

Write-Output "Sending a REST call to set auto-complete on the newly created pull request"

# REST call to set auto-complete on Pull Request
$pullRequestUpdateUrl = ($projectUrl + '/pullRequests/' + $pullRequestId + '?api-version=5.1')

$setAutoCompleteResult = Invoke-RestMethod -Method PATCH -Headers $headers -Body $setAutoCompleteJson -Uri $pullRequestUpdateUrl

Write-Output "Pull request set to auto-complete"
さて、トリガータブの継続的な統合をオンにします.パイプラインは自動的に有効になったプル要求を作成します.