使用 REST 时无法在 Azure Artifacts 中找到包 API

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

我正在使用 REST API 将包更新到另一个视图(推广),但我不断收到错误消息,指出我的 Feed 中不存在该包,我不知道为什么。

REST 调用 - 方法: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

请求正文:

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

这是我在 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
}

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

同意米尔布兰特的观点。看来您应该提供 packageName @ testlib 而不是 testLib.

您可以使用 REST API Artifact Details - Get Packages 列出此提要中的所有包:

GET https://feeds.dev.azure.com/{organization}/{project}/_apis/packaging/Feeds/{feedId}/packages?api-version=5.1-preview.1

所以我最终用下面的代码让它工作

$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" 

目标是检索视图,获取视图 ID,将其注入正文以调用 packagesbatch REST 调用。