Skip to content

Commit

Permalink
Added alternative license expression URLs to be allowed in without wa…
Browse files Browse the repository at this point in the history
…rning into Gallery. (#6649)
  • Loading branch information
agr authored Nov 10, 2018
1 parent 9855ce7 commit 85eeadd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
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

0 comments on commit 85eeadd

Please sign in to comment.