From 2dcf1a28061851de22c3569e14cda67af5dd75ab Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 3 Jul 2022 14:50:57 +0100 Subject: [PATCH] refacts --- .vs/isn/xs/UserPrefs.xml | 23 ++++------- src/isn.abst/Constants.cs | 8 ++++ .../SafeNameAttribute.cs | 2 +- src/isnd/Controllers/HomeController.cs | 7 +++- src/isnd/Controllers/Packages/AutoComplete.cs | 17 ++------ src/isnd/Controllers/Packages/Catalog.cs | 41 +++++++++++++------ .../Packages/CatalogRegistration.cs | 38 +++++++++++++++++ src/isnd/Controllers/Packages/Files.cs | 14 ++++--- src/isnd/Controllers/Packages/GetVersions.cs | 2 +- src/isnd/Controllers/Packages/Put.cs | 37 +++++++++-------- src/isnd/Data/Catalog/CatalogLeaf.cs | 2 +- src/isnd/Data/Package.cs | 6 +-- src/isnd/Data/PackageVersion.cs | 7 +++- src/isnd/Entities/ApiConfig.cs | 11 +++-- src/isnd/Interfaces/IPackageManager.cs | 1 + src/isnd/Services/PackageManager.cs | 34 +++++++++++---- test/isn.tests/PushTest.cs | 4 +- test/isnd.tests/isnd.tests.csproj | 10 +---- 18 files changed, 166 insertions(+), 98 deletions(-) create mode 100644 src/isn.abst/Constants.cs rename src/{isnd/Attributes => isn.abst}/SafeNameAttribute.cs (88%) create mode 100644 src/isnd/Controllers/Packages/CatalogRegistration.cs diff --git a/.vs/isn/xs/UserPrefs.xml b/.vs/isn/xs/UserPrefs.xml index fe5b78a..f7de760 100644 --- a/.vs/isn/xs/UserPrefs.xml +++ b/.vs/isn/xs/UserPrefs.xml @@ -1,33 +1,23 @@  - + - + + - - - - - - - - - - - - - - + + + @@ -35,4 +25,5 @@ + \ No newline at end of file diff --git a/src/isn.abst/Constants.cs b/src/isn.abst/Constants.cs new file mode 100644 index 0000000..699bb50 --- /dev/null +++ b/src/isn.abst/Constants.cs @@ -0,0 +1,8 @@ +namespace isn.abst +{ + public static class Constants + { + public const string PaquetFileEstension = "nupkg"; + public const string SpecFileEstension = "nuspec"; + } +} \ No newline at end of file diff --git a/src/isnd/Attributes/SafeNameAttribute.cs b/src/isn.abst/SafeNameAttribute.cs similarity index 88% rename from src/isnd/Attributes/SafeNameAttribute.cs rename to src/isn.abst/SafeNameAttribute.cs index 771e3b9..6f4596e 100644 --- a/src/isnd/Attributes/SafeNameAttribute.cs +++ b/src/isn.abst/SafeNameAttribute.cs @@ -4,7 +4,7 @@ using System.Linq; namespace isnd.Attributes { - internal class SafeNameAttribute : ValidationAttribute + public class SafeNameAttribute : ValidationAttribute { public override bool IsValid(object value) { diff --git a/src/isnd/Controllers/HomeController.cs b/src/isnd/Controllers/HomeController.cs index 7d11d02..3b7bca5 100644 --- a/src/isnd/Controllers/HomeController.cs +++ b/src/isnd/Controllers/HomeController.cs @@ -6,7 +6,7 @@ using System.Linq; using isnd.ViewModels; using Unleash; using System.Reflection; - +using Microsoft.EntityFrameworkCore; namespace isnd.Controllers { @@ -27,7 +27,10 @@ namespace isnd.Controllers { return View(new HomeIndexViewModel{ - PkgCount = _dbContext.Packages.Count(), + PkgCount = _dbContext.Packages + .Include(p => p.Versions) + .Where(p => p.Versions.Count > 0) + .Count(), UnleashClient = _unleashĈlient }); } diff --git a/src/isnd/Controllers/Packages/AutoComplete.cs b/src/isnd/Controllers/Packages/AutoComplete.cs index 3839216..bb025a5 100644 --- a/src/isnd/Controllers/Packages/AutoComplete.cs +++ b/src/isnd/Controllers/Packages/AutoComplete.cs @@ -17,26 +17,17 @@ namespace isnd.Controllers int skip = 0, int take = 25) { - CheckParams(take, semVerLevel); - if (ModelState.ErrorCount > 0) return BadRequest(ModelState); - - return Ok(packageManager.AutoComplete(id,skip,take,prerelease,packageType)); - } - protected void CheckParams(int maxTake) - { - if (maxTake > maxTake) + if (take > maxTake) { ModelState.AddModelError("take", "Maximum exceeded"); } - } - - protected void CheckParams(int take, string semVerLevel) - { - CheckParams(take); if (semVerLevel != PackageManager.BASE_API_LEVEL) { ModelState.AddModelError("semVerLevel", PackageManager.BASE_API_LEVEL + " expected"); } + if (ModelState.ErrorCount > 0) return BadRequest(ModelState); + + return Ok(packageManager.AutoComplete(id,skip,take,prerelease,packageType)); } } } \ No newline at end of file diff --git a/src/isnd/Controllers/Packages/Catalog.cs b/src/isnd/Controllers/Packages/Catalog.cs index c46301c..2fb1388 100644 --- a/src/isnd/Controllers/Packages/Catalog.cs +++ b/src/isnd/Controllers/Packages/Catalog.cs @@ -6,6 +6,7 @@ using isnd.Services; using isnd.Entities; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; namespace isnd.Controllers { @@ -27,8 +28,8 @@ namespace isnd.Controllers return Ok(PackageManager.CurrentCatalogPages[int.Parse(id)]); } - [HttpGet(_pkgRootPrefix + ApiConfig.CatalogLeaf + "/{id}/{*lower}")] - public async Task CatalogLeafAsync(string id, string lower) + [HttpGet(_pkgRootPrefix + ApiConfig.Registration + "/{id}/{*lower}")] + public async Task CatalogRegistrationAsync(string id, string lower) { string pkgType = ParamHelpers.Optional(ref lower); var pkgVersion = await dbContext.PackageVersions @@ -39,21 +40,35 @@ namespace isnd.Controllers v.Type == pkgType ); if (pkgVersion == null) return NotFound(); + return Ok(); + } + + + [HttpGet(_pkgRootPrefix + ApiConfig.CatalogLeaf + "/{id}/{version}/{*lower}")] + public async Task CatalogLeafAsync(string id, string version, string lower) + { + var pkgvs = this.packageManager.GetCatalogLeaf(id, version, lower) + .ToArray(); + + if (pkgvs.Count()==0) return NotFound(); + + List types = pkgvs.Select( + v => v.Type ?? "Dependency" + ).Distinct().ToList(); + if (!types.Contains("PackageDelete")) + types.Add("PackageDetails"); + var pub = pkgvs.Last().Package.CommitTimeStamp; + var firstpub = pkgvs.First().Package.CommitTimeStamp; - var pub = await dbContext.Commits - .Include(c => c.Versions) - .OrderBy(c => c.CommitTimeStamp) - .SingleOrDefaultAsync - ( - c => c.Action == PackageAction.PublishPackage - && c.Versions.Contains(pkgVersion) - ); return Ok(new CatalogLeaf { CommitId = id, - Id = pkgVersion.PackageId, - CommitTimeStamp = pkgVersion.LatestCommit.CommitTimeStamp - + Id = id, + CommitTimeStamp = firstpub, + Version = version, + Published = pub, + RefType = types.ToArray(), + }); } diff --git a/src/isnd/Controllers/Packages/CatalogRegistration.cs b/src/isnd/Controllers/Packages/CatalogRegistration.cs new file mode 100644 index 0000000..3985e10 --- /dev/null +++ b/src/isnd/Controllers/Packages/CatalogRegistration.cs @@ -0,0 +1,38 @@ +using System; +using Newtonsoft.Json; + +namespace isnd.Controllers +{ + public class CatalogRegistration + { + [JsonProperty("@id")] + public string Id { get; set; } + + + /* + "@id": "https://api.nuget.org/v3/registration3/nuget.versioning/4.3.0.json", + "catalogEntry": "https://api.nuget.org/v3/catalog0/data/2017.08.11.18.24.22/nuget.versioning.4.3.0.json", + "listed": true, + "packageContent": "https://api.nuget.org/v3-flatcontainer/nuget.versioning/4.3.0/nuget.versioning.4.3.0.nupkg", + "published": "2017-08-11T18:24:14.36+00:00", + "registration": "https://api.nuget.org/v3/registration3/nuget.versioning/index.json" + + + */ + [JsonProperty("catalogEntry")] + public string CatalogEntry { get; set; } + + [JsonProperty("listed")] + public bool Listed { get; set; } = true; + + [JsonProperty("packageContent")] + public string PackageContent { get; set; } + + [JsonProperty("published")] + public DateTime CommitTimeStamp { get; set; } + + [JsonProperty("registration")] + public string registration { get; set; } + + } +} \ No newline at end of file diff --git a/src/isnd/Controllers/Packages/Files.cs b/src/isnd/Controllers/Packages/Files.cs index 83b9223..668fa85 100644 --- a/src/isnd/Controllers/Packages/Files.cs +++ b/src/isnd/Controllers/Packages/Files.cs @@ -3,20 +3,23 @@ using System.IO; using Microsoft.AspNetCore.Mvc; using isnd.Attributes; using isnd.Entities; +using isn.abst; + namespace isnd.Controllers { public partial class PackagesController { - // Web get nupkg - [HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/{idf}-{lowerf}.nupkg")] + // Web get the paquet + [HttpGet(_pkgRootPrefix + ApiConfig.GetPackage + "/{id}/{lower}/{idf}-{lowerf}." + + Constants.PaquetFileEstension)] public IActionResult GetPackage( [FromRoute][SafeName][Required] string id, [FromRoute][SafeName][Required] string lower, [FromRoute] string idf, [FromRoute] string lowerf) { var pkgpath = Path.Combine(isndSettings.PackagesRootDir, - id, lower, $"{id}-{lower}.nupkg" + id, lower, $"{id}-{lower}." + Constants.PaquetFileEstension ); FileInfo pkgfi = new FileInfo(pkgpath); @@ -29,7 +32,8 @@ namespace isnd.Controllers } // Web get spec - [HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/{idf}-{lowerf}.nuspec")] + [HttpGet(_pkgRootPrefix + ApiConfig.GetNuspec + "/{id}/{lower}/{idf}-{lowerf}." + + Constants.SpecFileEstension)] public IActionResult GetNuspec( [FromRoute][SafeName][Required] string id, [FromRoute][SafeName][Required] string lower, @@ -37,7 +41,7 @@ namespace isnd.Controllers [FromRoute][SafeName][Required] string lowerf) { var pkgpath = Path.Combine(isndSettings.PackagesRootDir, - id, lower, $"{id}.nuspec"); + id, lower, $"{id}." + Constants.SpecFileEstension); FileInfo pkgfi = new FileInfo(pkgpath); if (!pkgfi.Exists) diff --git a/src/isnd/Controllers/Packages/GetVersions.cs b/src/isnd/Controllers/Packages/GetVersions.cs index 74a8060..8f93e15 100644 --- a/src/isnd/Controllers/Packages/GetVersions.cs +++ b/src/isnd/Controllers/Packages/GetVersions.cs @@ -6,7 +6,7 @@ namespace isnd.Controllers { public partial class PackagesController { - [HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/index.json")] + [HttpGet(_pkgRootPrefix + ApiConfig.GetVersion + "/{id}/{lower}/index.json")] public IActionResult GetVersions( string id, string lower, diff --git a/src/isnd/Controllers/Packages/Put.cs b/src/isnd/Controllers/Packages/Put.cs index 506001a..461d34f 100644 --- a/src/isnd/Controllers/Packages/Put.cs +++ b/src/isnd/Controllers/Packages/Put.cs @@ -16,6 +16,7 @@ using isnd.Helpers; using isnd.Entities; using Microsoft.AspNetCore.Http; using isnd.Data.Catalog; +using isn.abst; namespace isnd.Controllers { @@ -52,7 +53,7 @@ namespace isnd.Controllers { string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? Environment.GetEnvironmentVariable("TMP") ?? "/tmp", - $"isn-{Guid.NewGuid()}.nupkg"); + $"isn-{Guid.NewGuid()}."+Constants.PaquetFileEstension); using (FileStream fw = new FileStream(initpath, FileMode.Create)) { @@ -63,14 +64,14 @@ namespace isnd.Controllers { var archive = new ZipArchive(fw); - var nuspec = archive.Entries.FirstOrDefault(e => e.FullName.EndsWith(".nuspec")); - if (nuspec == null) return BadRequest(new { error = "no nuspec from archive" }); + var spec = archive.Entries.FirstOrDefault(e => e.FullName.EndsWith("." + Constants.SpecFileEstension)); + if (spec == null) return BadRequest(new { error = "no " + Constants.SpecFileEstension + " from archive" }); string pkgpath; NuGetVersion version; string pkgid; string fullpath; - using (var specstr = nuspec.Open()) + using (var specstr = spec.Open()) { NuspecCoreReader reader = new NuspecCoreReader(specstr); @@ -82,29 +83,29 @@ namespace isnd.Controllers string pkgidpath = Path.Combine(isndSettings.PackagesRootDir, pkgid); pkgpath = Path.Combine(pkgidpath, version.ToFullString()); - string name = $"{pkgid}-{version}.nupkg"; + string name = $"{pkgid}-{version}."+Constants.PaquetFileEstension; fullpath = Path.Combine(pkgpath, name); var destpkgiddir = new DirectoryInfo(pkgidpath); - Package package = dbContext.Packages.SingleOrDefault(p => p.Id == pkgid); - if (package != null) + Package pkg = dbContext.Packages.SingleOrDefault(p => p.Id == pkgid); + if (pkg != null) { - if (package.OwnerId != apikey.UserId) + if (pkg.OwnerId != apikey.UserId) { return new ForbidResult(); } - package.Description = pkgdesc; + pkg.Description = pkgdesc; } else { - package = new Package + pkg = new Package { Id = pkgid, Description = pkgdesc, OwnerId = apikey.UserId, LatestVersion = commit }; - dbContext.Packages.Add(package); + dbContext.Packages.Add(pkg); } if (!destpkgiddir.Exists) destpkgiddir.Create(); @@ -117,7 +118,7 @@ namespace isnd.Controllers // mais si elle ne l'est pas en base de donnéés, // on remplace la version sur disque. var pkgv = dbContext.PackageVersions.Where( - v => v.PackageId == package.Id + v => v.PackageId == pkg.Id ); if (pkgv !=null && pkgv.Count()==0) @@ -136,7 +137,7 @@ namespace isnd.Controllers files.Add(name); string fullstringversion = version.ToFullString(); var pkgvers = dbContext.PackageVersions.Where - (v => v.PackageId == package.Id && v.FullString == fullstringversion); + (v => v.PackageId == pkg.Id && v.FullString == fullstringversion); if (pkgvers.Count() > 0) { foreach (var v in pkgvers.ToArray()) @@ -147,7 +148,7 @@ namespace isnd.Controllers dbContext.PackageVersions.Add (new PackageVersion{ - Package = package, + Package = pkg, Major = version.Major, Minor = version.Minor, Patch = version.Patch, @@ -161,7 +162,7 @@ namespace isnd.Controllers { var pkgver = new PackageVersion { - Package = package, + Package = pkg, Major = version.Major, Minor = version.Minor, Patch = version.Patch, @@ -176,7 +177,7 @@ namespace isnd.Controllers await dbContext.SaveChangesAsync(); packageManager.ÛpdateCatalogFor(commit); - logger.LogInformation($"new package : {nuspec.Name}"); + logger.LogInformation($"new paquet : {spec.Name}"); } } using (var shacrypto = System.Security.Cryptography.SHA512.Create()) @@ -194,12 +195,12 @@ namespace isnd.Controllers } } } - string nuspecfullpath = Path.Combine(pkgpath, pkgid + ".nuspec"); + string nuspecfullpath = Path.Combine(pkgpath, pkgid + "." + Constants.SpecFileEstension); FileInfo nfpi = new FileInfo(nuspecfullpath); if (nfpi.Exists) nfpi.Delete(); - nuspec.ExtractToFile(nuspecfullpath); + spec.ExtractToFile(nuspecfullpath); } } diff --git a/src/isnd/Data/Catalog/CatalogLeaf.cs b/src/isnd/Data/Catalog/CatalogLeaf.cs index 7b312a2..8719ecc 100644 --- a/src/isnd/Data/Catalog/CatalogLeaf.cs +++ b/src/isnd/Data/Catalog/CatalogLeaf.cs @@ -8,7 +8,7 @@ namespace isnd.Data.Catalog { [JsonProperty("@type")] - public List RefType { get; set; } + public string[] RefType { get; set; } [JsonProperty("commitId")] public string CommitId { get; set; } diff --git a/src/isnd/Data/Package.cs b/src/isnd/Data/Package.cs index 370c895..eb792d3 100644 --- a/src/isnd/Data/Package.cs +++ b/src/isnd/Data/Package.cs @@ -11,7 +11,7 @@ namespace isnd.Data { [Key][Required] [StringLength(1024)] - public string Id { get; set; } + public string Id { get; set; } [Required] [ForeignKey("Owner")] @@ -30,8 +30,8 @@ namespace isnd.Data public virtual List Versions { get; set; } /// - /// Latest package version put, or post, - /// or { delete when no more active version }. + /// Latest version at put, posted, + /// or even deletion when no more active version. /// /// [Required][JsonIgnore] diff --git a/src/isnd/Data/PackageVersion.cs b/src/isnd/Data/PackageVersion.cs index 2331ca3..21f8bb1 100644 --- a/src/isnd/Data/PackageVersion.cs +++ b/src/isnd/Data/PackageVersion.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using isn.abst; using isnd.Data.Catalog; using Newtonsoft.Json; @@ -41,8 +42,10 @@ namespace isnd.Data public string CommitId { get => CommitNId.ToString(); } public virtual Commit LatestCommit{ get; set; } - public string NugetLink => $"/package/{PackageId}/{FullString}/{PackageId}-{FullString}.nupkg"; - public string NuspecLink => $"/package/{PackageId}/{FullString}/{PackageId}-{FullString}.nuspec"; + public string NugetLink => $"/{Constants.PaquetFileEstension}/{PackageId}/{FullString}/{PackageId}-{FullString}." + + Constants.PaquetFileEstension; + public string NuspecLink => $"/{Constants.SpecFileEstension}/{PackageId}/{FullString}/{PackageId}-{FullString}." + + Constants.SpecFileEstension; } } \ No newline at end of file diff --git a/src/isnd/Entities/ApiConfig.cs b/src/isnd/Entities/ApiConfig.cs index 8b5f65e..311f3cf 100644 --- a/src/isnd/Entities/ApiConfig.cs +++ b/src/isnd/Entities/ApiConfig.cs @@ -1,21 +1,20 @@ +using isn.abst; + namespace isnd.Entities { public static class ApiConfig { - public const string Publish = "put"; public const string Base = "index.json"; public const string Catalog = "catalog"; public const string CatalogPage = "catalog-page"; - public const string Get = "package"; + public const string GetPackage = Constants.PaquetFileEstension; + public const string GetVersion = "version"; public const string Search = "search"; public const string AutoComplete = "autocomplete"; public const string CatalogLeaf = "catalog-leaf"; - public const string CatalogPackageDetail = "package-detail"; - public const string Delete = "delete"; - public const string Registration = "registration"; - + internal const string GetNuspec = Constants.SpecFileEstension; } } \ No newline at end of file diff --git a/src/isnd/Interfaces/IPackageManager.cs b/src/isnd/Interfaces/IPackageManager.cs index 746027e..453a276 100644 --- a/src/isnd/Interfaces/IPackageManager.cs +++ b/src/isnd/Interfaces/IPackageManager.cs @@ -22,6 +22,7 @@ namespace isnd.Interfaces void ÛpdateCatalogFor(Commit commit); Task DeletePackageAsync(string pkgid, string version, string type); Task GetPackageAsync(string pkgid, string version, string type); + IEnumerable GetCatalogLeaf(string id, string version, string lower); } } \ No newline at end of file diff --git a/src/isnd/Services/PackageManager.cs b/src/isnd/Services/PackageManager.cs index 400b627..d9484e4 100644 --- a/src/isnd/Services/PackageManager.cs +++ b/src/isnd/Services/PackageManager.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using isn.Abstract; +using isnd.Controllers; using isnd.Data; using isnd.Data.Catalog; using isnd.Entities; @@ -51,8 +52,8 @@ namespace isnd.Services res.Add( new Resource { - Id = extUrl + ApiConfig.Base, - Type = "PackageBaseAddress/" + BASE_API_LEVEL, + Id = extUrl + ApiConfig.GetPackage, + Type = "PackageBaseAddress/3.0.0" , Comment = "Package Base Address service" }); if (unleashClient.IsEnabled("pkg-autocomplete", false)) @@ -79,14 +80,14 @@ namespace isnd.Services Type = "Catalog/"+ BASE_API_LEVEL, Comment = "Package Catalog Index" }); - if (unleashClient.IsEnabled("pkg-registration", false)) - res.Add( + + /* FIXME res.Add( new Resource { Id = extUrl + ApiConfig.Registration, - Type = "Catalog/" + BASE_API_LEVEL, + 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." - }); + }); */ return res; } @@ -256,7 +257,7 @@ namespace isnd.Services public async Task DeletePackageAsync(string pkgid, string version, string type) { - // TODO package deletion on disk + // TODO deletion on disk var commit = new Commit{ Action = PackageAction.DeletePackage, TimeStamp = DateTime.Now @@ -285,5 +286,24 @@ namespace isnd.Services v.Type == type ); } + + public async Task 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 GetCatalogLeaf(string id, string version, string lower) + { + return dbContext.PackageVersions + .Include(v=>v.Package) + .Where(v => v.PackageId == id && v.FullString == version + && (lower == null || lower == v.Type)); + } } } \ No newline at end of file diff --git a/test/isn.tests/PushTest.cs b/test/isn.tests/PushTest.cs index cc0d895..6057647 100644 --- a/test/isn.tests/PushTest.cs +++ b/test/isn.tests/PushTest.cs @@ -9,6 +9,7 @@ using Newtonsoft.Json; using isn.Abstract; using System.Linq; using Xunit; +using isn.abst; namespace isn.tests { @@ -60,7 +61,8 @@ dataTable.Rows.Add(dataRow); public void TestPush() { Program.LoadConfig(); - var report = Program.PushPkg(new string[] { "/home/paul/Nupkgs/Yavsc.Abstract.1.0.8.nupkg" }); + var report = Program.PushPkg(new string[] { "/home/paul/Nupkgs/Yavsc.Abstract.1.0.8." + + Constants.PaquetFileEstension }); } [Fact] diff --git a/test/isnd.tests/isnd.tests.csproj b/test/isnd.tests/isnd.tests.csproj index 89722eb..16d560a 100644 --- a/test/isnd.tests/isnd.tests.csproj +++ b/test/isnd.tests/isnd.tests.csproj @@ -10,24 +10,16 @@ 0.1.175 - + - - - - - \ No newline at end of file