-
Notifications
You must be signed in to change notification settings - Fork 310
/
Extensions.kt
234 lines (212 loc) · 8.28 KB
/
Extensions.kt
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
/*
* Copyright (C) 2020 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package org.ossreviewtoolkit.model.utils
import java.net.URI
import org.ossreviewtoolkit.clients.clearlydefined.ComponentType
import org.ossreviewtoolkit.clients.clearlydefined.Coordinates
import org.ossreviewtoolkit.clients.clearlydefined.Provider
import org.ossreviewtoolkit.clients.clearlydefined.SourceLocation
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.RemoteArtifact
import org.ossreviewtoolkit.model.TextLocation
import org.ossreviewtoolkit.model.VcsInfoCurationData
import org.ossreviewtoolkit.utils.common.percentEncode
internal fun TextLocation.prependPath(prefix: String): String =
if (prefix.isBlank()) path else "${prefix.removeSuffix("/")}/$path"
/**
* Map an [Identifier] to a ClearlyDefined [ComponentType] and [Provider]. Note that an
* [identifier's type][Identifier.type] in ORT currently implies a default provider. Return null if a mapping is not
* possible.
*/
fun Identifier.toClearlyDefinedTypeAndProvider(): Pair<ComponentType, Provider>? =
when (type) {
"Bower" -> ComponentType.GIT to Provider.GITHUB
"CocoaPods" -> ComponentType.POD to Provider.COCOAPODS
"Composer" -> ComponentType.COMPOSER to Provider.PACKAGIST
"Crate" -> ComponentType.CRATE to Provider.CRATES_IO
"DotNet", "NuGet" -> ComponentType.NUGET to Provider.NUGET
"Gem" -> ComponentType.GEM to Provider.RUBYGEMS
"GoDep", "GoMod" -> ComponentType.GO to Provider.GOLANG
"Maven" -> ComponentType.MAVEN to Provider.MAVEN_CENTRAL
"NPM" -> ComponentType.NPM to Provider.NPM_JS
"PyPI" -> ComponentType.PYPI to Provider.PYPI
"Pub" -> ComponentType.GIT to Provider.GITHUB
else -> null
}
/**
* Map an ORT [Identifier] to ClearlyDefined [Coordinates], or to null if mapping is not possible.
*/
fun Identifier.toClearlyDefinedCoordinates(): Coordinates? =
toClearlyDefinedTypeAndProvider()?.let { (type, provider) ->
Coordinates(
type = type,
provider = provider,
namespace = namespace.takeUnless { it.isEmpty() },
name = name,
revision = version.takeUnless { it.isEmpty() }
)
}
/**
* Create a ClearlyDefined [SourceLocation] from an [Identifier]. Prefer a [VcsInfoCurationData], but eventually fall
* back to a [RemoteArtifact], or return null if not enough information is available.
*/
fun Identifier.toClearlyDefinedSourceLocation(
vcs: VcsInfoCurationData?,
sourceArtifact: RemoteArtifact?
): SourceLocation? {
val vcsUrl = vcs?.url
val vcsRevision = vcs?.revision
val matchGroups = vcsUrl?.let { REG_GIT_URL.matchEntire(it)?.groupValues }
return when {
// GitHub is the only VCS provider supported by ClearlyDefined for now.
// TODO: Find out how to handle VCS curations without a revision.
vcsUrl != null && matchGroups != null && vcsRevision != null -> {
SourceLocation(
name = matchGroups[2],
namespace = matchGroups[1],
path = vcs.path,
provider = Provider.GITHUB,
revision = vcsRevision,
type = ComponentType.GIT,
url = vcsUrl
)
}
sourceArtifact != null -> {
toClearlyDefinedTypeAndProvider()?.let { (_, provider) ->
SourceLocation(
name = name,
namespace = namespace.takeUnless { it.isEmpty() },
provider = provider,
revision = version,
type = ComponentType.SOURCE_ARCHIVE,
url = sourceArtifact.url
)
}
}
else -> null
}
}
/** Regular expression to match VCS URLs supported by ClearlyDefined. */
private val REG_GIT_URL = Regex(".+://github.com/(.+)/(.+).git")
/**
* A subset of the Package URL types defined at https://github.com/package-url/purl-spec/blob/ad8a673/PURL-TYPES.rst.
*/
enum class PurlType(private val value: String) {
ALPINE("alpine"),
A_NAME("a-name"),
BOWER("bower"),
CARGO("cargo"),
COCOAPODS("cocoapods"),
COMPOSER("composer"),
CONAN("conan"),
CONDA("conda"),
CRAN("cran"),
DEBIAN("deb"),
DRUPAL("drupal"),
GEM("gem"),
GOLANG("golang"),
MAVEN("maven"),
NPM("npm"),
NUGET("nuget"),
PYPI("pypi"),
RPM("rpm");
override fun toString() = value
}
/**
* Map a [Package]'s type to the string representation of the respective [PurlType], or fall back to the lower-case
* [Package]'s type if the [PurlType] cannot be determined.
*/
fun Identifier.getPurlType() =
when (val lowerType = type.lowercase()) {
"bower" -> PurlType.BOWER
"composer" -> PurlType.COMPOSER
"conan" -> PurlType.CONAN
"crate" -> PurlType.CARGO
"go" -> PurlType.GOLANG
"gem" -> PurlType.GEM
"maven" -> PurlType.MAVEN
"npm" -> PurlType.NPM
"nuget" -> PurlType.NUGET
"pypi" -> PurlType.PYPI
else -> lowerType
}.toString()
/**
* Create the canonical [package URL](https://github.com/package-url/purl-spec) ("purl") based on the properties of
* the [Identifier]. Some issues remain with this specification
* (see e.g. https://github.com/package-url/purl-spec/issues/33).
*
* This implementation uses the package type as 'type' purl element as it is used
* [in the documentation](https://github.com/package-url/purl-spec/blob/master/README.rst#purl).
* E.g. 'maven' for Gradle projects.
*/
fun Identifier.toPurl() = if (this == Identifier.EMPTY) "" else createPurl(getPurlType(), namespace, name, version)
/**
* Create the canonical [package URL](https://github.com/package-url/purl-spec) ("purl") based on given properties:
* [type] (which must be a String representation of a [PurlType] instance, [namespace], [name] and [version].
* Optional [qualifiers] may be given and will be appended to the purl as query parameters e.g.
* pkg:deb/debian/curl@7.50.3-1?arch=i386&distro=jessie
* Optional [subpath] may be given and will be appended to the purl e.g.
* pkg:golang/google.golang.org/genproto#googleapis/api/annotations
*
*/
fun createPurl(
type: String,
namespace: String,
name: String,
version: String,
qualifiers: Map<String, String> = emptyMap(),
subpath: String = ""
): String = buildString {
append("pkg:")
append(type)
if (namespace.isNotEmpty()) {
append('/')
append(namespace.percentEncode())
}
append('/')
append(name.percentEncode())
append('@')
append(version.percentEncode())
qualifiers.onEachIndexed { index, entry ->
if (index == 0) append("?") else append("&")
append(entry.key.percentEncode())
append("=")
append(entry.value.percentEncode())
}
if (subpath.isNotEmpty()) {
val value = subpath.split('/').joinToString("/", prefix = "#") { it.percentEncode() }
append(value)
}
}
/**
* Return the repo manifest path parsed from this string. The string is interpreted as a URL and the manifest path is
* expected as the value of the "manifest" query parameter, for example:
* http://example.com/repo.git?manifest=manifest.xml.
*
* Return an empty string if no "manifest" query parameter is found or this string cannot be parsed as a URL.
*/
fun String.parseRepoManifestPath() =
runCatching {
URI(this).query.splitToSequence("&")
.map { it.split("=", limit = 2) }
.find { it.first() == "manifest" }
?.get(1)
?.takeUnless { it.isEmpty() }
}.getOrNull()