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

Rewrite the [DynamicJson] badge; affects [gitlab dynamic-xml dynamic-yaml] #2399

Merged
merged 9 commits into from
Dec 6, 2018

Conversation

paulmelnikow
Copy link
Member

@paulmelnikow paulmelnikow commented Nov 26, 2018

This starts the rewrite of the dynamic badges. I've pulled into BaseService an initial version of the query param validation from #2325.

I've extended from BaseJsonService to avoid duplicating the deserialization logic, though it means there is a bit of duplicated code among the three dynamic services. The way to unravel this would be to move the logic from _requestJson and friends from the base classes into functions so DynamicJson can inherit from BaseDynamic. Would that be worth it?

Marked blocker since it affects other code that may validate query parameters (e.g. #2377), the other dynamic badges, and further cleanup of server.js.

This introduces a regression of #1446 for this badge.

Ref: #2345 (partial fix)

@paulmelnikow paulmelnikow added service-badge Accepted and actionable changes, features, and bugs core Server, BaseService, GitHub auth blocker PRs and epics which block other work labels Nov 26, 2018
@shields-ci
Copy link

Warnings
⚠️ This PR modified service code for gitlab but not its test code.
That's okay so long as it's refactoring existing code.
Messages
📖 ✨ Thanks for your contribution to Shields, @paulmelnikow!

Generated by 🚫 dangerJS

@paulmelnikow paulmelnikow changed the title Rewrite the [DynamicJson] badge; affects [gitlab] Rewrite the [DynamicJson] badge; affects [gitlab dynamic-xml dynamic-yaml] Nov 26, 2018
@paulmelnikow paulmelnikow temporarily deployed to shields-staging-pr-2399 November 26, 2018 21:52 Inactive
@paulmelnikow paulmelnikow temporarily deployed to shields-staging-pr-2399 December 3, 2018 03:08 Inactive
@paulmelnikow paulmelnikow temporarily deployed to shields-staging-pr-2399 December 3, 2018 23:06 Inactive
@paulmelnikow
Copy link
Member Author

The way to unravel this would be to move the logic from _requestJson and friends from the base classes into functions so DynamicJson can inherit from BaseDynamic. Would that be worth it?

I've found another case where the coupling of the request logic to the base class is causing problems. #1721 is about using github auth on a particular request, but only when static auth is present (i.e. for self-hosting). So I want the logic from BaseGithubAuthService if I'm using static auth, and otherwise what's in BaseJsonService.

If these functions were extracted to separate modules, it might leave open more options open for handling this unusual case.

value: 'gh-badges',
colorB: colorsB.brightgreen,
value: 'shields.io',
colorB: 'lightgreen',
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 Author

Choose a reason for hiding this comment

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

Oops! Thanks for catching that.

@chris48s
Copy link
Member

chris48s commented Dec 6, 2018

@paulmelnikow
Copy link
Member Author

Yes, exactly right.

Have you have any thoughts on how we might solve that? (if its large, it doesn't necessarily have to go into this PR)

I suppose we'll need to add an 'isError' field to serviceData. It can be set automatically in _handleError(), and manually in handle() for rare case like Crates. Then add logic in _makeBadgeData() to handle it.

shields/services/base.js

Lines 392 to 437 in 8070f0b

static _makeBadgeData(overrides, serviceData) {
const {
style,
label: overrideLabel,
logo: overrideLogo,
logoColor: overrideLogoColor,
logoWidth: overrideLogoWidth,
link: overrideLink,
colorA: overrideColorA,
colorB: overrideColorB,
} = overrides
const {
label: serviceLabel,
message: serviceMessage,
color: serviceColor,
link: serviceLink,
} = serviceData
const {
color: defaultColor,
logo: defaultLogo,
label: defaultLabel,
} = this.defaultBadgeData
const badgeData = {
text: [
// Use `coalesce()` to support empty labels and messages, as in the
// static badge.
coalesce(overrideLabel, serviceLabel, defaultLabel, this.category),
coalesce(serviceMessage, 'n/a'),
],
template: style,
logo: makeLogo(style === 'social' ? defaultLogo : undefined, {
logo: overrideLogo,
logoColor: overrideLogoColor,
}),
logoWidth: +overrideLogoWidth,
links: toArray(overrideLink || serviceLink),
colorA: makeColor(overrideColorA),
}
const color = overrideColorB || serviceColor || defaultColor || 'lightgrey'
setBadgeColor(badgeData, color)
return badgeData
}

I'd like to fix this, though it's a bit fiddly. There are possible merge conflicts to contend with in base.js in #2441. It's a minor enough regression that I think we can do it in a follow on. If this bug were important it wouldn't have been open almost a year. 😉

The test I disabled here really belongs in base.spec.js, and should get moved as part of #1446.

@paulmelnikow paulmelnikow temporarily deployed to shields-staging-pr-2399 December 6, 2018 21:11 Inactive
@paulmelnikow paulmelnikow temporarily deployed to shields-staging-pr-2399 December 6, 2018 21:12 Inactive
@paulmelnikow paulmelnikow temporarily deployed to shields-staging-pr-2399 December 6, 2018 21:22 Inactive
@paulmelnikow
Copy link
Member Author

Gitlab failure seems transient.

@chris48s
Copy link
Member

chris48s commented Dec 6, 2018

for rare case like Crates

Hrm. On reflection, I think with crates what I did in PR #2462 was not the best way to do it. Perhaps that case would be better handled by throwing a custom ShieldsRuntimeError more like this situation:

if (Object.keys(json).length === 0) {
/* Note the 'not found' response from cdnjs is:
status code = 200, body = {} */
throw new NotFound()
}

If we say we should always be throwing an exception for cases like this, would that allow us to handle this issue one layer further out (where we catch the exceptions and wrap them in a badge)?

There are possible merge conflicts to contend with
It's a minor enough regression that I think we can do it in a follow on

Agreed - its just useful to think about how to do that, given its a bit fiddly

@paulmelnikow
Copy link
Member Author

If we say we should always be throwing an exception for cases like this, would that allow us to handle this issue one layer further out (where we catch the exceptions and wrap them in a badge)?

We apply overrides further out than handling errors, which probably makes sense… since on error we should still respect e.g. ?style=flat-square. Probably there are places even further up, like cache handling, where we may want to consider whether or not it was an error.

Custom ShieldsRuntimeError seems nice; it could let us attach the error to what gets bubbled up to handleRequest, which could let us adjust the cache settings appropriately for probably recoverable vs probably permanent errors.

Probably we could catch ShieldsRuntimeError here…

shields/services/base.js

Lines 330 to 332 in b2d5d3e

error instanceof InvalidResponse ||
error instanceof Inaccessible ||
error instanceof Deprecated

@paulmelnikow paulmelnikow merged commit 6a737b7 into master Dec 6, 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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker PRs and epics which block other work core Server, BaseService, GitHub auth service-badge Accepted and actionable changes, features, and bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants