Public/Set-JiraConfigServer.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
function Set-JiraConfigServer {
    <#
    .Synopsis
       Defines the configured URL for the JIRA server
    .DESCRIPTION
       This function defines the configured URL for the JIRA server that JiraPS should manipulate. By default, this is stored in a config.xml file at the module's root path.
    .EXAMPLE
       Set-JiraConfigServer 'https://jira.example.com:8080'
       This example defines the server URL of the JIRA server configured in the JiraPS config file.
    .EXAMPLE
       Set-JiraConfigServer -Server 'https://jira.example.com:8080' -ConfigFile C:\jiraconfig.xml
       This example defines the server URL of the JIRA server configured at C:\jiraconfig.xml.
    .INPUTS
       This function does not accept pipeline input.
    .OUTPUTS
       [System.String]
    .NOTES
       Support for multiple configuration files is limited at this point in time, but enhancements are planned for a future update.
    #>

    [CmdletBinding(SupportsShouldProcess = $false)]
    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
    param(
        # The base URL of the Jira instance.
        [Parameter(
            Position = 0,
            Mandatory = $true
        )]
        [ValidateNotNullOrEmpty()]
        [Alias('Uri')]
        [String] $Server,

        # Path where the file with the configuration will be stored.
        [String] $ConfigFile
    )

    # Using a default value for this parameter wouldn't handle all cases. We want to make sure
    # that the user can pass a $null value to the ConfigFile parameter...but if it's null, we
    # want to default to the script variable just as we would if the parameter was not
    # provided at all.

    if (-not ($ConfigFile)) {
        # Write-Debug "[Set-JiraConfigServer] ConfigFile was not provided, or provided with a null value"
        # This file should be in $moduleRoot/Functions/Internal, so PSScriptRoot will be $moduleRoot/Functions
        $moduleFolder = Split-Path -Path $PSScriptRoot -Parent
        # Write-Debug "[Set-JiraConfigServer] Module folder: $moduleFolder"
        $ConfigFile = Join-Path -Path $moduleFolder -ChildPath 'config.xml'
        # Write-Debug "[Set-JiraConfigServer] Using default config file at [$ConfigFile]"
    }

    if (-not (Test-Path -Path $ConfigFile)) {
        # Write-Debug "[Set-JiraConfigServer] Creating config file '$ConfigFile'"
        $xml = [XML] '<Config></Config>'

    }
    else {
        # Write-Debug "[Set-JiraConfigServer] Loading config file '$ConfigFile'"
        $xml = New-Object -TypeName XML
        $xml.Load($ConfigFile)
    }

    $xmlConfig = $xml.DocumentElement
    if ($xmlConfig.LocalName -ne 'Config') {
        throw "Unexpected document element [$($xmlConfig.LocalName)] in configuration file. You may need to delete the config file and recreate it using this function."
    }

    # Check for trailing slash and strip it if necessary
    $fixedServer = $Server.Trim()

    if ($fixedServer.EndsWith('/') -or $fixedServer.EndsWith('\')) {
        $fixedServer = $Server.Substring(0, $Server.Length - 1)
    }

    if ($xmlConfig.Server) {
        # Write-Debug "[Set-JiraConfigServer] Changing the existing Server element to the provided value '$Server'"
        $xmlConfig.Server = $fixedServer
    }
    else {
        # Write-Debug "[Set-JiraConfigServer] Creating new element Server"
        $xmlServer = $xml.CreateElement('Server')
        # Write-Debug "[Set-JiraConfigServer] Writing InnerText property with provided value '$Server'"
        $xmlServer.InnerText = $fixedServer
        # Write-Debug "[Set-JiraConfigServer] Adding element to existing XML file"
        [void] $xmlConfig.AppendChild($xmlServer)
    }

    # Write-Debug "[Set-JiraConfigServer] Saving XML file"
    try {
        $xml.Save($ConfigFile)
    }
    catch {
        $err = $_
        # Write-Debug "[Set-JiraConfigServer] Encountered an error saving the XML file"
        # Write-Debug "[Set-JiraConfigServer] Throwing exception"
        throw $err
    }
    # Write-Debug "[Set-JiraConfigServer] Complete"

}