99using System . Web . Mvc ;
1010using NuGet . Services . Entities ;
1111using NuGet . Versioning ;
12- using NuGetGallery . Auditing ;
1312using NuGetGallery . Filters ;
13+ using NuGetGallery . RequestModels ;
1414
1515namespace 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 {
0 commit comments