Automate Git integration by using APIs and Azure DevOps

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.

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:

  • Get connection details for the specified workspace.

  • Connect and disconnect a specific workspace from the Git repository and branch connected to it.

  • Initialize a connection for a workspace that is connected to Git.

  • See which items have incoming changes and which items have changes that weren't yet committed to Git with the Git status API.

  • Commit the changes made in the workspace to the connected remote branch.

  • Update the workspace with commits pushed to the connected branch.

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:

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.

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. 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. Describe the request body - In this part of the script you specify which items (such as reports and notebooks) to commit.

    $commitToGitBody = @{ 		
        mode = "All"
        comment = $commitMessage
    } | ConvertTo-Json
    
  3. Call the CommitAll REST API:

    $commitToGitUrl = "{0}/workspaces/{1}/git/commitToGit" -f $global:baseUrl, $workspace.Id
    
    $commitToGitResponse = Invoke-WebRequest -Headers $global:fabricHeaders -Uri $commitToGitUrl -Method POST -Body $commitToGitBody
    
  4. Get the Long Running OperationId for polling the status of the operation.

    $operationId = $commitToGitResponse.Headers['x-ms-operation-id']
    $retryAfter = $commitToGitResponse.Headers['Retry-After']   
    

Selective Commit

In this section we describe 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 Azure 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.

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 Azure 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.

Connect and Sync

This section describes the steps involved in connecting and syncing a workspace with Git. This script works in both directions. We commit changes from the workspace to Git and also update workspace items with changes from Git.

For the complete script, see Connect and sync with Git.

  1. Log into Azure and get authentication.
  2. Call the Connect API to connect the workspace to a Git repository and branch.
  3. Call the Initialize Connection API to initialize the connection between the workspace and the Git repository/branch.
  4. Based on the response from the Initialize Connection API, call either the Commit To Git or Update From Git API to complete the sync, or do nothing if no action required.

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 every x 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.