Public/Remove-JiraIssueLink.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function Remove-JiraIssueLink {
    <#
    .Synopsis
        Removes a issue link from a JIRA issue
    .DESCRIPTION
        This function removes a issue link from a JIRA issue.
    .EXAMPLE
        Remove-JiraIssueLink 1234,2345
        Removes two issue links with id 1234 and 2345
    .EXAMPLE
        Get-JiraIssue -Query "project = Project1 AND label = lingering" | Remove-JiraIssueLink
        Removes all issue links for all issues in project Project1 and that have a label "lingering"
    .INPUTS
        [JiraPS.IssueLink[]] The JIRA issue link which to delete
    .OUTPUTS
        This function returns no output.
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = 'Medium'
    )]
    param(
        # IssueLink to delete
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true
        )]
        [Object[]] $IssueLink,

        # Credentials to use to connect to Jira
        [Parameter(Mandatory = $false)]
        [PSCredential] $Credential
    )

    Begin {
        Write-Debug "[Remove-JiraIssueLink] Reading Jira server from config file"
        $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop

        $restUrl = "$server/rest/api/latest/issueLink/{0}"
    }

    Process {

        # As we are not able to use proper type casting in the parameters, this is a workaround
        # to extract the data from a JiraPS.Issue object
        if (($_) -and ($_.PSObject.TypeNames[0] -eq "JiraPS.Issue")) {
            $IssueLink = $_.issueLinks
        }

        # Validate IssueLink object
        $objectProperties = $IssueLink | Get-Member -MemberType *Property
        if (-not($objectProperties.Name -contains "id")) {
            $message = "The IssueLink provided does not contain the information needed. $($objectProperties | Out-String)"
            $exception = New-Object -TypeName System.ArgumentException -ArgumentList $message
            Throw $exception
        }

        # Validate input object from Pipeline
        if (($_) -and ($_.PSObject.TypeNames[0] -notin @("JiraPS.IssueLink", "JiraPS.Issue"))) {
            $message = "Wrong object type provided for Issue. Only JiraPS.IssueLink is accepted"
            $exception = New-Object -TypeName System.ArgumentException -ArgumentList $message
            Throw $exception
        }

        foreach ($link in $IssueLink) {
            Write-Debug "[Remove-JiraIssueLink] Processing issue key [$k]"
            $thisUrl = $restUrl -f $link.id
            if ($PSCmdlet.ShouldProcess($link.id, "Remove IssueLink")) {
                Write-Debug "[Remove-JiraIssueLink] Preparing for blastoff!"
                Invoke-JiraMethod -Method Delete -URI $thisUrl -Credential $Credential
            }
        }
    }

    End {
        Write-Debug "[Remove-JiraIssueLink] Complete"
    }
}