Skip to content

Commit

Permalink
Get-Timeline: Add cmdlet (#26)
Browse files Browse the repository at this point in the history
* Add Get-Timeline cmdlet

* Add Timeline class

* Add tests
  • Loading branch information
NickGraham101 authored Dec 2, 2023
1 parent f29ae03 commit 4b04ce2
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Tests/U029-Get-Timeline.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
Push-Location -Path $PSScriptRoot\..\

Describe "Get-Timeline unit tests" -Tag "Unit" {

. .\gandt-azure-devops-tools\Functions\Private\Invoke-AzDevOpsRestMethod.ps1

$SharedParams = @{
Instance = "notarealinstance"
PatToken = "not-a-real-token"
ProjectId = "notarealproject"
BuildId = "1234"
}

It "Will return a Timeline object with all types failed" {
$TestJson = @'
{
"records": [
{
"Result": "failed",
"Type": "Stage"
},
{
"Result": "failed",
"Type": "Job"
},
{
"Result": "succeeded",
"Type": "Task"
},
{
"Result": "failed",
"Type": "Task"
}
]
}
'@

Mock Invoke-AzDevOpsRestMethod { return ConvertFrom-Json $TestJson }

. .\gandt-azure-devops-tools\Classes\Timeline.ps1
. .\gandt-azure-devops-tools\Functions\Public\Build\Get-Timeline.ps1

$TestParams = $SharedParams

$Output = Get-Timeline @TestParams
$Output.GetType().Name | Should Be "Timeline"
$Output.BuildId | Should Be "1234"
$Output.FailedJobs | Should Be $true
$Output.FailedStages | Should Be $true
$Output.FailedTasks | Should Be $true
}

It "Will return a Timeline object with all types not failed" {
$TestJson = @'
{
"records": [
{
"Result": "succeeded",
"Type": "Stage"
},
{
"Result": "succeeded",
"Type": "Job"
},
{
"Result": "succeeded",
"Type": "Task"
},
{
"Result": "succeeded",
"Type": "Task"
}
]
}
'@

Mock Invoke-AzDevOpsRestMethod { return ConvertFrom-Json $TestJson }

. .\gandt-azure-devops-tools\Classes\Timeline.ps1
. .\gandt-azure-devops-tools\Functions\Public\Build\Get-Timeline.ps1

$TestParams = $SharedParams

$Output = Get-Timeline @TestParams
$Output.GetType().Name | Should Be "Timeline"
$Output.BuildId | Should Be "1234"
$Output.FailedJobs | Should Be $false
$Output.FailedStages | Should Be $false
$Output.FailedTasks | Should Be $false
}
}
6 changes: 6 additions & 0 deletions gandt-azure-devops-tools/Classes/Timeline.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Timeline {
[int]$BuildId
[bool]$FailedJobs
[bool]$FailedStages
[bool]$FailedTasks
}
57 changes: 57 additions & 0 deletions gandt-azure-devops-tools/Functions/Public/Build/Get-Timeline.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<#
.NOTES
API Reference: https://learn.microsoft.com/en-us/rest/api/azure/devops/build/timeline/get?view=azure-devops-rest-7.1
#>
function Get-Timeline {
[CmdletBinding()]
param (
#The Visual Studio Team Services account name
[Parameter(Mandatory=$true)]
[string]$Instance,

#A PAT token with the necessary scope to invoke the requested HttpMethod on the specified Resource
[Parameter(Mandatory=$true)]
[string]$PatToken,

#Parameter Description
[Parameter(Mandatory=$true)]
[string]$ProjectId,

#Parameter Description
[Parameter(Mandatory=$true, ParameterSetName="Id")]
[int]$BuildId
)

process {

$GetTimelineParams = @{
Instance = $Instance
PatToken = $PatToken
Collection = $ProjectId
Area = "build"
Resource = "builds"
ResourceId = $BuildId
ResourceComponent = "timeline"
ApiVersion = "7.1-preview.2"
}

$TimelineJson = Invoke-AzDevOpsRestMethod @GetTimelineParams

$Timeline = New-TimelineObject -BuildId $BuildId -TimelineJson $TimelineJson
$Timeline
}
}

function New-TimelineObject {
param (
$BuildId,
$TimelineJson
)

$Timeline = New-Object -TypeName Timeline
$Timeline.BuildId = $BuildId
$Timeline.FailedJobs = $null -ne ($TimelineJson.records | Where-Object {$_.Type -eq "Job" -and $_.Result -eq "failed"})
$Timeline.FailedStages = $null -ne ($TimelineJson.records | Where-Object {$_.Type -eq "Stage" -and $_.Result -eq "failed"})
$Timeline.FailedTasks = $null -ne ($TimelineJson.records | Where-Object {$_.Type -eq "Task" -and $_.Result -eq "failed"})
$Timeline
}

0 comments on commit 4b04ce2

Please sign in to comment.