-
Notifications
You must be signed in to change notification settings - Fork 139
/
GitIntegration-ConnectAndUpdateFromGit.ps1
170 lines (126 loc) · 6.84 KB
/
GitIntegration-ConnectAndUpdateFromGit.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
# This sample script calls the Fabric API to programmatically connect to and update the workspace with content in Git.
# For documentation, please see:
# https://learn.microsoft.com/en-us/rest/api/fabric/core/git/connect
# https://learn.microsoft.com/en-us/rest/api/fabric/core/git/initialize-connection
# https://learn.microsoft.com/en-us/rest/api/fabric/core/git/update-from-git
# https://learn.microsoft.com/en-us/rest/api/fabric/core/long-running-operations/get-operation-state
# 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. Fill in the parameters below
# 5. Change PowerShell directory to where this script is saved
# 6. > ./GitIntegration-ConnectAndUpdateFromGit.ps1
# Parameters - fill these in before running the script!
# =====================================================
$workspaceName = "<WORKSPACE NAME>" # The name of the workspace
$shouldDisconnect = $false # Determines whether the workspace should be disconnected before connecting to a new Azure DevOps connection details.
# AzureDevOps details
$azureDevOpsDetails = @{
gitProviderType = "AzureDevOps"
organizationName = "<ORGANIZATION NAME>"
projectName = "<PROJECT NAME>"
repositoryName = "<REPOSITORY NAME>"
branchName = "<BRANCH NAME>"
directoryName = "<DIRECTORY NAME>"
}
# End Parameters =======================================
$global:baseUrl = "<Base URL>" # Replace with environment-specific base URL. For example: "https://api.fabric.microsoft.com/v1"
$global:resourceUrl = "https://api.fabric.microsoft.com"
$global:fabricHeaders = @{}
function SetFabricHeaders() {
#Login to Azure
Connect-AzAccount | Out-Null
# Get authentication
$fabricToken = (Get-AzAccessToken -ResourceUrl $global:resourceUrl).Token
$global:fabricHeaders = @{
'Content-Type' = "application/json"
'Authorization' = "Bearer {0}" -f $fabricToken
}
}
function GetWorkspaceByName($workspaceName) {
# Get workspaces
$getWorkspacesUrl = "{0}/workspaces" -f $global:baseUrl
$workspaces = (Invoke-RestMethod -Headers $global:fabricHeaders -Uri $getWorkspacesUrl -Method GET).value
# Try to find the workspace by display name
$workspace = $workspaces | Where-Object {$_.DisplayName -eq $workspaceName}
return $workspace
}
function GetErrorResponse($exception) {
# Relevant only for PowerShell Core
$errorResponse = $_.ErrorDetails.Message
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
}
try {
SetFabricHeaders
$workspace = GetWorkspaceByName $workspaceName
# Verify the existence of the requested workspace
if(!$workspace) {
Write-Host "A workspace with the requested name was not found." -ForegroundColor Red
return
}
if ($shouldDisconnect)
{
# Disconnect from Git
Write-Host "Disconnecting the workspace '$workspaceName' from Git."
$disconnectUrl = "{0}/workspaces/{1}/git/disconnect" -f $global:baseUrl, $workspace.Id
Invoke-RestMethod -Headers $global:fabricHeaders -Uri $disconnectUrl -Method POST
Write-Host "The workspace '$workspaceName' has been successfully disconnected from Git." -ForegroundColor Green
}
# Connect to Git
Write-Host "Connecting the workspace '$workspaceName' to Git."
$connectUrl = "{0}/workspaces/{1}/git/connect" -f $global:baseUrl, $workspace.Id
$connectToGitBody = @{
gitProviderDetails =$azureDevOpsDetails
} | ConvertTo-Json
Invoke-RestMethod -Headers $global:fabricHeaders -Uri $connectUrl -Method POST -Body $connectToGitBody
Write-Host "The workspace '$workspaceName' has been successfully connected to Git." -ForegroundColor Green
# Initialize Connection
Write-Host "Initializing Git connection for workspace '$workspaceName'."
$initializeConnectionUrl = "{0}/workspaces/{1}/git/initializeConnection" -f $global:baseUrl, $workspace.Id
$initializeConnectionResponse = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $initializeConnectionUrl -Method POST -Body "{}"
Write-Host "The Git connection for workspace '$workspaceName' has been successfully initialized." -ForegroundColor Green
if ($initializeConnectionResponse.RequiredAction -eq "UpdateFromGit") {
# Update from Git
Write-Host "Updating the workspace '$workspaceName' from Git."
$updateFromGitUrl = "{0}/workspaces/{1}/git/updateFromGit" -f $global:baseUrl, $workspace.Id
$updateFromGitBody = @{
remoteCommitHash = $initializeConnectionResponse.RemoteCommitHash
workspaceHead = $initializeConnectionResponse.WorkspaceHead
} | ConvertTo-Json
$updateFromGitResponse = Invoke-WebRequest -Headers $global:fabricHeaders -Uri $updateFromGitUrl -Method POST -Body $updateFromGitBody
$operationId = $updateFromGitResponse.Headers['x-ms-operation-id']
$retryAfter = $updateFromGitResponse.Headers['Retry-After']
Write-Host "Long Running Operation ID: '$operationId' has been scheduled for updating the workspace '$workspaceName' from Git with a retry-after time of '$retryAfter' seconds." -ForegroundColor Green
# Poll Long Running Operation
$getOperationState = "{0}/operations/{1}" -f $global:baseUrl, $operationId
do
{
$operationState = Invoke-RestMethod -Headers $global:fabricHeaders -Uri $getOperationState -Method GET
Write-Host "Update from Git operation status: $($operationState.Status)"
if ($operationState.Status -in @("NotStarted", "Running")) {
Start-Sleep -Seconds $retryAfter
}
} while($operationState.Status -in @("NotStarted", "Running"))
}
else {
Write-Host "Expected RequiredAction to be UpdateFromGit but found $($initializeConnectionResponse.RequiredAction)" -ForegroundColor Red
}
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
}
else{
Write-Host "The workspace '$workspaceName' has been successfully updated with content from Git." -ForegroundColor Green
}
} catch {
$errorResponse = GetErrorResponse($_.Exception)
Write-Host "Failed to connect and update the workspace '$workspaceName' with content from Git. Error reponse: $errorResponse" -ForegroundColor Red
}