14

The documentation says that the TargetRefName can be updated when "PR retargeting feature is enabled" but I can't find this option anywhere and can't see any documentation about how to enable it either.

How do I enable PR retargeting so that I can update the PR's target?

https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/update?view=azure-devops-rest-5.1

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Gibb Sonn
  • 399
  • 4
  • 12

2 Answers2

21

see this image

Click on the more action button(...) and you will see "Change target branch"

Joundill
  • 6,828
  • 12
  • 36
  • 50
pedram
  • 705
  • 7
  • 6
1

I succeded to update the target branch with the API you provided with the following body:

{"targetRefName":"refs/heads/test"}

In PowerShell:

$pat = "YOUR-PAT"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,"$pat")))
$headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$url = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=5.1"

$json = '{"targetRefName":"refs/heads/master"}'

$response = Invoke-RestMethod -Uri $url -Method PATCH -ContentType application/json -Headers $headers -Body $json

It look like each PR could be re-targeted, I don't see an option enable/disable the feature.

By the way, I succeeded only in the above JSON, if you will try to get the PR and change the targetRefName you will get an error.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114