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

Add X-NuGet-Warning header to response when pushing SemVer2 package #4292

Merged
merged 2 commits into from
Jun 28, 2017
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
5 changes: 5 additions & 0 deletions src/NuGetGallery/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ await AuditingService.SaveAuditRecordAsync(

TelemetryService.TrackPackagePushEvent(package, user, User.Identity);

if (package.SemVerLevelKey == SemVerLevelKey.SemVer2)
{
return new HttpStatusCodeWithServerWarningResult(HttpStatusCode.Created, Strings.WarningSemVer2PackagePushed);
}

return new HttpStatusCodeResult(HttpStatusCode.Created);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Net;
using System.Web.Mvc;
using NuGet.Protocol;

namespace NuGetGallery
{
public class HttpStatusCodeWithServerWarningResult : HttpStatusCodeResult
{
private readonly string _warningMessage;

public HttpStatusCodeWithServerWarningResult(HttpStatusCode statusCode, string warningMessage)
: base((int)statusCode)
{
_warningMessage = warningMessage;
}

public override void ExecuteResult(ControllerContext context)
{
var response = context.RequestContext.HttpContext.Response;

if (!string.IsNullOrEmpty(_warningMessage) && !response.HeadersWritten)
{
response.AppendHeader(ProtocolConstants.ServerWarningHeader, _warningMessage);
}

base.ExecuteResult(context);
}
}
}
1 change: 1 addition & 0 deletions src/NuGetGallery/NuGetGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@
<Compile Include="Controllers\PagesController.cs" />
<Compile Include="Filters\ApiScopeRequiredAttribute.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Infrastructure\HttpStatusCodeWithServerWarningResult.cs" />
<Compile Include="Migrations\201704191802404_AddIndexPackageRegistrationKeySemVer.cs" />
<Compile Include="Migrations\201704191802404_AddIndexPackageRegistrationKeySemVer.Designer.cs">
<DependentUpon>201704191802404_AddIndexPackageRegistrationKeySemVer.cs</DependentUpon>
Expand Down
9 changes: 9 additions & 0 deletions src/NuGetGallery/Strings.Designer.cs

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

3 changes: 3 additions & 0 deletions src/NuGetGallery/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,7 @@ For more information, please contact '{2}'.</value>
&lt;li&gt;All existing push API keys for co-owners new to this policy will expire in {1} days.&lt;/li&gt;
&lt;/ol&gt;</value>
</data>
<data name="WarningSemVer2PackagePushed" xml:space="preserve">
<value>This package will only be available to download with SemVer 2.0.0 compatible NuGet clients, such as Visual Studio 2017 (version 15.3) and above or NuGet client 4.3 and above. For more information, see https://go.microsoft.com/fwlink/?linkid=852248.</value>
</data>
</root>
4 changes: 4 additions & 0 deletions tests/NuGetGallery.Facts/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@
<assemblyIdentity name="NuGet.Packaging.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.ApplicationInsights" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup>
Expand Down
17 changes: 17 additions & 0 deletions tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Moq;
using Newtonsoft.Json.Linq;
using NuGet.Packaging;
using NuGet.Protocol;
using NuGetGallery.Auditing;
using NuGetGallery.Authentication;
using NuGetGallery.Configuration;
Expand Down Expand Up @@ -86,6 +87,7 @@ public TestableApiController(MockBehavior behavior = MockBehavior.Default)
var package = new Package();
package.PackageRegistration = new PackageRegistration { Id = packageMetadata.Id };
package.Version = packageMetadata.Version.ToString();
package.SemVerLevelKey = SemVerLevelKey.ForPackage(packageMetadata.Version, packageMetadata.GetDependencyGroups().AsPackageDependencyEnumerable());

return Task.FromResult(package);
});
Expand Down Expand Up @@ -481,6 +483,21 @@ public async Task WillCreateAPackageFromTheNuGetPackage()
controller.MockEntitiesContext.VerifyCommitted();
}

[Fact]
public async Task WillReturnServerWarningWhenCreatingSemVer2Package()
{
var nuGetPackage = TestPackage.CreateTestPackageStream("theId", "1.0.42+metadata");

var user = new User() { EmailAddress = "confirmed@email.com" };
var controller = new TestableApiController();
controller.SetCurrentUser(user);
controller.SetupPackageFromInputStream(nuGetPackage);

var actionResult = await controller.CreatePackagePut();

Assert.IsType<HttpStatusCodeWithServerWarningResult>(actionResult);
}

[Fact]
public async Task WillCurateThePackage()
{
Expand Down
6 changes: 6 additions & 0 deletions tests/NuGetGallery.Facts/NuGetGallery.Facts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@
<Reference Include="NuGet.Common, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\NuGet.Common.4.3.0-preview1-2524\lib\net45\NuGet.Common.dll</HintPath>
</Reference>
<Reference Include="NuGet.Configuration, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\NuGet.Configuration.4.3.0-preview1-2524\lib\net45\NuGet.Configuration.dll</HintPath>
</Reference>
<Reference Include="NuGet.Frameworks, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\NuGet.Frameworks.4.3.0-preview1-2524\lib\net45\NuGet.Frameworks.dll</HintPath>
</Reference>
Expand All @@ -213,6 +216,9 @@
<Reference Include="NuGet.Packaging.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\NuGet.Packaging.Core.4.3.0-preview1-2524\lib\net45\NuGet.Packaging.Core.dll</HintPath>
</Reference>
<Reference Include="NuGet.Protocol, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\NuGet.Protocol.4.3.0-preview1-2524\lib\net45\NuGet.Protocol.dll</HintPath>
</Reference>
<Reference Include="NuGet.Services.KeyVault, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\NuGet.Services.KeyVault.1.0.0.0\lib\net45\NuGet.Services.KeyVault.dll</HintPath>
<Private>True</Private>
Expand Down
2 changes: 2 additions & 0 deletions tests/NuGetGallery.Facts/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
<package id="MvcHaack.Ajax.MVC4" version="2.0.0.0" targetFramework="net46" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net46" />
<package id="NuGet.Common" version="4.3.0-preview1-2524" targetFramework="net46" />
<package id="NuGet.Configuration" version="4.3.0-preview1-2524" targetFramework="net46" />
<package id="NuGet.Frameworks" version="4.3.0-preview1-2524" targetFramework="net46" />
<package id="NuGet.Logging" version="3.5.0-beta-1160" targetFramework="net46" />
<package id="NuGet.Packaging" version="4.3.0-preview1-2524" targetFramework="net46" />
<package id="NuGet.Packaging.Core" version="4.3.0-preview1-2524" targetFramework="net46" />
<package id="NuGet.Protocol" version="4.3.0-preview1-2524" targetFramework="net46" />
<package id="NuGet.Services.KeyVault" version="1.0.0.0" targetFramework="net46" />
<package id="NuGet.Versioning" version="4.3.0-preview1-2524" targetFramework="net46" />
<package id="Owin" version="1.0" targetFramework="net46" />
Expand Down