Recently I needed to automate the creation of Pull requests using Power Automate, but noted that there is no out-of-the-box action for that.
But as usual, I love to dig into the Rest API capabilities of each platform and I figured out how to do it using the “Send an HTTP Request to Azure DevOps” action.
Check out how to do it in this post.
Creating the request
To create the Pull request we simply need to use the Send an HTTP request to Azure DevOps action in Power Automate and send a POST request to the following endpoint:
/{project}/_apis/git/repositories/{repositoryID}/pullrequests?api-version=7.1-preview.1
With the body in the format:
{
"sourceRefName": "refs/heads/<source branch>",
"targetRefName": "refs/heads/<target branch>",
"title": "Pushing changes",
"description": "Adding something new",
"reviewers": [
{
"id": "89d0600d-42e3-4249-8715-e4baf900a6f6",
"isRequired": true
},
{
"id": "f7d55def-08dc-4293-bb44-93ac9bd49d5c",
"isRequired": false
}
]
}
You can specify as many approvers as you want in the approvers array and set them as required or optional. Note that the approver IDs are the DevOps user IDs and not the Entra User IDs.
Sample configured action below:
Results
After a flow with this action is executed, a pull request is successfully created in Azure DevOps:
Note:
To find the DevOps user ID we can use the Entitlements endpoint: User Entitlements – Search User Entitlements
Note this endpoint accepts the name parameter as one of the filters, which filters users based on whether their display name or email contains the specified value. For example, to find all users with ‘michel’ in their email or whose display name contains ‘michel’, use: $filter name eq ‘michel‘
As it is a GET request, if you are logged into DevOps with a user that has the right permissions, you can open the API link in the browser and find the user you need (check the id property).
https://vsaex.dev.azure.com/yourcompany/_apis/userentitlements?api-version=7.1-preview.4&$filter=name eq 'michel'
Even if this endpoint being not supported by the current HTTP action for Azure DevOps in Power Automate, we could leverage the standard HTTP connector to use it if required as part of a Power Automate flow, providing an access token etc (Maybe this can be a subject for another blog post here😊).
Resources
Pull Requests – Create – Microsoft Learn
User Entitlements – Search User Entitlements – Microsoft Learn