Search code examples
azureazure-devopsversion-controldevops

Show acceptance criteria in pull requests in Azure Devops


When reviewing PRs in Azure Devops, it would be ideal if the acceptance criteria of the task/story would be displayed in the description (e.g. before the commit messages). This to ensure that all required functionality has been implemented and all edge cases have been taken into account. It seems that you need to manually open the work item to find this additional info.

Can the Acceptance Criteria be displayed automatically in the PR's description?


Solution

  • Can the AC be displayed automatically in the PR's description?

    The short answer is Yes.

    First, I need to state that there is no out of box way to achieve your needs at this moment. In the latest sprint-176-update just released at October 01, MS introduced a new feature, Customize work item state when pull request is merged. But it only for the work item state not for other fields.

    To resolve this request, we could add a Build Validation on the target branch to invoke the REST API Pull Requests - Update to update the description with acceptance criteria of the task/story.

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

    From above REST API URL, we could to know that if we want use the REST API Pull Requests - Update, we need provide the pullRequestId.

    In the predefined variables, there is a variable System.PullRequest.PullRequestId, which we could use it to get the pullRequestId.

    After we get the pullRequestId, we could use another REST API Pull Request Work Items - List to get the work items associated with a pull request:

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

    We could get the workitems Id:

    enter image description here

    Now, we could use the Work Items - Get Work Item to get the value of acceptance criteria:

    enter image description here

    Finally, we could use the REST API Pull Requests - Update with update the description with acceptance criteria of the task/story.

    Below is my test powershell scripts:

    $PullRequestId = $Env:System_PullRequest_PullRequestId
    
    Write-Host "Current PullRequestId is $PullRequestId"
    
    $url = "https://dev.azure.com/<YourOrganizationName>/<YourProjectName>/_apis/git/repositories/<YourRepoId>/pullRequests/$($PullRequestId)/workitems?api-version=6.0"
    $PullRequestWorkItems= Invoke-RestMethod -Uri $url -Headers @{   
     Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
    } -Method Get
    
    $WorkItemId= $PullRequestWorkItems.value.id
    
    Write-Host This is WorkItems Id: $WorkItemId
    
    $Testurl = "https://dev.azure.com/<YourOrganizationName>/<YourProjectName>/_apis/wit/workitems/$($WorkItemId)?api-version=6.0"
    $WorkitemInfo= Invoke-RestMethod -Uri $Testurl -Headers @{   
     Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
    } -Method Get
    
    $AcceptanceCriteria= $WorkitemInfo.fields.'Microsoft.VSTS.Common.AcceptanceCriteria'
    
    Write-Host This is Acceptance Criteria info: $AcceptanceCriteria
    
    
    $UpdatePRurl = "https://dev.azure.com/<YourOrganizationName>/<YourProjectName>/_apis/git/repositories/a<YourRepoId>/pullRequests/$($PullRequestId)?api-version=6.0"
    
    $connectionToken="Your PAT"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    
    
    $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
    
    $body=@"
      {
        "description": "$($AcceptanceCriteria)"
      }
    "@
    
    Write-Host "$url"
    $response= Invoke-RestMethod -Uri $UpdatePRurl -ContentType "application/json" -Body $body -headers @{authorization = "Basic $base64AuthInfo"} -Method PATCH  
    

    This is the test result:

    enter image description here