src/tools/Get-DefectSuite.ps1

. "$PSScriptRoot/types/Defect.ps1"
. "$PSScriptRoot/types/DefectSuite.ps1"
. "$PSScriptRoot/functions/functions.ps1"
# . "$PSScriptRoot/types/DefectResponse.ps1"

Write-Output "Installing function contained in: $PSScriptRoot"

<#
    .SYNOPSIS
        Provides SRE-specific handling of Rally Tickets
    .DESCRIPTION
        Manage Default Suites and defects using the following commands
    .PARAMETER defectSuite
        Required default Defect Suite id
    .PARAMETER copyUrlToClipboard
        Copy the DS URL to the Windows Clipboard for pasting anywhere
    .PARAMETER copyLongLinkToClipboard
        Copy Id and Name to HTML fragment
    .PARAMETER copyShortLinkToClipboard
        Copy Id to HTML fragment
    .PARAMETER triageHighPriority
        This will:
            1. Create a new defect, cloning the information from the defect suite.
                a. The new Defect will have the following name structure: DS#### - SRE - Defect Suite Title
                b. The new Defect will be assigned to the Site Reliability Project.
                c. The new Defect will be attached to the Defect Suite.
            2. The Defect Suite's "SRE SLA Date" will be updated to 90 days from the time you click the button to fit with our SLA.
            3. The Defect Suite's "Escalation Status" will be set to "Waiting for Analysis".
    .PARAMETER triageLowPriority
        This will:
            1. Create a new defect, cloning the information from the defect suite.
                a. The new Defect will have the following name structure: DS#### - DEV - Defect Suite Title
                b. The new Defect will be assigned to the correct Dev Project according to product.
            2. The Defect Suite's "Escalation Status" will be set to "Backlog - Closed".
    .PARAMETER divertToDev
        This will:
            1. Attempt to find the existing SRE defect and update its: "State" to "Closed" and "Schedule State" to "Accepted" and "Resolution" to "Duplicate". (If none exists it should throw an exception and do nothing else as a safety.)
            2. This will create a new defect, cloning the information from the defect suite.
                a. The new Defect will have the following name structure: DS#### - DEV - Defect Suite Title
                b. The new Defect will be assigned to the correct Dev project according to product.
                c. The new Defect will be attached to the Defect Suite.
            3. The Defect Suite will be updated to:
                a. Remove the current owner
                b. Update its status to the most relevant "waiting" option. (i.e."In analysis" => "Waiting for analysis" and "Remediation in progress" => "Waiting for remediation")
                c. Add the "DivertedToDev" tag
    .PARAMETER syncPriorityToDefects
        Syncronize high-priority DS
    .PARAMETER triageUnshieldedProductDS
        Triage Unsheilded Product DS
    .PARAMETER openInBrowser
        Open browser window and navigate to Defect Suite in Rally
    .PARAMETER calculateCycleTimes
        Calculate cycle times
    .PARAMETER dsDataToUUID
        Returns the UUID value for the DS
    .EXAMPLE
#>

