5

Is it possible to get PR title and PR description in pipeline?

The idea is to notify team which task has been deployed. Allfeature need to be tested go to branch "development" I tried intergrate Ms Teams, subscribe pipeline, but none of the information is useful.

Build pipeline gave me branch "development" which is not helpful (not sure what was merged into "development") and who was requested the build

Deploy pipeline gave absolutely nothing useful.

Update:

The Picture below is result of querying Builds - Get Api. Trigger for a PR was completed. There is no infomation about the PR. enter image description here

Phuc Vuong
  • 183
  • 2
  • 12

2 Answers2

5

If your build is based on pull request commit , the sourceVersion will be displayed next to the source branch in the build summary page.

Click on this sourceVersion, you will jump to the detailed page containing pr title and description, in this page you can also see the merged changes.

enter image description here

enter image description here

Update:

or a way to query PR title in build or release process

How about getting pr title through rest api? We can first use Builds-Get rest api to get the pull request id in the specified build.

GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1

enter image description here

enter image description here

Then through the obtained pull request id, we can use Get Pull Request rest api to get the pull request title.

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=5.1

enter image description here

The above can be achieved by adding a powershell task(call rest api through script) in the build or release process.

Update2:

The difference between PR-triggered build and CI-triggered build:

enter image description here

If the build is triggered by pr, the trigger reason should be pullRequest. Below is my test in Postman:

enter image description here

Hugh Lin
  • 17,829
  • 2
  • 21
  • 25
  • Thanks for your response. I mean as variable in pipelines , or a way to query PR title in build or release process. When release was done, it oftens notify that "Release-x was deployed" (Intergration with MS Teams). But that notification is really little as no one in QC team knows that which task has been deployed. – Phuc Vuong Apr 20 '20 at 11:23
  • How about getting pr title through rest api? Please view my update. – Hugh Lin Apr 21 '20 at 10:27
  • Sorry for not giving enough info. I need the PR title when the build is trigger for PR was completed (i updated in the question). I guess the the reason why its impossible is that no way the build can tell which action has change the branch (a PR or a direct push into it), it just know branch is changed so it trigger builds base on that. Still, thanks a lot. I'm new to Azure Devops. Still has much to explore – Phuc Vuong Apr 21 '20 at 12:36
  • PR-triggered build and CI-triggered build are different.This will be marked under build. If this build is triggered by pr, then the response obtained through the rest api should contain pr info, the `reason` should be `pullRequest`. Please check whether the Build you query is a pr triggered build. For these, please view my update2. – Hugh Lin Apr 22 '20 at 09:06
  • Yes. But i couldnt do PR-triggered build, only CI-triggered build => all my test feature go into 1 branch, not seperate dev env for each task. This is where i'm kind of stuck. Cant query PR if its a Ci-triggered. I think maybe i query the lastest PR in the repository. it's a hack but there is no other solution. Thanks for your help. – Phuc Vuong Apr 22 '20 at 17:31
0

Based on Hugh Lin's answer, I made a simplified and working implementation:

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |

          # Call the Azure DevOps Services Rest API.
          $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$(Build.Repository.ID)/pullRequests/$(System.PullRequest.PullRequestId)?api-version=7.0"
          $headers = @{
            Authorization = "Bearer $(System.AccessToken)"
          }
          $pullRequestInfo = Invoke-RestMethod -Uri $url -Method 'GET' -ContentType 'application/json' -Headers $headers

          # Get PR title and description from the json response.
          $json = $($pullRequestInfo | ConvertTo-Json -Depth 100 | ConvertFrom-Json)
          $description = $json.description
          $title = $json.title

No need to make the first api call, as the pull request id is already stored in $(System.PullRequest.PullRequestId).