functions/update-adoworkitemcomment.ps1


<#
    .SYNOPSIS
        Updates (PATCH) an existing work item comment.
    .DESCRIPTION
        Wraps Comments - Update Work Item Comment endpoint. Allows changing text and format.
    .OUTPUTS
        ADO.TOOLS.WorkItem.Comment
    .PARAMETER Organization
        Azure DevOps organization name.
    .PARAMETER Project
        Project name or id.
    .PARAMETER Token
        PAT (vso.work_write scope).
    .PARAMETER WorkItemId
        Work item id owning the comment.
    .PARAMETER CommentId
        Comment id to update.
    .PARAMETER Text
        New comment text.
    .PARAMETER Format
        markdown | html
    .PARAMETER ApiVersion
        API version (default 7.1-preview.4).
    .EXAMPLE
        PS> Update-ADOWorkItemComment -Organization org -Project proj -Token $pat -WorkItemId 100 -CommentId 42 -Text "Corrected"
         
        Updates comment 42 on work item 100.
    .EXAMPLE
        PS> Update-ADOWorkItemComment -Organization org -Project proj -Token $pat -WorkItemId 100 -CommentId 42 -Text "<b>Updated</b>" -Format html
         
        Updates comment 42 with HTML formatted text.
    .LINK
        https://learn.microsoft.com/azure/devops
#>

function Update-ADOWorkItemComment {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions","")]
    [CmdletBinding()]
    [OutputType('ADO.TOOLS.WorkItem.Comment')]
    param(
        [Parameter(Mandatory = $true)] [string]$Organization,
        [Parameter(Mandatory = $true)] [string]$Project,
        [Parameter(Mandatory = $true)] [string]$Token,
        [Parameter(Mandatory = $true)] [int]$WorkItemId,
        [Parameter(Mandatory = $true)] [int]$CommentId,
        [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$Text,
        [Parameter(Mandatory = $false)] [ValidateSet('markdown','html')] [string]$Format = 'markdown',
        [Parameter(Mandatory = $false)] [string]$ApiVersion = '7.1-preview.4'
    )
    begin {
        Write-PSFMessage -Level Verbose -Message "Starting update of comment $CommentId on WorkItem $WorkItemId (Org: $Organization / Project: $Project)"
        Invoke-TimeSignal -Start
    }
    process {
        if (Test-PSFFunctionInterrupt) { return }
        try {
            $apiUri = "$Project/_apis/wit/workItems/$WorkItemId/comments/$CommentId?format=$Format"
            Write-PSFMessage -Level Verbose -Message "API URI: $apiUri"
            $body = @{ text = $Text } | ConvertTo-Json -Depth 4
            $response = Invoke-ADOApiRequest -Organization $Organization `
                                             -Token $Token `
                                             -ApiUri $apiUri `
                                             -Method 'PATCH' `
                                             -Body $body `
                                             -Headers @{'Content-Type'='application/json'} `
                                             -ApiVersion $ApiVersion
            Write-PSFMessage -Level Verbose -Message "Successfully updated comment $CommentId on WorkItem $WorkItemId"
            return $response.Results | Select-PSFObject * -TypeName 'ADO.TOOLS.WorkItem.Comment'
        }
        catch {
            Write-PSFMessage -Level Error -Message "Failed to update comment $CommentId on WorkItem $WorkItemId : $($_.ErrorDetails.Message)" -Exception $PSItem.Exception
            Stop-PSFFunction -Message "Stopping because of errors"
        }
    }
    end {
        Write-PSFMessage -Level Verbose -Message "Completed update of comment $CommentId on WorkItem $WorkItemId"
        Invoke-TimeSignal -End
    }
}