Today I got a question: how can we precreate 50 git repositories in an Azure DevOps Project?
I started to learn about the API in Azure DevOps and I found it very interesting.
With the API you can script and automate administration of projects, repositores, pull requests etc.
To keep this blog post simple and digestable I would like to focus on the intitial request - creating git repositories by code.
First, a few important general links on this topic:
In this blog post I use PowerShell, but it’s your choice, of course. Without further adue let’s dive in.
Step 1. Authentication
To call the API, you need a Personal Access Token (PAT). Create it in the web browser. Clck on the icon which has a “user” and a cogwheel, just left to your profile picture.
In the menu click on “Personal access tokens”. Click on “+ New Token”. For the sake of simplicity choose “Full access” in the Scopes
(I removed when I was done creating git repos, if you want to use the same PAT, choose the scopes wisely).
In the name field type “apitest” or whatever name you want, make sure you remember it. Then click on “Create” and you’re done.
Step 2. Prepare a PowerShell script
Open Visual Studio Code and create a new PowerShell file.
Use your newly created name and personal access token to create the Authorization Header that will be used in the next step.
Remember the “apitest”, the username of this PAT, make sure you spell exactly the same way as you did when you created it. Separate the username and the pat by a colon and create a base64 encoded token (
Step 3. Call the API to get the project id
Let’s try to call the Azure DevOps API and get the project id, we’ll use it as an input while creating git repositories.
Step 4. Last step. Create git repositories
Now we’re about to call the api to create several git repositories.
I create an array of repo names on line 4 and for every name I compose a request body (line 6 to 13) and then make a POST request.
As a response I get 201 (Created):
This is my first test with the Azure DevOps APIs, I am definitely taking it in my developer toolbox.