jira.psm1

$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
        }
    }
}