~fhusson

How to make a TFS Git pull request without the UI

With TFS 2017, we can use the command line VSTS CLI

With TFS 2015, we can use the REST API

Here is an example :

$repository = "thereponame"
$sourceBranch = "feature/helloworld"
$targetBranch = "develop"
$reviewerId = "18ff8b6a-6806-4580-b263-4267cd79f86f"
$teamProject = "myteamproject"
$collection = "mycollection"
$tfsBaseUrl = "http://myserver"
$apiVersion = "3.0-preview.1"

[uri] $url = "$tfsBaseUrl/$collection/$teamProject/_apis/git/repositories/$repository/pullRequests?api-version=$apiVersion"

# Prepend refs/heads/ to branches so shortened version can be used in title
$sourceRefName = "refs/heads/$sourceBranch"
$targetRefName = "refs/heads/$targetBranch"

# JSON for creating PR without Reviewer specified
$body= @"
{
    "sourceRefName": "$sourceRefName",
    "targetRefName": "$targetRefName",
    "Title": "Merge $sourceBranch to $targetBranch",
    "Description": "PR created from powershell via REST API",
    "Reviewers":[{"id":"$reviewerId"}]
}
"@

# Use URI and JSON above to invoke the REST call and capture the response.
$response = Invoke-RestMethod -Uri $url `
                              -UseDefaultCredentials `
                              -Method Post `
                              -ContentType "application/json" `
                              -Body $body  

Write-Host "PR Id  : " + $response.pullRequestId
Write-Host "PR Url : " + $response.url
Discuss on Twitter