This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathupdate_from_git_to_ws.ps1
More file actions
345 lines (314 loc) · 16.4 KB
/
update_from_git_to_ws.ps1
File metadata and controls
345 lines (314 loc) · 16.4 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
param
(
[parameter(Mandatory = $true)] [String] $baseUrl,
[parameter(Mandatory = $true)] [String] $fabricToken,
[parameter(Mandatory = $true)] [String] $workspaceName, # The name of the workspace,
[parameter(Mandatory = $true)] [String] $capacityId, # The capacity id of the workspace,
[parameter(Mandatory = $true)] [String] $folder, # The folder where the workspace items are located on the branch, should be: Join-Path $(Build.SourcesDirectory) $(directory_name)
[parameter(Mandatory = $false)] [bool] $resetConfig=$false # Used when the developer wants to reset the config files in the workspace (typically when a new feature branch is created)
)
## FROM GIT TO WORKSPACE
# Used when the developer creates a new branch from the development/main branch
# and wants to create or update the workspace in accordance with the new branch.
function GetErrorResponse($exception) {
# Relevant only for PowerShell Core
if ($exception.Exception.Response) {
return $exception.Exception.Message
}
else {
$errorResponse = $_
}
if(!$errorResponse) {
# This is needed to support Windows PowerShell
$result = $exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$errorResponse = $reader.ReadToEnd();
}
return $errorResponse
}
function getorCreateWorkspaceId($requestHeader, $contentType, $baseUrl, $workspaceName, $capacityId){
$params = @{
Uri = "$($baseUrl)/workspaces"
Method = "GET"
Headers = $requestHeader
ContentType = $contentType
}
$workspaces = (Invoke-RestMethod @params).value
$workspace = $workspaces | Where-Object {$_.displayName -eq $workspaceName}
if(!$workspace) {
Write-Host "A workspace with the requested name $workspaceName was not found, creating new workspace." -ForegroundColor Yellow
$params = @{
Uri = "$($baseUrl)/workspaces"
Method = "POST"
Headers = $requestHeader
ContentType = $contentType
Body = @{
displayName = $workspaceName
capacityId = $capacityId
} | ConvertTo-Json
}
$workspace = (Invoke-RestMethod @params)
Write-Host "Workspace $workspaceName with id $($workspace.id) was created." -ForegroundColor Green
return $workspace.id
}
else {
Write-Host "Workspace $workspaceName with id $($workspace.id) was found." -ForegroundColor Green
return $workspace.id
}
}
function createWorkspaceItem($baseUrl, $workspaceId, $requestHeader, $contentType, $itemMetadata, $itemDefinition){
if ($itemDefinition)
{
# if the item has a definition create the item with definition
$body = @{
displayName = $itemMetadata.displayName
description = $itemMetadata.description
type = $itemMetadata.type
definition = $itemDefinition.definition
}
}
else { #item does not have definition, only create the item with metadata
$body = @{
displayName = $itemMetadata.displayName
description = $itemMetadata.description
type = $itemMetadata.type
}
}
$params = @{
Uri = "$($baseUrl)/workspaces/$($workspaceId)/items"
Method = "POST"
Headers = $requestHeader
ContentType = $contentType
Body = $body | ConvertTo-Json -Depth 10
}
$item = (Invoke-RestMethod @params -ResponseHeadersVariable responseHeaders -StatusCodeVariable statusCode)
if ($statusCode -eq 202) { # status 202 is accepted instead of OK, which signals a long running operation
$item = (longRunningOperationPolling $responseHeaders.Location $responseHeaders.'Retry-After')
}
Write-Host "Sensitivity Labels won't make future item defintion updates possible. Please update Sensitivity Labels for created items before re-running this script." -ForegroundColor Yellow
return $item
}
function updateWorkspaceItemDefinition($baseUrl, $workspaceId, $requestHeader, $contentType, $itemMetadata, $itemDefinition, $itemConfig){
$uri = "$($baseUrl)/workspaces/$($workspaceId)/items/$($itemConfig.objectId)/updateDefinition"
if ($itemMetadata.type -eq "Notebook" -and !$itemDefinition.definition.format)
{
$itemDefinition.definition | Add-Member -Name "format" -value "ipynb" -MemberType NoteProperty -Force
}
$body = @{
definition = $itemDefinition.definition
}
Write-Host "Executing POST to update definition of item $($itemConfig.objectId) $($itemMetadata.displayName)" -ForegroundColor Green
# update the item definition
$params = @{
Uri = $uri
Method = "POST"
Headers = $requestHeader
ContentType = $contentType
Body = $body | ConvertTo-Json -Depth 10
}
Invoke-RestMethod @params -ResponseHeadersVariable responseHeaders -StatusCodeVariable statusCode
if ($statusCode -eq 202 -and $responseHeaders.Location -and $responseHeaders.'Retry-After') { # status 202 is accepted instead of OK, which signals a long running operation
longRunningOperationPolling $responseHeaders.Location $responseHeaders.'Retry-After'
}
}
function updateWorkspaceItem($baseUrl, $workspaceId, $requestHeader, $contentType, $itemMetadata, $itemDefinition, $itemConfig){
# Start updating the item metadata
$uri = "$($baseUrl)/workspaces/$($workspaceId)/items/$($itemConfig.objectId)"
$body = @{
displayName = $itemMetadata.displayName
description = $itemMetadata.description
}
$params = @{
Uri = $uri
Method = "PATCH"
Headers = $requestHeader
ContentType = $contentType
Body = $body | ConvertTo-Json -Depth 10
}
Write-Host "Executing PATCH to update item $($itemConfig.objectId) $($itemMetadata.displayName)" -ForegroundColor Green
Invoke-RestMethod @params
if ($itemDefinition) #for items with a definition, also update the definition
{
updateWorkspaceItemDefinition $baseUrl $workspaceId $requestHeader $contentType $itemMetadata $itemDefinition $itemConfig
}
}
function createOrUpdateWorkspaceItem($requestHeader, $contentType, $baseUrl, $workspaceId, $workspaceItems, $folder, $repoItems){
# Find if the item already exists in the workspace looking at the $itemConfigFileName file
$metadataFilePath = Join-Path $folder $itemMetadataFileName
if ([System.IO.File]::Exists($metadataFilePath)){
$itemMetadata = Get-Content -Path $metadataFilePath -Raw | ConvertFrom-Json
Write-Host "Found item metadata for $($itemMetadata.displayName)" -ForegroundColor Green
}
else {
Write-Host "Item $folder does not have the required metadata file, skipping." -ForegroundColor Yellow
return
}
$definitionFilePath = Join-Path $folder $itemDefinitionFileName
if ([System.IO.File]::Exists($definitionFilePath)){
$itemDefinition = Get-Content -Path $definitionFilePath -Raw | ConvertFrom-Json
Write-Host "Found item definition for $($itemMetadata.displayName)" -ForegroundColor Green
$contentFiles = Get-ChildItem -Path $folder | Where-Object {$_.Name -notlike $itemMetadataFileName -and $_.Name -notlike $itemDefinitionFileName -and $_.Name -notlike $itemConfigFileName}
#$contentFiles = Get-ChildItem -Path $folder | Where-Object {$_.Name -like "*content*"}
if ($contentFiles -and $contentFiles.Count -ge 1){ # if there is at least a content file then update the definition payload
Write-Host "Found $($contentFiles.Count) content file(s) for $($itemMetadata.displayName)" -ForegroundColor Green
foreach ($part in $itemDefinition.definition.parts){
$file = $contentFiles | Where-Object {$_.Name -eq $part.path}
$itemContent = Get-Content -Path $file.FullName -Raw
$byte_array = [System.Text.Encoding]::UTF8.GetBytes($itemContent)
$base64 = [System.Convert]::ToBase64String($byte_array)
$part.payload = $base64
}
$itemDefinition| ConvertTo-Json -Depth 10 | New-Item -Path $definitionFilePath -Force
Write-Host "updated item definition payload with content file for $($itemMetadata.displayName)" -ForegroundColor Green
}
else {
Write-Host "Missing content file or found more than one content file, skipping update definition for $folder." -ForegroundColor Yellow
return
}
}
$configFilePath = Join-Path $folder "$itemConfigFileName"
if (![System.IO.File]::Exists($configFilePath) -or $resetConfig){
# if the config file does not exist then create a new logicalId and save the new config file
# then create a new item and save the returned objectId in the config file
Write-Host "no $itemConfigFileName file found, creating new file." -ForegroundColor Yellow
$itemConfig = @{
logicalId = [guid]::NewGuid().ToString()
}
$itemConfig | ConvertTo-Json -Depth 10 | New-Item -Path $configFilePath -Force
}
else {
$itemConfig = Get-Content -Path $configFilePath -Raw | ConvertFrom-Json
Write-Host "Found item config file for $folder. Item missing objectId? $(!$itemConfig.objectId). Item missing logicalId? $(!$itemConfig.logicalId)" -ForegroundColor Green
}
if (!$itemConfig.objectId) {
# 3. if an objectId is not present and only a logicalId is present then
# Create a new object and save the objectId in the config file
Write-Host "Item $folder does not have an associated objectId, creating new Fabric item of type $($itemMetadata.type) with name $($itemMetadata.displayName)." -ForegroundColor Yellow
$item = createWorkspaceItem $baseUrl $workspaceId $requestHeader $contentType $itemMetadata $itemDefinition
Write-Host "item is $($item.displayName) with id $($item.id)"
# update the config file with the returned objectId
$itemConfig | add-member -Name "objectId" -value $item.id -MemberType NoteProperty -Force
Write-Host "itemConfig objectId is $($itemConfig.objectId)"
$itemConfig | ConvertTo-Json | Set-Content -Path $configFilePath
$repoItems += $item.id
}
else { #there is already a corresponding item in the workspace, we need to update it
# 1. if the file contains an objectId then it means there is an associated item in the workspace
$item = $workspaceItems | Where-Object {$_.id -eq $itemConfig.objectId}
if (!$item) { # the item might have been manually deleted from the workspace
# if this fails it might be because the item has just been deleted and for some time the
# old item name is still recognized as an existing item by Fabric and therefore the
# operation might fail because of name clashes
Write-Host "Item $($itemConfig.objectId) of type $($itemMetadata.type) was not found in the workspace, creating new item." -ForegroundColor Yellow
$item = createWorkspaceItem $baseUrl $workspaceId $requestHeader $contentType $itemMetadata $itemDefinition
$itemConfig.objectId = $item.id
$itemConfig | ConvertTo-Json | Set-Content -Path $configFilePath | Out-Null
$repoItems += $itemConfig.objectId
}
else {
$repoItems += $itemConfig.objectId
Write-Host "Item $($itemConfig.objectId) of type $($item.type) was found in the workspace, updating item." -ForegroundColor Green
updateWorkspaceItem $baseUrl $workspaceId $requestHeader $contentType $itemMetadata $itemDefinition $itemConfig
}
}
return $repoItems
}
function longRunningOperationPolling($uri, $retryAfter){
try {
# Get Long Running Operation
Write-Host "Polling long running operation ID $uri has been started with a retry-after time of $retryAfter seconds."
$params = @{
Uri = "$uri"
Method = "GET"
Headers = $requestHeader
ContentType = $contentType
}
do
{
$operationState = (Invoke-RestMethod @params -ResponseHeadersVariable responseHeaders)
Write-Host "Long running operation status: $($operationState.Status)"
if ($operationState.Status -in @("NotStarted", "Running")) {
Start-Sleep -Seconds 20
# Start-Sleep -Seconds $retryAfter
}
} while($operationState.Status -in @("NotStarted", "Running"))
if ($operationState.Status -eq "Failed") {
Write-Host "The long running operation has been completed with failure. Error reponse: $($operationState.Error | ConvertTo-Json)" -ForegroundColor Red
}
else{
Write-Host "The long running operation has been successfully completed." -ForegroundColor Green
if ($responseHeaders.Location) {
$uri = $responseHeaders.Location
}
else {
return
}
$paramsResult = @{
Uri = "$uri"
Method = "GET"
Headers = $requestHeader
ContentType = $contentType
}
$item = (Invoke-RestMethod @paramsResult)
return $item
}
} catch {
$errorResponse = GetErrorResponse($_)
Write-Host "The long running operation has been completed with failure. Error reponse: $errorResponse" -ForegroundColor Red
exit 1
}
}
try {
# TODO: consider removing the logicalId from the file as it's not used today.
Write-Host "this task is running Powershell version " $PSVersionTable.PSVersion
Write-Host "the folder we are working on is $folder"
Write-Host "Updating workspace items for workspace $workspaceName"
$itemConfigFileName = "item-config.json"
$itemMetadataFileName = "item-metadata.json"
$itemDefinitionFileName = "item-definition.json"
$authHeader = "Bearer $($fabricToken)"
$requestHeader = @{
Authorization = $authHeader
}
$contentType = "application/json"
# 1. Check if a workspace with given name already exists, if not create a new one
$workspaceId = getorCreateWorkspaceId $requestHeader $contentType $baseUrl $workspaceName $capacityId
# 2. For every Fabric item on the branch, check if they exist in the workspace
# first get a list of all items in the workspace
$params = @{
Uri = "$($baseUrl)/workspaces/$($workspaceId)/items"
Method = "GET"
Headers = $requestHeader
ContentType = $contentType
}
$workspaceItems = (Invoke-RestMethod @params).value
$repoItems = @() # keep track of found object ids (either from creation or config files) and remove all other object ids from the workspace
# if they exist update them else create new ones
$dir = Get-ChildItem -Path $folder -Recurse -Directory
foreach ($d in $dir) {
Write-Host $d.FullName
$repoItems = createOrUpdateWorkspaceItem $requestHeader $contentType $baseUrl $workspaceId $workspaceItems $d.FullName $repoItems
}
# 3. for items that are in the workspace but not in the repository (hence no folder), we need to delete them from the workspace
# use $repoItems to keep track of found object ids (either from creation or config files) and remove all other object ids from the workspace
foreach ($item in $workspaceItems){
if ($item.id -notin $repoItems -and $item.type -notin @("SQLEndpoint", "SemanticModel")){ # SemanticModel and SQL Endpoint items should not be deleted
Write-Host "Item $($item.id) $($item.displayName) is in the workspace but not in the repository, deleting." -ForegroundColor Yellow
$params = @{
Uri = "$($baseUrl)/workspaces/$($workspaceId)/items/$($item.id)"
Method = "DELETE"
Headers = $requestHeader
ContentType = $contentType
}
Invoke-RestMethod @params
}
}
Write-Host "Script execution completed successfully. Workspace items have been updated for workspace $workspaceName." -ForegroundColor Green
}
catch {
$errorResponse = GetErrorResponse($_)
Write-Host "Failed to run script to update workspace items for workspace $workspaceName. Error reponse: $errorResponse" -ForegroundColor Red
exit 1
}