Search code examples
powershellazure-devopsmicrosoft-fabric

Script to call the MS Fabric -Git update From Git API


I have below script for updating the MS Fabric workspace in ADO release pipeline. This is not working. Also the authentication is done on Service Principal for the token.

$accessToken = "$(accessToken)"  # Use the token from pipeline variables

$headers = @{
    Authorization = "Bearer $accessToken"
    "Content-Type" = "application/json"
}

$apiUrl = "https://api.fabric.microsoft.com/core/git/update-from-git?workspaceId=<workspace-id>&itemId=<item-id>"
$body = @{
    gitBranch = "feature"
} | ConvertTo-Json -Depth 10

$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body

Write-Output $response

Error:

2025-03-05T17:37:34.2927854Z Invoke-RestMethod : 
2025-03-05T17:37:34.2928743Z Not Found
2025-03-05T17:37:34.2930059Z Not Found
2025-03-05T17:37:34.2931039Z HTTP Error 404. The requested resource is not found.
2025-03-05T17:37:34.2932857Z At D:\a\_temp\.ps1:16 char:13
2025-03-05T17:37:34.2933909Z + $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $hea ...
2025-03-05T17:37:34.2934961Z +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2025-03-05T17:37:34.2936330Z     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 
2025-03-05T17:37:34.2937301Z    eption

Solution

  • I could reproduce the 404 error in local PowerShell prompt with your script, which didn't seem to call a valid API URL.

    You may seek to call this API instead to Update From Git. However, please note that it doesn't support identities of service principal or managed identities.

    api

    According to the document on Identity Support,

    Each API's reference page lists whether the API supports service principals and managed identities. When using APIs, consider whether the API call relies on other APIs or items that don't support the calling identity. In such cases, your call will fail.

    I think this is also why my call to List Workspaces succeeded, while the call to Get Git Status failed to get the git head to be updated from. I also tested to call that Get Git Status api authenticating with my user's bearer token and it worked.

    Adding my script to help you understand my tests.

    # Generate AAD token for service principal to access Microsoft Fabric
    $scope = "https://api.fabric.microsoft.com/.default"
    $grantType = "client_credentials"
    
    $body = @{
        client_id = $clientId
        scope = $scope
        client_secret = $clientSecret
        grant_type = $grantType
    }
    
    $response = Invoke-WebRequest -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Method POST -ContentType "application/x-www-form-urlencoded" -Body $body | ConvertFrom-json
    $accessToken = $response.access_token 
    
    $headers = @{
        Authorization = "Bearer $accessToken"
        "Content-Type" = "application/json"
    }
    
    # Succeeded to list workspaces
    $workspacesUrl = "https://api.fabric.microsoft.com/v1/workspaces"
    $workspaces = Invoke-RestMethod -Uri $workspacesUrl -Method Get -Headers $headers
    $devWorkspaceId = ($workspaces.value | Where-Object { $_.displayName -eq "Dev" }).id
    
    # Failed with service principal but worked when authenticating against the bearer token of my user account
    $gitStatusUrl = "https://api.fabric.microsoft.com/v1/workspaces/$devWorkspaceId/git/status"
    $gitStatus= Invoke-RestMethod -Uri $gitStatusUrl -Method Get -Headers $headers
    $gitStatus
    
    

    We are not sure whether you found that API https://api.fabric.microsoft.com/core/git/update-from-git?workspaceId=<workspace-id>&itemId=<item-id> from Microsoft Fabric REST API references - Microsoft Fabric REST APIs | Microsoft Learn. If yes, please edit your original post and share the API link. Furthermore, let us know if the script works outside pipelines to help narrow down the root cause.

    Azure Pipelines are simply an automation tool. You need to make sure your script works locally before integrating it in pipelines. Hope the information clarifies the actual cause of the issue.