Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrates [Nexus] service to new service model #2520

Merged
merged 19 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 181 additions & 91 deletions services/nexus/nexus.service.js
Original file line number Diff line number Diff line change
@@ -1,124 +1,214 @@
'use strict'
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved

const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const BaseJsonService = require('../base-json')
const Joi = require('joi')
const { isSnapshotVersion: isNexusSnapshotVersion } = require('./nexus-version')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary for this PR, though for future reference I've been trying to remove these renames as services are rewritten. (The renames are legacy too.)

const { addv: versionText } = require('../../lib/text-formatters')
const { version: versionColor } = require('../../lib/color-formatters')

module.exports = class Nexus extends LegacyService {
const versionRegex = /^\d+(\.\d+)*(-.*)?$/
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved

const searchApiSchema = Joi.object({
data: Joi.array()
.items(
Joi.object({
latestRelease: Joi.string().regex(versionRegex),
latestSnapshot: Joi.string().regex(versionRegex),
version: Joi.string().regex(versionRegex),
})
)
.required(),
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
}).required()

const resolveApiSchema = Joi.object({
data: Joi.object({
baseVersion: Joi.string().regex(versionRegex),
version: Joi.string().regex(versionRegex),
}).required(),
}).required()

const keywords = ['nexus', 'sonatype']

module.exports = class Nexus extends BaseJsonService {
static render({ message, color }) {
return {
message,
color,
}
}

static get category() {
return 'version'
}

static get route() {
return {
base: 'nexus',
// API pattern:
// /nexus/(r|s|<repo-name>)/(http|https)/<nexus.host>[:port][/<entry-path>]/<group>/<artifact>[:k1=v1[:k2=v2[...]]]
format:
'(r|s|[^/]+)/(https?)/((?:[^/]+)(?:/[^/]+)?)/([^/]+)/([^/:]+)(:.+)?',
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
capture: ['repo', 'scheme', 'host', 'groupId', 'artifactId', 'queryOpt'],
}
}

static get defaultBadgeData() {
return { color: 'blue', label: 'nexus' }
}

static get examples() {
return [
{
title: 'Sonatype Nexus (Releases)',
previewUrl: 'r/https/oss.sonatype.org/com.google.guava/guava',
pattern: 'r/:scheme/:host/:groupId/:artifactId',
namedParams: {
scheme: 'https',
host: 'oss.sonatype.org',
groupId: 'com.google.guava',
artifactId: 'guava',
},
staticExample: this.render({ message: 'v27.0.1-jre' }),
keywords,
},
{
title: 'Sonatype Nexus (Snapshots)',
previewUrl: 's/https/oss.sonatype.org/com.google.guava/guava',
pattern: 's/:scheme/:host/:groupId/:artifactId',
namedParams: {
scheme: 'https',
host: 'oss.sonatype.org',
groupId: 'com.google.guava',
artifactId: 'guava',
},
staticExample: this.render({ message: 'v24.0-SNAPSHOT' }),
keywords,
},
{
title: 'Sonatype Nexus (Repository)',
pattern: ':repo/:scheme/:host/:groupId/:artifactId',
namedParams: {
repo: 'developer',
scheme: 'https',
host: 'repository.jboss.org/nexus',
groupId: 'ai.h2o',
artifactId: 'h2o-automl',
},
staticExample: this.render({ message: '3.22.0.2' }),
keywords,
},
{
title: 'Sonatype Nexus (Query Options)',
pattern: ':repo/:scheme/:host/:groupId/:artifactId/:queryOpt',
namedParams: {
repo: 'fs-public-snapshots',
scheme: 'https',
host: 'repository.jboss.org/nexus',
groupId: 'com.progress.fuse',
artifactId: 'fusehq',
queryOpt: ':c=agent-apple-osx:p=tar.gz',
},
staticExample: this.render({
message: '7.0.1-SNAPSHOT',
color: 'orange',
}),
keywords,
documentation: `
<p>
Note that you can use query options with any Nexus badge type (Releases, Snapshots, or Repository)
</p>
<p>
Query options should be provided as key=value pairs separated by a semicolon
</p>
`,
},
]
}

static registerLegacyRouteHandler({ camp, cache }) {
// standalone sonatype nexus installation
async handle({ repo, scheme, host, groupId, artifactId, queryOpt }) {
const requestParams = this.getRequestParams({
repo,
scheme,
host,
groupId,
artifactId,
queryOpt,
})
const json = await this._requestJson(requestParams)
if (json.data.length === 0) {
return this.constructor.render({ message: 'no-artifact', color: 'red' })
}

let version = '0'
if (repo === 'r') {
version = json.data[0].latestRelease
} else if (repo === 's') {
json.data.every(artifact => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, though for future reference you should know about another pattern we use, which is transform. If you delegate this part of the responsibility to a transform method, you can use a for loop with a return statement which is easier to read.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was another copy/paste from the original implementation. I'm not familiar with transform but happy to optimize!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an example of the transform you are referring to?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, that's a good example. We should probably link that from the service-rewriting docs if there isn't already an example of a transform() function.

if (isNexusSnapshotVersion(artifact.latestSnapshot)) {
version = artifact.latestSnapshot
return
}
if (isNexusSnapshotVersion(artifact.version)) {
version = artifact.version
return
}
return true
})
} else {
version = json.data.baseVersion || json.data.version
}
return this.buildBadge(version)
}

getRequestParams({ repo, scheme, host, groupId, artifactId, queryOpt }) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably would be a bit more direct if it invoked _requestJson and was renamed to fetch. "Fetch" is another pattern we use a lot.

const options = {
qs: {
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
g: groupId,
a: artifactId,
},
}
let schema
let url = `${scheme}://${host}/`
// API pattern:
// /nexus/(r|s|<repo-name>)/(http|https)/<nexus.host>[:port][/<entry-path>]/<group>/<artifact>[:k1=v1[:k2=v2[...]]].<format>
// for /nexus/[rs]/... pattern, use the search api of the nexus server, and
// for /nexus/<repo-name>/... pattern, use the resolve api of the nexus server.
camp.route(
/^\/nexus\/(r|s|[^/]+)\/(https?)\/((?:[^/]+)(?:\/[^/]+)?)\/([^/]+)\/([^/:]+)(:.+)?\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const repo = match[1] // r | s | repo-name
const scheme = match[2] // http | https
const host = match[3] // eg, `nexus.example.com`
const groupId = encodeURIComponent(match[4]) // eg, `com.google.inject`
const artifactId = encodeURIComponent(match[5]) // eg, `guice`
const queryOpt = (match[6] || '').replace(/:/g, '&') // eg, `&p=pom&c=doc`
const format = match[7]

const badgeData = getBadgeData('nexus', data)

const apiUrl = `${scheme}://${host}${
repo === 'r' || repo === 's'
? `/service/local/lucene/search?g=${groupId}&a=${artifactId}${queryOpt}`
: `/service/local/artifact/maven/resolve?r=${repo}&g=${groupId}&a=${artifactId}&v=LATEST${queryOpt}`
}`

request(
apiUrl,
{ headers: { Accept: 'application/json' } },
(err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
} else if (res && res.statusCode === 404) {
badgeData.text[1] = 'no-artifact'
sendBadge(format, badgeData)
return
}
try {
const parsed = JSON.parse(buffer)
let version = '0'
switch (repo) {
case 'r':
if (parsed.data.length === 0) {
badgeData.text[1] = 'no-artifact'
sendBadge(format, badgeData)
return
}
version = parsed.data[0].latestRelease
break
case 's':
if (parsed.data.length === 0) {
badgeData.text[1] = 'no-artifact'
sendBadge(format, badgeData)
return
}
// only want to match 1.2.3-SNAPSHOT style versions, which may not always be in
// 'latestSnapshot' so check 'version' as well before continuing to next entry
parsed.data.every(artifact => {
if (isNexusSnapshotVersion(artifact.latestSnapshot)) {
version = artifact.latestSnapshot
return
}
if (isNexusSnapshotVersion(artifact.version)) {
version = artifact.version
return
}
return true
})
break
default:
version = parsed.data.baseVersion || parsed.data.version
break
}
if (version !== '0') {
badgeData.text[1] = versionText(version)
badgeData.colorscheme = versionColor(version)
} else {
badgeData.text[1] = 'undefined'
badgeData.colorscheme = 'orange'
}
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
}
)
if (repo === 'r' || repo === 's') {
schema = searchApiSchema
url += 'service/local/lucene/search'
} else {
schema = resolveApiSchema
url += 'service/local/artifact/maven/resolve'
options.qs.r = repo
options.qs.v = 'LATEST'
}

if (queryOpt) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not obvious from reading this what it's supposed to do. For future reference, stuff like this probably should be its own static method (or function) with a unit test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. The original implementation was plugging the query params directly into the url, but that was a lot more readable.

I'll extract this out into its own function with a comment. 🤦‍♂️ on the test. Not sure how I forgot that one!

const opts = queryOpt.split(':')
opts.forEach(opt => {
const kvp = opt.split('=')
const key = kvp[0]
const val = kvp[1]
options.qs[key] = val
})
)
}
return {
schema,
url,
options,
errorMessages: {
404: 'no-artifact',
},
}
}

