azureazure-devopscontinuous-integrationpipelineazure-devops-extensions

Azure Devops Custom Task with status checks


I am in the process of creating a custom pipeline task for Azure DevOps (e.g. Like PublishPipelineArtifact@1) by following these steps https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops and I am wanting to publish a successful/ failed pull request status (one of these) enter image description here

based on the outcome of my task but I am struggling to find the library to create/ set these. I believe it would effectively be calling this API: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-request-statuses?view=azure-devops-rest-7.1

If anyone is aware of the correct method in the npm package provided by Microsoft azure-pipelines-task-lib/task or another solution that would be appreciated!

I have read through the examples provided by Microsoft to attempt to look into tasks that publish their own statuses however I haven't been able to pinpoint anything.


Solution

  • Short answer - I don't think the task lib handles this directly.

    I believe what you're looking for is the ADO Node API - that will provide the abstraction layer over the REST API in the docs you linked. More specifically, you're likely going to want to use the createPullRequestStatus method here

    For the sake of example:

    Referencing the docs you sent above - the GET request to check task status on pipelines is formed this way:

    https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/statuses

    The URL above will take you to the published tasks in your pipeline - I can also see that the codecoverage task is included in the status policies on the repository.

    taskjson ADOtask

    Let's throw some Powershell together real quick to verify this

    $rawPAT = "PersonalAccessTokenFromAzureDevOps"
    $pat = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($rawPAT)"));
    
    $headers = {};
    $headers.Add('Authorization', "BASIC $pat")
    $headers.Add('Content-Type', 'application/json')
    
    
    Invoke-WebRequest -Uri https://dev.azure.com/{organizationId}/{projectId}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/statuses?api-version=7.1-preview.1 -Method 'POST' -Body "{ 'context': { 'name': 'test-status'} }" -Headers $headers
    

    Here is my updated status

    UpdateStatusInAdo

    and the PR policy

    PRStatusCheckAvailable