-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetupSourceTree.ps1
1082 lines (919 loc) · 43.9 KB
/
SetupSourceTree.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#settings
$devOpsCollectionUrl="https://<Your_AzureDevOps_FQDN>/tfs/<Your_Project_Collection_Name>"
$defaultPatName="SourceTree"
######## DO NOT MODIFY BELOW ########
## HELPERS
function Disable-CertificateChecks{
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
[ServerCertificateValidationCallback]::Ignore()
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]([string]::Join(",",$([enum]::GetNames([System.Net.SecurityProtocolType]) | ?{$_ -ne "SystemDefault"})))
}
function Get-Software ($filter) {
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" | %{
gp $_ -ErrorAction 0|?{$filter -eq $null -or $_.DisplayName -imatch $filter -or $_.Publisher -imatch $filter -or $_.InstallLocation -imatch $filter}|%{
[pscustomObject]@{
DisplayName= $_.DisplayName
Language = $_.Language
DisplayVersion = $_.DisplayVersion
Version = $_.Version
Publisher = $_.Publisher
InstallDate = if([string]::IsNullOrEmpty($_.InstallDate)){$null}else{[DateTime]::ParseExact($_.InstallDate,"yyyyMMdd",$null)}
InstallSource = $_.InstallSource
InstallLocation = $_.InstallLocation
UninstallString = $_.UninstallString
ModifyPath = $_.ModifyPath
EstimatedSize = $_.EstimatedSize/1KB
}
}
}
}
function Get-ApplicationPath ($name){
$app = (Get-Command $name -ErrorAction 0).Source
if($app -eq $null){
$app = Get-Software ($name -replace '.exe$','') | %{ Get-ChildItem $_.InstallLocation -Filter $name -Recurse | %{ $_.FullName } } |?{ $_ -ne $null }
}
return $app
}
function Write-ErrIfAny($msg){
if(-not [string]::IsNullOrEmpty($msg)){
throw $msg
}
}
function Test-Url($url, $count){
begin{if($count -eq $null){$count=3}}
process{
try{
$res = Invoke-WebRequest -WebSession $webSession -Method Head -Uri $url
$ok= $res.StatusCode -in 200,302,301;
return $ok
}catch{
if($_.Exception.Response.StatusCode -eq 401 -and $count -gt 0){
$global:webSession.UseDefaultCredentials = $false
$global:webSession.Credentials = $(Get-Credential -Message "[$($count-2)/3] Insert your credentials for $url");
return Test-Url $url ($count-1)
}
}
return $false
}
}
function New-ShellLink{
param ( [string]$SourceExe, [string]$ArgumentsToSourceExe, [string]$DestinationPath)
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($DestinationPath)
$Shortcut.TargetPath = $SourceExe
$Shortcut.Arguments = $ArgumentsToSourceExe
$Shortcut.Save()
}
function Invoke-DownloadFile{
param(
[Parameter(Mandatory=$true)]
$url,
[Parameter(Mandatory=$true)]
$destination,
[switch]$includeStats
)
$global:downloadCompleted=$false
$wc = New-Object Net.WebClient
$wc.UseDefaultCredentials = $Global:webSession.UseDefaultCredentials
$wc.Credentials = $Global:webSession.Credentials
$wc.Proxy = $Global:webSession.Proxy
$file = $url | Split-Path -Leaf
$start = Get-Date
$null = Register-ObjectEvent -InputObject $wc -EventName DownloadProgressChanged `
-MessageData @{start=$start;includeStats=$includeStats;url=$url} `
-SourceIdentifier WebClient.DownloadProgressChanged -Action {
filter Get-FileSize {
"{0:N2} {1}" -f $(
if ($_ -lt 1kb) { $_, 'Bytes' }
elseif ($_ -lt 1mb) { ($_/1kb), 'KB' }
elseif ($_ -lt 1gb) { ($_/1mb), 'MB' }
elseif ($_ -lt 1tb) { ($_/1gb), 'GB' }
elseif ($_ -lt 1pb) { ($_/1tb), 'TB' }
else { ($_/1pb), 'PB' }
)
}
$elapsed = ((Get-Date) - $event.MessageData.start)
#calculate average speed in Mbps
$averageSpeed = ($EventArgs.BytesReceived * 8 / 1MB) / $elapsed.TotalSeconds
$elapsed = $elapsed.ToString('hh\:mm\:ss')
#calculate remaining time considering average speed
$remainingSeconds = ($EventArgs.TotalBytesToReceive - $EventArgs.BytesReceived) * 8 / 1MB / $averageSpeed
$receivedSize = $EventArgs.BytesReceived | Get-FileSize
$totalSize = $EventArgs.TotalBytesToReceive | Get-FileSize
Write-Progress -Activity (" $($event.MessageData.url) {0:N2} Mbps" -f $averageSpeed) `
-Status ("{0} of {1} ({2}% in {3})" -f $receivedSize,$totalSize,$EventArgs.ProgressPercentage,$elapsed) `
-SecondsRemaining $remainingSeconds `
-PercentComplete $EventArgs.ProgressPercentage
if ($EventArgs.ProgressPercentage -eq 100){
Write-Progress -Activity (" $url {0:N2} Mbps" -f $averageSpeed) `
-Status 'Done' -Completed
if ($event.MessageData.includeStats.IsPresent){
([PSCustomObject]@{Name='Invoke-DownloadFile';TotalSize=$totalSize;Time=$elapsed}) | Out-Host
}
}
return;
}
$null = Register-ObjectEvent -InputObject $wc -EventName DownloadFileCompleted `
-MessageData @{destination=$destination;client=$wc} `
-SourceIdentifier WebClient.DownloadFileCompleted -Action {
Get-Item $event.MessageData.destination | Unblock-File | Out-Null
$global:downloadCompleted = $true;
$event.MessageData.client.Dispose();
return;
}
try {
$task = $wc.DownloadFileAsync($url, $destination)
while(-not $global:downloadCompleted){
# Waiting end of the download
}
}
catch [System.Net.WebException] {
Write-Error "Download of $url failed" -ErrorAction Stop
}
finally {
$wc.Dispose()
Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged -Force | Out-Null
Unregister-Event -SourceIdentifier WebClient.DownloadFileCompleted -Force | Out-Null
}
}
function Out-FileUtf8NoBOM{
[cmdletbinding()]
param(
[parameter(Position = 0)]
[string]$path,
[parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 1)]
[string]$input
)
Remove-Item $path -Force -ea 0 | out-null
""|out-file $path -Force
[System.IO.File]::WriteAllText($path,$input,[System.Text.Encoding]::ASCII) | out-null
}
## TFS Methods
function Get-TFSContext($collectionUrl){
$res = Invoke-RestMethod -WebSession $global:webSession `
"$collectionUrl/_apis/Contribution/HierarchyQuery" `
-Method Post -Headers @{
"accept"="application/json;api-version=5.0-preview.1;excludeUrls=true;enumsAsNumbers=true;msDateFormat=true;noArrayWrap=true"
"content-type"="application/json"
} `
-Body '{"contributionIds":["ms.vss-web.legacy-platform-data-provider"],"dataProviderContext":{"properties":{}}}'
$webContext = $res.dataProviders.'ms.vss-web.legacy-platform-data-provider'.pageContext.webContext
return [pscustomobject]@{
user=$webContext.user
collection=$webContext.collection
account=$webContext.account
}
}
function Get-TFSCurrentUserProfile($url){
$res = Invoke-RestMethod "$url/_api/_common/GetUserProfile?__v=5" -WebSession $global:webSession -Method Get -ErrorAction 0
if($res -eq $null){
throw "Get-TFSCurrentUserProfile::User profile not accessible for the URL: $url"
}
return [pscustomobject]@{
Username = $res.identity.SubHeader
Email = $res.identity.MailAddress
DisplayName = $res.identity.FriendlyDisplayName
}
}
function New-TFSPrivateAccessToken($tfsUrl, $patName){
##Get RequestToken
$pats = Invoke-RestMethod -WebSession $global:webSession "$tfsUrl/_apis/Token/SessionTokens?displayFilterOption=1&createdByOption=3&sortByOption=3&isSortAscending=true&startRowNumber=1&pageSize=100" -Method Get -Headers @{
"accept"="application/json;api-version=5.0-preview.1;excludeUrls=true;enumsAsNumbers=true;msDateFormat=true;noArrayWrap=true"
}
$pat = $pats | ?{ $_.IsValid -and $_.displayName -eq $patName} | select -first 1
$context = Get-TFSContext $tfsUrl
if($pat -ne $null){
#delete
Invoke-RestMethod -WebSession $global:webSession "$tfsUrl/_apis/Token/SessionTokens/$($pat.authorizationId)" -Method Delete | Out-Null;
#prepare the PAT query to renew it
$pat.authorizationId=[string]::Empty
$pat.validFrom = Get-Date
$pat.validTo = (Get-Date).AddYears(1)
#$pat.accessId = [Guid]::NewGuid().ToString()
$pat.targetAccounts=[string[]]@($context.collection.id)
$body = [pscustomobject]@{
"contributionIds" = [string[]]@("ms.vss-token-web.personal-access-token-issue-session-token-provider")
"dataProviderContext" = [pscustomobject]@{
"properties" = [pscustomobject]$pat
}
}
}else{
$body = [pscustomobject]@{
"contributionIds" = [string[]]@("ms.vss-token-web.personal-access-token-issue-session-token-provider")
"dataProviderContext" = [pscustomobject]@{
"properties" = [pscustomobject]@{
"clientId"="00000000-0000-0000-0000-000000000000"
"accessId"=[Guid]::NewGuid().ToString()
"authorizationId"=""
"validFrom" = Get-Date
"validTo" = (Get-Date).AddYears(1)
"userId" = $null
"displayName"=$patName
"scope"="vso.code_write vso.packaging_write vso.release_execute vso.profile"
"targetAccounts"=[string[]]@($context.collection.id)
"token"=$null
"alternateToken"=$null
"isValid"=$true
"isPublic"=$false
"publicData"=$null
"source"=$null
"claims"=$null
}
}
}
}
#create PAT
$resp = Invoke-RestMethod "$tfsUrl/_apis/Contribution/HierarchyQuery" -WebSession $global:webSession -Method Post -Body $($body|ConvertTo-Json -Depth 99 -Compress) -Headers @{
"accept"="application/json;api-version=5.0-preview.1;excludeUrls=true;enumsAsNumbers=true;msDateFormat=true;noArrayWrap=true"
"content-type"="application/json"
}
$res = $resp.dataProviders.'ms.vss-token-web.personal-access-token-issue-session-token-provider'.token
return $res;
}
## Git Methods
function New-GitCommitMessageTemplate{
return @"
#######################: 72 Characters :###############################
# Title line
type(scope): subject
# BLANK LINE: Do not add text below.
# Body: explain the propose of that commit to answer the how and why
Your commit message
# BLANK LINE: Do not add text below.
# Footer
# tags:
#
###########################: HELP :####################################
# # HELP
#
# Lines starting with # are ignored by Git
#
# ## Title line:
# * Maximum of 72 characters
# * Do NOT capitalize first letters
# * Format: <type>(<scope>): <subject>
# - <type>: cf. table below
# - (<scope>): optional, specify a place into the repository
# eg. (SolutionName/ComponentName)
# - <subject> MUST starts with an imperative verbs
# > add
# > update
# > remove
# > upgrade
# > downgrade
#
# | <type> | Description |
# |----------|--------------------------------------------------------|
# | build | Changes affecting the build or external dependencies |
# | ci | Changes to our CI configuration files and scripts\
# eg. Azure App Configuration, Azure Pipeline |
# | docs | Change performed only on the documentation |
# | feat | Adding a new feature/behavior to the source code |
# | fix | Correct a bug in the source code |
# | perf | Code changes which improves the performance |
# | refactor | Code changes which is not a fix or add a feature |
# | style | Beautifying the source code without build impacts\
# eg. white-spaces, code formatting... |
# | test | Adding missing unit tests or fixing an existing one |
# | revert | When a commit must be rolling back.\
# *Attention*\
# - <subject> MUST reuse the subject from the \
# previous commit.
# - <body> MUST start with:\
# 'This reverts commit <previous_commit_hash>' |
#
#
# ## Body line(-s)
# * MUST use the imperative, present tense
# *
# * Maximum of 72 characters per line for word wrapping
# * No line number limits
#
#
# ## Footer line
# * Format: tags: <branch_name>[; <INC|ENHCXXXXX>[; <INC|ENHCXXXXX>[; <INC|ENHCXXXXX>]]]
#
"@ -replace '[\r\n]$',''
}
function New-GitHooks{
$preCommit =
return @{
"commit-msg" =
{param($repositoryPath, $messagePath)
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$global:errorCount = 0;
function Write-Check{
param([string]$message, [switch]$passed, [switch]$warning)
$warnOnly = $warning.IsPresent -and $warning.ToBool()
$ok=$passed.IsPresent -and $passed.ToBool()
$mark = if($ok){
@{
Object = [regex]::Unescape("\u2714")
ForegroundColor = 'Green'
NoNewLine = $true
}
}else{
if($warnOnly){
@{
Object = [regex]::Unescape("\u26a0")
ForegroundColor = 'Yellow'
NoNewLine = $true
}
}else{
@{
Object = [regex]::Unescape("\u2716")
ForegroundColor = 'Red'
NoNewLine = $true
}
}
}
Write-Host "[ " -NoNewline
Write-Host @mark
Write-Host " ] " -NoNewline
Write-Host $message
if(-not $ok -and -not $warnOnly){
$global:errorCount += 1;
}
}
$currentBranchName = $(git rev-parse --abbrev-ref HEAD)
$currentBranchNameTrimmed = $currentBranchName -replace '^[^\/]+\/',''
#BranchName checks
$pattern="^(master|develop|feature\/(INC|ENHC)\d{5,7}|hotfix\/INC\d{5,7}|bugfix\/.+|release\/CHG\d{5,7})"
Write-Check "The current branch name $currentBranchName follow the pattern -> $pattern" -passed:($currentBranchName -match $pattern)
#Commit Check
$message = Get-Content $messagePath -ErrorAction 0;
if($null -eq $message){$message = ""}
$lines = $message.Split([Environment]::NewLine)
$linesChanged = $false
$firstLine = $lines | Select-Object -First 1
#first line
$pattern="^(?<type>build|ci|docs|feat|fix|perf|refactor|style|test|revert)(?:\((?<scope>[^\)]+)\))?:\s(?<subject>.+)"
## Rewite full Message when git-flow release finish
$mergingCommit = "$(git rev-parse -q --verify MERGE_HEAD)"
$isMerging = $mergingCommit -ne [string]::Empty
if($isMerging -and $firstLine -imatch "^merge branch 'release\/[^']+'(?:\s+)?$"){
$releaseBranchName = $firstLine -replace "^merge branch '(release\/[^']+)'(?:\s+)?$",'$1'
$releaseName = $releaseBranchName -replace "[^\/]+\/([^\/]+)$",'$1'
$firstLine = "build: merge release $releaseName"
$lines = @(
$firstLine,
"",
"Merge '$releaseBranchName' back to '$currentBranchName with the following:"
)
($(git cherry -v $currentBranchName $releaseBranchName) | Out-String) -split "[\r\n]+" | ?{ -not [string]::IsNullOrEmpty($_) } | %{
$title = $_ -ireplace '^\+\s[\dabcdef]{40}\s',''
if($title -imatch $pattern){
$title = $title -replace $pattern,'${subject}'
}
if($title.Length -gt 70){
$title = $title.SubString(0, 67)+"..."
}
$lines+="* $title"
}
$lines += ""
$lines += "`r`n`r`ntags: $releaseName";
$linesChanged = $true;
}
if($isMerging -and $firstLine -imatch "^merge tag '[^']+'.*"){
$firstLine = "build: merge tag $($firstLine -replace "^merge tag ('[^']+').*",'$1')"
$linesChanged = $true;
}
## ends: Rewite full Message when git-flow release finish
$matches = [regex]::Matches($firstLine, $pattern);
Write-Check "Title line follow the pattern -> $pattern" -passed:(-not ($matches -eq $null -or $matches.Count -eq 0))
#second line
Write-Check "The second line is empty" -passed:($($lines | Select-Object -First 1 -Skip 1) -eq "")
# Content lines less than 73 chars
Write-Check "The message lines contains of maximum 72 characters" -passed:($($lines | Where-Object{$_.Length -gt 72}).Count -eq 0)
#Footer line
$footerBlankLine=$($lines | Select-Object -Last 2|Select-Object -First 1) -eq "";
$startTags =($($lines| Select-Object -Last 1) -match "^tags:\s$currentBranchNameTrimmed")
Write-Check "The line before the footer is empty" -passed:$footerBlankLine -warning
Write-Check "The footer starts with required contents -> tags: $currentBranchNameTrimmed" -passed:$startTags -warning
$lastLine = $($lines| Select-Object -Last 1).Trim() -replace ',',';'
$linesChanged = $linesChanged -bor $lastLine -ne $($lines| Select-Object -Last 1).Trim()
if(-not $startTags){
$linesChanged = $true;
$lastLine = if($lastLine -match '^(?:[\r\n]+)?tags:'){
$matches = [regex]::Matches($lastLine, '^(?:[\r\n]+)?tags:(.*)')
$tagsValue = $matches.Groups.Item(1).Value.Trim()
"$($lastLine -replace '^([\r\n]+).+','$1')tags: $currentBranchNameTrimmed"+$(if($tagsValue.Length -gt 0){"; $tagsValue"})
}else{
"$lastLine`r`n`r`ntags: $currentBranchNameTrimmed"
}
write-host "Tags added to the last commit message line"
}
if(-not $footerBlankLine -and $startTags){
$linesChanged = $true;
$lastLine = "`r`n$lastLine";
}
if($linesChanged){
$lines[0]=$firstLine
$lines[$lines.Length-1]=$lastLine
[System.IO.File]::WriteAllText($messagePath,$([string]::Join("`r`n",$lines)),[System.Text.Encoding]::ASCII) | out-null
Write-Host "Commit message updated !"
}
#exit
if($global:errorCount -gt 0){
write-output "$global:errorCount errors, please make sure your commit message follow guidelines."
}
exit $global:errorCount
}.ToString()
"prepare-commit-msg" =
{param($repositoryPath, $messagePath)
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$currentBranchName = $(git rev-parse --abbrev-ref HEAD)
$currentBranchNameTrimmed = $currentBranchName -replace '^[^\/]+\/',''
$revertCommitHash = "$(git rev-parse -q --verify REVERT_HEAD)"
$isReverting = $revertCommitHash -ne ""
#building the message
if($isReverting){
$firstLine = $(git log --format=%B -n 1 $revertCommitHash).Split([Environment]::NewLine) | Select-Object -First 1
$firstLine = $firstLine -replace '^build|ci|docs|feat|fix|perf|refactor|style|test|revert'
$firstLine = $(if($firstLine -match '^\('){""}else{":"})+$firstLine;
$message=@"
revert$firstLine
This reverts commit $revertCommitHash.
tags: $currentBranchNameTrimmed
"@ -replace '^\s+|\s+$','';
[System.IO.File]::WriteAllText($messagePath,$message,[System.Text.Encoding]::ASCII) | out-null
}
exit 0}.ToString()
}
}
function Set-GitGlobalConfig($userDisplayName, $userEmail, [object[]]$UrlTokens){
$gitBin = Get-ApplicationPath git.exe | select -First 1
#disable globally SSL Verification checks in case of self-signed certificate
&$gitbin config --global http.sslVerify false
&$gitbin config --global user.name "$userDisplayName"
&$gitbin config --global user.email $userEmail
$UrlTokens | %{
$token64 = $([Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$($_.token)"))).Trim()
&$gitbin config --global http.$($_.url).extraHeader "Authorization: Basic $token64"
}
$usrGitPath = "$env:USERPROFILE\.git"
mkdir $usrGitPath -Force | out-null
#set default commit message template
$templatePath = "$usrGitPath\git_msg_template.txt"
New-GitCommitMessageTemplate | Out-FileUtf8NoBOM $templatePath
&$gitbin config --global commit.template "$templatePath"
#Hooks
$hooksPath = "$usrGitPath\hooks"
mkdir $hooksPath -Force | out-null
&$gitbin config --global core.hookspath `"~/.git/hooks`"
$hooks=New-GitHooks
$hooks.Keys | %{
"#!/bin/sh`r`npowershell.exe `"-NoProfile`" `"-ExecutionPolicy`" `"RemoteSigned`" `"-File`" `$(echo ~/.git/hooks/$_.ps1) `$(pwd) `$(pwd)'/'`$1 `$2 `$3 `$4 `$5 `$6`r`nexit" | Out-FileUtf8NoBOM "$hooksPath\$_"
$hooks[$_] | Out-FileUtf8NoBOM "$hooksPath\$_.ps1"
}
}
function Get-GitOnlineVersions{
$pattern = if([Environment]::Is64BitOperatingSystem){'Git-\d+\.\d+\.\d+.*-64-bit\.exe$'}else{'Git-\d+\.\d+\.\d+.*-32-bit\.exe$'}
$res = Invoke-RestMethod https://api.github.com/repos/git-for-windows/git/releases -WebSession $global:webSession
$res | ?{ -not $_.prerelease } | %{
[pscustomobject]@{
version=[Version]($_.name -replace '.+(\d+\.\d+\.\d+)(\((\d+)\))?$','$1.$2' -replace '[\(\)]|\.$')
url=$_.assets | ?{$_.name -imatch $pattern } | %{ $_.browser_download_url } | select -First 1
}
}
}
function Install-Git([bool]$latest){
begin{
$versions = Get-GitOnlineVersions;
$version = $versions | select -First 1
}
process{
if($latest -ne $true){
Write-Host "Available Git versions: "
$versions | %{ write-host $(" $(if($version -eq $_){"[*]"}else{"[ ]"}) $($_.version)") }
$choice = $null
while($true){
$choice = $(Read-Host "Which version must be installed ? [$($version.version)]").Trim()
if([string]::IsNullOrEmpty($choice)){
break;
}
[Version]$v = $null
if([Version]::TryParse($choice, [ref]$v)){
$x = $versions| ?{ $_.version -eq $v } | select -First 1
if($x -ne $null){
$version = $x
break;
}
}
}
}
#Download
$fileName = Split-Path $version.url -Leaf
$fileFullName = "$env:TEMP\$fileName"
Remove-Item $fileFullName -Force -ErrorAction 0;
## Buggy with GitHub - https://powershell.org/forums/topic/bits-transfer-with-github/
#Start-BitsTransfer -Source $version.url -Destination $fileFullName
$wc = New-Object Net.WebClient
$wc.UseDefaultCredentials = $Global:webSession.UseDefaultCredentials
$wc.Credentials = $Global:webSession.Credentials
$wc.Proxy = $Global:webSession.Proxy
Invoke-DownloadFile $version.url $fileFullName
#Install
Write-Warning "`tFollow the installation wizard`r`nthen the setup process will continue."
Start-Sleep -Seconds 2
$p = Start-Process $fileFullName -PassThru
do {start-sleep -Milliseconds 500}
until ($p.HasExited)
}
}
## Sourcetree Methods
function Set-SourceTreeCredentials([uri]$url,[string]$userEmail, [string]$patToken){
begin{
$sourcetreeBin = Get-ApplicationPath sourcetree.exe | Sort-Object | select -First 1
$sourceTreeDirectory = Split-Path $sourcetreeBin
[Reflection.Assembly]::LoadFrom("$sourceTreeDirectory\SourceTree.Accounts.Windows.dll") | Out-Null
[Reflection.Assembly]::LoadFrom("$sourceTreeDirectory\SourceTree.Api.UI.Wpf.dll") | Out-Null
}
process{
$credCategory="sourcetree-rest"
$credKey = "{0}://{1}{2}{3}" -f
$url.Scheme,
$(if([string]::IsNullOrEmpty($userEmail)){[string]::Empty}else{($userEmail -replace '@','_')+"@"}),
$url.Host,
$(if($url.Authority.Contains(':')){":$($url.Port)"}else{[string]::Empty})
$credManager = New-Object SourceTree.Accounts.Windows.CredentialManagerSecretManager
$credManager.SaveSecretByCategory($credCategory,$credKey,$patToken);
#Write-Verbose "$credKey::$userEmail::$patToken" -Verbose
#Write-Verbose "Read::$([SourceTree.Utils.StringExtensions]::ToUnsecureString($credManager.ReadSecretByCategory($credCategory, $credKey)))" -Verbose
}
}
function Set-SourceTreeAccounts([string]$url, $userDisplayName, $userEmail){
begin{
$filePath = "$env:APPDATA\Atlassian\SourceTree\accounts.json"
[System.Collections.ArrayList]$accounts = $null;
if(Test-Path $filePath){
$o = ([object[]](Get-Content $filePath | Out-String | ConvertFrom-Json)) | ?{ $_.HostInstance.BaseUrl -ine $devOpsCollectionUrl }
if($o.Count -eq 0 -or $o.Count -eq $null){[object[]]$o=@([object]$o)}else{$o=[object[]]$o}
$accounts = ([System.Collections.ArrayList]$o)|?{[string]::IsNullOrEmpty($_)};
}
if($accounts -eq $null -or $accounts.Count -eq 0){
$accounts = [System.Collections.ArrayList]@(,$([pscustomobject]@{
"`$id"= "1"
"`$type"= "SourceTree.Api.Host.Identity.Model.IdentityAccount, SourceTree.Api.Host.Identity"
"IsDefault"= $false
"Authenticate"= $true
"HostInstance"= [pscustomobject]@{
"`$id"= "2"
"`$type"= "SourceTree.Host.Bitbucket.BitbucketInstance, SourceTree.Host.Bitbucket"
"Host"=[pscustomobject]@{
"`$id"= "3"
"`$type"= "SourceTree.Host.Bitbucket.BitbucketHost, SourceTree.Host.Bitbucket"
"Id"= "bitbucket"
}
"BaseUrl" = "https://bitbucket.org/"
"Protocol"= "HTTPS"
}
"Credentials" = [pscustomobject]@{
"`$id"= "4"
"`$type"= "SourceTree.Api.Account.OAuth.TwoZero.OAuthTwoZeroCredentials, SourceTree.Api.Account.OAuth.TwoZero"
"Username"= ""
"AuthenticationScheme" = [pscustomobject]@{
"`$type"="SourceTree.Api.Account.OAuth.TwoZero.OAuthTwoZeroBearerAuthenticationScheme, SourceTree.Api.Account.OAuth.TwoZero"
"Value"= "Personal Access Token"
"Name"= "OAuth"
"Description"= "OAuth Token"
"HeaderValuePrefix"= "Bearer"
"UsernameIsRequired"= $false
}
"EmailHash"= $null
"DisplayName"= ""
"AvatarURL" = $null
"Id" = $null
"Email" = $null
}
}))
}
### Get last json Id
$script:lastId = [int](
(@($accounts.'$id')+($accounts|Get-Member -ErrorAction 0|%{$accounts.($_.Name).'$id'})
)|?{-not[string]::IsNullOrEmpty($_) -and -not [string]::IsNullOrWhiteSpace($_)}|Sort-Object -Descending |select -First 1
)
function nextId{
$script:lastId+=1
$script:lastId.ToString()
}
}
process{
$accounts.Add($([pscustomobject]@{
"`$id"= nextId
"`$type"= "SourceTree.Model.ScmAccount, SourceTree.Api.Host.Scm"
"IsDefault"= $false
"Authenticate"= $true
"HostInstance"= [pscustomobject]@{
"`$id"= nextId
"`$type"= "Sourcetree.Host.Msft.TeamServices.VstsHostInstance, Sourcetree.Host.Msft.TeamServices"
"Host"=[pscustomobject]@{
"`$id"= nextId
"`$type"= "Sourcetree.Host.Msft.TeamServices.VstsHost, Sourcetree.Host.Msft.TeamServices"
"Id"= "vsts"
}
"BaseUrl" = $url
"Protocol"= "HTTPS"
}
"Credentials" = [pscustomobject]@{
"`$id"= nextId
"`$type"= "Sourcetree.Api.Account.Pat.PersonalAccessTokenCredentials, Sourcetree.Api.Account.Pat"
"Username"= $userEmail
"AuthenticationScheme" = [pscustomobject]@{
"`$type"="Sourcetree.Api.Account.Pat.PersonalAccessTokenAuthenticationScheme, Sourcetree.Api.Account.Pat"
"Value"= "Personal Access Token"
"Name"= "Personal Access Token"
"Description"= "Personal Access Token"
"HeaderValuePrefix"= "Personal Access Token"
"UsernameIsRequired"= $false
}
"EmailHash"= $null
"DisplayName"= $userDisplayName
"AvatarURL" = $null
"Id" = $null
"Email" = $null
}
})) | Out-Null
$accounts|ConvertTo-Json -Depth 99 |Out-File $filePath -Encoding utf8
}
}
function Set-SourceTreeUserSettings($userDisplayName, $userEmail){
begin{
$gitBin = Get-ApplicationPath git.exe | select -First 1
$sourcetreeBin = Get-ApplicationPath sourcetree.exe | Sort-Object | select -First 1
$sourceTreeDirectory = Split-Path $sourcetreeBin
[Reflection.Assembly]::LoadFrom("$sourceTreeDirectory\SourceTree.Accounts.Windows.dll") | Out-Null
[Reflection.Assembly]::LoadFrom("$sourceTreeDirectory\SourceTree.Api.UI.Wpf.dll") | Out-Null
$sourceTreeUserConfig = (Get-ChildItem -Filter 'user.config' -Recurse $env:LOCALAPPDATA\Atlassian).FullName |Sort-Object|select -First 1
$configManager = New-Object SourceTree.Configuration.DefaultConfigurationManager
$licenseManager = New-Object SourceTree.Licence.DefaultLicenceManager $configManager
$ClientConfigPathsType = [System.Configuration.ConfigurationManager].Assembly.GetType("System.Configuration.ClientConfigPaths")
$_roamingConfigDirectoryField = $ClientConfigPathsType.GetField("_roamingConfigDirectory", [System.Reflection.BindingFlags]"Instance,NonPublic")
$_roamingConfigFilenameField = $ClientConfigPathsType.GetField("_roamingConfigFilename", [System.Reflection.BindingFlags]"Instance,NonPublic")
$_localConfigDirectoryField = $ClientConfigPathsType.GetField("_roamingConfigDirectory", [System.Reflection.BindingFlags]"Instance,NonPublic")
$_localConfigFilenameField = $ClientConfigPathsType.GetField("_localConfigFilename", [System.Reflection.BindingFlags]"Instance,NonPublic")
$_applicationUriField = $ClientConfigPathsType.GetField("_applicationUri", [System.Reflection.BindingFlags]"Instance,NonPublic")
$_applicationConfigUriField = $ClientConfigPathsType.GetField("_applicationConfigUri", [System.Reflection.BindingFlags]"Instance,NonPublic")
$s_current=$ClientConfigPathsType.GetField("s_current",[System.Reflection.BindingFlags]"Static,NonPublic").GetValue($null)
$clientConfigPaths=[pscustomobject]@{
PreviousRoamingConfigDirectory=$null
PreviousRoamingConfigFilename=$null
PreviousLocalConfigDirectory=$null
PreviousLocalConfigFilename=$null
PreviousApplicationUri=$null
PreviousApplicationConfigUri=$null
}
Add-Member -InputObject $clientConfigPaths -Name RoamingConfigDirectory -MemberType ScriptProperty -Value { $_roamingConfigDirectoryField.GetValue($s_current) } -SecondValue {param($value) $this.PreviousRoamingConfigDirectory = $_roamingConfigDirectoryField.GetValue($s_current); $_roamingConfigDirectoryField.SetValue($s_current,$value); }
Add-Member -InputObject $clientConfigPaths -Name RoamingConfigFilename -MemberType ScriptProperty -Value { $_roamingConfigFilenameField.GetValue($s_current) } -SecondValue {param($value) $this.PreviousRoamingConfigFilename = $_roamingConfigFilenameField.GetValue($s_current); $_roamingConfigFilenameField.SetValue($s_current,$value); }
Add-Member -InputObject $clientConfigPaths -Name LocalConfigDirectory -MemberType ScriptProperty -Value { $_localConfigDirectoryField.GetValue($s_current) } -SecondValue {param($value) $this.PreviousLocalConfigDirectory = $_localConfigDirectoryField.GetValue($s_current); $_localConfigDirectoryField.SetValue($s_current,$value); }
Add-Member -InputObject $clientConfigPaths -Name LocalConfigFilename -MemberType ScriptProperty -Value { $_localConfigFilenameField.GetValue($s_current) } -SecondValue {param($value) $this.PreviousLocalConfigFilename = $_localConfigFilenameField.GetValue($s_current); $_localConfigFilenameField.SetValue($s_current,$value); }
Add-Member -InputObject $clientConfigPaths -Name ApplicationUri -MemberType ScriptProperty -Value { $_applicationUriField.GetValue($s_current) } -SecondValue {param($value) $this.PreviousApplicationUri = $_applicationUriField.GetValue($s_current); $_applicationUriField.SetValue($s_current,$value); }
Add-Member -InputObject $clientConfigPaths -Name ApplicationConfigUri -MemberType ScriptProperty -Value { $_applicationConfigUriField.GetValue($s_current) } -SecondValue {param($value) $this.PreviousApplicationConfigUri = $_applicationConfigUriField.GetValue($s_current); $_applicationConfigUriField.SetValue($s_current,$value); }
Add-Member -InputObject $clientConfigPaths -Name RestorePrevious -MemberType ScriptMethod -Value {
Get-Member -InputObject $this -MemberType NoteProperty | ?{$_.Name -imatch 'previous'} | %{
if(-not [string]::IsNullOrEmpty($this.($_.Name))){
$this.($($_.Name -replace 'Previous','')) = $this.($_.Name)
$this.($_.Name) = $null
}
}
}
}
process{
try{
$clientConfigPaths.RoamingConfigFilename = $sourceTreeUserConfig
$clientConfigPaths.RoamingConfigDirectory = Split-Path $sourceTreeUserConfig
$clientConfigPaths.LocalConfigFilename = $sourceTreeUserConfig -replace '\\Roaming\\','\Local\'
$clientConfigPaths.LocalConfigDirectory = Split-Path $clientConfigPaths.LocalConfigFilename
$clientConfigPaths.ApplicationUri = $sourcetreeBin
$clientConfigPaths.ApplicationConfigUri = "$sourcetreeBin.config"
$allSettings = [SourceTree.Properties.Settings]::Default
$allSettings.FirstLaunchSinceHgAdded = $false;
$allSettings.FirstLaunch=$false;
$allSettings.AgreedToEULA = $true;
$allSettings.AgreedToEULAVersion = $licenseManager.EulaVersion;
$allSettings.DefaultEmail = $userDisplayName;
$allSettings.DefaultFullName = $userDisplayName;
$allSettings.Save()
}finally{
$clientConfigPaths.RestorePrevious()
}
}
}
function Remove-SourceTreeSSLVerification{
begin{
$sourcetreeBin = Get-ApplicationPath sourcetree.exe | Sort-Object | select -First 1
$sourceTreeExeConfig = "$sourcetreeBin.config"
[xml]$confDocument = Get-Content $sourceTreeExeConfig
}
process{
$sysNode = $confDocument.DocumentElement.SelectSingleNode("system.net");
if($sysNode -eq $null){
$sysNode = $confDocument.CreateElement("system.net");
$confDocument.configuration.AppendChild($sysNode) | out-null
}
$spmNode = $sysNode.SelectSingleNode('settings/servicePointManager');
if($spmNode -eq $null){
$settingsNode = $sysNode.settings
if($sysNode.settings -eq $null){
$settingsNode = $confDocument.CreateElement("settings")
$sysNode.AppendChild($settingsNode) | out-null
}
$spmNode = $confDocument.CreateElement("servicePointManager");
$settingsNode.AppendChild($spmNode) | out-null
}
$spmNode.SetAttribute("checkCertificateName","false");
$spmNode.SetAttribute("checkCertificateRevocationList","false");
$confDocument.Save($sourceTreeExeConfig)
#$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
#[System.IO.File]::WriteAllLines($sourceTreeExeConfig, $confDocument.OuterXml, $Utf8NoBomEncoding)
}
}
function Get-SourceTreeOnlineVersions{
$res = Invoke-WebRequest https://www.sourcetreeapp.com/download-archives -UseDefaultCredentials -WebSession $global:webSession
$matches = [regex]::Matches($res.RawContent,'"(?<url>https?:\/\/[^"]+\/[^"]+-(?<version>\d+\.\d+\.\d+)\.exe)"',[System.Text.RegularExpressions.RegexOptions]"IgnoreCase,Multiline,ECMAScript")
$matches | %{
[pscustomobject]@{
version=[Version]$_.Groups['version'].Value
url=$_.Groups['url'].Value
}
} | sort -Property version -Descending -Unique
}
function Install-SourceTree([bool]$latest){
begin{
$versions = Get-SourceTreeOnlineVersions;
$version = $versions | select -First 1
}
process{
if($latest -ne $true){
Write-Host "Available Sourcetree versions: "
$versions | %{ write-host $(" $(if($version -eq $_){"[*]"}else{"[ ]"}) $($_.version)") }
$choice = $null
while($true){
$choice = $(Read-Host "Which version must be installed ? [$($version.version)]").Trim()
if([string]::IsNullOrEmpty($choice)){
break;
}
[Version]$v = $null
if([Version]::TryParse($choice, [ref]$v)){
$x = $versions| ?{ $_.version -eq $v } | select -First 1
if($x -ne $null){
$version = $x
break;
}
}
}
}
if(Test-Path "$env:LOCALAPPDATA\SourceTree"){
Remove-Item "$env:LOCALAPPDATA\SourceTree" -Force -ErrorAction 0
}
#Download
$fileName = Split-Path $version.url -Leaf
$fileFullName = "$env:TEMP\$fileName"
Remove-Item $fileFullName -Force -ErrorAction 0;
Invoke-DownloadFile $version.url $fileFullName
#Install
Write-Warning "`tFollow the installation wizard, when you arrive at the step to register, close the installation wizard`r`nthen the setup process will continue."
Start-Sleep -Seconds 2
$p = Start-Process $fileFullName -PassThru
do {start-sleep -Milliseconds 500}
until ($p.HasExited)
#Start-Sleep -Seconds 5
kill -Name pageant -ErrorAction 0 -Force
}
}
function Complete-SourceTreeInstallation{
$sourcetreeBin = Get-ApplicationPath sourcetree.exe | Sort-Object | select -First 1
#Shortcuts installation/update
##StartMenu & Desktop & StartUp Menu
@(
[System.Environment]::GetFolderPath("DesktopDirectory")
,([System.Environment]::GetFolderPath("StartMenu")+"\Programs\Atlassian")
) |%{
if(-not (Test-Path $_)){
mkdir $_ -Force | out-null
}
$lnkFullName = "{0}\SourceTree.lnk" -f $_
(New-ShellLink $sourcetreeBin "" $lnkFullName) | out-null
}
}
## MAIN
function main{
#Ensure SSL Verification is disabled in case the certificate is self-signed
Disable-CertificateChecks
#Initialize Global variables
$global:webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$global:webSession.UseDefaultCredentials = $true
$global:webSession.Proxy = [System.Net.WebRequest]::GetSystemWebProxy()
Write-Host "Main::Starts"
#Check SourceTree & Git Installed
$errMessage="";
$gitBin = Get-ApplicationPath git.exe | select -First 1