Skip to content

Commit 4bbd2cf

Browse files
committed
feat(nexus): add filter option to transform and handle methods for version selection
Fixes #11742
1 parent 8949c46 commit 4bbd2cf

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

services/nexus/nexus.service.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import Joi from 'joi'
2+
import { matcher } from 'matcher'
3+
import { compare } from 'mvncmp'
24
import { renderVersionBadge } from '../version.js'
35
import { url } from '../validators.js'
46
import { BaseJsonService, NotFound, pathParams, queryParams } from '../index.js'
@@ -18,6 +20,7 @@ const queryParamSchema = Joi.object({
1820
queryOpt: Joi.string()
1921
.regex(/(:[\w.]+=[^:]*)+/i)
2022
.optional(),
23+
filter: Joi.string().optional(),
2124
}).required()
2225

2326
const openApiQueryParams = queryParams(
@@ -33,6 +36,11 @@ Query options should be provided as key=value pairs separated by a colon.
3336
Possible values: <a href="https://help.sonatype.com/en/searching-for-components.html">Searching for Component</a>
3437
`,
3538
},
39+
{
40+
name: 'filter',
41+
example: '1.2.*',
42+
description: 'Glob filter to select matching versions',
43+
},
3644
)
3745

3846
export default class Nexus extends BaseJsonService {
@@ -149,17 +157,27 @@ export default class Nexus extends BaseJsonService {
149157
return { json }
150158
}
151159

152-
transform({ repo, json }) {
160+
transform({ repo, json, filter }) {
153161
if (json.items.length === 0) {
154162
const versionType = repo === 's' ? 'snapshot ' : ''
155163
throw new NotFound({
156164
prettyMessage: `artifact or ${versionType}version not found`,
157165
})
158166
}
167+
168+
if (filter) {
169+
const versions = json.items.map(item => item.version)
170+
const filtered = matcher(versions, filter)
171+
if (filtered.length === 0) {
172+
throw new NotFound({ prettyMessage: 'no matching versions found' })
173+
}
174+
return { version: filtered.sort(compare).reverse()[0] }
175+
}
176+
159177
return { version: json.items[0].version }
160178
}
161179

162-
async handle({ repo, groupId, artifactId }, { server, queryOpt }) {
180+
async handle({ repo, groupId, artifactId }, { server, queryOpt, filter }) {
163181
const { json } = await this.fetch({
164182
repo,
165183
server,
@@ -168,7 +186,7 @@ export default class Nexus extends BaseJsonService {
168186
queryOpt,
169187
})
170188

171-
const { version } = this.transform({ repo, json })
189+
const { version } = this.transform({ repo, json, filter })
172190
return renderVersionBadge({ version })
173191
}
174192
}

0 commit comments

Comments
 (0)