Skip to content

Commit

Permalink
#37: add full semver support to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Jun 21, 2024
1 parent d4acb04 commit e9a697f
Show file tree
Hide file tree
Showing 11 changed files with 514 additions and 62 deletions.
21 changes: 6 additions & 15 deletions src/PackageRegistryService/API/Handlers/DownloadsHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using PackageRegistryService.Models;
using static AVPRIndex.Domain;

namespace PackageRegistryService.API.Handlers
{
Expand All @@ -27,24 +28,14 @@ await database.Downloads

public static async Task<Results<BadRequest<string>, NotFound<string>, Ok<PackageDownloads>>> GetDownloadsByNameAndVersion(string name, string version, ValidationPackageDb database)
{
var splt = version.Split('.');
if (splt.Length != 3)
var semVerOpt = SemVer.tryParse(version);
if (semVerOpt is null)
{
return TypedResults.BadRequest("version was not a of valid format MAJOR.MINOR.REVISION");
return TypedResults.BadRequest($"{version} is not a valid semantic version.");
}
var semVer = semVerOpt.Value;

int major; int minor; int revision;

if (
!int.TryParse(splt[0], out major)
|| !int.TryParse(splt[1], out minor)
|| !int.TryParse(splt[2], out revision)
)
{
return TypedResults.BadRequest("version was not a of valid format MAJOR.MINOR.REVISION");
}

var downloads = await database.Downloads.FindAsync(name, major, minor, revision);
var downloads = await database.Downloads.FindAsync(name, semVer.Major, semVer.Minor, semVer.Patch, semVer.PreRelease, semVer.BuildMetadata);

return downloads is null
? TypedResults.NotFound($"No download stats for package '{name}' version '{version}' available.")
Expand Down
26 changes: 9 additions & 17 deletions src/PackageRegistryService/API/Handlers/PackageHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using PackageRegistryService.Models;
using Microsoft.EntityFrameworkCore;
using PackageRegistryService.Pages.Components;
using AVPRIndex;
using static AVPRIndex.Domain;

namespace PackageRegistryService.API.Handlers
{
Expand All @@ -27,7 +29,7 @@ public static async Task<Results<Ok<ValidationPackage[]>, Conflict<string>>> Get
public static async Task<Results<Ok<ValidationPackage>, NotFound<string>, Conflict<string>>> GetLatestPackageByName(string name, ValidationPackageDb database)
{
var package = await database.ValidationPackages
.Where(p => p.Name == name)
.Where(p => p.Name == name && p.BuildMetadataVersionSuffix == "" && p.BuildMetadataVersionSuffix == "") // only serve stable package versions here
.OrderByDescending(p => p.MajorVersion)
.ThenByDescending(p => p.MinorVersion)
.ThenByDescending(p => p.PatchVersion)
Expand All @@ -51,24 +53,14 @@ public static async Task<Results<Ok<ValidationPackage>, NotFound<string>, Confli

public static async Task<Results<BadRequest<string>, NotFound<string>, Conflict<string>, Ok<ValidationPackage>>> GetPackageByNameAndVersion(string name, string version, ValidationPackageDb database)
{
var splt = version.Split('.');
if (splt.Length != 3)
var semVerOpt = SemVer.tryParse(version);
if (semVerOpt is null)
{
return TypedResults.BadRequest("version was not a of valid format MAJOR.MINOR.REVISION");
return TypedResults.BadRequest($"{version} is not a valid semantic version.");
}
var semVer = semVerOpt.Value;

int major; int minor; int revision;

if (
!int.TryParse(splt[0], out major)
|| !int.TryParse(splt[1], out minor)
|| !int.TryParse(splt[2], out revision)
)
{
return TypedResults.BadRequest("version was not a of valid format MAJOR.MINOR.REVISION");
}

var package = await database.ValidationPackages.FindAsync(name, major, minor, revision);
var package = await database.ValidationPackages.FindAsync(name, semVer.Major, semVer.Minor, semVer.Patch, semVer.PreRelease, semVer.BuildMetadata);

if (package is null)
{
Expand All @@ -88,7 +80,7 @@ public static async Task<Results<BadRequest<string>, NotFound<string>, Conflict<

public static async Task<Results<Ok<ValidationPackage>, Conflict, UnauthorizedHttpResult, UnprocessableEntity<string>>> CreatePackage(ValidationPackage package, ValidationPackageDb database)
{
var existing = await database.ValidationPackages.FindAsync(package.Name, package.MajorVersion, package.MinorVersion, package.PatchVersion);
var existing = await database.ValidationPackages.FindAsync(package.Name, package.MajorVersion, package.MinorVersion, package.PatchVersion, package.PreReleaseVersionSuffix, package.BuildMetadataVersionSuffix);
if (existing != null)
{
return TypedResults.Conflict();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ public static async Task<Results<Ok, UnprocessableEntity, NotFound>> Verify(Pack
hashedPackage.PackageName,
hashedPackage.PackageMajorVersion,
hashedPackage.PackageMinorVersion,
hashedPackage.PackagePatchVersion
hashedPackage.PackagePatchVersion,
hashedPackage.PackagePreReleaseVersionSuffix,
hashedPackage.PackageBuildMetadataVersionSuffix
);

var existingPackage = await
database.ValidationPackages.FindAsync(
hashedPackage.PackageName,
hashedPackage.PackageMajorVersion,
hashedPackage.PackageMinorVersion,
hashedPackage.PackagePatchVersion
hashedPackage.PackagePatchVersion,
hashedPackage.PackagePreReleaseVersionSuffix,
hashedPackage.PackageBuildMetadataVersionSuffix
);

if (existingHash is null || existingPackage is null)
Expand Down
6 changes: 6 additions & 0 deletions src/PackageRegistryService/Data/DataInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static void SeedData(ValidationPackageDb context)
MajorVersion = i.Metadata.MajorVersion,
MinorVersion = i.Metadata.MinorVersion,
PatchVersion = i.Metadata.PatchVersion,
PreReleaseVersionSuffix = i.Metadata.PreReleaseVersionSuffix,
BuildMetadataVersionSuffix= i.Metadata.BuildMetadataVersionSuffix,
PackageContent = Encoding.UTF8.GetBytes(content),
ReleaseDate = new(i.LastUpdated.Year, i.LastUpdated.Month, i.LastUpdated.Day),
Tags = i.Metadata.Tags,
Expand Down Expand Up @@ -78,6 +80,8 @@ public static void SeedData(ValidationPackageDb context)
PackageMajorVersion = i.Metadata.MajorVersion,
PackageMinorVersion = i.Metadata.MinorVersion,
PackagePatchVersion = i.Metadata.PatchVersion,
PackagePreReleaseVersionSuffix = i.Metadata.PreReleaseVersionSuffix,
PackageBuildMetadataVersionSuffix = i.Metadata.BuildMetadataVersionSuffix,
Hash = hash,
};
});
Expand All @@ -94,6 +98,8 @@ public static void SeedData(ValidationPackageDb context)
PackageMajorVersion = i.Metadata.MajorVersion,
PackageMinorVersion = i.Metadata.MinorVersion,
PackagePatchVersion = i.Metadata.PatchVersion,
PackagePreReleaseVersionSuffix = i.Metadata.PreReleaseVersionSuffix,
PackageBuildMetadataVersionSuffix = i.Metadata.BuildMetadataVersionSuffix,
Downloads = 0
};
});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e9a697f

Please sign in to comment.