-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecosystem.go
More file actions
242 lines (218 loc) · 7.27 KB
/
Copy pathecosystem.go
File metadata and controls
242 lines (218 loc) · 7.27 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
232
233
234
235
236
237
238
239
240
241
242
package purl
import (
"strings"
)
const (
ecosystemAlpine = "alpine"
ecosystemArch = "arch"
ecosystemComposer = "composer"
ecosystemGitHubActions = "github-actions"
ecosystemGolang = "golang"
ecosystemMaven = "maven"
ecosystemNPM = "npm"
ecosystemPackagist = "packagist"
ecosystemRubyGems = "rubygems"
purlTypeGem = "gem"
purlTypeGitHubActions = "githubactions"
)
// purlTypeForEcosystem maps ecosystem names to PURL types.
// Most ecosystems use their name as the PURL type, but some differ.
var purlTypeForEcosystem = map[string]string{
ecosystemAlpine: "apk",
ecosystemArch: "alpm",
ecosystemRubyGems: purlTypeGem,
ecosystemPackagist: ecosystemComposer,
ecosystemGitHubActions: purlTypeGitHubActions,
}
// ecosystemAliases maps alternate names to canonical ecosystem names.
var ecosystemAliases = map[string]string{
"go": ecosystemGolang,
purlTypeGem: ecosystemRubyGems,
ecosystemComposer: ecosystemPackagist,
}
// osvEcosystemNames maps PURL types to OSV ecosystem names.
var osvEcosystemNames = map[string]string{
purlTypeGem: "RubyGems",
ecosystemNPM: ecosystemNPM,
"pypi": "PyPI",
"cargo": "crates.io",
"conan": "ConanCenter",
"cran": "CRAN",
ecosystemGolang: "Go",
"hackage": "Hackage",
ecosystemMaven: "Maven",
"julia": "Julia",
"nuget": "NuGet",
"opam": "opam",
ecosystemComposer: "Packagist",
"hex": "Hex",
"pub": "Pub",
"swift": "SwiftURL",
purlTypeGitHubActions: "GitHub Actions",
}
// depsdevSystemNames maps PURL types to deps.dev system names.
var depsdevSystemNames = map[string]string{
ecosystemNPM: "NPM",
purlTypeGem: "RUBYGEMS",
"pypi": "PYPI",
"cargo": "CARGO",
ecosystemGolang: "GO",
ecosystemMaven: "MAVEN",
"nuget": "NUGET",
}
// defaultNamespaces defines default namespaces for certain ecosystems.
var defaultNamespaces = map[string]string{
ecosystemAlpine: ecosystemAlpine,
ecosystemArch: ecosystemArch,
}
// NormalizeEcosystem returns the canonical ecosystem name.
// Handles aliases like "go" -> "golang", "gem" -> "rubygems".
func NormalizeEcosystem(ecosystem string) string {
lower := strings.ToLower(ecosystem)
if canonical, ok := ecosystemAliases[lower]; ok {
return canonical
}
return lower
}
// EcosystemToPURLType converts an ecosystem name to the corresponding PURL type.
// Returns the input unchanged if no mapping exists.
func EcosystemToPURLType(ecosystem string) string {
normalized := NormalizeEcosystem(ecosystem)
if t, ok := purlTypeForEcosystem[normalized]; ok {
return t
}
return normalized
}
// PURLTypeToEcosystem converts a PURL type back to an ecosystem name.
// This is the inverse of EcosystemToPURLType.
func PURLTypeToEcosystem(purlType string) string {
// Reverse lookup
for eco, pt := range purlTypeForEcosystem {
if pt == purlType {
return eco
}
}
return purlType
}
// EcosystemToOSV converts an ecosystem name to the OSV ecosystem name.
// OSV uses specific capitalization and naming conventions.
func EcosystemToOSV(ecosystem string) string {
purlType := EcosystemToPURLType(ecosystem)
if osv, ok := osvEcosystemNames[purlType]; ok {
return osv
}
return ecosystem
}
// PURLTypeToOSV converts a PURL type to the OSV ecosystem name and reports
// whether the type is one OSV recognises. Unlike EcosystemToOSV, it takes the
// PURL type directly (skipping the ecosystem-name normalisation when the
// caller already has a parsed PURL) and returns ok=false on a miss rather
// than passing the input through, so callers that emit OSV records can fall
// back to a GIT range instead of writing an ecosystem the OSV schema will
// reject.
func PURLTypeToOSV(purlType string) (string, bool) {
osv, ok := osvEcosystemNames[purlType]
return osv, ok
}
// PURLTypeToDepsdev converts a PURL type to the deps.dev system name.
// Returns empty string if the type is not supported by deps.dev.
func PURLTypeToDepsdev(purlType string) string {
if system, ok := depsdevSystemNames[purlType]; ok {
return system
}
return ""
}
// MakePURL constructs a PURL from ecosystem-native package identifiers.
//
// It handles namespace extraction for ecosystems:
// - npm: @scope/pkg -> namespace="@scope", name="pkg"
// - maven: group:artifact -> namespace="group", name="artifact"
// - golang: github.com/foo/bar -> namespace="github.com/foo", name="bar"
// - composer: vendor/package -> namespace="vendor", name="package"
// - alpine: pkg -> namespace="alpine", name="pkg"
// - arch: pkg -> namespace="arch", name="pkg"
func MakePURL(ecosystem, name, version string) *PURL {
purlType := EcosystemToPURLType(ecosystem)
namespace := ""
pkgName := name
// Handle default namespaces
if ns, ok := defaultNamespaces[NormalizeEcosystem(ecosystem)]; ok {
namespace = ns
}
// Extract namespace from name based on ecosystem conventions
switch NormalizeEcosystem(ecosystem) {
case ecosystemNPM:
if strings.HasPrefix(name, "@") {
parts := strings.SplitN(name, "/", 2) //nolint:mnd
if len(parts) == 2 { //nolint:mnd
namespace = parts[0] // Keep the @ for packageurl-go
pkgName = parts[1]
}
}
case ecosystemGolang:
if idx := strings.LastIndex(name, "/"); idx > 0 {
namespace = name[:idx]
pkgName = name[idx+1:]
}
case ecosystemMaven:
if strings.Contains(name, ":") {
parts := strings.SplitN(name, ":", 2) //nolint:mnd
namespace = parts[0]
pkgName = parts[1]
}
case ecosystemPackagist, ecosystemComposer:
if strings.Contains(name, "/") {
parts := strings.SplitN(name, "/", 2) //nolint:mnd
namespace = parts[0]
pkgName = parts[1]
}
case ecosystemGitHubActions:
// GitHub Actions: owner/repo or owner/repo/path -> namespace=owner, name=repo (path ignored)
if strings.Contains(name, "/") {
parts := strings.SplitN(name, "/", 3) //nolint:mnd
namespace = parts[0]
pkgName = parts[1]
}
}
return New(purlType, namespace, pkgName, version, nil)
}
// MakePURLString is like MakePURL but returns the PURL as a string.
func MakePURLString(ecosystem, name, version string) string {
purlType := EcosystemToPURLType(ecosystem)
namespace, pkgName := splitNamespace(ecosystem, name)
return buildPURLString(purlType, namespace, pkgName, version, "")
}
// SupportedEcosystems returns a list of all supported ecosystem names.
// This includes both PURL types and common aliases.
func SupportedEcosystems() []string {
seen := make(map[string]bool)
var result []string
// Add all known PURL types
for _, t := range KnownTypes() {
if !seen[t] {
seen[t] = true
result = append(result, t)
}
}
// Add ecosystem aliases
for alias := range ecosystemAliases {
if !seen[alias] {
seen[alias] = true
result = append(result, alias)
}
}
// Add ecosystems that map to different PURL types
for eco := range purlTypeForEcosystem {
if !seen[eco] {
seen[eco] = true
result = append(result, eco)
}
}
return result
}
// IsValidEcosystem returns true if the ecosystem is recognized.
func IsValidEcosystem(ecosystem string) bool {
normalized := NormalizeEcosystem(ecosystem)
purlType := EcosystemToPURLType(normalized)
return IsKnownType(purlType)
}