-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitbucketCommitReport.ps1
248 lines (179 loc) · 10.4 KB
/
BitbucketCommitReport.ps1
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<#
.AUTHOR: Ryan Kajiura - http://ca.linkedin.com/in/ryankajiura
.SYNOPSIS
Script that will generate an HTML report on the Bitbucket project configurations
.DESCRIPTION
This script will access all Bitbucket GET API and proivde a report. Default output will the users download directory
.OUTPUTS
Default save location will be logged in users download directory
Z.PARAMETER -DebugMode
List parameter and variables
.PARAMETER -Verbose
See more detailed progress as the script is running.
.Example Providng ID/Password
GetBitbucketCommitsReport -BitbucketDomain "http://ABC123.com" -username "ryank" -password "abc123" -UsersCommit "ryana, ryanb, ryanc, ryand";
.Example Providng ID/Password - change output directory
GetBitbucketCommitsReport -BitbucketDomain "http://ABC123.com" -username "ryank" -password "abc123" -UsersCommit "ryana, ryanb, ryanc, ryand" -outputlocation "$($env:USERPROFILE)\Downloads\BitbucketAuditReport.html";
.Example Providng ID/Password - change output directory and number of records to return
GetBitbucketCommitsReport -BitbucketDomain "http://ABC123.com" -username "ryank" -password "abc123" -UsersCommit "ryana, ryanb, ryanc, ryand" -outputlocation "$($env:USERPROFILE)\Downloads\BitbucketAuditReport.html" -RecordLimit = 10;
.Reference
Bitbucket Resources
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html
#>
#function GetBitbucketCommitsReport {
# param(
# [Parameter(Mandatory=$true)] [String] ${BitbucketDomain}
# , [Parameter(Mandatory=$true)] [String] ${username}
# , [Parameter(Mandatory=$true)] [String] ${password}
# , [Parameter(Mandatory=$true)] [String[]] ${UsersCommit}
# , [Parameter(Mandatory=$false)] [String] ${outputlocation} = "$($env:USERPROFILE)\Downloads\BitbucketCommitReport.html"
# , [Parameter(Mandatory=$false)] [String] ${limits} = 1000
# , [Parameter(Mandatory=$false)] [bool] $DebugMode = $false
# )
clear;
# write-host "function being executed '$($MyInvocation.MyCommand)' ";
${outputlocation} = "$($env:USERPROFILE)\Downloads\BitbucketCommitReport.html"
# Main Production Server
${BitbucketDomain} = "https://git.pyrsoftware.ca/stash";
${username} = "kajiurya";
${password} = "Pokerst@r00";
${limits} = 1000
$ProjectKey = "SER";
$ProjectName = "Server";
$RepoName = "coreserver";
$FindCommitId = "d43814fe59c";
#${token} = "11b9c1d4675f24417cd264662157c357f0"; # "Report" under kajiurya account
## https://git.pyrsoftware.ca/stash/rest/api/1.0/projects/
## https://git.pyrsoftware.ca/stash/projects/SER/repos/coreserver/commits?merges=include
## t: d43814fe59c and the next one was from c71db0bd044 we would like to know jiras that are includ
#################################################################################################
## Variables - Static
#################################################################################################
[DateTime] $STARTTIME = Get-Date;
[String] $SPACER = "<br />";
[String] $BitbucketURI = "${BitbucketDomain}/rest/api/1.0";
#################################################################################################
## Variables - Session
#################################################################################################
$temp = "";
$htmlreport = @();
$htmlbody = @();
$ListTemp = @{};
$counter = 0;
#######################################################################################################
### DEBUG
#######################################################################################################
# if ($DebugMode -eq $True) {
# foreach ( $key in (Get-Command -Name $MyInvocation.InvocationName).Parameters.Keys ) {
# $value = (Get-Variable $key -ErrorAction SilentlyContinue).Value
# if ( ${value} -or ${value} -eq 0 ) {
# Write-Host "Function parameter...${key} -> ${value}";
# }
# }
# }
#region BeingProcess
#BEGIN {
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes( ( "{0}:{1}" -f ${username}, ${password} ) ) );
$header = @{ Authorization="Basic ${base64AuthInfo}" };
$header["X-Atlassian-Token"] = "nocheck";
#Start-Job -Name "Projects" -ScriptBlock {
$Projects = Invoke-RestMethod -Headers ${header} -Uri "${BitbucketURI}/projects?limit=${limits}" -Method Get;
# https://git.pyrsoftware.ca/stash/rest/api/1.0/projects/
#};
#Wait-Job -Name "Projects";
# } # BEGIN
#endregion BeingProcess
#region Process
#PROCESS{
#---------------------------------------------------------------------
# Getting all Projects - Commits and convert to HTML fragment
#---------------------------------------------------------------------
#region Commits
Write-Host "Collecting Projects commits Information...";
foreach ( $proj in $Projects.values | ? { $_.name -eq $ProjectName -or $_.key -eq $ProjectKey } | SELECT key ) {
Write-Host "Collecting Projects '$($proj.key)' commits Information...";
##Start-Job -Name "Project" -ScriptBlock {
$repos = Invoke-RestMethod -Headers ${header} -Uri "${BitbucketURI}/projects/$($proj.key)/repos?limit=${limits}" -Method Get;
##};
##Wait-Job -Name "Project";
foreach ( $projRepo in $repos.values |
# ? { $_.slug -eq "coreserver" } |
select slug
)
{
##Start-Job -Name "ProjectCommits" -ScriptBlock {
Write-Host "Fetching slug '$($projRepo.slug)' from project '$($ProjectName)' and key '$($proj.key)'...";
#$commits = Invoke-RestMethod -Headers ${header} -Uri "${BitbucketURI}/projects/$($proj.key)/repos/$($projRepo.slug)/commits?limit=${limits}" -Method Get;
$commits = Invoke-RestMethod -Headers ${header} -Uri "${BitbucketURI}/projects/$($proj.key)/repos/$($projRepo.slug)/commits?merges=include&limit=${limits}" -Method Get;
##};
##Wait-Job -Name "ProjectCommits";
$temp = $commits.values | ? { $_.displayId -in $FindCommitId -or $_.id -eq $FindCommitId }
if ( ( $temp | measure ).Count -eq 0) {
continue;
}
foreach ( $commitid in $commits.values ) {
$JobListTemp = New-Object -TypeName PSObject;
$JobListTemp | Add-Member -MemberType NoteProperty -Name id -Value $commitid.id;
$JobListTemp | Add-Member -MemberType NoteProperty -Name displayId -Value $commitid.displayId;
$JobListTemp | Add-Member -MemberType NoteProperty -Name AuthorName -Value $commitid.author.name;
$JobListTemp | Add-Member -MemberType NoteProperty -Name CommitterName -Value $commitid.committer.name;
$JobListTemp | Add-Member -MemberType NoteProperty -Name CommitterDate -Value ( ([datetime]"1/1/1970").AddMilliseconds( $commitid.committerTimestamp).DateTime );
$JobListTemp | Add-Member -MemberType NoteProperty -Name message -Value $commitid.message;
$JobListTemp | Add-Member -MemberType NoteProperty -Name parentsId -Value ($commitid.parents).id;
$JobListTemp | Add-Member -MemberType NoteProperty -Name parentsdisplayId -Value ($commitid.parents).displayId;
$ListTemp.Add( $counter, $JobListTemp );
$counter++;
$temp = $commitid | ? { $_.displayId -eq $FindCommitId -or $_.id -eq $FindCommitId }
if ( ( $temp | measure ).Count -gt 0 ) {
break;
}
} #$commitid
} #$repos.values
$htmlbody += $ListTemp.Values | Sort CommitterDate -Descending | ConvertTo-Html -Fragment;
$htmlbody += ${SPACER};
} #$Projects.values
#endregion Commits
# } # PROCESS
#endregion Process
#region EndProcess
#End {
#---------------------------------------------------------------------
# Generate the HTML report and output to file
#---------------------------------------------------------------------
#region ReportDetail
Write-Host "Producing HTML report";
$reportime = Get-Date -format f;
$elapsedTime = ([datetime]$( ( Get-Date ) - ${StartTime}).Ticks );
#Common HTML head and styles
$htmlhead = "<html>
<style>
BODY {{font-family: Arial; font-size: 8pt;}}
H1 {{font-size: 20px;}}
H2 {{font-size: 18px;}}
H3 {{font-size: 16px;}}
TABLE {{ border: 1px solid black; border-collapse: collapse; font-size: 8pt; }}
TH {{ border: 1px solid black; background: #dddddd; padding: 5px; color: #000000; }}
TD {{ border: 1px solid black; padding: 5px; }}
td.pass {{ background: #7FFF00; }}
td.warn {{ background: #FFE600; }}
td.fail {{ background: #FF0000; color: #ffffff; }}
td.info {{ background: #85D4FF; }}
</style>
<body>
<h1 align=""center"">Server Info: {0}</h1>
<h3 align=""center"">Generated: {1:HH:mm:ss}</h3>
<h3 align=""center"">Elapsed Time to Generate Report: {2:HH:mm:ss}</h3>" -f (
${BitbucketDomain},
${reportime},
${elapsedTime}
);
$htmltail = "</body>
</html>"
$htmlreport = ${htmlhead} + ${htmlbody} + ${htmltail};
#endregion ReportDetail
Write-Verbose -Verbose ( "Total Duration: {0:HH:mm:ss}" -f ${elapsedTime} ) ;
write-Verbose "Generated File ${outputlocation}";
$htmlreport | Out-File ${outputlocation} -Encoding Utf8 -force;
#} #End
#endregion EndProcess
#} #Function