DEV Community

Lev Eidelman Nagar
Lev Eidelman Nagar

Posted on

Open a Pull Request In Azure Devops From Powershell

I'm naturally lazy and I like to be able to do things from the terminal as much as possible, so I came up with a small Powershell function to open a new pull requests in Azure Devops.
Feel free to adopt and adapt this to your own needs.

The code

Function Open-PullRequest([string]$targetRef="develop") {
  if (-not (Get-Command git)) {
    Write-Host 'Could not find git!' -ForegroundColor Red
    return;
  }

  $branch = git rev-parse --abbrev-ref HEAD --
  $remote = git remote get-url origin

  # This repo uses SSH
  if ($remote.startsWith('git@ssh.'))
  {
    $remote = $remote.replace('git@ssh.dev.azure.com:v3/', '')
    $remote = $remote.split('/')
    $remote = "$($remote[0])/$($remote[1])/_git/$($remote[2])"
    $url = "https://dev.azure.com/$remote/pullrequestcreate?sourceRef=$branch&targetRef=$targetRef"

    # Open default browser
    Start-Process $url
  }
  # This repo uses HTTPS
  else {
    $remote = $remote.split('@')
    $remote = $remote[1]
    $url = "https://$remote/pullrequestcreate?sourceRef=$branch&targetRef=$targetRef"

    # Open default browser
    Start-Process $url
  }
}
Enter fullscreen mode Exit fullscreen mode

Installation

Copy this function into your Powershell profile.

Usage

Open-PullRequest # Will open a new pull request from your current branch to develop
Open-PullRequest -targetRef REMOTE_BRANCH # Will open a new pull request from your current branch to whatever branch you choose
Enter fullscreen mode Exit fullscreen mode

New Pull Request Page

Caveat

I've only tested this with Powershell Core 6.2.4 on windows 10

Top comments (1)

Collapse
 
gersondias profile image
Gerson Dias

tks, this helps me to create a function to open the PR page from the terminal, really handy! (just have to use

az repos pr list -s $branch

to figure out the current id to build the url)