'Using Nuget.Protocol to delete a package from Azure DevOps
I'm writing a simple application to implement package retention in our AzureDevOps NuGet feed, but I can't delete a package -- nothing happens.
(I want to keep all release packages, but delete all pre-release packages for branches that nolonger exist and keep only the latest three pre-release packages for branches that do exist.)
// Log in and whatnot.
PackageSource packageSource = new PackageSource("https://pkgs.dev.azure.com/Organisation/_packaging/Organisation/nuget/v3/index.json")
{
Credentials = new PackageSourceCredential(
source: "https://pkgs.dev.azure.com/Organisation/_packaging/Organisation/nuget/v3/index.json",
username: "[email protected]",
passwordText: _Pat,
isPasswordClearText: true,
validAuthenticationTypesText: null)
};
NuGet.Protocol.Core.Types.SourceRepository repository = NuGet.Protocol.Core.Types.Repository.Factory.GetCoreV3(packageSource);
PackageSearchResource packageSearch = repository.GetResourceAsync<NuGet.Protocol.Core.Types.PackageSearchResource>().Result;
PackageUpdateResource packageUpdate = repository.GetResourceAsync<PackageUpdateResource>().Result;
// Delete a package -- doesn't work!
packageUpdate.Delete("Organisation.MyDll", "1.0.1-branch.build", packageSource => _Pat, packageSource => true, false, _Logger);
The .Delete
doesn't throw an exception or log any error, but the package remains in AzureDevOps.
I think I need to add .Wait()
because of the async
nonsense, but even so: the packages appear srikethrough in the Azure DevOps website, but not in the feed's recycle bin, and they're still listed in NuGet package manager in VisualStudio.
How do you actually delete the things?
Update
So apparently the C# only delists and you have to use the HTTP API directly to actually delete. However, it's impossible to authenticate.
private static void Delete(NuGet.Packaging.Core.PackageIdentity packageIdentity)
{
// https://docs.microsoft.com/en-us/nuget/api/package-publish-resource
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-NuGet-ApiKey", "VSTS");
//httpClient.DefaultRequestHeaders.Add("X-NuGet-ApiKey", _Pat);
//httpClient.DefaultRequestHeaders.Add("X-NuGet-ApiKey", "[email protected]:" + _Pat);
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", ":" + _Pat);
//httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", "[email protected]:" + _Pat);
//httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _Pat);
// https://docs.microsoft.com/en-us/rest/api/azure/devops/artifactspackagetypes/nuget/delete-package-version?view=azure-devops-rest-6.0
HttpResponseMessage rx = httpClient.DeleteAsync($"https://pkgs.dev.azure.com/Organisation/_apis/packaging/feeds/FeedName/nuget/packages/{packageIdentity.Id}/versions/{packageIdentity.Version}?api-version=6.0-preview.1").Result;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|