Skip to content

Commit d8dc32d

Browse files
author
Scott Bommarito
authored
Fix deprecation custom message issues (#7284)
1 parent 03e16e4 commit d8dc32d

7 files changed

Lines changed: 142 additions & 115 deletions

File tree

src/NuGetGallery/Controllers/ManageDeprecationJsonApiController.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
using System.Web.Mvc;
1010
using NuGet.Services.Entities;
1111
using NuGet.Versioning;
12-
using NuGetGallery.Auditing;
1312
using NuGetGallery.Filters;
13+
using NuGetGallery.RequestModels;
1414

1515
namespace NuGetGallery
1616
{
1717
public partial class ManageDeprecationJsonApiController
1818
: AppController
1919
{
20+
private const int MaxCustomMessageLength = 1000;
21+
2022
private readonly IPackageService _packageService;
2123
private readonly IPackageDeprecationService _deprecationService;
2224
private readonly IFeatureFlagService _featureFlagService;
@@ -50,28 +52,22 @@ public virtual JsonResult GetAlternatePackageVersions(string id)
5052
[RequiresAccountConfirmation("deprecate a package")]
5153
[ValidateAntiForgeryToken]
5254
public virtual async Task<JsonResult> Deprecate(
53-
string id,
54-
IEnumerable<string> versions,
55-
bool isLegacy,
56-
bool hasCriticalBugs,
57-
bool isOther,
58-
string alternatePackageId,
59-
string alternatePackageVersion,
60-
string customMessage)
55+
DeprecatePackageRequest request)
6156
{
6257
var status = PackageDeprecationStatus.NotDeprecated;
6358

64-
if (isLegacy)
59+
if (request.IsLegacy)
6560
{
6661
status |= PackageDeprecationStatus.Legacy;
6762
}
6863

69-
if (hasCriticalBugs)
64+
if (request.HasCriticalBugs)
7065
{
7166
status |= PackageDeprecationStatus.CriticalBugs;
7267
}
7368

74-
if (isOther)
69+
var customMessage = request.CustomMessage;
70+
if (request.IsOther)
7571
{
7672
if (string.IsNullOrWhiteSpace(customMessage))
7773
{
@@ -81,19 +77,29 @@ public virtual async Task<JsonResult> Deprecate(
8177
status |= PackageDeprecationStatus.Other;
8278
}
8379

84-
if (versions == null || !versions.Any())
80+
if (customMessage != null)
81+
{
82+
if (customMessage.Length > MaxCustomMessageLength)
83+
{
84+
return DeprecateErrorResponse(
85+
HttpStatusCode.BadRequest,
86+
string.Format(Strings.DeprecatePackage_CustomMessageTooLong, MaxCustomMessageLength));
87+
}
88+
}
89+
90+
if (request.Versions == null || !request.Versions.Any())
8591
{
8692
return DeprecateErrorResponse(HttpStatusCode.BadRequest, Strings.DeprecatePackage_NoVersions);
8793
}
8894

89-
var packages = _packageService.FindPackagesById(id, PackageDeprecationFieldsToInclude.DeprecationAndRelationships);
95+
var packages = _packageService.FindPackagesById(request.Id, PackageDeprecationFieldsToInclude.DeprecationAndRelationships);
9096
var registration = packages.FirstOrDefault()?.PackageRegistration;
9197
if (registration == null)
9298
{
9399
// This should only happen if someone hacks the form or if the package is deleted while the user is filling out the form.
94100
return DeprecateErrorResponse(
95101
HttpStatusCode.NotFound,
96-
string.Format(Strings.DeprecatePackage_MissingRegistration, id));
102+
string.Format(Strings.DeprecatePackage_MissingRegistration, request.Id));
97103
}
98104

99105
var currentUser = GetCurrentUser();
@@ -111,37 +117,37 @@ public virtual async Task<JsonResult> Deprecate(
111117
{
112118
return DeprecateErrorResponse(
113119
HttpStatusCode.Forbidden,
114-
string.Format(Strings.DeprecatePackage_Locked, id));
120+
string.Format(Strings.DeprecatePackage_Locked, request.Id));
115121
}
116122

117123
PackageRegistration alternatePackageRegistration = null;
118124
Package alternatePackage = null;
119-
if (!string.IsNullOrWhiteSpace(alternatePackageId))
125+
if (!string.IsNullOrWhiteSpace(request.AlternatePackageId))
120126
{
121-
if (!string.IsNullOrWhiteSpace(alternatePackageVersion))
127+
if (!string.IsNullOrWhiteSpace(request.AlternatePackageVersion))
122128
{
123-
alternatePackage = _packageService.FindPackageByIdAndVersionStrict(alternatePackageId, alternatePackageVersion);
129+
alternatePackage = _packageService.FindPackageByIdAndVersionStrict(request.AlternatePackageId, request.AlternatePackageVersion);
124130
if (alternatePackage == null)
125131
{
126132
return DeprecateErrorResponse(
127133
HttpStatusCode.NotFound,
128-
string.Format(Strings.DeprecatePackage_NoAlternatePackage, alternatePackageId, alternatePackageVersion));
134+
string.Format(Strings.DeprecatePackage_NoAlternatePackage, request.AlternatePackageId, request.AlternatePackageVersion));
129135
}
130136
}
131137
else
132138
{
133-
alternatePackageRegistration = _packageService.FindPackageRegistrationById(alternatePackageId);
139+
alternatePackageRegistration = _packageService.FindPackageRegistrationById(request.AlternatePackageId);
134140
if (alternatePackageRegistration == null)
135141
{
136142
return DeprecateErrorResponse(
137143
HttpStatusCode.NotFound,
138-
string.Format(Strings.DeprecatePackage_NoAlternatePackageRegistration, alternatePackageId));
144+
string.Format(Strings.DeprecatePackage_NoAlternatePackageRegistration, request.AlternatePackageId));
139145
}
140146
}
141147
}
142148

143149
var packagesToUpdate = new List<Package>();
144-
foreach (var version in versions)
150+
foreach (var version in request.Versions)
145151
{
146152
var normalizedVersion = NuGetVersionFormatter.Normalize(version);
147153
var package = packages.SingleOrDefault(v => v.NormalizedVersion == normalizedVersion);
@@ -150,7 +156,7 @@ public virtual async Task<JsonResult> Deprecate(
150156
// This should only happen if someone hacks the form or if a version of the package is deleted while the user is filling out the form.
151157
return DeprecateErrorResponse(
152158
HttpStatusCode.NotFound,
153-
string.Format(Strings.DeprecatePackage_MissingVersion, id));
159+
string.Format(Strings.DeprecatePackage_MissingVersion, request.Id));
154160
}
155161
else
156162
{

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
<Compile Include="Migrations\201905071526573_DevelopmentDependencyMetadata.Designer.cs">
253253
<DependentUpon>201905071526573_DevelopmentDependencyMetadata.cs</DependentUpon>
254254
</Compile>
255+
<Compile Include="RequestModels\DeprecatePackageRequest.cs" />
255256
<Compile Include="Services\IPackageDeprecationService.cs" />
256257
<Compile Include="Queries\AutocompleteDatabasePackageIdsQuery.cs" />
257258
<Compile Include="Queries\AutocompleteDatabasePackageVersionsQuery.cs" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Web.Mvc;
6+
7+
namespace NuGetGallery.RequestModels
8+
{
9+
public class DeprecatePackageRequest
10+
{
11+
public string Id { get; set; }
12+
public IEnumerable<string> Versions { get; set; }
13+
public bool IsLegacy { get; set; }
14+
public bool HasCriticalBugs { get; set; }
15+
public bool IsOther { get; set; }
16+
public string AlternatePackageId { get; set; }
17+
public string AlternatePackageVersion { get; set; }
18+
19+
[AllowHtml]
20+
public string CustomMessage { get; set; }
21+
}
22+
}

src/NuGetGallery/Strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NuGetGallery/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,4 +1121,7 @@ The {1} Team</value>
11211121
<data name="UploadPackage_EmbeddedIconNotAccepted" xml:space="preserve">
11221122
<value>The &lt;icon&gt; element is not currently supported.</value>
11231123
</data>
1124+
<data name="DeprecatePackage_CustomMessageTooLong" xml:space="preserve">
1125+
<value>Your custom message is too long. It must be under {0} characters.</value>
1126+
</data>
11241127
</root>

src/NuGetGallery/Views/Packages/_DisplayPackageDeprecation.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
@if (!string.IsNullOrEmpty(Model.CustomMessage))
6464
{
6565
<b>Additional Details</b>
66-
<p>@Model.CustomMessage</p>
66+
<p>@Html.PreFormattedText(Model.CustomMessage)</p>
6767
}
6868
</div>
6969
</div>

0 commit comments

Comments
 (0)