-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghe_branch.ps1
320 lines (292 loc) · 8.88 KB
/
ghe_branch.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#
# ghe_branch.ps1 : GheBranches Implementation Classes
#
class GheBranch
{
[String] $Owner
[String] $Repository
[String] $Name
[Boolean] $Protected
[System.DateTime] $CommitterDate
[String] $CommitterLogin
[String] $CommitterEmail
[String] $Sha
# Default constructor to allow construction with a hashtable
GheBranch() {}
# Copy constructors
GheBranch([GheBranch] $Branch)
{
[GheBranch].GetProperties() | % {
$name = $_.Name
$value = $Branch.psobject.properties[$name].Value
$this.psobject.properties[$name].Value = $value
}
}
}
class GheBranchCollection
{
Hidden [GheClient] $_Client
Hidden [GheUserCollection] $_Users
[Octokit.RepositoriesClient] $RepoClient
[Octokit.Repository] $RepoObject
[System.Collections.ArrayList] $Values
# Default constructor
GheBranchCollection(
[GheClient] $GheClient,
[String] $OrgaName,
[String] $RepoName) : base()
{
$this._Client = $GheClient
$this.RepoClient = [Octokit.RepositoriesClient]::new($GheClient.ApiConnection)
$this.RepoObject = $this.RepoClient.Get($OrgaName, $RepoName).Result
$this.Values = [System.Collections.ArrayList]::new()
# Fill the collection with GitHub repository branches information
[Octokit.RepositoryCommitsClient] $cmit_clt = $this.RepoClient.Commit
ForEach($brch_obj in $this.RepoClient.GetAllBranches($this.RepoObject.Id).Result)
{
$cmit_obj = $cmit_clt.Get($this.RepoObject.Id, $brch_obj.Commit.sha).Result
[GheBranch] $brch_ghe = @{
"Owner" = $OrgaName;
"Repository" = $RepoName;
"Name" = $brch_obj.Name;
"Protected" = $brch_obj.Protection.Enabled;
"CommitterDate" = $cmit_obj.Commit.Committer.Date.DateTime; # Warning UTC format, should be converted
"CommitterLogin" = $cmit_obj.Committer.Login;
"CommitterEmail" = $cmit_obj.Commit.Committer.Email;
"Sha" = $brch_obj.Commit.Sha;
}
$this.Values.Add($brch_ghe)
}
}
# Copy constructor (Does not copy Values content)
GheBranchCollection(
[GheBranchCollection] $BranchColl) : base()
{
$this._Client = $BranchColl._Client
$this._Users = $BranchColl._Users
$this.RepoClient = $BranchColl.RepoClient
$this.RepoObject = $BranchColl.RepoObject
$this.Values = [System.Collections.ArrayList]::new()
}
[void] ExportToCsv([String]$ExportFilePath)
{ $this.ExportToCsv($ExportFilePath, $null, $null) }
[void] ExportToCsv([String]$ExportFilePath, [String[]]$SortOrder, [String[]]$SelectFields)
{
$Collection = $this.Values
if($SortOrder)
{ $Collection = $Collection | Sort-Object $sortorder }
if($SelectFields)
{ $Collection = $Collection.ForEach({ $_ | Select-Object -property $SelectFields }) }
# Export user list into csv File
$Collection | Export-Csv -Path $ExportFilePath -Encoding UTF8 -NoTypeInformation -Force
}
}
enum GheBranchDiffStatus
{
# Protected branch name and expiration date are not checked
Protected
BadName
NotExpired
NoTarget
CompareErr
NotMerged
Expired
}
class GheBranchDiff : GheBranch
{
# Private Fields
Hidden [System.DateTime] $_TargetDate
# Exported Field
[GheBranchDiffStatus] $DiffStatus
[String] $TargetName
[String] $TargetDate
[String] $TargetSha
GheBranchDiff(
[GheBranch] $Branch,
[GheBranchDiffStatus] $DiffStatus) : base($Branch)
{
$this.DiffStatus = $DiffStatus
}
GheBranchDiff(
[GheBranch] $Branch,
[GheBranchDiffStatus] $DiffStatus,
[String] $TargetName) : base($Branch)
{
$this.DiffStatus = $DiffStatus
$this.TargetName = $TargetName
}
GheBranchDiff(
[GheBranch] $Branch,
[GheBranchDiffStatus] $DiffStatus,
[String] $TargetName,
[Octokit.GitHubCommit] $TargetCommit) : base($Branch)
{
$this._TargetDate = $TargetCommit.Commit.Committer.Date.Date
$this.DiffStatus = $DiffStatus
$this.TargetName = $TargetName
$this.TargetDate = $this._TargetDate.ToString()
$this.TargetSha = $TargetCommit.Sha
}
}
class GheBranchCompare : GheBranchCollection
{
GheBranchCompare(
[GheBranchCollection] $SourceColl,
[System.DateTime] $ExpirationDate,
[System.Object[]] $BranchNameMap
) : base($SourceColl)
{
# Analyze the branch Collection
[Octokit.RepositoryCommitsClient] $cmit_clt = $this.RepoClient.Commit
ForEach($brch_obj in $SourceColl.Values)
{
# Check if the branch is protected
if($brch_obj.Protected)
{
$this.Values.Add([GheBranchDiff]::new($brch_obj,
[GheBranchDiffStatus]::Protected))
continue
}
# Check if the branch does follow the naming convention with the
# regex list and compute the target branch name
$trgt_name = ""
$brch_fnd = $null
ForEach($regex_map in $BranchNameMap)
{
$regex_res = $regex_map[0].Match($brch_obj.Name)
$brch_fnd = $regex_res.Success
if($brch_fnd)
{
$trgt_name = $regex_map[1] -f ( $regex_res.Groups | % {$_.Value} )
break
}
}
if($brch_fnd -eq $false)
{
$this.Values.Add([GheBranchDiff]::new($brch_obj,
[GheBranchDiffStatus]::BadName))
continue
}
# Check if the branch is not expired yet
$cmit_obj = $cmit_clt.Get($this.RepoObject.Id, $brch_obj.Sha).Result
if($brch_obj.CommitterDate -gt $ExpirationDate)
{
$this.Values.Add([GheBranchDiff]::new($brch_obj,
[GheBranchDiffStatus]::NotExpired))
continue
}
# If there is no BranchNameMap, we do not have any solution to compare
# source branch to target branch, so we are finished.
if(!$BranchNameMap)
{
$this.Values.Add([GheBranchDiff]::new($brch_obj,
[GheBranchDiffStatus]::Expired))
continue
}
# Find the target branch with the branch name
[Octokit.Branch] $trgt_obj =
$this.RepoClient.GetBranch($this.RepoObject.Id, $trgt_name).Result
if(!$trgt_obj)
{
$this.Values.Add([GheBranchDiff]::new($brch_obj,
[GheBranchDiffStatus]::NoTarget, $trgt_name))
continue
}
# Check if the source branch has been merged into the target
[Octokit.CompareResult] $comp_obj =
$cmit_clt.Compare($this.RepoObject.Id, $trgt_obj.Commit.Sha, $brch_obj.Sha).Result
if(!$comp_obj)
{
$this.Values.Add([GheBranchDiff]::new($brch_obj,
[GheBranchDiffStatus]::CompareErr, $trgt_name))
}
elseif($comp_obj.AheadBy -gt 0)
{
$this.Values.Add([GheBranchDiff]::new($brch_obj,
[GheBranchDiffStatus]::NotMerged,
$trgt_name, $comp_obj.BaseCommit))
}
else
{
$this.Values.Add([GheBranchDiff]::new($brch_obj,
[GheBranchDiffStatus]::Expired,
$trgt_name, $comp_obj.BaseCommit))
}
}
}
# $Branches are [System.Object[]] instead than GheBranchCollection by design as a
# specific GheBranchCollection range can be used instead than the full collection.
[GheMailMsg] CreateMailMsg(
[System.Net.Mail.MailMessage] $MailTemplate,
[Hashtable] $MailContent,
[System.Object[]] $Branches)
{
# Create the mail content depending on required chapters
$mail_txt = ""
$mail_obj = $null
ForEach($grp_brch in $Branches | Group DiffStatus)
{
$chap_txt = $MailContent[$grp_brch.Name]
if($chap_txt)
{
$mail_txt += $chap_txt
$mail_txt += $grp_brch.Group.ForEach(
{ "{0}/{1}/{2}/branches/all?utf8=?&query={3}" -f $ServerUri, $_.Owner, $_.Repository, $_.Name }
) -join "`n"
$mail_txt += "`n"
}
}
# If there is a mail content, create the mail
if($mail_txt)
{
$mail_obj = [GheMailMsg]::new($MailTemplate)
$mail_obj.Body = $mail_obj.Body -f $mail_txt
}
return $mail_obj
}
[GheMailMsgCollection] CreateMailCollection(
[System.Net.Mail.MailMessage] $MailAdmin,
[System.Net.Mail.MailMessage] $MailUsers,
[Hashtable] $MailContent)
{
# Get the user collection if it does not already exist
if(!$this._Users)
{ $this._Users = [GheUserCollection]::Get($this._Client) }
# Create message for all concerned and identified users
$mail_coll = [GheMailMsgCollection]::new()
$brch_coll = $this.Values | Sort CommitterLogin, CommitterEmail, DiffStatus, Name
$brch_pool = [GheBranchCollection]::new($this)
ForEach($grp_usr in ($brch_coll | Group CommitterLogin))
{
# If the CommitterLogin cannot be found in the GitHub Users, or if the user is disabled
# store branches into Administrator mail pool
$usr_obj = $this._Users[$grp_usr.Name]
if(($usr_obj -eq $null) -or
($usr_obj.suspension_status -ne [GheUserStatus]::active))
{
$brch_pool.Values.AddRange($grp_usr.Group)
continue
}
# Create the mail message with Committer branches
$mail_obj = $this.CreateMailMsg($MailUsers, $MailContent, $grp_usr.Group)
if($mail_obj)
{
$mail_obj.To.Add($usr_obj.email)
$mail_coll.Add($mail_obj)
}
}
# Create message for administrators
$mail_obj = $this.CreateMailMsg($MailAdmin, $MailContent, $brch_pool.Values)
if($mail_obj)
{
$this._Users.Values | where-object {
($_.role -eq "admin") -and
($_.suspension_status -eq [GheUserStatus]::active)
} | ForEach-Object {$mail_obj.To.Add($_.email)}
$mail_coll.Add($mail_obj)
}
return $mail_coll
}
[void] ExportToCsv([String] $ExportFilePath)
{ ([GheBranchCollection]$this).ExportToCsv($ExportFilePath, @("DiffStatus", "Name"), $null) }
}