Update Azure DevOps Wiki Pages through PowerShell and Rest Api

Often there is a need to automatically create pages in a Wiki repository, for example:

• To create an iteration report

• Generate a release readme

• And other reporting tasks

Creating a page is quite simple through Rest Api request Pages – Create Or Update. But if you try to refresh the page through the same request, the result is an exception:

{“$id”:”1″,”innerException”:null,”message”:”The page ‘/PageName.md’ specified in the add operation already exists in the wiki. Please specify a new page path.”,”typeName”:”Microsoft.TeamFoundation.Wiki.Server.WikiPageAlreadyExistsException, Microsoft.TeamFoundation.Wiki.Server”,”typeKey”:”WikiPageAlreadyExistsException”,”errorCode”:0,”eventId”:3000}

This is because when updating a request, it is necessary to pass Etag in the request headers. Etag can be obtained through the Rest Api request Pages – Get Page , which returns Etag in the headers. Example:

$restApiUpdateWikiPut = “$orgUrl/$teamProject/_apis/wiki/wikis/$wikiRepoName/pages?path={path}&api-version=6.1-preview.1”

function InvokeGetETag ($GetUrl)

{

$Headers = $null

Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType “application/json” -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo)} -ResponseHeadersVariable ‘Headers’

return $Headers[“ETag”]

}

$updateUrl = $restApiUpdateWikiPut.Replace(“{path}”, $wkPagePath)

$eTagRaw = InvokeGetETag $updateUrl

$ETag = $eTagRaw -join “”

Next, you need to add Etag to the update page request as “If-Match”=”eTag”:

$restApiUpdateWikiPut = “$orgUrl/$teamProject/_apis/wiki/wikis/$wikiRepoName/pages?path={path}&api-version=6.1-preview.1”

function InvokePutRequest ($PutUrl, $body, $eTag)

{

return Invoke-RestMethod -Uri $PutUrl -Method Put -ContentType “application/json” -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo);”If-Match”=”$eTag”} -Body $body

}

InvokePutRequest $updateUrl $wikiContentUpdate $eTag

The example of a PowerShell script for creating and updating wiki pages can be found here::

https://github.com/ashamrai/AzureDevOpsExtensions/blob/master/CustomPSTasks/UpdateWikiPage.ps1

Update the name of an Azure DevOps release with the logging commands

The release name in Azure DevOps has a rather modest format and a set of variables that can be used to form the name, also small: How do I manage the names for new releases? If you need to have a more flexible name, you can use the logging commands. The logging commands allow you to work in a simple way with:

  • Formatted output in builds
  • Setting variable values in build tasks
  • Artifacts
  • Build and release properties

The format is simple, for example, for PowerShell:

Write-Host “##vso[command;parameters]value_to_set_or_message”

Let’s take a PowerShell example of updating the release name with a commit message. Do the following:

  1. Get the commit number from the release environment.
  2. Get detailed commit information through Rest Api.
  3. Update the release name using the logger command.

The commit number can be obtained from the environment variable $env: BUILD_SOURCEVERSION. Then you can use it to get the commit information through Rest Api request Commits – Get, which will also require the following environment variables:

  1. Url of Azure DevOps organization: $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
  2. Project Name: $ env:SYSTEM_TEAMPROJECT
  3. Repository Name: $ env:BUILD_REPOSITORY_NAME

Next, you need to get commit information:

$user=“”

$token=$env:SYSTEM_ACCESSTOKEN

$base64AuthInfo=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((“{0}:{1}” -f $user,$token)))

$orgUrl=$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI

$teamProject=$env:SYSTEM_TEAMPROJECT

$commitId=$env:BUILD_SOURCEVERSION

$repoName=$env:BUILD_REPOSITORY_NAME

$restApiGetCommit=$orgUrl/$teamProject/_apis/git/repositories/$repoName/commits/$commitId`?api-version=6.0″

function InvokeGetRequest ($GetUrl)

{

return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType “application/json” -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo)}

}

$resCommit = InvokeGetRequest $restApiGetCommit

From the query result, we need the comment property. However, it can contain the newline characters. You can split the commit message through “‘n” to get only the first string. Next, you can set the new name for the new release using the release.updatereleasename logging command:

$commitArray=$resCommit.comment -split ‘\n’

if (-not [string]::IsNullOrEmpty($commitArray[0]))

{

Write-Host “##vso[release.updatereleasename]”$commitArray[0]

}

You can add this PowerShell script as a task in a release definition:

In this case, you need to allow access to the agent token.

The example of execution results:

The example of PowerShell script you can find here: https://github.com/ashamrai/AzureDevOpsExtensions/blob/master/CustomPSTasks/UpdateReleaseName.ps1