Problem
Typing, typing, typing – and clicking. Azure DevOps sometimes requires lots of typing and mousing around to get some things done.1 Creating PRs can be tedious when done manually in Azure DevOps. This is especially painful when you’re doing a mass change to twenty microservice repos. Using the VSTeam PowerShell Module, I can easily create ready-to-review PRs with a few lines of code. Depending on your configuration, you still may have some clicking to do, but much less.
DO NOT Google VSteam. There are NSFW hits for things I never knew existed.
Solution
The VSTeam module is a great module if you are using Azure DevOps. I leverage it in many helper scripts for automating Azure DevOps, but my favorite one is New-AzDoPR
(a.k.a New-PR
). It is available here
# absolute minimal call
New-PR
This will create a new PR from the current branch to another (defaulting to main
), set the title to the last commit message, and launch the browser for the PR’s page in AzDO.
If you have default approvers set up, that will be already filled in, and you may have to pick an associated work item if that is required. And you can even click AutoComplete
if your process allows it.
To make AzDO API calls with VSTeam, you will have to call Set-VSTeamAccount
to set credentials as a Personal Access Token (PAT). I call that in my $Profile
so it’s in all my sessions. To get a PAT click on User Settings
in upper right of your AzDO page, then Personal Access Tokens
. For creating PRs, I created a PAT with Code (read and write); Package (read)
permissions.
# do this once per session, or in your $Profile
Set-VSTeamAccount -Account $MyAzDoAccountName -PersonalAccessToken $MyAzDoPAT
New-PR
takes the following parameters.
Name | Description | Default Value |
---|---|---|
Directory | Directory of source code. Value can be piped in. | $PWD |
Title | PR Title | Last commit message |
Description | PR Description | Title |
ToBranch | Branch to merge current one into | main |
AzDoProject | Project to use | Parsed from remote url |
RepoName | Repository name | Parsed from remote url |
SuppressBrowser | Set to not launch the browser | False |
If you have several folders on branches that need PRs and the repo can be parsed from the remote, you can pipe them into the function.
"Folder1","Folder2" | New-PR
By combining this with ForEach-Git helper, which I blogged about earlier, you can quickly update repos and create a bunch of PRs.
# check out a branch and run a script to edit something
ForEach-Git -ShowFolder { git checkout main && git pull && git checkout -B myBranch && editCodeFunction }
# you may want to validate things before creating PRs
ForEach-Git -ShowFolder { git commit -am 'some cool change' && git push && New-PR }
Now you can create hundreds of PRs with one simple command. Your teammates are gonna luv this.
-
That’s why I love Yaml pipelines for deployment since I can automate changes to them. ↩