REORG+histo

This commit is contained in:
Paul Schneider 2021-09-05 15:44:47 +01:00
commit 6dd76ac1a5
28 changed files with 948 additions and 159 deletions

View file

@ -6,11 +6,13 @@ using isnd.Data;
namespace isnd.Controllers
{
// TODO Web hook CI
public class NewUpdateController : Controller
{
[Authorize(Policy = Constants.RequireAdminPolicyName)]
public IActionResult NewRelease(NewReleaseInfo version)
{
throw new NotImplementedException("web hook");
return View(version);
}
}

View file

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using isnd.Data;
using isnd.ViewModels;
using isnd.Helpers;
namespace isnd
{
[AllowAnonymous]
@ -78,16 +78,11 @@ namespace isnd
return NotFound();
}
if (!IsOwner(packageVersion)) return Unauthorized();
if (!User.IsOwner(packageVersion)) return Unauthorized();
return View(packageVersion);
}
bool IsOwner(PackageVersion v)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
return v.Package.OwnerId == userId;
}
// POST: PackageVersion/Delete/5
[HttpPost, ActionName("Delete")]
@ -97,7 +92,7 @@ namespace isnd
var packageVersion = await _context.PackageVersions.Include(p => p.Package)
.FirstOrDefaultAsync(m => m.PackageId == PackageId && m.FullString == FullString);
if (packageVersion == null) return NotFound();
if (!IsOwner(packageVersion)) return Unauthorized();
if (!User.IsOwner(packageVersion)) return Unauthorized();
_context.PackageVersions.Remove(packageVersion);
await _context.SaveChangesAsync();

View file

@ -0,0 +1,62 @@
using System.Linq;
using System.Threading.Tasks;
using isnd.Data.Catalog;
using isnd.Helpers;
using isnd.Services;
using isnd.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace isnd.Controllers
{
public partial class PackagesController
{
// https://docs.microsoft.com/en-us/nuget/api/catalog-resource#versioning
[HttpGet(_pkgRootPrefix + ApiConfig.Catalog)]
public IActionResult CatalogIndex()
{
return Ok(PackageManager.CurrentCatalogIndex);
}
[HttpGet(_pkgRootPrefix + ApiConfig.CatalogPage + "-{id}")]
public IActionResult Index(string id)
{
// https://docs.microsoft.com/en-us/nuget/api/catalog-resource#versioning
return Ok(PackageManager.CurrentCatalogPages[int.Parse(id)]);
}
[HttpGet(_pkgRootPrefix + ApiConfig.CatalogLeaf + "/{id}/{*lower}")]
public async Task<IActionResult> CatalogLeafAsync(string id, string lower)
{
string pkgType = ParamHelpers.Optional(ref lower);
var pkgVersion = await _dbContext.PackageVersions
.Include(v => v.LatestCommit)
.Include(v => v.Package)
.SingleOrDefaultAsync(
v => v.PackageId == id &&
v.FullString == lower &&
v.Type == pkgType
);
if (pkgVersion == null) return NotFound();
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
});
}
}
}

View file

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NuGet.Packaging.Core;
using NuGet.Versioning;
using isnd.Data;
using isnd.Helpers;
using Microsoft.AspNetCore.Http;
using isnd.Data.Catalog;
using System.ComponentModel.DataAnnotations;
namespace isnd.Controllers
{
public partial class PackagesController
{
[HttpDelete(_pkgRootPrefix + ApiConfig.Delete + "/{id}/{*lower}")]
public async Task<IActionResult> Delete(
[FromRoute][SafeName][Required] string id,
[FromRoute][SafeName][Required] string lower)
{
string pkgtype = ParamHelpers.Optional(ref lower);
var report = await _packageManager.DeletePackageAsync(id, lower, pkgtype);
return Ok(report);
}
}
}

View file

