Public/Update-Bug.ps1

function Update-Bug {
    param( 
        [string]$ChannelId,
        [string]$EntityId,
        [string]$GroupId,
        [string]$TeamId,
        [string]$Domain = "teams-stag.appvity.com",
        $BugId,
        $BugObject,
        [string]$Title,
        [string]$AssignedTo,
        [string]$Attachments,
        [string]$Body,
        [string]$Complete,
        [string]$CompletedDate,
        [string]$DueDate,
        [string]$Effort,
        [string]$Name,
        [string]$Owner,
        [string]$Priority,
        [string]$RelatedItems,
        [string]$StartDate,
        [string]$Status,
        [string]$ProjectId,
        $PhaseId,
        $BucketId,
        [string]$Severity,
        [string]$Cookie
    )

    $validate = ''
    if(!$Domain){
        $validate = $validate + ', Domain'
    }
    if(!$ChannelId){
        $validate = $validate + ', ChannelId'
    }
    <#
    if(!$EntityId){
        $validate = $validate + ', EntityId'
    }
    if(!$GroupId){
        $validate = $validate + ', GroupId'
    }
    #>

    if(!$TeamId){
        $validate = $validate + ', TeamId'
    }
    if(!$ProjectId){
        $validate = $validate + ', ProjectId'
    }
    if(!$BugId){
        $validate = $validate + ', BugId'
    }
    if($validate) {
        $validate = $validate.TrimStart(',').TrimStart() + ' is require'
        Write-Host $validate -F Red
        return
    }

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    $ReportedBy = @()
    #get task detail
    $bugDetail = Get-BugDetails -ChannelId $ChannelId -TeamId $TeamId -Domain $Domain -BugId $BugId -Cookie $Cookie

    if($bugDetail) {
        if(!$Title) {$Title = $bugDetail.name}
        if(!$AssignedTo) {$AssignedTo = $bugDetail.assignedTo}
        if(!$Attachments) {$Attachments = $bugDetail.attachments}
        if(!$Body) {$Body = $bugDetail.body}
        if(!$Bucket) {$Bucket = $bugDetail.bucket}
        if(!$Complete) {$Complete = $bugDetail.complete}
        if(!$CompletedDate) {$CompletedDate = $bugDetail.completedDate}
        if(!$DueDate) {$DueDate = $bugDetail.dueDate}
        if(!$Effort) {$Effort = $bugDetail.effort}
        if(!$Owner) {$Owner = $bugDetail.owner}
        if(!$Phase) {$Phase = $bugDetail.phase}
        if(!$Priority) {$Priority = $bugDetail.priority}
        if(!$RelatedItems) {$RelatedItems = $bugDetail.relatedItems}
        if(!$StartDate) {$StartDate = $bugDetail.startDate}
        if(!$Status) {$Status = $bugDetail.status}
        if(!$Source) {$Source = $bugDetail.source}
        if(!$Severity) {$Severity = $bugDetail.Severity}
        if(!$ReportedBy) {$ReportedBy = $bugDetail.ReportedBy}
    }

    if($BugObject) {
        #$object = $BodyObject | ConvertFrom-Json

        if($BugObject.Title) {$Title = $BugObject.Title}
        if($BugObject.AssignedTo) {$AssignedTo = $BugObject.AssignedTo}
        if($BugObject.Attachments) {$Attachments = $BugObject.Attachments}
        if($BugObject.Body) {$Body = $BugObject.Body}
        if($BugObject.Complete) {$Complete = $BugObject.Complete}
        if($BugObject.CompletedDate) {$CompletedDate = $BugObject.CompletedDate}
        if($BugObject.DueDate) {$DueDate = $BugObject.DueDate}
        if($BugObject.Effort) {$Effort = $BugObject.Effort}
        if($BugObject.Name) {$Name = $BugObject.Name}
        if($BugObject.Owner) {$Owner = $BugObject.Owner}
        if($BugObject.Priority) {$Priority = $BugObject.Priority}
        if($BugObject.RelatedItems) {$RelatedItems = $BugObject.RelatedItems}
        if($BugObject.StartDate) {$StartDate = $BugObject.StartDate}
        if($BugObject.Status) {$Status = $BugObject.Status}
        if($BugObject.BucketId) {$BucketId = $BugObject.BucketId}
        if($BugObject.PhaseId) {$PhaseId = $BugObject.PhaseId}
        if($BugObject.Severity) {$Severity = $BugObject.Severity}
    }
    
    <#
    #validation
    if(!$Title) {
        Write-Host "Title not found." -F red
        return
    }
    if(!$Source) {
        Write-Host "Source not found." -F red
        return
    }
    if(!$Priority) {
        Write-Host "Priority not found." -F red
        return
    }
    if(!$Status) {
        Write-Host "Status not found." -F red
        return
    }
    #>


    $phaseName = ''
    $bucketName = ''
    
    if($PhaseId) {
        $phases = Get-Phases -Domain $Domain -ChannelId $ChannelId -TeamId $TeamId -ProjectId $ProjectId -Cookie $cookie
        $phase = $phases | Where {$_.id -eq $PhaseId} | Select displayName
        $phaseName = $phase.displayName

        if($BucketId) {
            $buckets = Get-Buckets -Domain $Domain -ChannelId $ChannelId -TeamId $TeamId -ProjectId $ProjectId -PhaseId $PhaseId -Cookie $cookie
            $bucket = $buckets | Where {$_.id -eq $BucketId} | Select displayName
            $bucketName = $bucket.displayName
        }
    }


    $Arr = @{
        name = $Title
        status = $Status
        body = $Body
        source = $Source
        priority = $Priority
        startDate = $StartDate
        dueDate = $DueDate
        attachments = $Attachments
        bucket = $BucketId
        bucketName = $bucketName
        complete = $Complete
        completedDate = $CompletedDate
        effort = $Effort
        owner = $Owner
        phase = $PhaseId
        phaseName = $phaseName
        projectId = $ProjectId
        relatedItems = @()
        assignedTo = @()
        reportedBy = @()
        severity = $Severity

    } | ConvertTo-Json

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Url = 'https://' + $Domain.TrimEnd('/') + '/odata/bugs(' + $BugId + ')'

    $Params = @{
        Uri = $Url
        Method = 'PATCH'
        Headers = $hd
        Body = $Arr
    }

    try {
        Invoke-WebRequest @Params -WebSession $session
    }
    catch{
        Write-Error $_.Exception.Message
    }
}