jira.psm1

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
$Script:PSModuleRoot = $PSScriptRoot
# Importing from [D:\a\1\s\jira\Public]
# .\jira\Public\Open-JSession.ps1
function Open-JSession
{
    [Alias("New-JSession")]
    [cmdletbinding(DefaultParameterSetName = 'Default')]
    param(
        [Parameter(
            Mandatory,
            ParameterSetName = 'Save'
        )]
        [Parameter(
            ParameterSetName = 'Default'
        )]
        [PSCredential]
        $Credential,

        [Parameter(
            Mandatory,
            ParameterSetName = 'Save',
            Position = 0
        )]
        [Parameter(
            ParameterSetName = 'Default',
            Position = 0
        )]
        [string]
        $Uri,

        [Parameter(
            ParameterSetName = 'Save'
        )]
        [switch]
        $Save,

        [Parameter()]
        [switch]
        $PassThru
    )

    begin
    {
        $cmTarget = 'JiraModule'
    }

    end
    {
        if ([string]::IsNullOrEmpty($Uri))
        {
            Write-Verbose "Using Saved Uri"
            $Uri = Get-PSFConfigValue -FullName jira.Uri

            if ([string]::IsNullOrEmpty($Uri))
            {
                throw [JiraModule.JiraConnectionException]::new(
                    'No Jira URI or server endpoint was provided, call Open-JSession -Uri https://myjiraserver.org -SaveUri'
                )
            }
        }

        Write-Verbose "Uri [$Uri]"

        if ($null -eq $Credential -or
            $Credential -eq [PScredential]::Empty)
        {
            Write-Verbose "Credential was empty, using Stored Credential for [$cmTarget]"
            $Credential = Get-StoredCredential -Target $cmTarget
            if ($null -eq $Credential)
            {
                throw [JiraModule.JiraAuthenticationException]::new(
                    'No credential was provided and no previoud credential was saved. Run [Open-JSession -Credential $Credential -SaveCredential]'
                )
            }
        }

        Write-Verbose "Credential [$($credential.UserName)]"
        JiraModule\Open-JSession -Credential $Credential -Uri $uri -PassThru:$PassThru

        if ($Save)
        {
            Write-Verbose "Saving URI [$Uri] for future use"
            $null = Set-PSFConfig -Module jira -Name Uri -Value $Uri -PassThru |
                Register-PSFConfig

            Write-Verbose "Storing [$Credential.UserName] credential for [$cmTarget]"

            $storedCredential = @{
                Target         = $cmTarget
                UserName       = $Credential.UserName
                SecurePassword = $Credential.Password
            }
            $null = New-StoredCredential @storedCredential -Comment "for use with the Jira module"
        }
    }
}

# .\jira\Public\Remove-JSessionConfig.ps1
function Remove-JSessionConfig
{
    [Alias("Remove-JiraSessionConfig")]
    [cmdletbinding(SupportsShouldProcess)]
    param()

    begin
    {
        $cmTarget = 'JiraModule'
    }

    end
    {
        if ($PSCmdlet.ShouldProcess("Jira Module Default Connection Values"))
        {
            Write-Verbose "Clearing Jira Uri"
            Unregister-PSFConfig -Module jira -Name Uri

            Write-Verbose "Removing stored credential for [$cmTarget]"
            Remove-StoredCredential -Target $cmTarget -ErrorAction Ignore
        }
    }
}