Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Last active June 11, 2021 00:52
Show Gist options
  • Save sheldonhull/8827e563c40a5ee598ebbd8ddd0cc639 to your computer and use it in GitHub Desktop.
Save sheldonhull/8827e563c40a5ee598ebbd8ddd0cc639 to your computer and use it in GitHub Desktop.
[Update Azure DevOps description using patch request with api] #powershell #azuredevops
<#
.LINK
Operation Types: https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/update?view=azure-devops-rest-6.0#operation
How to Use This API: https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/update?view=azure-devops-rest-6.0
Gotcha:
However, this isn't always the case. For instance, running a script on my machine, using my own base64 encoded PAT, ../_apis/../projects needs `Basic` but ../_apis/../pullrequests needs `Bearer`. When running through AzDO, they all need `Bearer`.
https://developercommunity.visualstudio.com/t/using-powershell-script-to-call-azure-devops-api-w/816540
... but it appears that if you use your account's PAT, you need to use `Basic` and encode it with a : (colon) at the beginning of the PAT:
#>
$WorkItemId = 12345
[reflection.assembly]::loadwithpartialname('System.Web')
$content = Get-Content fileofrawhtml.html -Raw
# $encoded = [system.web.httputility]::HtmlEncode($content)
# $minify = $encoded -replace "`n", '<br>' -replace '"', '`"'
$Body = @(
[ordered]@{
op = 'replace'
path = '/fields/System.Description'
value = [string]$content
}
)| ConvertTo-Json -Depth 100 -Compress
$SplatPatch = @{
Headers = @{
Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$ENV:AZURE_DEVOPS_TOKEN"))
'Content-Type' = 'application/json-patch+json; charset=utf-8;'
}
Method = 'PATCH'
Uri = ('{0}/{1}/_apis/wit/workitems/{2}?api-version=6.0' -f $ENV:AZURE_DEVOPS_ORGANIZATION, $ENV:AZURE_DEVOPS_PROJECT, $WorkItemId)
Body = "[$Body]"
ContentType = 'application/json-patch+json; charset=utf-8'
}
Write-Verbose "Request: $($SplatPatch.GetEnumerator() | Format-List | Out-String)" -Verbose
$response = Invoke-RestMethod @SplatPatch
Write-Host $response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment