-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathPackageHelper.cs
More file actions
231 lines (203 loc) · 9.66 KB
/
PackageHelper.cs
File metadata and controls
231 lines (203 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// 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;
using System.Collections.Generic;
using System.Linq;
using NuGet.Services.Entities;
using NuGetGallery.Packaging;
namespace NuGetGallery
{
public static class PackageHelper
{
public static string ParseTags(string tags)
{
if (tags == null)
{
return null;
}
return tags.Replace(',', ' ').Replace(';', ' ').Replace('\t', ' ').Replace(" ", " ");
}
public static bool ShouldRenderUrl(string url, bool secureOnly = false)
{
if (!string.IsNullOrEmpty(url) && Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
{
if (secureOnly)
{
return uri.IsHttpsProtocol();
}
return uri.Scheme == Uri.UriSchemeHttps
|| uri.Scheme == Uri.UriSchemeHttp;
}
return false;
}
/// <summary>
/// If the input uri is http => check if it's a known domain and convert to https.
/// If the input uri is https => leave as is
/// If the input uri is not a valid uri or not http/https => return false
/// </summary>
public static bool TryPrepareUrlForRendering(string uriString, out string readyUriString, bool rewriteAllHttp = false)
{
Uri returnUri = null;
readyUriString = null;
if (Uri.TryCreate(uriString, UriKind.Absolute, out var uri))
{
if (uri.IsHttpProtocol())
{
if (rewriteAllHttp || uri.IsDomainWithHttpsSupport())
{
returnUri = uri.ToHttps();
}
else
{
returnUri = uri;
}
}
else if (uri.IsHttpsProtocol() || uri.IsHttpProtocol())
{
returnUri = uri;
}
}
if (returnUri != null)
{
readyUriString = returnUri.AbsoluteUri;
return true;
}
return false;
}
public static bool IsGitRepositoryType(string repositoryType)
{
return ServicesConstants.GitRepository.Equals(repositoryType, StringComparison.OrdinalIgnoreCase);
}
public static void ValidateNuGetPackageMetadata(PackageMetadata packageMetadata)
{
// TODO: Change this to use DataAnnotations
if (packageMetadata.Id.Length > NuGet.Services.Entities.Constants.MaxPackageIdLength)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "ID", NuGet.Services.Entities.Constants.MaxPackageIdLength);
}
if (packageMetadata.Authors != null && packageMetadata.Authors.Flatten().Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Authors", "4000");
}
if (packageMetadata.Copyright != null && packageMetadata.Copyright.Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Copyright", "4000");
}
if (packageMetadata.Description == null)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyMissing, "Description");
}
else if (packageMetadata.Description != null && packageMetadata.Description.Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Description", "4000");
}
if (packageMetadata.IconUrl != null && packageMetadata.IconUrl.AbsoluteUri.Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "IconUrl", "4000");
}
if (packageMetadata.LicenseUrl != null && packageMetadata.LicenseUrl.AbsoluteUri.Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "LicenseUrl", "4000");
}
if (packageMetadata.ProjectUrl != null && packageMetadata.ProjectUrl.AbsoluteUri.Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "ProjectUrl", "4000");
}
if (packageMetadata.Summary != null && packageMetadata.Summary.Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Summary", "4000");
}
if (packageMetadata.ReleaseNotes != null && packageMetadata.ReleaseNotes.Length > 35000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "ReleaseNotes", "35000");
}
if (packageMetadata.Tags != null && packageMetadata.Tags.Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Tags", "4000");
}
if (packageMetadata.Title != null && packageMetadata.Title.Length > 256)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Title", "256");
}
if (packageMetadata.Version != null && packageMetadata.Version.ToFullString().Length > 64)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Version", "64");
}
if (packageMetadata.Language != null && packageMetadata.Language.Length > 20)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Language", "20");
}
if (packageMetadata.Id.Length + (packageMetadata.Version?.ToFullString().Length ?? 0) > 160)
{
throw new EntityException(ServicesStrings.NuGetPackageIdVersionCombinedTooLong, "160");
}
// Validate dependencies
if (packageMetadata.GetDependencyGroups() != null)
{
var packageDependencies = packageMetadata.GetDependencyGroups().ToList();
foreach (var dependency in packageDependencies.SelectMany(s => s.Packages))
{
// NuGet.Core compatibility - dependency package id can not be > 128 characters
if (dependency.Id != null && dependency.Id.Length > NuGet.Services.Entities.Constants.MaxPackageIdLength)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Dependency.Id", NuGet.Services.Entities.Constants.MaxPackageIdLength);
}
// NuGet.Core compatibility - dependency versionspec can not be > 256 characters
if (dependency.VersionRange != null && dependency.VersionRange.ToString().Length > 256)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Dependency.VersionSpec", "256");
}
}
// NuGet.Core compatibility - flattened dependencies should be <= Int16.MaxValue
if (packageDependencies.Flatten().Length > Int16.MaxValue)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "Dependencies", Int16.MaxValue);
}
// Verify there are no duplicate dependency groups
if (packageDependencies.Select(pd => pd.TargetFramework).Distinct().Count() != packageDependencies.Count)
{
throw new EntityException(ServicesStrings.NuGetPackageDuplicateDependencyGroup);
}
}
// Validate repository metadata
if (packageMetadata.RepositoryType != null && packageMetadata.RepositoryType.Length > 100)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "RepositoryType", "100");
}
if (packageMetadata.RepositoryUrl != null && packageMetadata.RepositoryUrl.AbsoluteUri.Length > 4000)
{
throw new EntityException(ServicesStrings.NuGetPackagePropertyTooLong, "RepositoryUrl", "4000");
}
}
public static string GetSelectListText(Package package)
{
var tags = new List<string>();
if (package.IsLatestSemVer2)
{
tags.Add("Latest");
}
var deprecation = package.Deprecations.SingleOrDefault();
if (deprecation != null)
{
var deprecationReasons = new List<string>();
if (deprecation.Status.HasFlag(PackageDeprecationStatus.Legacy))
{
deprecationReasons.Add("Legacy");
}
if (deprecation.Status.HasFlag(PackageDeprecationStatus.CriticalBugs))
{
deprecationReasons.Add("Critical Bugs");
}
if (deprecation.Status.HasFlag(PackageDeprecationStatus.Other))
{
deprecationReasons.Add("Other");
}
if (deprecationReasons.Any())
{
tags.Add("Deprecated - " + string.Join(", ", deprecationReasons));
}
}
return NuGetVersionFormatter.ToFullString(package.Version) + (tags.Any() ? $" ({string.Join(", ", tags)})" : string.Empty);
}
}
}