7

I have a classic env. setup like following:

I have 2 branches: Develop and Master.

Is there any way in Azure DevOps to setup the following rule:

  1. When a deploy is succeeded on dev environment (defined in the release pipeline of azure devops) ------> create automatically a pull request to merge develop into Master.

  2. or the other one: if a Build of develop branch is succeded -------> create automatically a pull request to merge develop into Master.

Any help will be appreciated.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Haithem KAROUI
  • 1,533
  • 4
  • 18
  • 39

3 Answers3

12

Edit:

I just uploaded an extension that does it: https://marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest


You can use Azure DevOps Rest API to create a Pull Request, so in the end of the Build / Release add a PowerShell task that do it, for example:

$body =  @{
             sourceRefName= "$(Build.SourceBranch)"
             targetRefName = "refs/heads/master"
             title = "PR from Pipeline"
     }

$head = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"  }
$json = ConvertTo-Json $body
$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.Name)/pullrequests?api-version=5.0"
Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $json -ContentType application/json

Photo3

You need to Allow scripts to access the OAuth token (check the checbox in the Agent Job options):

Photo1

Results:

enter image description here

I put the basic parameters in the body (from branch, to branch, title) but you can add more parameters like reviewers, check the docs here.

Community
  • 1
  • 1
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • If you receive the "You need the Git 'PullRequestContribute' permission to perform this action" error upon running the script in a release pipeline, who do you grant these permissions to? – Dicky Moore Sep 12 '19 at 13:51
  • 1
    @DickyMoore To the build service account, did you get the error even you enable the "Allow scripts to access....."? – Shayki Abramczyk Sep 12 '19 at 13:53
  • 1
    As mentioned in the documentation, you can add a reviewer with the following: `"reviewers": [{"id": "d6245f20-2af8-44f4-9451-8107cb2767db"}` How can I get the Id of the user I want to add as reviewer?? – Mohamad Mousheimish Aug 12 '20 at 07:22
  • @MohamadMousheimish you can check the code of my extension that mentions above, there are different ways, check there. – Shayki Abramczyk Aug 12 '20 at 08:44
  • Receiving this error with @ShaykiAbramczyk extension > "message":"TF401179: An active pull request for the source and target branch already exists." – MANZARBEITIA Oct 03 '21 at 16:16
  • @MANZARBEITIA it means that there is already PR open... – Shayki Abramczyk Oct 03 '21 at 20:23
  • @ShaykiAbramczyk this issue only happened using YAML, when I used the classic editor it worked as expected. – MANZARBEITIA Oct 04 '21 at 05:55
  • @MANZARBEITIA and the settings are the same? please open an issue in my GitHub https://github.com/shayki5/azure-devops-create-pr-task/issues – Shayki Abramczyk Oct 04 '21 at 06:31
0
  1. there is no build-in tasks for that, but you can script this yourself using the oauth token, or using your own auth to issue a request against the api.
  2. pretty much the same approach can be used here, or you can use Branch policies to force pull requests to be validated before merging them to master (which in my mind is better, because merging from develop to master on every commit is pretty pointless).
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • Thank you for your answer, can please provide more details about this : "you can script this yourself using the oauth token, or using your own auth to issue a request against the api." thanks – Haithem KAROUI May 17 '19 at 10:13
  • 1
    I've been doing this with github, you just need to call the api for merge: https://developer.github.com/v3/repos/merging/ – 4c74356b41 May 17 '19 at 10:30
0

Using python and the devops rest api people already mentioned you could do something like this.

# Tested in python 3.10
# pip install requests
import base64
import requests

# Fill the following variables with real values
personal_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'  # https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&viewFallbackFrom=vsts&tabs=Windows
organization = "myorganization"
project_id = "00000000-0000-0000-0000-000000000000"
repository_id = "00000000-0000-0000-0000-000000000000"

authorization = str(base64.b64encode(bytes(f":{personal_access_token}", "ascii")), "ascii")
headers = {"Content-type": "application/json", "Authorization": f"Basic {authorization}"}

prs_url = f"https://dev.azure.com/{organization}/{project_id}/_apis/git/repositories/{repository_id}/pullrequests?api-version=5.1"

# create PR
response = requests.post(
    f"{prs_url}",
    headers=headers,
    data=json.dumps({
        "sourceRefName": "refs/heads/release",
        "targetRefName": "refs/heads/master",
        "title": "release to master",
    }),
)
Cesc
  • 904
  • 1
  • 9
  • 17