Public/Add-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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function Add-JiraIssueLink {
    <#
    .Synopsis
        Adds a link between two Issues on Jira
    .DESCRIPTION
        Creates a new link of the specified type between two Issue.
    .EXAMPLE
        $link = [PSCustomObject]@{
            outwardIssue = [PSCustomObject]@{key = "TEST-10"}
            type = [PSCustomObject]@{name = "Composition"}
        }
        Add-JiraIssueLink -Issue TEST-01 -IssueLink $link
        Creates a link "is part of" between TEST-01 and TEST-10
    .INPUTS
        [JiraPS.Issue[]] The JIRA issue that should be linked
        [JiraPS.IssueLink[]] The JIRA issue link that should be used
    #>

    [CmdletBinding()]
    param(
        # Issue key or JiraPS.Issue object returned from Get-JiraIssue
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias('Key')]
        [Object[]] $Issue,

        # Issue Link to be created.
        [Parameter(Mandatory = $true)]
        [Object[]] $IssueLink,

        # Write a comment to the issue
        [String] $Comment,

        # Credentials to use to connect to Jira
        [Parameter(Mandatory = $false)]
        [System.Management.Automation.PSCredential] $Credential<#,
 
        [Switch] $PassThru#>

    )

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

        $issueLinkURL = "$($server)/rest/api/latest/issueLink"
    }

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

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

        foreach ($i in $Issue) {
            Write-Debug "[Add-JiraIssueLink] Obtaining reference to issue"
            $issueObj = Get-JiraIssue -InputObject $i -Credential $Credential

            foreach ($link in $IssueLink) {
                if ($link.inwardIssue) {
                    $inwardIssue = [PSCustomObject]@{key = $link.inwardIssue.key}
                }
                else {
                    $inwardIssue = [PSCustomObject]@{key = $issueObj.key}
                }

                if ($link.outwardIssue) {
                    $outwardIssue = [PSCustomObject]@{key = $link.outwardIssue.key}
                }
                else {
                    $outwardIssue = [PSCustomObject]@{key = $issueObj.key}
                }

                $body = [PSCustomObject]@{
                    type         = [PSCustomObject]@{name = $link.type.name}
                    inwardIssue  = $inwardIssue
                    outwardIssue = $outwardIssue
                }
                if ($Comment) {$body["comment"] = [PSCustomObject]@{body = $Comment}}
                $json = ConvertTo-Json $body

                $null = Invoke-JiraMethod -Method POST -URI $issueLinkURL -Body $json -Credential $Credential
            }
        }
    }

    end {
        Write-Debug "[Add-JiraIssueLink] Complete"
    }
}