-
-
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
[badge/dynamic/json] User defined JSON source badge #820
Conversation
…_source # Conflicts: # package.json
@espadrine Any chance i could get this reviewed and hopefully merged? 😄 |
The test framework is merged. Would you be willing to add tests? https://github.com/badges/shields/tree/master/service-tests |
What about using a standard syntax such as JSONPath (for JSON) and XPath (for XML) for this, rather than inventing a new one? You can use a library like https://yarnpkg.com/en/package/jsonpath to evaluate a JSONPath query. This would also allow more complex expressions, like getting the final item in an array. |
@paulmelnikow @Daniel15 var items = decodeURI(match[3]).split('=>');
items.forEach(function(item){
data = data[decodeURI(item)];
}); |
For JSON, another option I just learned about is jq, which is sort of like awk for json. It could yield more power, such as conditional expressions, and counting the elements in a list. |
@RedSparr0w would you have some time to address these comments? This is an awesome feature and it would be great to get it merged. Also, if anyone would like to adopt this PR, please feel free to do so: grab the branch, add more commits addressing the comments, and open a new PR referencing this one. Will close in one month if no one has done so. |
Should be able to create the test for this either today/tomorrow, |
@paulmelnikow Added tests, |
91f8848
to
ad44684
Compare
Travis seems to be failing with the following
I assume i just need to change the PR title? |
ad44684
to
9659051
Compare
Thanks so much for reviving this! I will review within the next few days, and look into JSONPath/XPath/iq as soon as I can. |
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.
Looks good!
frontend/fragments/try-footer.html
Outdated
@@ -448,6 +471,17 @@ <h2 id="like-this"> Like This? </h2> | |||
document.location = url; | |||
} | |||
|
|||
function generateDynamicImage() { | |||
var url = '/badge/dynamic/' + escapeField(dynamicImageMaker.type.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.
I think you want to start with the var url = document.getElementById('imgUrlPrefix').textContent
line from makeImage()
above. Otherwise it will try to serve from shields.io
instead of img.shields.io
in production.
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.
To test this you can run make website
. The dynamic badge won't work of course, though you should be able to confirm that it's generating URLs that begin with https://img.shields.io/
.
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.
Ah did not think about that, i updated it to match the static badge function.
service-tests/json.js
Outdated
.expectJSONTypes(Joi.object().keys({ | ||
name: Joi.equal('custom badge'), | ||
value: Joi.equal('') | ||
})); |
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.
Since these are both literals, you can simplify this:
.expectJSON({ name: 'custom badge', 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.
Nice catch, did not see that.
service-tests/json.js
Outdated
t.create('JSON from uri | with prefix & suffix & label') | ||
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.version&prefix=v&suffix= dev&label=Shields') | ||
.expectJSONTypes(Joi.object().keys({ | ||
name: Joi.equal('Shields'), |
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 can replace Joi.equal('Shields')
with a bare literal 'Shields'
.
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 to know!
812565b
to
ea0619c
Compare
Not sure why i had to resolve the conflicts twice, |
Looks good to me! |
Thanks! |
🎃 |
Are there any plans to expand the feature to the initial idea with json and xml support? @RedSparr0w By the way, could you please update the starting post of this pr accordingly. You still mention xml there but drop it on the way. That could be confusing if somebody finds this old (and big pr with a lot of comments!). |
@RedSparr0w Thanks to everyone for this very useful project. I have a requirement for the |
Hey @reddog could you open a new issue to discuss? |
@paulmelnikow Opened at #1480 |
There used to be a very helpful link (actually two), to pages that let's you check and build your query. Can you please readd them to the starting post @RedSparr0w ? I used http://www.jsonquerytool.com/ in the meantime. |
@tooomm i believe it may have been: http://jsonpath.com/ , Does this look right? |
Yes, I remember that one! Thank you. 👍 |
No worries, will update the main post to include the link. |
Is there a way to make the colorB also dynamic? Right now I think it has to be passed as hex value in URL. Loading it from the JSON/XML would solve the problem but I'm not sure that is possible. Any hints? |
Not currently unfortunately, But #1525 will add something in line with what you are wanting 😄 |
@RedSparr0w thanks, I'll wait for it to be merged. |
Usage
Allow users to specify their own API/source of json data.
JSONPath query helper
Should also help to minimize the amount of new badges requested as users could easily create a 1 off badge for themselves.
Format
Badge url:
/badge/dynamic/json.svg?
?a=b
paramsuri
(required)uri=http://path.to.json
query
(required)query=$.version
label
label=ProjectX
colorB
colorB=10ADED
prefix
prefix=v
suffix
suffix=%20dev
logo
logo=twitter
style
style=for-the-badge
Example badges:
JSON
url:
https://img.shields.io/badge/dynamic/json.svg?uri=https://raw.githubusercontent.com/badges/shields/master/package.json&query=$.version&label=Shields.io&prefix=v&suffix=-master&colorB=10ADED&style=flat-square
badge:
Closes #548.