Currently playing with Kubernetes and Azure DevOps for my side project, I wanted to be able to write a comment during the build process, to allow me to post some “important” informations.

Fortunately, Azure DevOps is “just” a set of REST APIs so after looking for the information on Internet, I’ve been able to find the one I was looking for:

Using this endpoint, it’s quite easy to write a comment for your pull request and, thanks to Powershell, this can be done in a more easier way:

- task: PowerShell@2
  displayName: "Write Pull Request Comment"
    targetType: "inline"
    script: |                      
      Write-Host "Write Pull Request comment"

      $organization = "thomaslebrun"
      $project = "MyProject"
      $repositoryId = "XXXX-XXXXX-XXXXX-XXXXX"
      $pullRequestId = $(System.PullRequest.PullRequestId)

      $pullRequestThreadUrl = "$organization/$project/_apis/git/repositories/$repositoryId/pullRequests/$pullRequestId/threads?api-version=5.1"

      # Create a new comment (set as Closed)
      $comment = @{
           comments = @(
                   content="TEST TLE FROM BUILD: $organization"
              status = 4

         $json = @($comment) | ConvertTo-Json
         $commentResult = Invoke-RestMethod -Uri $pullRequestThreadUrl -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $(system_accesstoken)"}

As you can see, the API is expecting the organization, the project name and the repositoryId (found in the URL of the repository settings).

Also, as we are in a build process, we are able to access to the AccessToken of the user (needed to be authenticated for calling the APIs)
Once the call is done, the result can be see directly:

Of course, it’s possible to change the status according to your needs (by default, value is 1 which means the comment will be open and should be Resolved)

Happy coding! 🙂

Streaming Kubernetes' logs using SignalR Set a colon in an environment variable for Kubernetes

Leave a Reply

Your email address will not be published. Required fields are marked *