@ -0,0 +1,58 @@
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using isnd.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace isnd.Controllers
{
public partial class PackagesController
{
// TODO GET GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.{LOWER_VERSION}.nupkg
// LOWER_ID URL string yes The package ID, lowercase
// LOWER_VERSION URL string yes The package version, normalized and lowercased
// response 200 : the package
[HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/{idf}-{lowerf}.nupkg")]
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"
);
FileInfo pkgfi = new FileInfo(pkgpath);
if (!pkgfi.Exists)
{
return BadRequest("!pkgfi.Exists");
}
return File(pkgfi.OpenRead(), "application/zip; charset=binary");
}
// TODO GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.nuspec
// response 200 : the nuspec
[HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/{idf}-{lowerf}.nuspec")]
public IActionResult GetNuspec(
[FromRoute][SafeName][Required] string id,
[FromRoute][SafeName][Required] string lower,
[FromRoute][SafeName][Required] string idf,
[FromRoute][SafeName][Required] string lowerf)
{
var pkgpath = Path.Combine(_isndSettings.PackagesRootDir,
id, lower, $"{id}.nuspec");
FileInfo pkgfi = new FileInfo(pkgpath);
if (!pkgfi.Exists)
{
return BadRequest("!pkgfi.Exists");
}
return File(pkgfi.OpenRead(), "text/xml; charset=utf-8");
}
}
}

View file

@ -142,6 +142,7 @@ namespace isnd.Controllers
foreach (var v in pkgvers.ToArray())
_dbContext.PackageVersions.Remove(v);
}
// FIXME default type or null
if (types==null || types.Count==0)
_dbContext.PackageVersions.Add
(new PackageVersion{
@ -152,7 +153,7 @@ namespace isnd.Controllers
Patch = version.Patch,
IsPrerelease = version.IsPrerelease,
FullString = version.ToFullString(),
Type = "<null-type>",
Type = null,
LatestCommit = commit
});
else

View file

@ -0,0 +1,54 @@
using System.Linq;
using System.Threading.Tasks;
using isnd.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace isnd.Controllers
{
public partial class PackagesController
{
// GET: PackageVersion
public async Task<IActionResult> Index(PackageIndexViewModel model)
{
var applicationDbContext = _dbContext.Packages.Include(p => p.Versions).Where(
p => ( model.Prerelease || p.Versions.Any(v => !v.IsPrerelease))
&& ((model.query == null) || p.Id.StartsWith(model.query)));
model.data = await applicationDbContext.ToArrayAsync();
return View(model);
}
public async Task<IActionResult> Details(string pkgid)
{
if (pkgid == null)
{
return NotFound();
}
var packageVersion = _dbContext.PackageVersions
.Include(p => p.Package)
.Where(m => m.PackageId == pkgid)
.OrderByDescending(p => p)
;
if (packageVersion == null)
{
return NotFound();
}
bool results = await packageVersion.AnyAsync();
var latest = await packageVersion.FirstAsync();
return View("Details", new PackageDetailViewModel
{
latest = latest,
pkgid = pkgid,
totalHits = packageVersion.Count(),
data = packageVersion.Take(10).ToArray()
});
}
}
}

View file

