Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bamboo UI scraping to retrieve Deploy Project Environment Tasks #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added BambooEditEnvironmentPageResults.txt
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<#
.SYNOPSIS
Gets all environments for a Bamboo Deploy Project or describes a single Bamboo Deploy Project environment.
.DESCRIPTION
If -EnvironmentId is specified it describes only that Deploy Project environment.
.PARAMETER EnvironmentId
Mandatory - Id for the Bamboo Deploy Project environment to find tasks for
.EXAMPLE
Get-BambooDeployProjectEnvironmentTasks
.EXAMPLE
Get-BambooDeployProjectEnvironmentTasks -EnvironmentId 70778893 -Verbose
#>
function Get-BambooDeployProjectEnvironmentTasks {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidatePattern('\w+')]
[string]$EnvironmentId
)

Write-Warning "Get-BambooDeployProjectEnvironmentTasks: The Bamboo API does not allow for querying of 'Deploy Project Environment Tasks' so we do some rather nasty screen scraping of the web UI here"

$resource = "deploy/config/configureEnvironmentTasks.action"
$uriParams = @{"environmentId" = $EnvironmentId}

$bambooResponse = Invoke-BambooWebRequest -Resource $resource -UriParams $uriParams

$taskDetails = New-Object System.Collections.ArrayList
$items = $bambooResponse.ParsedHTML.body.getElementsByClassName('item')

foreach ($item in $items) {

$itemTitle = ($item.getElementsByClassName('item-title'))[0].innerHTML
if ($itemTitle -ne $null) {

$itemDescription = "---"
try {
$itemDescription = ($item.getElementsByClassName('item-description'))[0].innerHTML
}
catch {
$itemDescription = "---"
}

$taskDetail = New-Object PSObject -Property @{
orderId = [Int]$item.getAttribute('data-item-id')
title = $itemTitle
description = $itemDescription
isDisabled = $item.innerText.Contains("Disabled")
}
$taskDetails.Add($taskDetail) > $null
}
}

return $taskDetails

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>PsBamboo.DeployEnvironmentTasks</Name>
<ViewSelectedBy>
<TypeName>PsBamboo.DeployEnvironmentTasks</TypeName>
</ViewSelectedBy>
<TableControl>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>orderId</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>description</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>isDisabled</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>isScript</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
93 changes: 93 additions & 0 deletions Functions/Invoke-BambooWebRequest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<#
.SYNOPSIS
Generic helper cmdlet to invoke web requests agains a target Bamboo server.
.DESCRIPTION
This cmdlet extends the original Invoke-WebRequest cmdlet with Bamboo specific parameters, so it does user authorization and provides easier
resource access.

.PARAMETER Resource
Mandatory - Bamboo REST API Resource that needs to be accessed
.PARAMETER Method
Optional - HTTP method to be used for the call. (Default is GET)
.PARAMETER AuthenticationMode
Optional - Authentication Mode to access Bamboo Server
.PARAMETER AuthenticationToken
Optional - Authentication Token to access Bamboo Server

.PARAMETER UriParams
Optional - Parameters that needs to be appended to the GET url.
.PARAMETER Headers
Optional - HTTP Headers that needs to be added for the REST call.
.PARAMETER Body
Optional - HTTP Body payload

.EXAMPLE
Invoke-BambooWebRequest -Resource "plan"
.EXAMPLE
Invoke-BambooWebRequest -Resource "plan/PRJ-PLAN/enable" -Method Delete

.LINK
https://developer.atlassian.com/bamboodev/rest-apis
#>
function Invoke-BambooWebRequest {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Resource,

[ValidateSet('Get','Put','Post','Delete')]
[string]$Method = 'Get',

[ValidateSet('latest')]
[string]$Server = $script:BambooServer,
[string]$Uri="$Server/$Resource",

[string]$AuthenticationMode=$script:AuthenticationMode,
[string]$AuthenticationToken = $script:AuthenticationToken,

[string]$ContentType='text/html',

[psobject]$UriParams=@{},
[psobject]$Headers=@{},
[psobject]$Body
)

if (-Not $UriParams) { $UriParams = @{} }

switch ($AuthenticationMode) {
"BASIC" {
$Headers.Authorization = "$AuthenticationMode $AuthenticationToken"
}
default {
Write-Verbose "Accessing Bamboo without credentials."
}
}

if ($UriParams -and $UriParams.Keys) {
$Params = ''
foreach($key in $UriParams.Keys) {
$Params+="$key=$($UriParams.$key)&"
}
if ($Params) {
$Uri = "$($Uri)?$Params"
}
}
$response = $null

try {
Write-Verbose "$Method : $Uri"
$response = Invoke-WebRequest -Uri $Uri -Method:$Method -Headers:$Headers -DisableKeepAlive -ContentType $ContentType
} catch {
if ($_.ErrorDetails) {
Write-Error $_.ErrorDetails.Message
} else {
Write-Error $_
}
}

Write-Verbose "Response: $response"
if ($response -is [xml]) {
Write-Debug ($response.OuterXml -replace '><',">`n<")
}
$response
}
59 changes: 30 additions & 29 deletions PsBamboo.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,36 @@
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module
FunctionsToExport = @(
'Add-BambooPlanBranch'
'Copy-BambooPlan'
'Disable-BambooPlan'
'Disable-BambooPlanBranch'
'Enable-BambooPlan'
'Enable-BambooPlanBranch'
'Expand-BambooResource'
'Get-BambooArtifact'
'Get-BambooCurrentUser'
'Get-BambooDeployEnvironmentResult'
'Get-BambooDeployProject'
'Get-BambooDeployProjectEnvironment'
'Get-BambooInfo'
'Get-BambooPlan'
'Get-BambooPlanBranch'
'Get-BambooProject'
'Get-BambooQueuedBuild'
'Get-BambooResult'
'Get-BambooBuildLog'
'Get-BambooServer'
'Invoke-BambooRestMethod'
'Resume-BambooBuild'
'Set-BambooAuthentication'
'Set-BambooServer'
'Start-BambooBuild'
'Stop-BambooQueuedBuild'
)
# Functions to export from this module
FunctionsToExport = @(
'Add-BambooPlanBranch'
'Copy-BambooPlan'
'Disable-BambooPlan'
'Disable-BambooPlanBranch'
'Enable-BambooPlan'
'Enable-BambooPlanBranch'
'Expand-BambooResource'
'Get-BambooArtifact'
'Get-BambooCurrentUser'
'Get-BambooDeployEnvironmentResult'
'Get-BambooDeployProject'
'Get-BambooDeployProjectEnvironment'
'Get-BambooDeployProjectEnvironmentTasks'
'Get-BambooInfo'
'Get-BambooPlan'
'Get-BambooPlanBranch'
'Get-BambooProject'
'Get-BambooQueuedBuild'
'Get-BambooResult'
'Get-BambooBuildLog'
'Get-BambooServer'
'Invoke-BambooRestMethod'
'Resume-BambooBuild'
'Set-BambooAuthentication'
'Set-BambooServer'
'Start-BambooBuild'
'Stop-BambooQueuedBuild'
)

# Cmdlets to export from this module
# CmdletsToExport = '*'
Expand Down
1 change: 1 addition & 0 deletions Tests/Module/expected_commands.csv
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Get-BambooCurrentUser
Get-BambooDeployEnvironmentResult
Get-BambooDeployProject
Get-BambooDeployProjectEnvironment
Get-BambooDeployProjectEnvironmentTasks
Get-BambooInfo
Get-BambooPlan
Get-BambooPlanBranch
Expand Down