ps/Modules/Cole.PowerShell.Developer/Public/Get-ScrumTeamsFromConfluence.ps1
2023-05-30 22:51:22 -07:00

117 lines
4.1 KiB
PowerShell

function Get-ScrumTeamsFromConfluence {
<#
.SYNOPSIS
Get the list of scrum teams from the Alkami Confluence page that determines the teams.
Requires that the page has not changed.
.LINK
https://confluence.alkami.com/display/AA/Current+Scrum+Teams
#>
[CmdletBinding()]
[OutputType([object[]])]
param (
[Parameter(Mandatory = $false, Position = 0)]
[string]$FullUrl = "https://confluence.alkami.com/display/AA/Current+Scrum+Teams",
[Parameter(Mandatory = $false, Position = 1)]
[string]$ArticleID,
[Parameter(Mandatory = $false, Position = 2)]
[PSCredential]$Credential
)
$logLead = (Get-LogLeadName)
if ($null -eq $Credential) {
$Credential = (Get-CredentialFromEnvironmentVariables)
}
if ($null -eq $Credential) {
Write-Error "$logLead : Can not talk to Jira without credentials. Returning."
return
}
$headers = (Get-BasicAuthWebHeader -Credential $Credential)
$headers["Content-Type"] = "application/json"
# left this data so I can separate the two things between "get random confluence page" and "get this specific page"
# I don't have a second page to read from _yet_, so I left this here for now
# Also works the same as the Add-JiraComment function, roughly.
# $url = (Get-ConfluenceBaseUrl)
# $baseUrl = "https://confluence.alkami.com/display/AA/Current+Scrum+Teams"
# Use this URL to ensure the ticket number as provided exists
# $confluencePageUrl = (Join-UrlComponents -BaseUrl $url -Path "/rest/api/content/$ArticleID")
# do rest apis still use IWR for this particular function?
$arguments = @{
Headers = $headers
Uri = $FullUrl
Method = 'Get'
}
try {
$response = Invoke-WebRequest @arguments
} catch {
Write-Host (Get-LastWebRequestErrorText)
Write-Host $arguments
Write-ErrorObject -ErrorItem $PSItem
Write-Error "$logLead : Could not connect to Confluence. Ensure proper credentials and try again, or verify the page hasn't moved."
return
}
if ($null -eq $response) {
Write-Host "$logLead : Got no data from Confluence."
}
$table = $null
try {
$table = $response.ParsedHtml.GetElementsByClassName("wrapped relative-table confluenceTable")
} catch [System.Runtime.InteropServices.COMException] {
Write-Host "$logLead : Not sure why the IE COM engine can't parse this table regularly. It appears to be a bug with an OOM condition.$([System.Environment]::NewLine)Reopen your terminal and try again I guess. Thanks COM."
return
} catch [System.NotSupportedException] {
Write-Host "$logLead : COM puked. No idea why. Reset your terminal I guess and try again?"
return
}
if ($null -eq $table) {
Write-Host "$logLead : Could not parse the response data from Confluence. Raw data being returned."
return $response
}
$headers = @()
$headersFound = $false
$tableData = @()
foreach ($row in $table[0].tBodies[0].Rows) {
$team = @{}
foreach ($cell in $row.Cells) {
if (!$headersFound) {
$headers += $cell.innerText
} else {
$targetText = $cell.innerText
$anchors = @($cell.getElementsByTagName('a'))
if ($anchors.Count -gt 0) {
$href = $anchors[0].href
if ($href.IndexOf('jira.alkami.com') -gt -1) {
$targetText = $href
}
}
$team[$headers[$cellCounter]] = $targetText
}
$cellCounter += 1
}
if ($headersFound) {
$tableData += $team
}
$headersFound = $true
$cellCounter = 0
}
if ($tableData.Count -eq 0) {
Write-Warning "$logLead : There were no rows somehow in the table object output. This is clearly a problem. Raw data being returned."
return $response
}
$scrumTeams = (ConvertTo-ScrumTeam $tableData)
return $scrumTeams
}