Search code examples
restazure-devopsazure-artifacts

Can't find Package in Azure Artifacts while using REST API


I am using the REST API to update the package to another view (Promote) but I keep getting an error that the package doesn't exist in my feed and I have no idea why.

REST Call - Method: PATCH

https://pkgs.dev.azure.com/MyOrg/_apis/packaging/feeds/5aa467a8-14d8-4590-8579-37d9769f6159/npm/testLib/versions/0.0.12345?api-version=6.1-preview.1

Request Body:

{
    "views":
    {
        "op":"add", 
        "path":"/views/-", 
        "value":"Test"
    }
}

And here is the error I am getting back in Postman

{
    "success": "false",
    "error": "NotFound",
    "reason": "Cannot find the file testLib-0.0.12345.tgz in package 'foundations 0.0.74965' in feed 'MyFeed'",
    "innerException": null,
    "message": "Cannot find the file testLib-0.0.12345.tgz in package 'foundations 0.0.74965' in feed 'MyFeed'",
    "typeName": "Microsoft.VisualStudio.Services.Packaging.Shared.WebApi.Exceptions.PackageNotFoundException, Microsoft.VisualStudio.Services.Packaging.Shared.WebApi, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    "typeKey": "PackageNotFoundException",
    "errorCode": 0,
    "eventId": 3000
}

Proof of Artifact


Solution

  • So I ended up getting this to work with the following code

    $package = Get-Content -Path "$(Build.ArtifactStagingDirectory)/libs/${{ package.value }}/package.json" -Raw | ConvertFrom-Json
    
    $version = $package.version
    Write-Output "Updating ${{ package.key }} $version"
       
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "{Promoting npm package}","{$(System.AccessToken)}")))
    
    $uri = "https://pkgs.dev.azure.com/MyOrg/_apis/packaging/feeds/5aa467a8-14d8-4590-8579-37d9769f6159/npm/packagesbatch?api-version=6.1-preview.1"
    $viewURI = "https://feeds.dev.azure.com/MyOrg/_apis/packaging/Feeds/MYFEED/views/${{ parameters.environment }}?api-version=5.1-preview.1"
    
    Write-Output "Retrieving View from Azure Artifacts"
    $viewID = ((Invoke-WebRequest -Uri $viewURI -Headers @{Authorization="Bearer $(System.AccessToken)"} -Method Get).Content | ConvertFrom-Json).id
    
    $body = '{"data":{"viewId":"' + $viewID +'"},"operation":0,"packages":[{"id":"@cn/${{ package.key }}","version":"' + $version + '","protocolType":"Npm"}]}'
    
    Write-Output "Deploying ${{ package.key }} to Azure Artifacts"
    Invoke-WebRequest -Uri $uri -Headers @{Authorization="Bearer $(System.AccessToken)"} -Method Post -Body $body -ContentType "application/json" 
    

    The goal was to retrieve the view, get the View ID, inject it into the body to call the packagesbatch REST call.