@ -18,7 +18,6 @@ using isnd.Interfaces;
namespace isnd.Controllers
{
[AllowAnonymous]
public partial class PackagesController : Controller
{
@ -33,6 +32,7 @@ namespace isnd.Controllers
readonly ApplicationDbContext _dbContext;
private readonly IPackageManager _packageManager;
private readonly IUnleash _unleashĈlient;
const string _pkgRootPrefix = "~/";
public PackagesController(
ILoggerFactory loggerFactory,
@ -51,72 +51,12 @@ namespace isnd.Controllers
_ressources = _packageManager.GetResources(_unleashĈlient).ToArray();
}
// dotnet add . package -s http://localhost:5000/packages isn
// packages/FindPackagesById()?id='isn'&semVerLevel=2.0.0
// Search
// GET {@id}?q={QUERY}&skip={SKIP}&take={TAKE}&prerelease={PRERELEASE}&semVerLevel={SEMVERLEVEL}&packageType={PACKAGETYPE}
// GET: PackageVersion
public async Task<IActionResult> Index(PackageIndexViewModel model)
{
var applicationDbContext = _dbContext.Packages.Include(p => p.Versions).Where(
p => ( model.Prerelease || p.Versions.Any(v => !v.IsPrerelease))
&& ((model.query == null) || p.Id.StartsWith(model.query)));
model.data = await applicationDbContext.ToArrayAsync();
return View(model);
}
// GET: PackageVersion/Details/5
public async Task<IActionResult> Details(string pkgid)
{
if (pkgid == null)
{
return NotFound();
}
var packageVersion = _dbContext.PackageVersions
.Include(p => p.Package)
.Where(m => m.PackageId == pkgid)
.OrderByDescending(p => p)
;
if (packageVersion == null)
{
return NotFound();
}
bool results = await packageVersion.AnyAsync();
var latest = await packageVersion.FirstAsync();
return View("Details", new PackageDetailViewModel { latest = latest, pkgid = pkgid, totalHits = packageVersion.Count(), data = packageVersion.Take(10).ToArray() } );
}
const string _pkgRootPrefix = "~/" + ApiConfig.BaseApiPath ;
[HttpGet(_pkgRootPrefix + ApiConfig.Base)]
public IActionResult ApiIndex()
{
return Ok(_ressources);
}
//
[HttpGet(_pkgRootPrefix + ApiConfig.Catalog)]
public IActionResult CatalogIndex()
{
// https://docs.microsoft.com/en-us/nuget/api/catalog-resource#versioning
return Ok(PackageManager.CurrentCatalogIndex);
}
[HttpGet(_pkgRootPrefix + ApiConfig.Catalog + "-{id}")]
public IActionResult Index(string id)
{
// https://docs.microsoft.com/en-us/nuget/api/catalog-resource#versioning
return Ok(PackageManager.CurrentCatalogPages[int.Parse(id)]);
}
// GET /autocomplete?id=isn.protocol&prerelease=true
[HttpGet(_pkgRootPrefix + ApiConfig.AutoComplete)]
public IActionResult AutoComplete(
@ -170,48 +110,5 @@ namespace isnd.Controllers
id, parsedVersion, prerelease, packageType, skip, take)
});
}
// TODO GET GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.{LOWER_VERSION}.nupkg
// LOWER_ID URL string yes The package ID, lowercase
// LOWER_VERSION URL string yes The package version, normalized and lowercased
// response 200 : the package
[HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/{idf}-{lowerf}.nupkg")]
public IActionResult GetPackage(
[FromRoute] string id, [FromRoute] string lower,
[FromRoute] string idf, [FromRoute] string lowerf)
{
var pkgpath = Path.Combine(_isndSettings.PackagesRootDir,
id, lower, $"{id}-{lower}.nupkg"
);
FileInfo pkgfi = new FileInfo(pkgpath);
if (!pkgfi.Exists)
{
return BadRequest("!pkgfi.Exists");
}
return File(pkgfi.OpenRead(), "application/zip; charset=binary");
}
// TODO GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.nuspec
// response 200 : the nuspec
[HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/{idf}-{lowerf}.nuspec")]
public IActionResult GetNuspec(
[FromRoute][SafeName][Required] string id,
[FromRoute][SafeName][Required] string lower,
[FromRoute][SafeName][Required] string idf,
[FromRoute][SafeName][Required] string lowerf)
{
var pkgpath = Path.Combine(_isndSettings.PackagesRootDir,
id, lower, $"{id}.nuspec");
FileInfo pkgfi = new FileInfo(pkgpath);
if (!pkgfi.Exists)
{
return BadRequest("!pkgfi.Exists");
}
return File(pkgfi.OpenRead(), "text/xml; charset=utf-8");
}
}
}