buildBadge(version) {
let message, color
if (version !== '0') {
message = versionText(version)
color = versionColor(version)
} else {
message = 'undefined'
color = 'orange'
}

return this.constructor.render({ message, color })
}
}
23 changes: 7 additions & 16 deletions services/nexus/nexus.tester.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'use strict'

const Joi = require('joi')
const { invalidJSON } = require('../response-fixtures')

const {
isVPlusDottedVersionNClausesWithOptionalSuffix: isVersion,
} = require('../test-validators')
const t = (module.exports = require('../create-service-tester')())

t.create('search release version')
.get('/r/https/repository.jboss.org/nexus/jboss/jboss-client.json')
.expectJSONTypes(
Joi.object().keys({
name: 'nexus',
value: Joi.string().regex(/^v4(\.\d+)+$/),
value: isVersion,
})
)

Expand All @@ -23,7 +24,7 @@ t.create('search snapshot version')
.expectJSONTypes(
Joi.object().keys({
name: 'nexus',
value: Joi.string().regex(/-SNAPSHOT$/),
value: isVersion,
})
)

Expand All @@ -50,7 +51,7 @@ t.create('resolve version')
.expectJSONTypes(
Joi.object().keys({
name: 'nexus',
value: Joi.string().regex(/^v3(\.\d+)+$/),
value: isVersion,
})
)

Expand All @@ -61,7 +62,7 @@ t.create('resolve version with query')
.expectJSONTypes(
Joi.object().keys({
name: 'nexus',
value: Joi.string().regex(/^v7(\.\d+)+-SNAPSHOT$/),
value: isVersion,
})
)

Expand All @@ -75,13 +76,3 @@ t.create('connection error')
.get('/r/https/repository.jboss.org/nexus/jboss/jboss-client.json')
.networkOff()
.expectJSON({ name: 'nexus', value: 'inaccessible' })

t.create('json parsing error')
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
.get('/r/https/repository.jboss.org/nexus/jboss/jboss-client.json')
.intercept(nock =>
nock('https://repository.jboss.org')
.get('/nexus/service/local/lucene/search')
.query({ g: 'jboss', a: 'jboss-client' })
.reply(invalidJSON)
)
.expectJSON({ name: 'nexus', value: 'invalid' })