Avina’s blog
Published in

Avina’s blog

Automation using available APIs — Azure DevOps

========================================

There are a lot of tools available in the market to automate DevOps practices in the software development lifecycle, like Jenkins, Azure DevOps, et. al.

In this article, we focus on Azure DevOps and how to make the most of automation.

Azure DevOps does a decent job in providing features ranging from managing sprints, work items, etc., to creating CI/CD pipelines and the documentation provided is also useful.

There are APIs provided which can help us in further automation based on our specific scenarios. One such specific user scenario I came across was user wanting to create a Pull request once their Release pipeline completes. I found customer scenario valid as, to fully automate deployments, one might need to create a Pull Request not beforehand but only after the release has completed. Let us take this as an example, understand how we can achieve this task and then apply the learning to any other automation scenarios you may encounter.

We require the three things listed below to achieve this -

  1. A bash/command line task- https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/bash?view=azure-devops or https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/command-line?view=azure-devops&tabs=yaml
  2. Azure DevOps API to create Pull requesthttps://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull requests/create?view=azure-devops-rest-5.1
  3. API to retrieve Git repositorieshttps://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/list?view=azure-devops-rest-5.1

First, we need to add a Bash/Command line task as the last task at the end of the pipeline and then would provide the command like below -

========================================

curl -u “User:PAT” -H “Content-Type: application/json” -d “{“sourceRefName”: “refs/heads/test”,”targetRefName”:”refs/heads/master”,”title”: “A new feature”,”description”: “Adding a new feature”}” https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?api-version=5.1

========================================

Let’s understand all the inputs we provided to the script -

• We should create a PAT token for authentication and provide it in place of PAT in the script. We can do so as mentioned here — https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=current-page#create-personal-access-tokens-to-authenticate-access .Make sure to copy the PAT and save it somewhere safe as it is not stored on Azure DevOps and you will not be able to retrieve it again.

• The command above passes the following inputs to the request body of the Create Pull request API -

  • sourceRefname (The name of the source branch of the pull request),
  • targetRefName (The name of the target branch of the pull request),
  • title (The title of the pull request),
  • description (The description of the pull request)

You can add other inputs to the request body based on your requirements as per — https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull requests/create?view=azure-devops-rest-5.1#request-body

• For the API, you will then have to provide your Azure DevOps organization name, project name where you have the repository for the branches of which you want to create a Pull Request and the repository id for the same repository.

You can find out the repository id by running the below API from the browser -https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.1

After this, you should see an active Pull Request from your source branch to destination as below —

PR from test to master

=======================================

Similarly, you can automate Pull Request creation or achieve any other automation via APIs. You can refer to the below document for the list of available Azure DevOps APIs —

REST API Browser | Microsoft Docs

========================================

I hope you find this article helpful! 😃

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store