-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atlassian.Bitbucket.Reports.psm1
192 lines (167 loc) · 7.1 KB
/
Atlassian.Bitbucket.Reports.psm1
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using module .\Atlassian.Bitbucket.Repository.psm1
using module .\Atlassian.Bitbucket.Repository.Deployment.psm1
using module .\Atlassian.Bitbucket.Repository.Environment.psm1
<#
.SYNOPSIS
Build a report of deployments for all repos in a project
.DESCRIPTION
Build a report of deployments for all repos in a project
.EXAMPLE
C:\PS> Get-BitbucketRepository -ProjectKey 'KEY' | Get-BitbucketProjectDeploymentReport
# Get the JSON report
.EXAMPLE
C:\PS> Get-BitbucketRepository -ProjectKey 'ZEUS' | Get-BitbucketProjectDeploymentReport -Format HTML | Out-File C:\Temp\report.html
# Generate and save the HTML report.
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER Repo
Repos used to build the report. Pipeline in input from Get-BitbucketRepository.
.PARAMETER Environments
The environments used to generate the deployment report.
.PARAMETER Format
Return the report in JSON or HTML format.
#>
function Get-BitbucketProjectDeploymentReport {
[CmdletBinding()]
param(
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[Alias("Team")]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
ValueFromPipeline = $true,
HelpMessage = 'Repos used to build the report. Use the Get-BitbucketRepo command.')]
$Repo,
[string[]]$Environments = ('Test', 'Staging', 'Production'),
[ValidateSet('JSON', 'HTML')]
[string]$Format = 'JSON'
)
Begin {
$content = @()
$_repos = @()
}
Process {
$_repos += $Repo
}
End {
for ($r = 0; $r -lt $_repos.Count; $r++) {
$_repo = $_repos[$r]
Write-Progress -Id 0 -Activity "Getting Deployments for Repo: $($_repo.name)" -PercentComplete ((($r + 1) / $_repos.Count) * 100)
$_env = Get-BitbucketRepositoryEnvironment -Workspace $Workspace -RepoSlug $_repo.slug | Where-Object { $_.Name -in $Environments }
$envList = @()
for ($e = 0; $e -lt $_env.Count; $e++) {
Write-Progress -Id 1 -Activity "Environment: $($_env[$e].name)" -PercentComplete ((($e + 1) / $_env.Count) * 100)
$Fields = ('values.deployable.commit.message', 'values.deployable.commit.date', 'values.deployable.commit.author.user')
$deployment = Get-BitbucketRepositoryDeployment -Workspace $Workspace -RepoSlug $_repo.slug -EnvironmentUUID $_env[$e].uuid -Limit 1 -Fields $Fields
if ($deployment) {
$envList += [PSCustomObject]@{
EnvironmentName = $_env[$e].name
State = $deployment.state.name
Status = $deployment.state.status.name
Commit = [PSCustomObject]@{
Hash = $deployment.deployable.commit.hash
Message = $deployment.deployable.commit.message
Date = $deployment.deployable.commit.date
Author = [PSCustomObject]@{
User = [PSCustomObject]@{
DisplayName = $deployment.deployable.commit.author.user.display_name
}
}
}
Pipeline = $deployment.release.name
URL = $deployment.release.url
Time = $deployment.last_update_time
}
}
}
$content += [PSCustomObject]@{
RepoName = $_repo.name
Environments = $envList
}
}
switch ($Format) {
'HTML' {
return Get-BitbucketProjectDeploymentReportHTML -Environments $Environments -Content $Content
}
Default {
return $content
}
}
}
}
function Get-BitbucketProjectDeploymentReportHTML {
[OutputType('System.String')]
[CmdletBinding()]
param(
[string[]]$Environments = ('Dev', 'Test', 'Staging', 'Production'),
$Content
)
$HTMLReport = Get-Content "$PSScriptRoot\Templates\DeploymentReport.html"
$HTMLRow = Get-Content "$PSScriptRoot\Templates\DeploymentReportRow.html"
$HTMLCell = Get-Content "$PSScriptRoot\Templates\DeploymentReportCell.html"
$rowID = 1
$rows = @()
# Build each row
foreach ($repo in $content) {
# Build each Cell
$cells = @()
$previous = -1
foreach ($env in $Environments) {
$deployment = $repo.environments | Where-Object { $_.EnvironmentName -eq $env }
# Attempt to grab the pipeline run
[int]$current = 0
if ($deployment) {
[int]::TryParse($deployment.Pipeline.Replace('#', ''), [ref]$current) | Out-Null
}
if ($previous -ne -1) {
if ($previous -gt $current) {
$cells += '<div class="compare gt">></div>'
}
elseif ($previous -eq $current) {
$cells += '<div class="compare">=</div>'
}
else {
$cells += '<div class="compare lt"><</div>'
}
}
$previous = $current
if ($deployment) {
# Calculate Status
if ($deployment.State -eq 'COMPLETED') {
$status = $deployment.Status
}
else {
$status = $deployment.State
}
$cells += $HTMLCell.
Replace('##ROW_ID##', $rowID).
Replace('##ENVIRONMENT_NAME##', $env).
Replace('##STATUS##', $status).
Replace('##URL##', $deployment.URL).
Replace('##PIPELINE##', $deployment.Pipeline).
Replace('##COMMIT_HASH##', $deployment.Commit.Hash.Substring(0, 7)).
Replace('##COMMIT_MESSAGE##', $deployment.Commit.Message).
Replace('##TIME##', $deployment.Time)
}
else {
$cells += $HTMLCell.
Replace('##ROW_ID##', $rowID).
Replace('##ENVIRONMENT_NAME##', $env).
Replace('##STATUS##', 'BLANK').
Replace('##URL##', '').
Replace('##PIPELINE##', '').
Replace('##COMMIT_HASH##', '').
Replace('##COMMIT_MESSAGE##', '').
Replace('##TIME##', '')
}
}
$rows += $HTMLRow.
Replace('##ROW_ID##', $rowID).
Replace('##REPO##', $repo.RepoName).
Replace('##CELLS##', $cells)
$rowID += 1
}
return $HTMLReport.
Replace('##DATE##', (Get-Date).ToString()).
Replace('##ROWS##', $rows)
}