-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
nuget.rb
232 lines (200 loc) · 6.83 KB
/
nuget.rb
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
232
# frozen_string_literal: true
module Ecosystem
class Nuget < Base
def registry_url(package, version = nil)
"#{@registry_url}/packages/#{package.name}/#{version}"
end
def download_url(package, version)
return nil unless version.present?
"https://api.nuget.org/v3-flatcontainer/#{package.name.downcase}/#{version}/#{package.name.downcase}.#{version}.nupkg"
end
def install_command(package, version = nil)
"Install-Package #{package.name}" + (version ? " -Version #{version}" : "")
end
def check_status_url(package)
"https://api.nuget.org/v3-flatcontainer/#{package.name.downcase}/index.json"
end
def check_status(package)
url = check_status_url(package)
response = Faraday.get(url)
return "removed" if [400, 404, 410].include?(response.status)
url = registry_url(package)
response = Faraday.get(url)
return "removed" if [400, 404, 410].include?(response.status)
return "removed" if response.body.include? 'This package has been deleted from the gallery.'
return "removed" if response.body.include? "This package's content is hidden"
rescue Faraday::Error => e
nil
end
def recently_updated_package_names
name_endpoints.reverse[0..1].map { |url| get_names(url) }.flatten.uniq
rescue
[]
end
def name_endpoints
get("https://api.nuget.org/v3/catalog0/index.json")["items"].map { |i| i["@id"] }
end
def get_names(endpoint)
get(endpoint)["items"].map { |i| i["nuget:id"] }
end
def all_package_names
endpoints = name_endpoints
segment_count = endpoints.length - 1
names = []
endpoints.reverse[0..segment_count].each do |endpoint|
package_ids = get_names(endpoint)
package_ids.each { |id| names << id.downcase }
end
return names
rescue
[]
end
def fetch_package_metadata(name)
h = {
name: name,
}
h[:releases] = get_releases(name)
h[:download_stats] = download_stats(name)
h[:versions] = versions_metadata(h)
return {} unless h[:versions].any?
h
end
def download_stats(name)
get_json("https://azuresearch-usnc.nuget.org/query?q=packageid:#{name.downcase}")
rescue
{}
end
def get_releases(name)
latest_version = get_json("https://api.nuget.org/v3/registration5-gz-semver2/#{name.downcase}/index.json")
if latest_version["items"][0]["items"]
releases = []
latest_version["items"].each do |items|
releases << items["items"]
end
releases.flatten!
elsif releases.nil?
releases = []
latest_version["items"].each do |page|
json = get_json(page["@id"])
releases << json["items"]
end
releases.flatten!
end
releases
rescue StandardError
[]
end
def map_package_metadata(package)
return false if package[:releases].nil?
item = package[:releases].last["catalogEntry"]
{
name: package[:name].try(:downcase),
description: description(item),
homepage: item["projectUrl"],
keywords_array: Array(item["tags"]).reject(&:blank?),
repository_url: repo_fallback(item["projectUrl"], item["licenseUrl"], item["packageUrl"]),
releases: package[:releases],
licenses: item["licenseExpression"],
downloads: package[:download_stats]['data'].try(:first).try(:fetch,'totalDownloads'),
downloads_period: 'total',
download_stats: package[:download_stats],
}
end
def repo_fallback(repo, license, homepage)
repo = "" if repo.nil?
homepage = "" if homepage.nil?
license = "" if license.nil?
repo_url = UrlParser.try_all(repo) rescue ""
homepage_url = UrlParser.try_all(homepage) rescue ""
license_url = UrlParser.try_all(license) rescue ""
if repo_url.present?
repo_url
elsif homepage_url.present?
homepage_url
elsif license_url.present?
license_url
else
""
end
end
def description(item)
item["description"].blank? ? item["summary"] : item["description"]
end
def versions_metadata(pkg_metadata, existing_version_numbers = [])
pkg_metadata[:releases].map do |item|
{
number: item["catalogEntry"]["version"],
published_at: item["catalogEntry"]["published"],
metadata: {
downloads: version_downloads(pkg_metadata, item["catalogEntry"]["version"])
}
}
end
end
def version_downloads(pkg_metadata, version)
return nil unless pkg_metadata[:download_stats] && pkg_metadata[:download_stats]['data'].present?
pkg_metadata[:download_stats]['data'][0]['versions'].find{|v| v['version'] == version}.try(:fetch,'downloads')
rescue
nil
end
def dependencies_metadata(_name, version, package)
current_version = package[:releases].find { |v| v["catalogEntry"]["version"] == version }
dep_groups = current_version.fetch("catalogEntry", {})["dependencyGroups"] || []
deps = dep_groups.map do |dep_group|
next unless dep_group["dependencies"]
dep_group["dependencies"].map do |dependency|
{
name: dependency["id"],
requirements: parse_requirements(dependency["range"]),
}
end
end.flatten.compact
deps.map do |dep|
{
package_name: dep[:name].downcase,
requirements: dep[:requirements],
kind: "runtime",
optional: false,
ecosystem: self.class.name.demodulize.downcase,
}
end
end
def parse_requirements(range)
return unless range.present?
parts = range[1..-2].split(",")
requirements = []
low_bound = range[0]
high_bound = range[-1]
low_number = parts[0].strip
high_number = parts[1].try(:strip)
# lowest
low_sign = low_bound == "[" ? ">=" : ">"
high_sign = high_bound == "]" ? "<=" : "<"
# highest
if high_number != low_number
requirements << "#{low_sign} #{low_number}" if low_number.present?
requirements << "#{high_sign} #{high_number}" if high_number.present?
elsif high_number == low_number
requirements << "= #{high_number}"
elsif low_number.present?
requirements << "#{low_sign} #{low_number}"
end
requirements << ">= 0" if requirements.empty?
requirements.join(" ")
end
def maintainers_metadata(name)
json = get_json("https://azuresearch-usnc.nuget.org/query?q=packageid:#{name.downcase}")
json['data'][0]['owners'].map do |user|
{
uuid: user,
login: user
}
end
rescue StandardError
[]
end
def maintainer_url(maintainer)
"https://www.nuget.org/profiles/#{maintainer.login}"
end
end
end