Public/Remove-JiraRemoteLink.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function Remove-JiraRemoteLink {
    <#
    .Synopsis
       Removes a remote link from a JIRA issue
    .DESCRIPTION
       This function removes a remote link from a JIRA issue.
    .EXAMPLE
       Remove-JiraRemoteLink Project1-1001 10000,20000
       Removes two remote link from issue "Project1-1001"
    .EXAMPLE
       Get-JiraIssue -Query "project = Project1" | Remove-JiraRemoteLink 10000
       Removes a specific remote link from all issues in project "Project1"
    .INPUTS
       [JiraPS.Issue[]] The JIRA issue from which to delete a link
    .OUTPUTS
       This function returns no output.
    #>

    [CmdletBinding(
        ConfirmImpact = 'High',
        SupportsShouldProcess = $true
    )]
    param(
        # Issue from which to delete a remote link.
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias("Key")]
        [Object[]] $Issue,

        # Id of the remote link to delete.
        [Parameter(Mandatory = $true)]
        [Int[]] $LinkId,

        # Credentials to use to connect to JIRA.
        # If not specified, this function will use anonymous access.
        [Parameter(Mandatory = $false)]
        [PSCredential] $Credential,

        # Suppress user confirmation.
        [Switch] $Force
    )

    Begin {
        try {
            Write-Debug "[Remove-JiraRemoteLink] Reading Jira server from config file"
            $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop
        }
        catch {
            $err = $_
            Write-Debug "[Remove-JiraRemoteLink] Encountered an error reading configuration data."
            throw $err
        }

        $restUrl = "$server/rest/api/latest/issue/{0}/remotelink/{1}"

        if ($Force) {
            Write-Debug "[Remove-JiraRemoteLink] -Force was passed. Backing up current ConfirmPreference [$ConfirmPreference] and setting to None"
            $oldConfirmPreference = $ConfirmPreference
            $ConfirmPreference = 'None'
        }
    }

    Process {

        foreach ($k in $Issue) {
            Write-Debug "[Remove-JiraRemoteLink] Processing issue key [$k]"
            $issueObj = Get-JiraIssue $k -Credential $Credential

            foreach ($l in $LinkId) {
                $thisUrl = $restUrl -f $k, $l
                Write-Debug "[Remove-JiraRemoteLink] RemoteLink URL: [$thisUrl]"

                Write-Debug "[Remove-JiraRemoteLink] Checking for -WhatIf and Confirm"
                if ($PSCmdlet.ShouldProcess($issueObj.Key, "Remove RemoteLink from [$issueObj] from JIRA")) {
                    Write-Debug "[Remove-JiraRemoteLink] Preparing for blastoff!"
                    Invoke-JiraMethod -Method Delete -URI $thisUrl -Credential $Credential
                }
                else {
                    Write-Debug "[Remove-JiraRemoteLink] Runnning in WhatIf mode or user denied the Confirm prompt; no operation will be performed"
                }
            }
        }
    }

    End {
        if ($Force) {
            Write-Debug "[Remove-JiraGroupMember] Restoring ConfirmPreference to [$oldConfirmPreference]"
            $ConfirmPreference = $oldConfirmPreference
        }

        Write-Debug "[Remove-JiraRemoteLink] Complete"
    }
}