Creating Visual Studio Online Pull Requests from Powershell

Just a quick one today.

Problem

You do all your git-fu from powershell but then you're ready to create your PR and you have to switch over to the (often very slow) Visual Studio Online web portal in order to create your Pull Request.

Solution

Simply add this function to your powershell profile (If you need to know how to do this):

$domain = "https://yourcompany.visualstudio.com";
$project = "YourProject"
$vstsUrl = "$($domain)/$($project)/_git/";
$masterBranch = "master";
$developmentBranch = "development";

function pr($target){  
    $targetBranch = $NULL;
    if ($target.ToLower() -eq "master" -OR $target.ToLower() -eq "m"){
        $targetBranch = $masterBranch;
    }
    elseif ($target.ToLower() -eq "dev" -OR $target.ToLower() -eq "d"){
        $targetBranch = $developmentBranch;
    }   

    $r = git remote -v 
    $repoName = ([regex]'(?<=git/)([A-Z]|[a-z]|[0-9]|_|-|\.)*').Match($r).Value
    $currentBranch = git rev-parse --abbrev-ref HEAD
    $fullUrl = "$($vstsUrl)$($repoName)/pullrequestcreate?sourceRef=$($currentBranch)&targetRef=$($targetBranch)"; 
    Start-Process "chrome.exe" $fullUrl 
}
Usage
  • Set $domain, $project, $masterBranch, $developmentBranch to values appropriate for you
  • Open an admin powershell prompt
  • Ensure you are in any directory of your git repo.
  • Issue a PR to master: pr m
  • Or Issue a PR to development: pr d
What is my Project/Domain

Easiest way to find out is to simply create a PR manually and look at what the url is. Once setup, you will not need to do this again.

Sam Shiles

Read more posts by this author.