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

Add [dynamic-yaml] badge #1623

Merged
merged 11 commits into from
Apr 8, 2018
Merged

Add [dynamic-yaml] badge #1623

merged 11 commits into from
Apr 8, 2018

Conversation

jacobtomlinson
Copy link
Contributor

@jacobtomlinson jacobtomlinson commented Mar 29, 2018

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.

@shields-ci
Copy link

shields-ci commented Mar 29, 2018

Messages
📖

✨ Thanks for your contribution to Shields, @jacobtomlinson!

📖

Thanks for contributing to our documentation. We ❤️ our documentarians!

Generated by 🚫 dangerJS

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.

Thank you, this looks great! Main thing is to update the tests so they're exercising the code using a yaml data source.


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 });
Copy link
Member

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?

@@ -123,6 +123,9 @@ export default class Usage extends React.PureComponent {
<p>
<code>/badge/dynamic/json.svg?url=&lt;URL&gt;&amp;label=&lt;LABEL&gt;&amp;query=&lt;<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>&gt;&amp;colorB=&lt;COLOR&gt;&amp;prefix=&lt;PREFIX&gt;&amp;suffix=&lt;SUFFIX&gt;</code>
</p>
<p>
<code>/badge/dynamic/yaml.svg?url=&lt;URL&gt;&amp;label=&lt;LABEL&gt;&amp;query=&lt;<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>&gt;&amp;colorB=&lt;COLOR&gt;&amp;prefix=&lt;PREFIX&gt;&amp;suffix=&lt;SUFFIX&gt;</code>
</p>
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@paulmelnikow paulmelnikow changed the title Add dynamic yaml badge Add [dynamic-yaml] badge Mar 29, 2018
@jacobtomlinson
Copy link
Contributor Author

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.

@jacobtomlinson
Copy link
Contributor Author

I have updated the tests to use a YAML data source and added yaml to the list of options in the dynamic badge maker as requested.

Please let me know if there is anything else you need me to change.

@paulmelnikow
Copy link
Member

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')
Copy link
Member

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

Copy link
Contributor Author

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.

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')
Copy link
Member

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

Copy link
Contributor Author

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.

.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+)?$/)
Copy link
Member

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:

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

Copy link
Contributor Author

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>
Copy link
Member

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

Copy link
Contributor Author

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.


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({
Copy link
Member

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' });

Copy link
Contributor Author

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.

@@ -123,6 +123,9 @@ export default class Usage extends React.PureComponent {
<p>
<code>/badge/dynamic/json.svg?url=&lt;URL&gt;&amp;label=&lt;LABEL&gt;&amp;query=&lt;<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>&gt;&amp;colorB=&lt;COLOR&gt;&amp;prefix=&lt;PREFIX&gt;&amp;suffix=&lt;SUFFIX&gt;</code>
</p>
<p>
<code>/badge/dynamic/yaml.svg?url=&lt;URL&gt;&amp;label=&lt;LABEL&gt;&amp;query=&lt;<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>&gt;&amp;colorB=&lt;COLOR&gt;&amp;prefix=&lt;PREFIX&gt;&amp;suffix=&lt;SUFFIX&gt;</code>
Copy link
Member

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

Copy link
Contributor Author

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.

@jacobtomlinson
Copy link
Contributor Author

Thanks for the review comments @platan. I'll get this PR updated later on with the request changes.

@jacobtomlinson
Copy link
Contributor Author

@platan I have made the changes!

@platan
Copy link
Member

platan commented Mar 30, 2018

@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 invalid (https://github.com/badges/shields/blob/master/doc/service-tests.md#tutorial)? Currently we have such result for https://www.google.com/robots.txt
YAML:
image
JSON:
image
And for https://img.shields.io/badge/x-y-z.png
YAML:
image
JSON:
image
XML badge returns
image for both resources.

@paulmelnikow @RedSparr0w What do you think about this?

@jacobtomlinson
Copy link
Contributor Author

A consistent error message would be good. I think invalid would be fine but not sure if it accurately describes what the problem is. Perhaps something like invalid data source, parse error or load error would be more useful when troubleshooting.

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?

@platan
Copy link
Member

platan commented Mar 31, 2018

@jacobtomlinson I agree with you, invalid data source or parse error is better than invalid. In my opinion we can merge this PR and prepare a separate one for handling parsing errors. Let's wait for opinion of the rest of the team.

@RedSparr0w
Copy link
Member

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

@paulmelnikow
Copy link
Member

My preference would be invalid json / invalid xml / invalid yaml which is terse and consistent with other badges.

@platan
Copy link
Member

platan commented Apr 1, 2018

@RedSparr0w or @paulmelnikow can you approve this PR? Then we could merge it and new PR with a better error message can be raised.

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.

Left two minor comments. @platan feel free to give it the final 👍 and merge. Thanks!

test(dynamicBadgeUrl, () => {
const yamlUrl = 'http://example.com/foo.yaml';
const query = '$.bar';
const prefix = 'value: ';
Copy link
Member

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.

Copy link
Member

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'
Copy link
Member

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?

Copy link
Member

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?

Copy link
Contributor Author

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.

@paulmelnikow paulmelnikow added the service-badge New or updated service badge label Apr 2, 2018
@jacobtomlinson
Copy link
Contributor Author

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 npm install again to generate a new package-lock.json which resolves the merge conflicts it was complaining about.

Should be good to merge now!

@platan platan self-assigned this Apr 3, 2018
@platan
Copy link
Member

platan commented Apr 3, 2018

@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.
@jacobtomlinson @paulmelnikow What do you think about this? Maybe this depends on a NPM version? Should we stick to a specific version of NPM?

@jacobtomlinson
Copy link
Contributor Author

jacobtomlinson commented Apr 3, 2018 via email

@jacobtomlinson
Copy link
Contributor Author

What action needs to happen here to get this merged?

@platan platan merged commit d56b696 into badges:master Apr 8, 2018
@platan
Copy link
Member

platan commented Apr 8, 2018

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

@jacobtomlinson jacobtomlinson deleted the dynamic-yaml branch April 9, 2018 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service-badge New or updated service badge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add yaml dynamic badge
5 participants