isn/src/isnd/Services/PackageManager.cs

339 lines
13 KiB
C#
Raw Normal View History

2021-09-05 15:44:47 +01:00
using System;
2021-07-05 12:55:52 +01:00
using System.Collections.Generic;
using System.Linq;
2021-09-05 15:44:47 +01:00
using System.Text;
using System.Threading.Tasks;
2022-04-09 19:53:26 +01:00
using isn.Abstract;
2022-07-03 14:50:57 +01:00
using isnd.Controllers;
2021-08-15 19:09:01 +01:00
using isnd.Data;
2022-07-10 17:05:05 +01:00
using isnd.Data.Packages;
using isnd.Data.Packages.Catalog;
2021-08-23 03:21:08 +01:00
using isnd.Entities;
2021-09-05 15:44:47 +01:00
using isnd.Helpers;
2021-08-23 03:21:08 +01:00
using isnd.Interfaces;
2021-08-11 23:25:15 +01:00
using isnd.ViewModels;
2021-07-05 12:55:52 +01:00
using Microsoft.EntityFrameworkCore;
2021-08-23 03:21:08 +01:00
using Microsoft.Extensions.Options;
2021-07-05 12:55:52 +01:00
using NuGet.Versioning;
using Unleash;
namespace isnd.Services
{
2021-08-23 03:21:08 +01:00
public class PackageManager : IPackageManager
2021-07-05 12:55:52 +01:00
{
2022-04-17 14:10:20 +01:00
public const string BASE_API_LEVEL = "3.5.0";
2022-04-17 14:48:18 +01:00
2021-07-05 12:55:52 +01:00
ApplicationDbContext dbContext;
2022-07-10 17:05:05 +01:00
2021-08-28 18:41:22 +01:00
public PackageManager(ApplicationDbContext dbContext,
2021-08-27 21:22:34 +01:00
IOptions<IsndSettings> siteConfigOptionsOptions)
2021-07-05 12:55:52 +01:00
{
this.dbContext = dbContext;
2021-08-28 21:26:39 +01:00
isndSettings = siteConfigOptionsOptions.Value;
2021-09-04 13:41:00 +01:00
extUrl = isndSettings.ExternalUrl + "/";
2021-08-23 03:21:08 +01:00
CurrentCatalogIndex = GetCatalogIndex();
2021-07-05 12:55:52 +01:00
}
2021-08-23 03:21:08 +01:00
2021-09-02 23:37:28 +01:00
public IEnumerable<Resource> GetResources(IUnleash unleashClient)
{
var res = new List<Resource>();
// stable
if (unleashClient.IsEnabled("pkg-push", true))
res.Add(
new Resource
{
2021-09-08 01:55:00 +01:00
Id = extUrl + ApiConfig.Publish,
2022-04-17 14:10:20 +01:00
Type = "PackagePublish/2.0.0", // TODO BASE_API_LEVEL
2021-09-08 01:55:00 +01:00
Comment = "Package Publish service"
2021-09-02 23:37:28 +01:00
});
// under dev, only leash in release mode
2022-07-10 17:05:05 +01:00
if (unleashClient.IsEnabled("pkg-get", true))
2021-09-02 23:37:28 +01:00
res.Add(
new Resource
{
2022-07-03 14:50:57 +01:00
Id = extUrl + ApiConfig.GetPackage,
2022-07-10 17:05:05 +01:00
Type = "PackageBaseAddress/3.0.0",
2021-09-08 01:55:00 +01:00
Comment = "Package Base Address service"
2021-09-02 23:37:28 +01:00
});
2022-07-10 17:05:05 +01:00
if (unleashClient.IsEnabled("pkg-autocomplete", true))
2021-09-02 23:37:28 +01:00
res.Add(
new Resource
{
2021-09-08 01:55:00 +01:00
Id = extUrl + ApiConfig.AutoComplete,
2022-04-17 14:10:20 +01:00
Type = "SearchAutocompleteService/" + BASE_API_LEVEL,
2021-09-08 01:55:00 +01:00
Comment = "Auto complete service"
2021-09-02 23:37:28 +01:00
});
2022-04-17 14:10:20 +01:00
if (unleashClient.IsEnabled("pkg-search", true))
2021-09-02 23:37:28 +01:00
res.Add(
new Resource
{
2021-09-08 01:55:00 +01:00
Id = extUrl + ApiConfig.Search,
2022-04-17 14:10:20 +01:00
Type = "SearchQueryService/" + BASE_API_LEVEL,
2021-09-08 01:55:00 +01:00
Comment = "Search Query service"
2021-09-02 23:37:28 +01:00
});
2022-07-10 17:05:05 +01:00
if (unleashClient.IsEnabled("pkg-catalog", true))
2021-09-05 15:44:47 +01:00
res.Add(
new Resource
{
2021-09-08 01:55:00 +01:00
Id = extUrl + ApiConfig.Catalog,
2022-07-10 17:05:05 +01:00
Type = "Catalog/" + BASE_API_LEVEL,
2021-09-08 01:55:00 +01:00
Comment = "Package Catalog Index"
2022-04-17 16:22:40 +01:00
});
2022-07-10 17:05:05 +01:00
/* FIXME */
res.Add(
2022-04-17 16:22:40 +01:00
new Resource
{
2022-07-10 17:05:05 +01:00
Id = extUrl + "v" + BASE_API_LEVEL + "/" + ApiConfig.Registration,
Type = "RegistrationsBaseUrl",
2022-04-17 16:22:40 +01:00
Comment = "Base URL of storage where isn package registration info is stored in GZIP format. This base URL includes SemVer 2.0.0 packages."
2022-07-10 17:05:05 +01:00
});
res.Add(
new Resource
{
Id = extUrl + "v3.0.0-beta/" + ApiConfig.Registration,
Type = "RegistrationsBaseUrl/3.0.0-beta",
Comment = "Base URL of storage where isn package registration info is stored in GZIP format. This base URL includes SemVer 2.0.0 packages."
});
res.Add(
new Resource
{
Id = extUrl + "v3.0.0-rc/" + ApiConfig.Registration,
Type = "RegistrationsBaseUrl/3.0.0-rc",
Comment = "Base URL of storage where isn package registration info is stored in GZIP format. This base URL includes SemVer 2.0.0 packages."
});
res.Add(new Resource
{
Id = extUrl + "v3.4.0/" + ApiConfig.Registration,
Type = "RegistrationsBaseUrl/3.4.0",
Comment = "Base URL of storage where isn package registration info is stored in GZIP format. This base URL includes SemVer 2.0.0 packages."
});
res.Add(new Resource
{
Id = extUrl + "v3.6.0/" + ApiConfig.Registration,
Type = "RegistrationsBaseUrl/3.6.0",
Comment = "Base URL of storage where isn package registration info is stored in GZIP format. This base URL includes SemVer 2.0.0 packages."
});
2021-09-02 23:37:28 +01:00
return res;
}
2021-08-23 03:21:08 +01:00
public PackageIndexViewModel SearchByName(string query,
int skip, int take, bool prerelease = false,
2021-07-05 12:55:52 +01:00
string packageType = null)
{
2021-08-10 00:10:26 +01:00
2021-07-05 12:55:52 +01:00
var scope = dbContext.Packages
.Where(
2021-09-05 15:44:47 +01:00
p => (PackageIdHelpers.CamelCaseMatch(p.Id, query) || PackageIdHelpers.SeparatedByMinusMatch(p.Id, query))
2021-07-05 12:55:52 +01:00
&& (prerelease || p.Versions.Any(v => !v.IsPrerelease))
&& (packageType == null || p.Versions.Any(v => v.Type == packageType))
);
2021-08-10 00:10:26 +01:00
var total = scope.Count();
var pkgs = scope.Skip(skip).Take(take).ToArray();
2021-08-11 23:25:15 +01:00
return new PackageIndexViewModel
2021-08-23 03:21:08 +01:00
{
2021-09-12 20:45:43 +01:00
Query = query,
TotalHits = total,
Data = pkgs
2021-08-23 03:21:08 +01:00
};
}
public AutoCompleteResult AutoComplete(string id,
int skip, int take, bool prerelease = false,
2021-07-05 12:55:52 +01:00
string packageType = null)
{
var scope = dbContext.PackageVersions.Where(
v => v.PackageId == id
&& (prerelease || !v.IsPrerelease)
&& (packageType == null || v.Type == packageType)
)
.OrderBy(v => v.FullString);
return new AutoCompleteResult
2021-08-23 03:21:08 +01:00
{
totalHits = scope.Count(),
data = scope.Select(v => v.FullString)
2021-07-05 12:55:52 +01:00
.Skip(skip).Take(take).ToArray()
2021-08-23 03:21:08 +01:00
};
2021-07-05 12:55:52 +01:00
}
// TODO stocker MetaData plutôt que FullString en base,
// et en profiter pour corriger ce listing
public string[] GetVersions(
string id,
NuGetVersion parsedVersion,
bool prerelease = false,
string packageType = null,
int skip = 0,
int take = 25)
{
return dbContext.PackageVersions.Where(
v => v.PackageId == id
&& (prerelease || !v.IsPrerelease)
&& (packageType == null || v.Type == packageType)
&& (parsedVersion.CompareTo(new SemanticVersion(v.Major, v.Minor, v.Patch)) < 0)
)
.OrderBy(v => v.FullString)
.Select(v => v.FullString)
.Skip(skip).Take(take).ToArray();
}
2021-08-23 03:21:08 +01:00
public static CatalogIndex CurrentCatalogIndex { get; protected set; }
2021-08-27 21:22:34 +01:00
public static List<Page> CurrentCatalogPages { get; protected set; }
2021-08-28 21:26:39 +01:00
private IsndSettings isndSettings;
2021-09-02 23:37:28 +01:00
private string extUrl;
2021-08-23 03:21:08 +01:00
public virtual CatalogIndex GetCatalogIndex()
{
if (CurrentCatalogIndex == null)
{
2021-08-29 00:51:43 +01:00
ÛpdateCatalogFor();
2021-08-23 03:21:08 +01:00
}
return CurrentCatalogIndex;
}
2021-09-05 15:44:47 +01:00
public void ÛpdateCatalogFor(Commit reason = null)
2021-08-23 03:21:08 +01:00
{
2021-08-28 18:41:22 +01:00
int i = 0;
int p = 0;
2021-08-27 21:22:34 +01:00
var oldIndex = CurrentCatalogIndex;
var oldPages = CurrentCatalogPages;
2022-07-10 17:05:05 +01:00
string baseid = extUrl + ApiConfig.Catalog;
string basepageid = extUrl + ApiConfig.CatalogPage;
2021-08-27 21:22:34 +01:00
CurrentCatalogIndex = new CatalogIndex
{
2021-09-05 15:44:47 +01:00
Id = baseid,
2021-08-29 00:08:34 +01:00
Items = new List<PageRef>()
2021-08-27 21:22:34 +01:00
};
CurrentCatalogPages = new List<Page>();
var scope = dbContext.Commits.OrderBy(c => c.TimeStamp);
2022-07-10 17:05:05 +01:00
2021-08-27 21:22:34 +01:00
PageRef pageRef = null;
Page page = null;
2021-08-28 21:26:39 +01:00
i = isndSettings.CatalogPageLen;
2021-08-27 21:22:34 +01:00
foreach (var commit in scope)
{
2021-08-28 21:26:39 +01:00
if (i >= this.isndSettings.CatalogPageLen)
2021-08-27 21:22:34 +01:00
{
2021-08-28 18:41:22 +01:00
page = new Page
{
2021-09-05 15:44:47 +01:00
Id = basepageid + "-" + p++,
Parent = baseid,
2021-08-27 21:22:34 +01:00
CommitId = commit.CommitId,
CommitTimeStamp = commit.CommitTimeStamp,
2021-08-29 00:08:34 +01:00
Items = new List<PackageRef>()
2021-08-27 21:22:34 +01:00
};
CurrentCatalogPages.Add(page);
2021-08-28 18:41:22 +01:00
pageRef = new PageRef
{
2021-09-05 15:44:47 +01:00
Id = page.Id,
CommitId = commit.CommitId,
CommitTimeStamp = commit.CommitTimeStamp
2021-08-27 21:22:34 +01:00
};
CurrentCatalogIndex.Items.Add(pageRef);
i = 0;
}
2021-08-28 18:41:22 +01:00
var validPkgs = dbContext.Packages
.Include(pkg => pkg.Versions)
2021-08-29 00:08:34 +01:00
.Include(pkg => pkg.LatestVersion)
2021-08-28 18:41:22 +01:00
.Where(
2021-09-02 23:37:28 +01:00
pkg => pkg.Versions.Count(v => v.CommitId == commit.CommitId) > 0
2021-08-28 18:41:22 +01:00
);
// pkg.Versions.OrderByDescending(vi => vi.CommitNId).First().FullString
foreach (var pkg in validPkgs)
2021-08-27 21:22:34 +01:00
{
2021-09-02 23:37:28 +01:00
var v = pkg.Versions.
2022-07-10 17:05:05 +01:00
Where(cv => cv.CommitId == commit.CommitId)
2021-09-02 23:37:28 +01:00
.OrderByDescending(vc => vc.CommitNId).First();
2021-08-28 18:41:22 +01:00
2021-09-05 15:44:47 +01:00
StringBuilder refid = new StringBuilder(extUrl);
2022-07-10 17:05:05 +01:00
refid.AppendFormat("{0}/{1}/{2}", ApiConfig.CatalogLeaf, v.PackageId
2021-09-05 15:44:47 +01:00
, v.FullString);
2022-07-10 17:05:05 +01:00
if (v.Type != null)
refid.AppendFormat("/{0}", v.Type);
2021-09-05 15:44:47 +01:00
2021-08-28 18:41:22 +01:00
var pkgref = new PackageRef
{
Version = v.FullString,
LastCommit = v.LatestCommit,
CommitId = v.LatestCommit.CommitId,
CommitTimeStamp = v.LatestCommit.CommitTimeStamp,
2021-09-05 15:44:47 +01:00
RefId = refid.ToString(),
Id = v.PackageId,
RefType = v.LatestCommit.Action == PackageAction.PublishPackage
? "nuget:PackageDetails" :
"nuget:PackageDelete"
2021-08-28 18:41:22 +01:00
};
page.Items.Add(pkgref);
}
2021-09-05 15:44:47 +01:00
reason = commit;
2021-08-27 21:22:34 +01:00
i++;
}
2021-09-05 15:44:47 +01:00
if (reason != null)
2021-08-27 21:22:34 +01:00
{
2021-09-05 15:44:47 +01:00
CurrentCatalogIndex.CommitId = reason.CommitId;
2021-08-27 21:22:34 +01:00
}
2021-09-05 15:44:47 +01:00
else
2021-08-28 18:41:22 +01:00
{
2021-09-05 15:44:47 +01:00
// From a fresh db
2021-08-28 18:41:22 +01:00
CurrentCatalogIndex.CommitId = "none";
}
2021-08-23 03:21:08 +01:00
}
2021-09-05 18:55:38 +01:00
public async Task<PackageDeletionReport> DeletePackageAsync(string pkgid, string version, string type)
2021-07-05 12:55:52 +01:00
{
2022-07-03 14:50:57 +01:00
// TODO deletion on disk
2022-07-10 17:05:05 +01:00
var commit = new Commit
{
2021-09-05 15:44:47 +01:00
Action = PackageAction.DeletePackage,
TimeStamp = DateTime.Now
};
2021-09-05 17:10:50 +01:00
dbContext.Commits.Add(commit);
2021-09-05 15:44:47 +01:00
var pkg = await dbContext.PackageVersions.SingleOrDefaultAsync(
2021-09-05 18:55:38 +01:00
v => v.PackageId == pkgid &&
v.FullString == version &&
v.Type == type
2021-09-05 15:44:47 +01:00
);
if (pkg == null)
2021-07-05 12:55:52 +01:00
{
2022-07-10 17:05:05 +01:00
return new PackageDeletionReport { Deleted = false };
2021-07-05 12:55:52 +01:00
}
2021-09-05 15:44:47 +01:00
dbContext.PackageVersions.Remove(pkg);
await dbContext.SaveChangesAsync();
ÛpdateCatalogFor(commit);
2022-07-10 17:05:05 +01:00
return new PackageDeletionReport { Deleted = true, DeletedVersion = pkg };
2021-07-05 12:55:52 +01:00
}
2021-09-05 18:55:38 +01:00
public async Task<PackageVersion> GetPackageAsync(string pkgid, string version, string type)
{
return await dbContext.PackageVersions.SingleOrDefaultAsync(
v => v.PackageId == pkgid &&
v.FullString == version &&
v.Type == type
);
}
2022-07-03 14:50:57 +01:00
public async Task<CatalogRegistration> GetPackageRegistrationAsync(string pkgid, string version, string type)
{
var pkgVersion = await GetPackageAsync(pkgid, version, type);
return new CatalogRegistration
{
Id = pkgVersion.PackageId,
CommitTimeStamp = pkgVersion.LatestCommit.CommitTimeStamp,
PackageContent = extUrl + pkgVersion.FullString
};
}
public IEnumerable<PackageVersion> GetCatalogLeaf(string id, string version, string lower)
{
return dbContext.PackageVersions
2022-07-10 17:05:05 +01:00
.Include(v => v.Package)
2022-07-03 14:50:57 +01:00
.Where(v => v.PackageId == id && v.FullString == version
&& (lower == null || lower == v.Type));
}
2021-07-05 12:55:52 +01:00
}
}