Skip to content

Commit e282457

Browse files
🩹 [Patch]: Harden site URI parsing, surface site-info probe errors, avoid redundant config writes
1 parent 4cd4886 commit e282457

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎src/functions/private/Config/Initialize-ConfluenceConfig.ps1‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,29 @@
2929
}
3030
}
3131

32+
$dirty = $false
3233
if ($Force -or -not $stored) {
3334
$config = ConvertTo-ConfluenceHashtable -InputObject $script:Confluence.DefaultConfig
35+
$dirty = $true
3436
} else {
3537
$config = ConvertTo-ConfluenceHashtable -InputObject $stored
3638
foreach ($key in $script:Confluence.DefaultConfig.Keys) {
3739
if (-not $config.ContainsKey($key)) {
3840
$config[$key] = $script:Confluence.DefaultConfig[$key]
41+
$dirty = $true
3942
}
4043
}
4144
}
4245

43-
$config['ID'] = $id
44-
$null = Set-Context -ID $id -Context $config -Vault $vault
46+
if ($config['ID'] -ne $id) {
47+
$config['ID'] = $id
48+
$dirty = $true
49+
}
50+
51+
# Only persist when we actually created or changed the stored config, to avoid
52+
# unnecessary vault writes on every cmdlet call (this runs from most cmdlets).
53+
if ($dirty) {
54+
$null = Set-Context -ID $id -Context $config -Vault $vault
55+
}
4556
$script:Confluence.Config = $config
4657
}

‎src/functions/public/Site/Get-ConfluenceCloudId.ps1‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,16 @@ function Get-ConfluenceCloudId {
5858
}
5959

6060
# Derive the host whether the caller passed a bare subdomain, a host, or a URL.
61-
$siteHost = if ($trimmed -match '^[A-Za-z][A-Za-z0-9+.-]*://') {
62-
([uri]$trimmed).Host
63-
} elseif ($trimmed -match '[/.]') {
64-
([uri]"https://$trimmed").Host
65-
} else {
66-
"$trimmed.atlassian.net"
61+
try {
62+
$siteHost = if ($trimmed -match '^[A-Za-z][A-Za-z0-9+.-]*://') {
63+
([uri]$trimmed).Host
64+
} elseif ($trimmed -match '[/.]') {
65+
([uri]"https://$trimmed").Host
66+
} else {
67+
"$trimmed.atlassian.net"
68+
}
69+
} catch {
70+
throw "Could not determine the site host from '$Site': $($_.Exception.Message)"
6771
}
6872

6973
if ([string]::IsNullOrWhiteSpace($siteHost)) {

‎src/functions/public/Site/Get-ConfluenceSiteInfo.ps1‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function Get-ConfluenceSiteInfo {
3636
# _links.base (the browsable site URL) appears on any v2 collection response;
3737
# try a couple so this works whether the token has read:space or only read:page.
3838
$siteUrl = $null
39+
$lastError = $null
3940
foreach ($probe in '/wiki/api/v2/spaces', '/wiki/api/v2/pages') {
4041
try {
4142
$response = Invoke-ConfluenceRestMethod -ApiEndpoint $probe -Query @{ limit = 1 } -Context $Context
@@ -44,9 +45,13 @@ function Get-ConfluenceSiteInfo {
4445
break
4546
}
4647
} catch {
48+
$lastError = $_
4749
continue
4850
}
4951
}
52+
if (-not $siteUrl -and $lastError) {
53+
Write-Verbose "Could not resolve the browsable site URL from the probe endpoints: $($lastError.Exception.Message)"
54+
}
5055

5156
[pscustomobject]@{
5257
CloudId = $cloudId

0 commit comments

Comments
 (0)