-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomation.ps1
More file actions
568 lines (441 loc) · 20.9 KB
/
Automation.ps1
File metadata and controls
568 lines (441 loc) · 20.9 KB
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
#Set by the YAML script or can be entered manually to setup workspaces
param(
[Parameter(Mandatory=$true)]
[String]$requiresLogin,
[Parameter(Mandatory=$false)]
[String]$automationUsername,
[Parameter(Mandatory=$false)]
[SecureString]$automationPassword,
[Parameter(Mandatory=$true)]
[String]$pullRequestNumber,
[Parameter(Mandatory=$true)]
[SecureString]$gitHubToken,
[Parameter(Mandatory=$true)]
[SecureString]$gmhecTenantId
)
#This is a script to automate the process of connecting to Git Integration in the workspace.
#User will login via PowerShell Azure Az module then the script will connect to Git for each of the workspaces.
#Instructions:
# 1. Install PowerShell (https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell)
# 2. Install Azure PowerShell Az module (https://learn.microsoft.com/en-us/powershell/azure/install-azure-powershell)
# 3. Run PowerShell as an administrator
# 4. Change PowerShell directory to where this script is saved
# 5. Run the command > ./Git Integration Automation.ps1
# 6. Wait for the operations to be completed.
$global:resourceUrl = "https://api.fabric.microsoft.com"
$global:fabricHeaders = @{}
$global:gitCredentials = @{}
$global:workspaceInfo = @{}
$global:errorCount = 0
#Set a list of workspaceNames to utilize in getWorkspaceByName() API
#Remove for upload to GitHub
$global:testWorkspaceNames = @()
$global:prodWorkspaceNames = @()
if ($requiresLogin.ToLower() -eq "false") {
$requiresLogin = $false
} else {
$requiresLogin = $true
}
function formatPRDescription ($description) {
Write-Host "ENTER formatPRDescription"
# Extract the lines with [x]
$selectedWorkspaces = $description -split "`n" | Where-Object { $_ -match "\[x\]" }
# Remove the "- [x] " prefix
$workspaceNames = $selectedWorkspaces -replace "^\- \[x\]", ""
#override if no workspaces were selected
if ($workspaceNames.Length -gt 0) {
# Create duplicates ending with [PROD] and [TEST]
$global:prodWorkspaceNames = @()
$global:testWorkspaceNames = @()
foreach ($item in $workspaceNames) {
$item = $item.trim()
$global:testWorkspaceNames += "$item [Test]"
$global:prodWorkspaceNames += "$item"
}
}
Write-Host "SUCCESS: Exit formatPRDescription"
}
function getGitHubPRDetails ($pullRequestNumber, $gitHubToken) {
#Add an override PR#1 for debugging and running locally
if ($pullRequestNumber -ne "1") {
Write-Host "ENTER getGitHubPRDetails"
$apiUrl = "https://api.github.com/repos/middlebury-adi/powerbi-itsadi/pulls/{0}" -f $pullRequestNumber
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer {0}" -f $gitHubToken
}
$response = Invoke-RestMethod -Headers $headers -Uri $apiUrl -Method Get
$description = $response.body
formatPRDescription $description
Write-Host "SUCCESS: Exit getGitHubPRDetails"
}
}
if ($null -ne $pullRequestNumber -and $pullRequestNumber -ne "none") {
getGitHubPRDetails $pullRequestNumber $gitHubToken
}
function connectViaAutomation() {
try {
# Connect through an automated service principal
Write-Host "ENTER connectViaAutomation"
$secureString = ConvertTo-SecureString -String $automationPassword -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($automationUsername, $secureString)
Connect-AzAccount -Tenant $gmhecTenantId -Credential $Cred
getFabricHeaders
Write-Host "SUCCESS: Exit connectViaAutomation" -ForegroundColor Green
} catch {
throw "ERROR: in connectViaAutomation: $($_.Exception.Message)"
}
}
function connectViaUserLogin() {
try {
# Connect through a login PopUp. Does not work with MFA and Automation
Write-Host "ENTER connectViaUserLogin"
Connect-AzAccount -Tenant $gmhecTenantId
getFabricHeaders
Write-Host "SUCCESS: Exit connectViaUserLogin" -ForegroundColor Green
} catch {
throw "ERROR: in connectViaUserLogin: $($_.Exception.Message)"
}
}
function getFabricHeaders() {
try {
#Get The Fabric Token
Write-Host "ENTER: getFabricHeaders"
$fabricToken = (Get-AzAccessToken -ResourceUrl $global:resourceUrl).Token
Write-Host $fabricToken
$global:fabricHeaders = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer {0}" -f $fabricToken
}
Write-Host "SUCCESS: Exit getFabricHeaders" -ForegroundColor Green
} catch {
throw "ERROR: in getFabricHeader: $($_.Exception.Message)"
}
}
function setGitHubCredentials($worksapceId, $gitCreds) {
Write-Host "ENTER setGitHubCredentials"
$apiUrl = "{0}/v1/workspaces/{1}/git/myGitCredentials" -f $global:resourceUrl, $worksapceId
$body = @{
source = "ConfiguredConnection"
connectionId = $gitCreds.connectionId
} | ConvertTo-Json
$response = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method PATCH -Body $body -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "setGitHubCredentials" $response
if(!$validateResponse) {
setGitHubCredentials $workspaceId $gitCreds
}
Write-Host "SUCCESS: Exit ENTER setGitHubCredentials" -ForegroundColor Green
}
function getGitHubConnectionId($workspaceId) {
Write-Host "ENTER getGitHubConnectionId"
$apiUrl = "{0}/v1/workspaces/{1}/git/myGitCredentials" -f $global:resourceUrl, $workspaceId
#check if the workspace has the git connection
$gitCreds = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method GET -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "getGitHubConnectionId" $response
if(!$validateResponse) {
getGitHubConnectionId $workspaceId
}
if ($gitCreds.source -eq 'None') {
#get the creds from the the *** Workspace
$itsWorkspaceId = '***'
$apiUrl = "{0}/v1/workspaces/{1}/git/myGitCredentials" -f $global:resourceUrl, $itsWorkspaceId
$gitCreds = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method GET -SkipHttpErrorCheck -StatusCodeVariable scv
if ($gitCreds.source -eq 'None') {
Write-Host "Error retreiving git credentials... Please login to the *** [PROD] workspace via the webapp" -ForegroundColor Red
} else {
setGitHubCredentials $workspaceId $gitCreds
}
}
Write-Host "SUCCESS: Exit getGitHubConnectionId" -ForegroundColor Green
return $gitCreds.connectionId
}
function getWorkspaceInfo() {
Write-Host "ENTER getWorkspaceInfo"
$apiUrl = "{0}/v1/workspaces" -f $global:resourceUrl
$global:workspaceInfo = (Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method GET -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv).value
$validateResponse = validateResponse $rhv $scv "getWorkspaceInfo" $response
if(!$validateResponse) {
getWorkspaceInfo
}
Write-Host "SUCCESS: Exit getWorkspaceInfo" -ForegroundColor Green
}
function getWorkspaceByName($workspaceName) {
try {
Write-Host "ENTER getWorkspaceByName for: $workspaceName"
# Try to find the workspace by display name
$workspace = $global:workspaceInfo | Where-Object {$_.DisplayName -eq $workspaceName}
if (!$workspace) {
Write-Host "A workspace with the requested named was not found." -ForegroundColor Red
} else {
Write-Host "SUCCESS: Exit getWorkspaceByName" -ForegroundColor Green
}
return $workspace
} catch {
throw "ERROR: in getWorkspaceByName: $($_.Exception.Message)"
}
}
function disconnectWorkspace($workspaceId, $workspaceName) {
Write-Host "ENTER disconnectWorkspace for: $workspaceName"
$apiUrl = "{0}/v1/workspaces/{1}/git/disconnect" -f $global:resourceUrl, $workspaceId
$response = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method POST -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "disconnectWorkspace" $response
if(!$validateResponse) {
disconnectWorkspace $workspaceId $workspaceName
}
Write-Host "SUCCESS disconnectWorkspace for: $workspacename" -ForegroundColor Green
}
function connectWorkspaceToGit($workspaceId, $workspaceName, $isProd) {
Write-Host "ENTER connectWorkspaceToGit"
$apiUrl = "{0}/v1/workspaces/{1}/git/connect" -f $global:resourceUrl, $workspaceId
$workspaceName = $workspaceName.ToUpper() -replace " - ", "_" -replace " ", "_"
if ($isProd) {
$workspaceName = $workspaceName + "_[PROD]"
}
Write-Host "Workspace name: $workspaceName"
$gitHubDetails = @{
gitProviderType = "GitHub"
ownerName = "***"
repositoryName = "***"
branchName = "main"
directoryName = $workspaceName
#The directoryName needs to already exist in the GitHub.
#Not able to create a new directory from the API
}
$connectToGitBody = @{
gitProviderDetails = $gitHubDetails
myGitCredentials = @{
source = "ConfiguredConnection"
connectionId = $global:gitCredentials
}
} | ConvertTo-Json
$response = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method POST -Body $connectToGitBody -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "connectWorkspaceToGit" $response
if(!$validateResponse) {
connectWorkspaceToGit $workspaceId $workspaceName $isProd
}
Write-Host "SUCCESS: in connectWorkspaceToGit for Workspace: $workspaceName" -ForegroundColor Green
}
function initializeGitConnection($workspaceId) {
Write-Host "ENTER: initializeGitConnection"
$body = @{"initializationStrategy" = "PreferWorkspace"} | ConvertTo-Json
$apiUrl = "{0}/v1/workspaces/{1}/git/initializeConnection" -f $global:resourceUrl, $workspaceId
$response = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method POST -Body $body -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "initializeGitConnection" $response
if(!$validateResponse) {
initializeGitConnection $workspaceId
}
Write-Host "SUCCESS: in initializeGitConnection" -ForegroundColor Green
return $response
}
function checkGitStatus($workspaceId) {
Write-Host "ENTER: checkGitStatus"
$errors = New-Object System.Collections.ArrayList
$workspaceChanges = New-Object System.Collections.ArrayList
$gitChanges = New-Object System.Collections.ArrayList
$apiUrl = "{0}/v1/workspaces/{1}/git/status" -f $global:resourceUrl, $workspaceId
$response = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method GET -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "checkGitStatus" $response
if(!$validateResponse) {
checkGitStatus $workspaceId
}
$changes = $response.changes
if ($changes.Length -gt 0) {
#Check the content of the changes for any errors
foreach ($change in $changes) {
$conflictType = $change.conflictType
$changeId = $change.itemMetadata.itemIdentifier.objectId
if (($null -eq $change.remoteChange) -and ($null -ne $change.workspaceChange) -and ($conflictType -eq "None")) {
#add to workspace array
$workspaceChanges.add($changeId)
}
if (($null -ne $change.remoteChange) -and ($null -eq $change.workspaceChange) -and ($conflictType -eq "None")) {
#add to git array
$gitChanges.add($changeId)
}
if ($conflictType -ne "None") {
#add to error array
$errors.add($changeId)
}
}
#create the response array
$gitStatus = @{
"workspaceHead" = $response.workspaceHead
"remoteCommitHash" = $response.remoteCommitHash
"gitErrors" = $errors
"workspaceChanges" = $workspaceChanges
"gitChanges" = $gitChanges
}
Write-Host "SUCCESS: Exit checkGitStatus" -ForegroundColor Green
return $gitStatus
} else {
Write-Host "SUCCESS: Exit checkGitStatus" -ForegroundColor Green
return "NoChanges"
}
}
function checkCurrentGitConnection($workspaceId, $workspaceName, $isProd) {
Write-Host "ENTER checkCurrentGitConnection for: $workspaceName"
$apiUrl = "{0}/v1/workspaces/{1}/git/connection" -f $global:resourceUrl, $workspaceId
$response = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method Get -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "checkCurrentGitConnection" $response
if(!$validateResponse) {
checkCurrentGitConnection $workspaceId $workspaceName $isProd
}
$branchName = $response.gitProviderDetails.branchName
$state = $response.gitConnectionState
if (($state -eq "ConnectedAndInitialized") -and ($branchName -ne "main")) {
disconnectWorkspace $workspaceId $workspaceName
connectWorkspaceToGit $workspaceId $workspaceName $isProd
$gitInitResponse = initializeGitConnection $workspaceId
checkCurrentGitConnection $workspaceId $workspaceName $isProd
Write-Host "SUCCESS: in checkCurrentGitConnection" -ForegroundColor Green
return $gitInitResponse
}
if (($state -eq "ConnectedAndInitialized") -and ($branchName -eq "main")) {
#check the gitStatus for pending commits
$gitStatus = checkGitStatus $workspaceId
$gitErrors = $gitStatus.gitErrors
$workspaceChanges = $gitStatus.workspaceChanges
$gitChanges = $gitStatus.gitChanges
if (($gitErrors.Length -eq 0) -and ($workspaceChanges.Length -eq 0) -and ($gitChanges.Length -eq 0)) {
$gitInitResponse = "AlreadyConnected"
Write-Host "Workspace $workspaceName is already ConnectedAndInitialized"
}
if (($gitErrors.Length -eq 0) -and ($workspaceChanges.Length -ne 0)) {
#Changes from workspace
updateGitFromWorkspace $workspaceName $workspaceId
}
if (($gitErrors.Length -eq 0) -and ($gitChanges.Length -ne 0)) {
#Changes from Git
updateWorkspaceFromGit $workspaceName $workspaceId $gitStatus
}
if ($gitErrors.Length -gt 0) {
#handle $errors
#commitWithErrors $gitStatus $workspaceId
}
Write-Host "SUCCESS: in checkCurrentGitConnection" -ForegroundColor Green
return "No Action Needed"
}
if ($state -eq "NotConnected") {
connectWorkspaceToGit $workspaceId $workspaceName $isProd
$gitInitResponse = initializeGitConnection $workspaceId
checkCurrentGitConnection $workspaceId $workspaceName $isProd
Write-Host "SUCCESS: in checkCurrentGitConnection" -ForegroundColor Green
return $gitInitResponse
}
if (($state -eq "Connected") -and ($branchName -eq "main")) {
$gitInitResponse = initializeGitConnection $workspaceId
checkCurrentGitConnection $workspaceId $workspaceName $isProd
Write-Host "SUCCESS: in checkCurrentGitConnection" -ForegroundColor Green
return $gitInitResponse
}
if ($state.requiredAction -eq "CommitToGit") {
Write-Host "SUCCESS: in checkCurrentGitConnection" -ForegroundColor Green
return $state
}
}
function pollLongOperations($operationId, $retryAfter, $workspaceId) {
Write-Host "ENTER pollLongOperations"
$apiUrl = "{0}/v1/operations/{1}" -f $global:resourceUrl, $operationId
do {
$operationState = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method GET -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
Write-Host "UPDATE: Git operation status: $($operationState.Status)"
if ($operationState.Status -in @("NotStarted", "Running")) {
Start-Sleep -Seconds $retryAfter
}
} while($operationState.Status -in @("NotStarted", "Running"))
if ($operationState.Status -eq "Failed") {
Write-Host "Failed to update the workspace $workspaceName with content from Git. Error reponse: $($operationState.Error | ConvertTo-Json)" -ForegroundColor Red
$global:errorCount ++
#Check if error is too loarge of commit
if ($operationState.Error.moreDetails.errorCode -eq "Git_GitProviderCommitMaxSizeExceeded") {
#Send to batch processing
#TODO
}
}
Write-Host "SUCCESS: Exit pollLongOperations" -ForegroundColor Green
}
function updateWorkspaceFromGit($workspaceName, $workspaceId, $gitStatus) {
Write-Host "ENTER updateWorkspaceFromGit for: $workspaceName"
$apiUrl = "{0}/v1/workspaces/{1}/git/updateFromGit" -f $global:resourceUrl, $workspaceId
$conflictResolution = @{
"conflictResolutionType" = "Workspace"
"conflictResolutionPolicy" = "PreferRemote"
}
$options = @{
"allowOverrideItems" = $true
}
$updateFromGitBody = @{
"workspaceHead" = $gitStatus.WorkspaceHead
"remoteCommitHash" = $gitStatus.RemoteCommitHash
"conflictResolution" = $conflictResolution
"options" = $options
} | ConvertTo-Json
Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method POST -Body $updateFromGitBody -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "updateWorkspaceFromGit" $scv
if(!$validateResponse) {
updateWorkspaceFromGit $workspaceName $workspaceId $gitStatus
}
pollLongOperations $rhv['x-ms-operation-id'] $rhv['Retry-After'] $workspaceId
}
function updateGitFromWorkspace($workspaceName, $workspaceId) {
Write-Host "ENTER updateGitFromWorkspace for: $workspaceName"
$apiUrl = "{0}/v1/workspaces/{1}/git/commitToGit" -f $global:resourceUrl, $workspaceId
$commitToGitBody = @{
mode = "All"
comment = "Updating from Workspace for $workspaceName [Skip CI]"
} | ConvertTo-Json
Invoke-RestMethod -Headers $global:fabricHeaders -Uri $apiUrl -Method POST -Body $commitToGitBody -SkipHttpErrorCheck -StatusCodeVariable scv -ResponseHeadersVariable rhv
$validateResponse = validateResponse $rhv $scv "updateGitFromWorkspace" $scv
if(!$validateResponse) {
updateGitFromWorkspace $workspaceName $workspaceId
}
pollLongOperations $rhv['x-ms-operation-id'] $rhv['Retry-After'] $workspaceId
Write-Host "SUCCESS: Exit updateGitFromWorkspace for: $WorkspaceName" -ForegroundColor Green
}
function rateLimit($sleepTime) {
Write-Host "ERROR: rate limit exceeded. Retrying in $sleepTime seconds..." -ForegroundColor Red
$convertedNumber = [double]::Parse($sleepTime)
Start-Sleep -Seconds $convertedNumber
}
function validateResponse($rhv, $scv, $functionName, $response) {
if ($scv -ne "200" -and $scv -ne "202") {
Write-Host "ERROR: in $functionName: " + $($response | ConvertTo-Json) -ForegroundColor Red
$global:errorCount ++
} elseif ($scv -eq "429") {
rateLimit $rhv["Retry-After"]
return $false
}
}
try {
if ($requiresLogin -eq $true) {
connectViaUserLogin
} else {
connectViaAutomation
}
getWorkspaceInfo
foreach ($name in $global:testWorkspaceNames) {
$name = $name.trim()
$worksapceInfo = getWorkspaceByName $name
$gitStatus = checkGitStatus $worksapceInfo.id
if ($gitStatus -ne "NoChanges" -and $gitStatus -ne "Error") {
$global:gitCredentials = getGitHubConnectionId $worksapceInfo.id
checkCurrentGitConnection $worksapceInfo.id $worksapceInfo.displayName $false
}
Write-Host "COMPLETE: Git Updates Complete for $name" -ForegroundColor Green
}
foreach ($name in $global:prodWorkspaceNames) {
$worksapceInfo = getWorkspaceByName $name
$gitStatus = checkGitStatus $worksapceInfo.id
if ($gitStatus -ne "NoChanges" -and $gitStatus -ne "Error") {
$global:gitCredentials = getGitHubConnectionId $worksapceInfo.id
checkCurrentGitConnection $worksapceInfo.id $worksapceInfo.displayName $true
} else {
Write-Host "COMPLETE: Git Updates Complete for $name" -ForegroundColor Green
}
}
if ($global:errorCount -gt 0) {
throw "Encountered an error during the script. Please review logs for more details."
}
} catch {
Write-Error "ERROR $($_.Exception.Message)" -ErrorAction Stop
}