function Get-DefectSuite {
    [CmdletBinding()]
    param(
        [Parameter(Position=0)]
        [string][alias("ds")] $dsId,
        [switch][alias("cu")] $copyUrlToClipboard,
        [switch][alias("clong")] $copyLongLinkToClipboard,
        [switch][alias("cshort")] $copyShortLinkToClipboard, 
        [switch][alias("thp")] $triageHighPriority,
        [switch][alias("tlp")] $triageLowPriority,
        [switch][alias("dd")] $divertToDev,
        [switch][alias("sync")] $syncPriorityToDefects,
        [switch][alias("tu")] $triageUnshieldedProductDS,
        [switch][alias("web")] $openInBrowser,
        [switch][alias("calc")] $calculateCycleTimes,
        [switch][alias("uuid")] $dsDataToUUID,
        [switch][alias("pull")] $pullDsData,
        [switch][alias("h")] $help,
        [switch][alias("v")] $version
    )

    "$(Get-Date).ToString('yyyy-MM-dd HH:mm:ss'): $args" | Out-File -Append -FilePath "$(dsFolderPath)/work.log"
    
    $dsId = $dsId.ToUpper()
    if(-not $dsId -match "DS\d{3,5}"){
        return Write-Output "Defect Suite id must match pattern 'DS\d{3,5}"
    }
    if($version){
        $currentVersion = getVersion
        $latestVersion = getLatestVersion
        if($currentVersion[-1] -lt $latestVersion[-1]){
            Write-Output "There is a newer version of RallyQuickToolsCli available"
            Write-Output "Your version: $($currentVersion -join '.')"
            Write-Output "Latest version: $($latestVersion -join '.')"
            Write-Output "If you would like to upgrade to the latest version type: Update-Module RallyQuickToolsCli"
        }else{
            Write-Output "Current version is: $($currentVersion -join '.')"
        }
    }


    # Lookup Defect Suite
    if($dsId){
        getDsDataFromDsId($dsId) | dsDataToName
    }
    if($dsDataToUUID){
        getDsDataFromDsId($dsId) | dsDataToUUID
    }
    if($copyUrlToClipboard){
        $uuid = getDsDataFromDsId($dsId) | dsDataToUUID
        "https://rally1.rallydev.com/#/detail/defectsuite/$uuid"  | clip
    }
    if($openInBrowser){
        $uuid = getDsDataFromDsId($dsId) | dsDataToUUID
        Start-Process "https://rally1.rallydev.com/#/detail/defectsuite/$uuid"
    }
    if($copyLongLinkToClipboard){
        $dsData = getDsDataFromDsId($dsId) 
        $linkUrl = "https://rally1.rallydev.com/#/detail/defectsuite/$($dsData | dsDataToUUID)"
        $linkText = "$dsId - $($dsData | dsDataToName )"
        wrapWithHtml($linkUrl, $linkText) | clip
    }
    if($copyShortLinkToClipboard){
        $dsData = getDsDataFromDsId($dsId) 
        $linkUrl = "https://rally1.rallydev.com/#/detail/defectsuite/$($dsData | dsDataToUUID)"
        wrapWithHtml($linkUrl, $dsId) | clip
    }
    if($triageHighPriority){
        <#
        This will:
            1. Create a new defect, cloning the information from the defect suite.
                a. The new Defect will have the following name structure: DS#### - SRE - Defect Suite Title
                b. The new Defect will be assigned to the Site Reliability Project.
                c. The new Defect will be attached to the Defect Suite.
            2. The Defect Suite's "SRE SLA Date" will be updated to 90 days from the time you click the button to fit with our SLA.
            3. The Defect Suite's "Escalation Status" will be set to "Waiting for Analysis".
        #>

        getDsDataFromDsId($dsId) 
        | dsDataToRefUrl 
        | getDsFullFromRefUrl 
        | dsFullToDefect 
        | mapToSreDefect # DS#### - SRE - Defect Suite Title
        | updateDsToSre #TODO: Items 2 and 3
        | postDefect
    }
    if($pullDsData){
        if(-not(dsFolderPath)){
            return 'ERROR: No folder path set
            To pull data you must set a local Defect Suite folder with [Environment]::SetEnvironmentVariable("DS_FOLDER_PATH","<your_defectsuite_folder_path", "User")'

        }
        getDsDataFromDsId($dsId) 
        | dsDataToRefUrl 
        | getDsFullFromRefUrl 
        | dsFullToYamlFile 
    }
    if($triageLowPriority){
        <#
        This will:
            1. Create a new defect, cloning the information from the defect suite.
                a. The new Defect will have the following name structure: DS#### - DEV - Defect Suite Title
                b. The new Defect will be assigned to the correct Dev Project according to product.
            2. The Defect Suite's "Escalation Status" will be set to "Backlog - Closed".
        #>

        getDsDataFromDsId($dsId) 
        | dsDataToRefUrl 
        | getDsFullFromRefUrl 
        | dsFullToDefect 
        | mapToDevDefect # "DS#### - DEV - Defect Suite Title"
        | postDefect
    }
    if($divertToDev){
        <#
        This will:
            1. Attempt to find the existing SRE defect and update its: "State" to "Closed" and "Schedule State" to "Accepted" and "Resolution" to "Duplicate". (If none exists it should throw an exception and do nothing else as a safety.)
            2. This will create a new defect, cloning the information from the defect suite.
                a. The new Defect will have the following name structure: DS#### - DEV - Defect Suite Title
                b. The new Defect will be assigned to the correct Dev project according to product.
                c. The new Defect will be attached to the Defect Suite.
            3. The Defect Suite will be updated to:
                a. Remove the current owner
                b. Update its status to the most relevant "waiting" option. (i.e."In analysis" => "Waiting for analysis" and "Remediation in progress" => "Waiting for remediation")
                c. Add the "DivertedToDev" tag
        #>

        getDsDataFromDsId($dsId) 
        | dsDataToRefUrl 
        | getDsFullFromRefUrl 
        | updateExistingSreDefect # "Closed" and "Accepted" and "Duplicate"
        | updateDsToDev # Item 3 {a,b,c}
        | dsFullToDefect 
        | mapToDevDefect # "DS#### - DEV - Defect Suite Title"
        | defectToYamlFile
    }
    if($syncPriorityToDefects){
        Write-Error "Not yet implemented"
    }
    if($triageUnshieldedProductDS){
        Write-Error "Not yet implemented"
    }
    if($calculateCycleTimes){
        Write-Error "Not yet implemented"
    }
    if($help){
        Get-Help Get-DefectSuite
    }
}