Automate Git integration by using APIs

The Microsoft Fabric Git integration tool enables teams to work together using source control to build an efficient and reusable release process for their Fabric content.

With Microsoft Fabric REST APIs, you can automate Fabric procedures and processes to complete tasks faster and with fewer errors. This efficiency leads to cost savings and improved productivity.

This article describes how to use the Git integration REST APIs to automate Git integration in Microsoft Fabric.

Prerequisites

To work with Fabric Git APIs, you need:

You can use the REST APIs without PowerShell, but the scripts in this article use PowerShell. To run the scripts, you need to take the following steps:

Git integration API functions

The Git integration REST APIs can help you achieve the continuous integration and continuous delivery (CI/CD) of your content. Here are a few examples of what can be done by using the APIs:

Examples

Use the following PowerShell scripts to understand how to perform several common automation processes. To view or copy the text in a PowerShell sample, use the links in this section. You can also see all the examples in the Fabric Git integration samples GitHub repo.

Connect and update

This section describes the steps involved in connecting and updating a workspace with Git.

For the complete script, see Connect and update from Git.

  1. Sign in and get access token - Sign in to Fabric as a user (not a service principal). Use the Connect-AzAccount command to sign in. To get an access token, use the Get-AzAccessToken command.

    Your code should look something like this:

    $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
        }
    }
    
  2. Call the Connect API to connect the workspace to a Git repository and branch.

    
    # Connect to Git
    
    Write-Host "Connecting the workspace '$workspaceName' to Git."
    
    $connectUrl = "{0}/workspaces/{1}/git/connect" -f $global:baseUrl, $workspace.Id
    
    # AzureDevOps details
    
    $azureDevOpsDetails = @{
        gitProviderType = "AzureDevOps"
        organizationName = "<ORGANIZATION NAME>"
        projectName = "<PROJECT NAME>"
        repositoryName = "<REPOSITORY NAME>"
        branchName = "<BRANCH NAME>"
        directoryName = "<DIRECTORY NAME>"
    }
    
    $connectToGitBody = @{
        gitProviderDetails =$azureDevOpsDetails
    } | ConvertTo-Json
    
    Invoke-RestMethod -Headers $global:fabricHeaders -Uri $connectUrl -Method POST -Body $connectToGitBody
    
  3. Call the Initialize Connection API to initialize the connection between the workspace and the Git repository/branch.

     # 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 "{}"
    
  4. Based on the response from the Initialize Connection API, call either the Update From Git API to complete the update, or do nothing if no action required.

    The following script updates and monitors the progress:

    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"))
    }
    

Update from Git

In this section, we describe the steps involved in updating a workspace with the changes from Git. In this script, we update the workspace items with changes from Git, but we leave the Git repository unchanged.

For the complete script, see Update workspace from Git.

  1. Log into Git and get authentication.
  2. Call the Get Status API to build the update from Git request body.
  3. Call the Update From Git API to update the workspace with commits pushed to the connected branch.

Commit all

This section gives a step by step description of how to programmatically commit all changes from the workspace to Git.

For the complete script, see Commit all changes to Git.

  1. Log into Git and get authentication.
  2. Connect to workspace.
  3. Call the Commit to Git REST API.
  4. Get the Long Running OperationId for polling the status of the operation.

Selective Commit

This section describes the steps involved in committing only specific changes from the workspace to Git.

For the complete script, see Commit select changes to Git.

  1. Log into Git and get authentication.
  2. Connect to workspace.
  3. Call the Get status API to see which items workspace were changed.
  4. Select the specific items to commit.
  5. Call the Commit to Git API to commit the selected changes from the workspace to the connected remote branch.

Monitor the progress of long running operations

For the complete script, see Poll a long running operation.

  1. Retrieve the operationId from the Update From Git or the Commit to Git script.
  2. Call the Get LRO Status API at specified intervals (in seconds) and print the status.

Considerations and limitations

  • Git integration using APIs is subject to the same limitations as the Git integration user interface.
  • Service principal isn't supported.
  • Refreshing a semantic model using the Enhanced refresh API causes a Git diff after each refresh.