Create Azure DevOps Wiki Pages through Azure CLI

780 Views Asked by At

I am using these commands to create a test Azure Wiki page.

#Azure DevOps Login < Works OK
echo '$env:ado_documentation_api_key' | az devops login --org https://dev.azure.com/***

#Create Code Wiki < Works OK
$Wiki_id = az devops wiki create --name test --type codewiki --version master --repository Azure.Agents --mapped-path /Documentation --query id -o tsv

#Create Wiki Page < Doesn't work
az devops wiki page create --path 'Documentation' --wiki $Wiki_id --file-path ./wiki.md --encoding utf-8

Expected behaviour:

To upload a new wiki page in Azure DevOps.

Actual behaviour:

The versionType should be 'branch' and version cannot not be null
Parameter name: versionDescriptor

Any assistance to resolve this issue, would be appreciated.

2

There are 2 best solutions below

1
On BEST ANSWER

Reproduce the same issue when use the same Rest API to create Wiki Page.

enter image description here

The cause of the issue is from the Azure DevOps CLI extension itself.

When you add the --debug argument in Azure DevOps CLI, you will see that the az devops wiki page create command is using the Rest API version 5.0.

enter image description here

Refer to this doc about Pages - Create Or Update Version 5.0 It doesn't support the parameters: versionType.

This parameter is available after Rest API version 5.1.

The root cause of the issue is that Azure DevOps CLI requires versionType parameter, but Azure DevOps CLI is using old version of Rest API which doesn't support the version type argument.

This is a known issue with Azure DevOps CLI creating wiki pages. Refer to this Feedback ticket: Create wiki page using CLI extension fails with error "The versionType should be 'branch' and version cannot not be null"

For a workaround, you can use Rest API to create the wiki page: Pages - Create Or Update

Here is a PowerShell example:

$content = [IO.File]::ReadAllText("wiki\file.md")
$data= @{content=$content;} | ConvertTo-Json;
$OrganizationName = "organizationName"
$ProjectName = "ProjectName"
$WikiName = "WikiName"
$WikiPath = "MainPage"
$PAT="PAT Token"

$uri = "https://dev.azure.com/$OrganizationName/$ProjectName/_apis/wiki/wikis/$WikiName/pages?path=$WikiPath/$WikiSubPagePath&api-version=6.0"

$Header = @{
    'Authorization' = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
}

$params = @{
    Uri = $uri;
    Headers    = $Header;
    Method     = 'Put';
    ContentType = "application/json";
    body = $data;
}

Invoke-RestMethod @params
2
On

How to Create & Update a wiki page on Azure Devops when Az devops cli is not working on a Wiki based on a Repo 'The versionType should be 'branch' and version cannot not be null'

# Prepare Header for DevOps API Call
[hashtable]$header = $null
# Detect if we're running on an devops agent or not
if (($null -eq $runningOnAgent ) -or ($runningOnAgent -eq $false)) {
  Write-Output "You're running on an Dev Pc"

  if ($null -eq $pat) {
    Write-Error "PAT is empty" -ErrorAction Stop
    exit 1
  }

  $B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("`:$pat"))
  $header = @{authorization = "Basic $B64Pat" }
  $env:AZURE_DEVOPS_EXT_PAT = $pat

}
else {
  Write-Output "You're running on an Azure DevOps Agent"
  $token = $env:AZURE_DEVOPS_EXT_PAT
  $header = @{authorization = "Bearer $token" }
}


# Create a new page 

$content = [IO.File]::ReadAllText($filenameIndexStatusDestination)
$data = @{content = $content; } | ConvertTo-Json;
$uri = "https://dev.azure.com/$ORG_NAME/$PROJECT_NAME/_apis/wiki/wikis/$WIKI_NAME/pages?path=$WIKI_PAGE_NAME&versionDescriptor.version=main&comment=comment&api-version=7.1-preview.1"

$params = @{
  Uri         = $uri;
  Headers     = $header;
  Method      = 'Put';
  ContentType = "application/json";
  body        = $data;
}

Invoke-RestMethod @params


# Update Wiki

$currentPage = az devops wiki page show --detect true --path $WIKI_PAGE_NAME --wiki $WIKI_NAME --include-content | ConvertFrom-Json
Write-Output "display etag value : $($currentPage.eTag)"

#Force to a change in the page 
$date = Get-Date
$data = $data.Replace("Execution", "Execution [$date]")

$headerWithVersion = $header.Clone()
$headerWithVersion.Add("If-Match", $currentPage.eTag)

$uri = "https://dev.azure.com/$ORG_NAME/$PROJECT_NAME/_apis/wiki/wikis/$WIKI_NAME/pages/$($currentPage.page.id)?comment=comment&api-version=7.1-preview.1"

$params = @{
  Uri         = $uri;
  Headers     = $headerWithVersion;
  Method      = 'Patch';
  ContentType = "application/json";
  body        = $data;
}

Invoke-RestMethod @params