-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add [dynamic-yaml] badge #1623
Add [dynamic-yaml] badge #1623
Conversation
Generated by 🚫 dangerJS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, this looks great! Main thing is to update the tests so they're exercising the code using a yaml data source.
services/yaml/yaml.tester.js
Outdated
|
||
t.create('YAML from url') | ||
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$.name&style=_shields_test') | ||
.expectJSON({ name: 'custom badge', value: 'gh-badges', colorB: colorsB.brightgreen }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't these tests need to pull from a yml file instead of a json file?
frontend/components/usage.js
Outdated
@@ -123,6 +123,9 @@ export default class Usage extends React.PureComponent { | |||
<p> | |||
<code>/badge/dynamic/json.svg?url=<URL>&label=<LABEL>&query=<<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>>&colorB=<COLOR>&prefix=<PREFIX>&suffix=<SUFFIX></code> | |||
</p> | |||
<p> | |||
<code>/badge/dynamic/yaml.svg?url=<URL>&label=<LABEL>&query=<<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>>&colorB=<COLOR>&prefix=<PREFIX>&suffix=<SUFFIX></code> | |||
</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add yaml
to the list in the dynamic badge maker?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
I thought it was valid to leave the JSON tests as JSON is valid YAML. I can definitely add more which explicitly tests the YAML syntax. |
I have updated the tests to use a YAML data source and added Please let me know if there is anything else you need me to change. |
JSON is valid YAML?! Mind = blown 😀 Thanks for the updates. I'll give this another read, and perhaps someone else would like to as well. |
.expectJSON({ name: 'Package Name', value: 'no url specified', colorB: colorsB.red }); | ||
|
||
t.create('No query specified') | ||
.get('.json?url=https://raw.githubusercontent.com/kubernetes/charts/568291d6e476c39ca8322c30c3f601d0383d4760/stable/coredns/Chart.yaml&label=Package Name&style=_shields_test') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've used a link to specified commit, which great in my opinion :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I thought it would be consistent when testing.
services/yaml/yaml.tester.js
Outdated
module.exports = t; | ||
|
||
t.create('Connection error') | ||
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$.name&label=Package Name&style=_shields_test') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you, just for the sake of a consistency, change an uri value to a yaml resource (from examples below)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops must've missed that one.
services/yaml/yaml.tester.js
Outdated
.get('.json?url=https://raw.githubusercontent.com/kubernetes/charts/568291d6e476c39ca8322c30c3f601d0383d4760/stable/coredns/Chart.yaml&query=$.version') | ||
.expectJSONTypes(Joi.object().keys({ | ||
name: 'custom badge', | ||
value: Joi.string().regex(/^\d+(\.\d+)?(\.\d+)?$/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use an isSemver
validator here:
shields/services/xml/xml.tester.js
Lines 55 to 57 in 66c5d91
.expectJSONTypes(Joi.object().keys({ | |
name: 'custom badge', | |
value: isSemver |
Or even better you can change expect to
.expectJSON({ name: 'custom badge', value: '0.8.0' });
since we are using resource from a specific commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just copied the test from the json dynamic badge. Happy to update if you prefer.
@@ -42,6 +42,7 @@ export default class DynamicBadgeMaker extends React.Component { | |||
onChange={event => this.setState({ datatype: event.target.value })}> | |||
<option value="" disabled>data type</option> | |||
<option value="json">json</option> | |||
<option value="yaml">yaml</option> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we sort them in an alphabetical order (json, xml, yaml)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See other comment about order.
services/yaml/yaml.tester.js
Outdated
|
||
t.create('YAML from url | with prefix & suffix & label') | ||
.get('.json?url=https://raw.githubusercontent.com/kubernetes/charts/568291d6e476c39ca8322c30c3f601d0383d4760/stable/coredns/Chart.yaml&query=$.version&prefix=v&suffix= dev&label=Shields') | ||
.expectJSONTypes(Joi.object().keys({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you thin about using this approach?
.expectJSON({ name: 'Shields', value: 'v0.8.0 dev' });
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much neater, again I just copied the other test.
frontend/components/usage.js
Outdated
@@ -123,6 +123,9 @@ export default class Usage extends React.PureComponent { | |||
<p> | |||
<code>/badge/dynamic/json.svg?url=<URL>&label=<LABEL>&query=<<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>>&colorB=<COLOR>&prefix=<PREFIX>&suffix=<SUFFIX></code> | |||
</p> | |||
<p> | |||
<code>/badge/dynamic/yaml.svg?url=<URL>&label=<LABEL>&query=<<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>>&colorB=<COLOR>&prefix=<PREFIX>&suffix=<SUFFIX></code> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we sort them in an alphabetical order (json, xml, yaml)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes sure. I went with this order because the usage of json and yaml are the same and so thought I would keep them together.
Thanks for the review comments @platan. I'll get this PR updated later on with the request changes. |
@platan I have made the changes! |
@jacobtomlinson thanks for fixes. That was quick! The code looks great in my opinion. I have one extra thing. What would we like to see as a badge value when resource is not a valid yaml file? Something simple like @paulmelnikow @RedSparr0w What do you think about this? |
A consistent error message would be good. I think Happy to go with whatever you think. Do you want me to implement it for all three dynamic badges in this PR or raise a new PR for it? |
@jacobtomlinson I agree with you, |
Sounds good to me, I originally left it as the default error message so the user could try and fix the badge/problem themselves, but I think something like 'invalid json', 'invalid source', 'parse error' would work better |
My preference would be invalid json / invalid xml / invalid yaml which is terse and consistent with other badges. |
@RedSparr0w or @paulmelnikow can you approve this PR? Then we could merge it and new PR with a better error message can be raised. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left two minor comments. @platan feel free to give it the final 👍 and merge. Thanks!
frontend/lib/badge-url.spec.js
Outdated
test(dynamicBadgeUrl, () => { | ||
const yamlUrl = 'http://example.com/foo.yaml'; | ||
const query = '$.bar'; | ||
const prefix = 'value: '; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't these three tests of dynamicBadgeUrl
testing exactly the same thing? Maybe we should strip out two of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Let's remove two of them.
server.js
Outdated
case 'yaml': | ||
requestOptions = { | ||
headers: { | ||
Accept: 'application/yaml, text/yaml, text/plain' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From this Quora answer it seems we probably should use 'application/x-yaml, text/x-yaml, text/plain'
here. Unless someone has a better source saying something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
github.com, search in code:
"text/x-yaml" content type
39,389 code results"text/yaml" content type
79,485 code results"application/x-yaml" content type
34,264 code results"application/yaml" content type
21,948 code results
Should we add all of them since it's not standardized?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's probably best.
I have removed the extra tests and added the MIME types as suggested by @paulmelnikow. I have also merged in the latest master and run Should be good to merge now! |
@jacobtomlinson thanks. Code looks OK. I have one doubt about package-lock.json. In all dependencies of fsevents "bundled" (https://docs.npmjs.com/files/package-lock.json#bundled) property is replaced with "resolved" and "integrity". I'm not sure if this change changes anything. 4 months ago we had change from "resolved"+"integrity" to "bundled" 0da212b. |
I don't feel I can add useful input here. To be honest this is the first
project I've ever seen which tracks the package lock, I've always seen it
in the `.gitignore`.
Happy to change it however you wish.
|
What action needs to happen here to get this merged? |
@jacobtomlinson Thanks for your contribution and your patience 👍 Would you like to prepare a next PR with an error message when data source is invalid? You could try with messages proposed by Paul (#1623 (comment)). @RedSparr0w Thank you for a review. |
This PR closes #1617 by adding support for a dynamic YAML badge.
I have added
js-yaml
to handle the YAML parsing, but then the rest of the work has mostly been duplicating the JSON dynamic badge as YAML is just a superset of JSON.I've tested it locally and run the test suite with no issues showing in the tests I added.
Please let me know if there is anything you want me to change.