Skip to content

Commit 85eeadd

Browse files
authored
Added alternative license expression URLs to be allowed in without warning into Gallery. (#6649)
1 parent 9855ce7 commit 85eeadd

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/NuGetGallery/Helpers/LicenseExpressionRedirectUrlHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NuGetGallery.Helpers
77
{
88
public static class LicenseExpressionRedirectUrlHelper
99
{
10-
private const string LicenseExpressionDeprecationUrlFormat = "https://licenses.nuget.org/{0}";
10+
public const string LicenseExpressionDeprecationUrlFormat = "https://licenses.nuget.org/{0}";
1111

1212
public static string GetLicenseExpressionRedirectUrl(string licenseExpression)
1313
{

src/NuGetGallery/Services/PackageUploadService.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Data.SqlClient;
88
using System.IO;
99
using System.Linq;
10+
using System.Net;
1011
using System.Threading;
1112
using System.Threading.Tasks;
1213
using System.Xml.Linq;
@@ -170,6 +171,7 @@ private async Task<PackageValidationResult> CheckLicenseMetadataAsync(PackageArc
170171
var licenseUrl = nuspecReader.GetLicenseUrl();
171172
var licenseMetadata = nuspecReader.GetLicenseMetadata();
172173
var licenseDeprecationUrl = GetExpectedLicenseUrl(licenseMetadata);
174+
var alternativeDeprecationUrl = GetExpectedAlternativeUrl(licenseMetadata);
173175

174176
if (licenseMetadata == null)
175177
{
@@ -217,7 +219,7 @@ private async Task<PackageValidationResult> CheckLicenseMetadataAsync(PackageArc
217219
string.Join(" ", licenseMetadata.WarningsAndErrors)));
218220
}
219221

220-
if (licenseDeprecationUrl != licenseUrl)
222+
if (licenseDeprecationUrl != licenseUrl && (alternativeDeprecationUrl == null || alternativeDeprecationUrl != licenseUrl))
221223
{
222224
if (licenseMetadata.Type == LicenseType.File)
223225
{
@@ -378,6 +380,25 @@ private static string GetExpectedLicenseUrl(LicenseMetadata licenseMetadata)
378380
throw new InvalidOperationException($"Unsupported license metadata type: {licenseMetadata.Type}");
379381
}
380382

383+
/// <summary>
384+
/// 15.9 client does url encoding with <see cref="WebUtility.UrlEncode(string)"/> which
385+
/// replaces spaces with "+". We shouldn't work about it, but we should fix the client,
386+
/// too.
387+
/// </summary>
388+
private static string GetExpectedAlternativeUrl(LicenseMetadata licenseMetadata)
389+
{
390+
if (licenseMetadata != null && licenseMetadata.Type == LicenseType.Expression)
391+
{
392+
return new Uri(
393+
string.Format(
394+
LicenseExpressionRedirectUrlHelper.LicenseExpressionDeprecationUrlFormat,
395+
WebUtility.UrlEncode(licenseMetadata.License)))
396+
.AbsoluteUri;
397+
}
398+
399+
return null;
400+
}
401+
381402
private static bool HasChildElements(XElement xElement)
382403
=> xElement.Elements().Any();
383404

tests/NuGetGallery.Facts/Services/PackageUploadServiceFacts.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,24 @@ public async Task WarnsWhenInvalidLicenseUrlSpecifiedWithLicenseFile(string lice
567567
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);
568568
}
569569

570+
[Fact]
571+
public async Task AcceptsAlternativeLicenseUrl()
572+
{
573+
_nuGetPackage = GeneratePackageWithLicense(
574+
licenseUrl: new Uri("https://licenses.nuget.org/Apache-1.0%2B+OR+MIT"),
575+
licenseExpression: "Apache-1.0+ OR MIT",
576+
licenseFilename: null,
577+
licenseFileContents: null);
578+
579+
var result = await _target.ValidateBeforeGeneratePackageAsync(
580+
_nuGetPackage.Object,
581+
GetPackageMetadata(_nuGetPackage));
582+
583+
Assert.Equal(PackageValidationResultType.Accepted, result.Type);
584+
Assert.Null(result.Message);
585+
Assert.Empty(result.Warnings);
586+
}
587+
570588
[Theory]
571589
[InlineData(null)]
572590
[InlineData(RegularLicenseUrl)]

0 commit comments

Comments
 (0)