Skip to content

Instantly share code, notes, and snippets.

@Seekatar
Created January 25, 2022 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seekatar/f541fb7d4e603fd9b648c1e18cff8a4b to your computer and use it in GitHub Desktop.
Save Seekatar/f541fb7d4e603fd9b648c1e18cff8a4b to your computer and use it in GitHub Desktop.
PowerShell script for creating a new PR in Azure DevOps
<#
.SYNOPSIS
Create a new PR
.DESCRIPTION
Long description
.PARAMETER Directory
Source directory of the PR, defaults to current directory
.PARAMETER Title
Title for PR. Will pull from last commit comment
.PARAMETER Description
Title for PR. Will pull from last commit comment
.PARAMETER ToBranch
To branch for PR, defaults to main
.PARAMETER AzDoProject
Azure DevOps project. Defaults to pulling from remote url
.PARAMETER RepoName
Azure DevOps repo name. Defaults to pulling from remote url
.PARAMETER SuppressBrowser
Do not launch the browser to the PR page.
.EXAMPLE
New-AzDoPR
Create a new PR with the defaults
.EXAMPLE
New-AzDoPR -AzDoProject $MyProject -RepoName $MyRepoName
Create a new PR for the specified project and repo.
#>
function New-AzDoPR {
[Cmdletbinding(SupportsShouldProcess)]
param(
[Parameter(ValueFromPipeline)]
[string] $Directory = $PWD,
[string] $Title,
[string] $Description,
[string] $ToBranch = 'main',
[string] $AzDoProject,
[string] $RepoName,
[switch] $SuppressBrowser
)
begin {
if (!(Get-Command Get-VSTeamGitRepository)) {
throw "VSTeam is not installed. Install it using Install-Package VSTeam"
}
if (!(Get-Command git)) {
throw "git is not installed."
}
}
process {
Push-Location $Directory
try {
if (!$AzDoProject -and !$RepoName) {
try {
if ((git remote -v )[0] -match 'dev\.azure\.com/[^/]*/(?<project>[^/]+)/.*/(?<repo>\S*)') {
$AzDoProject = $matches.project
$RepoName = $matches.repo
} else {
throw "Couldn't parse Project and Repo from git remote"
}
} catch {
}
}
if (!$AzDoProject -or !$RepoName) {
throw 'Must pass in AzDoProject and RepoName parameters'
}
$repo = Get-VSTeamGitRepository -ProjectName $AzDoProject -Name $RepoName
$fromBranch = git branch --show-current --no-color
if (!$Title) {
$t = git log --pretty=oneline -n 1 --no-color
if ($t -match "\s(.*)$") {
$Title = $matches[1]
}
}
if (!$Title) {
throw "Must supply title -- or have on in last commit"
}
if ($repo) {
if ($PSCmdlet.ShouldProcess("$fromBranch->$ToBranch on Project $RepoName", "Create a PR")) {
$pr = Add-VSTeamPullRequest -ProjectName $AzDoProject `
-RepositoryId $repo.ID `
-SourceRefName "refs/heads/$fromBranch" `
-TargetRefName "refs/heads/$ToBranch" `
-Title $Title `
-Description ($Description ? $Description : $Title)
if ($pr -and $pr.url) {
$uri = ($pr.url -replace "_apis/git/repositories", "_git") -replace '/pullRequests/', '/pullrequest/'
if ($SuppressBrowser) {
"Link to UI:"
$uri
} else {
Start-Process $uri
}
}
}
} else {
Write-Warning "No repo $($RepoName)"
}
} catch {
Write-Error "Error!`n$_`n$($_.ScriptStackTrace)"
} finally {
Pop-Location
}
}
}
if (!(Test-Path alias:New-PR)) {
New-Alias -Name New-PR -Value New-AzDoPR
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment