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

Conversation

calebcartwright
Copy link
Member

@calebcartwright calebcartwright commented Dec 13, 2018

Ports the Nexus service to the new service model. Some related/relevant conversation in #2347 (and closes #2347). Also adds support for authentication which resolves #1699

I also added some thoughts/comments inline

@shields-ci
Copy link

shields-ci commented Dec 13, 2018

Messages
📖 ✨ Thanks for your contribution to Shields, @calebcartwright!

Generated by 🚫 dangerJS against c9f38b2

@paulmelnikow paulmelnikow added the service-badge Accepted and actionable changes, features, and bugs label Dec 13, 2018
@calebcartwright
Copy link
Member Author

calebcartwright commented Dec 18, 2018

I'm currently making I added some updates to this based on some discussions/work from other threads (tests for color validation, tests for auth validation, etc.)

Copy link
Member

@paulmelnikow paulmelnikow left a comment

Choose a reason for hiding this comment

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

Thanks so much for this!

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.)

services/nexus/nexus.service.js Outdated Show resolved Hide resolved
})
const json = await this._requestJson(requestParams)
if (json.data.length === 0) {
return this.constructor.render({ version: 'no-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 not really a version, it's an error. Maybe it would be better to handle this by throwing either NotFound or InvalidResponse with a custom prettyMessage.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed. This is the part I was referencing in my comment above on the Joi schema (not adding a min(1) ) to maintain consistency with the existing implementation.

A NotFound seems like the most logical error to me. Sounds like I can throw that with a custom message of no-artifact to maintain the same behavior (which currently is nexus | no-artifact)?

Copy link
Member

Choose a reason for hiding this comment

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

It seems like a strangely formatted message; feel free to change it.

message = versionText(version)
color = versionColor(version)
} else {
message = 'undefined'
Copy link
Member

Choose a reason for hiding this comment

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

This seems like an invalid response. Could it be handled with Joi instead? ^^

Copy link
Member Author

Choose a reason for hiding this comment

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

I honestly don't know on this one (relying on the LegacyService implementation) 😄

Based on a quick mental walkthrough of the Legacy implementation, I think this could happen under two circumstances:

  1. snapshot badge is requested, but the specified group/artifact has no snapshot versions.
  2. repository badge is requested, response has no value for baseVersion field (which I am assuming can/does happen in real life based on this line from the legacy service), nor does the response have a value for the version field.

The first one feels a lot like the above scenario where throwing NotFound could be used. It could be covered with a schema (although it would get a little complex), but I think a pretty message conveying no snapshot versions found might be more helpful for the user than invalid. Thoughts?

The second one would be much more manageable with Joi than the first IMO, but I'm not sure what Nexus guarantees in the response. For example is baseVersion optional while version is guaranteed? Or is it that one value is guaranteed, but it could be either version or baseVersion, or neither?

Copy link
Member

Choose a reason for hiding this comment

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

I'm familiar with what Nexus is, but haven't used it, so I'm gonna have to think about this when I'm a little fresher. If anyone else has another opinion meanwhile, please feel free to respond!

Copy link
Member Author

Choose a reason for hiding this comment

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

I added some links below to the Neuxs API docs, but they didn't offer much assistance IMO. I updated the service impl. to throw InvalidResponse errors in each of those 2 circumstances while leaving the Joi schema a bit more flexible

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.

services/nexus/nexus.service.js Outdated Show resolved Hide resolved
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!

})
)

t.create('search release version of an inexistent artifact')
t.create('live: search release version of an inexistent 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 comes up a lot. Maybe we should add something to the test runner which includes [mock] next to all the ones which use mocks (or vice versa).

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. I saw the live: prefix convention somewhere the other day and started using it too without thinking about it.

services/nexus/nexus.tester.js Show resolved Hide resolved
@calebcartwright
Copy link
Member Author

Thanks for the feedback! I think I fixed each of the line items but let me know if I missed any

})
}

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.

}

if (!version) {
throw new InvalidResponse({ prettyMessage: 'invalid artifact version' })
Copy link
Member

@paulmelnikow paulmelnikow Dec 19, 2018

Choose a reason for hiding this comment

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

It's not necessary to change this, though if you move the error-checking bit back to handle(), you can use return statements above.

r: 'fs-public-snapshots',
v: 'LATEST',
c: 'agent-apple-osx',
p: 'tar.gz',
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

@paulmelnikow paulmelnikow left a comment

Choose a reason for hiding this comment

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

All looks good!

Only thing that needs more discussion is this item: #2520 (comment)

@paulmelnikow paulmelnikow merged commit 6d3798f into badges:master Dec 19, 2018
@shields-deployment
Copy link

This pull request was merged to master branch. This change is now waiting for deployment, which will usually happen within a few days. Stay tuned by joining our #ops channel on Discord!

After deployment, changes are copied to gh-pages branch:

@paulmelnikow
Copy link
Member

Thanks! This was a beast!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service-badge Accepted and actionable changes, features, and bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate failing nexus badge tests Support authentication for private nexus repo
3 participants