This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecosystems.rb
More file actions
83 lines (73 loc) · 3.33 KB
/
ecosystems.rb
File metadata and controls
83 lines (73 loc) · 3.33 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
# frozen_string_literal: true
module Git
module Pkgs
# Maps ecosystem names between bibliothecary, purl, and OSV formats.
# Bibliothecary uses lowercase names internally.
# Purl uses its own type names.
# OSV uses mixed case names that differ from both.
module Ecosystems
# Mapping from bibliothecary ecosystem names to OSV and purl equivalents
MAPPINGS = {
"npm" => { osv: "npm", purl: "npm" },
"rubygems" => { osv: "RubyGems", purl: "gem" },
"pypi" => { osv: "PyPI", purl: "pypi" },
"cargo" => { osv: "crates.io", purl: "cargo" },
"maven" => { osv: "Maven", purl: "maven" },
"nuget" => { osv: "NuGet", purl: "nuget" },
"packagist" => { osv: "Packagist", purl: "composer" },
"go" => { osv: "Go", purl: "golang" },
"hex" => { osv: "Hex", purl: "hex" },
"pub" => { osv: "Pub", purl: "pub" }
}.freeze
# Reverse mappings for lookups from OSV/purl to bibliothecary
OSV_TO_BIBLIOTHECARY = MAPPINGS.transform_values { |v| v[:osv] }.invert.freeze
PURL_TO_BIBLIOTHECARY = MAPPINGS.transform_values { |v| v[:purl] }.invert.freeze
class << self
# Convert bibliothecary ecosystem name to OSV format
# @param ecosystem [String] bibliothecary ecosystem name (e.g., "rubygems")
# @return [String, nil] OSV ecosystem name (e.g., "RubyGems") or nil if not mapped
def to_osv(ecosystem)
MAPPINGS.dig(ecosystem.to_s.downcase, :osv)
end
# Convert bibliothecary ecosystem name to purl type
# @param ecosystem [String] bibliothecary ecosystem name (e.g., "rubygems")
# @return [String, nil] purl type (e.g., "gem") or nil if not mapped
def to_purl(ecosystem)
MAPPINGS.dig(ecosystem.to_s.downcase, :purl)
end
# Convert OSV ecosystem name to bibliothecary format
# @param osv_ecosystem [String] OSV ecosystem name (e.g., "RubyGems")
# @return [String, nil] bibliothecary ecosystem name (e.g., "rubygems") or nil if not mapped
def from_osv(osv_ecosystem)
OSV_TO_BIBLIOTHECARY[osv_ecosystem]
end
# Convert purl type to bibliothecary ecosystem name
# @param purl_type [String] purl type (e.g., "gem")
# @return [String, nil] bibliothecary ecosystem name (e.g., "rubygems") or nil if not mapped
def from_purl(purl_type)
PURL_TO_BIBLIOTHECARY[purl_type]
end
# Check if an ecosystem is supported for vulnerability scanning
# @param ecosystem [String] bibliothecary ecosystem name
# @return [Boolean]
def supported?(ecosystem)
MAPPINGS.key?(ecosystem.to_s.downcase)
end
# List all supported bibliothecary ecosystem names
# @return [Array<String>]
def supported_ecosystems
MAPPINGS.keys
end
# Generate a purl (package URL) for a given ecosystem and package name
# @param ecosystem [String] bibliothecary ecosystem name (e.g., "rubygems")
# @param name [String] package name
# @return [String, nil] purl string (e.g., "pkg:gem/rails") or nil if ecosystem not supported
def generate_purl(ecosystem, name)
purl_type = to_purl(ecosystem)
return nil unless purl_type
"pkg:#{purl_type}/#{name}"
end
end
end
end
end