Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added alternative license expression URLs to be allowed in without warning into Gallery #6649

Merged
merged 1 commit into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NuGetGallery.Helpers
{
public static class LicenseExpressionRedirectUrlHelper
{
private const string LicenseExpressionDeprecationUrlFormat = "https://licenses.nuget.org/{0}";
public const string LicenseExpressionDeprecationUrlFormat = "https://licenses.nuget.org/{0}";

public static string GetLicenseExpressionRedirectUrl(string licenseExpression)
{
Expand Down
23 changes: 22 additions & 1 deletion src/NuGetGallery/Services/PackageUploadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand Down Expand Up @@ -170,6 +171,7 @@ private async Task<PackageValidationResult> CheckLicenseMetadataAsync(PackageArc
var licenseUrl = nuspecReader.GetLicenseUrl();
var licenseMetadata = nuspecReader.GetLicenseMetadata();
var licenseDeprecationUrl = GetExpectedLicenseUrl(licenseMetadata);
var alternativeDeprecationUrl = GetExpectedAlternativeUrl(licenseMetadata);

if (licenseMetadata == null)
{
Expand Down Expand Up @@ -217,7 +219,7 @@ private async Task<PackageValidationResult> CheckLicenseMetadataAsync(PackageArc
string.Join(" ", licenseMetadata.WarningsAndErrors)));
}

if (licenseDeprecationUrl != licenseUrl)
if (licenseDeprecationUrl != licenseUrl && (alternativeDeprecationUrl == null || alternativeDeprecationUrl != licenseUrl))
{
if (licenseMetadata.Type == LicenseType.File)
{
Expand Down Expand Up @@ -378,6 +380,25 @@ private static string GetExpectedLicenseUrl(LicenseMetadata licenseMetadata)
throw new InvalidOperationException($"Unsupported license metadata type: {licenseMetadata.Type}");
}

/// <summary>
/// 15.9 client does url encoding with <see cref="WebUtility.UrlEncode(string)"/> which
/// replaces spaces with "+". We shouldn't work about it, but we should fix the client,
/// too.
/// </summary>
private static string GetExpectedAlternativeUrl(LicenseMetadata licenseMetadata)
{
if (licenseMetadata != null && licenseMetadata.Type == LicenseType.Expression)
{
return new Uri(
string.Format(
LicenseExpressionRedirectUrlHelper.LicenseExpressionDeprecationUrlFormat,
WebUtility.UrlEncode(licenseMetadata.License)))
.AbsoluteUri;
}

return null;
}

private static bool HasChildElements(XElement xElement)
=> xElement.Elements().Any();

Expand Down
18 changes: 18 additions & 0 deletions tests/NuGetGallery.Facts/Services/PackageUploadServiceFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,24 @@ public async Task WarnsWhenInvalidLicenseUrlSpecifiedWithLicenseFile(string lice
Assert.Equal("To provide better experience for older clients when a license file is packaged, <licenseUrl> should be set to 'https://aka.ms/deprecateLicenseUrl'.", result.Warnings[0].PlainTextMessage);
}

[Fact]
public async Task AcceptsAlternativeLicenseUrl()
{
_nuGetPackage = GeneratePackageWithLicense(
licenseUrl: new Uri("https://licenses.nuget.org/Apache-1.0%2B+OR+MIT"),
licenseExpression: "Apache-1.0+ OR MIT",
licenseFilename: null,
licenseFileContents: null);

var result = await _target.ValidateBeforeGeneratePackageAsync(
_nuGetPackage.Object,
GetPackageMetadata(_nuGetPackage));

Assert.Equal(PackageValidationResultType.Accepted, result.Type);
Assert.Null(result.Message);
Assert.Empty(result.Warnings);
}

[Theory]
[InlineData(null)]
[InlineData(RegularLicenseUrl)]
Expand Down