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

[badge/dynamic/json] fix colorscheme on error #1445

Merged
merged 10 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7606,6 +7606,7 @@ cache({
var badgeData = getBadgeData('custom badge', query);

if (!query.uri){
setBadgeColor(badgeData, 'red');
badgeData.text[1] = 'no uri specified';
sendBadge(format, badgeData);
return;
Expand All @@ -7620,14 +7621,20 @@ cache({
if (err != null || !res || res.statusCode !== 200)
throw 'inaccessible';

badgeData.colorscheme = 'brightgreen';

switch (type){
case 'json':
data = (typeof data == 'object' ? data : JSON.parse(data));
badgeData.text[1] = (prefix || '') + jp.query(data, pathExpression).join(', ') + (suffix || '');
var jsonpath = jp.query(data, pathExpression);
if (!jsonpath.length)
Copy link
Member

Choose a reason for hiding this comment

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

For fewer surprises, would it be better to use Array.isArray?

Copy link
Member Author

Choose a reason for hiding this comment

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

From some testing it seems jsonpath will always return an array,
If the query finds nothing, jsonpath will just return an empty array []

examples based on the following json

{
  name: 'project',
  version: '1.2.3'
}
?query= jsonpath returned
$.version ['1.2.3']
$.notfound []

throw 'no result';
var innerText = jsonpath.join(', ');
badgeData.text[1] = (prefix || '') + innerText + (suffix || '');
break;
}
} catch(e) {
badgeData.colorB = 'lightgrey';
setBadgeColor(badgeData, 'lightgrey');
badgeData.text[1] = e;
} finally {
sendBadge(format, badgeData);
Expand Down
36 changes: 26 additions & 10 deletions service-tests/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const colorscheme = require('../lib/colorscheme.json');
const mapValues = require('lodash.mapvalues');

const colorsB = mapValues(colorscheme, 'colorB');

const t = new ServiceTester({ id: 'badge/dynamic/json', title: 'User Defined JSON Source Data' });
module.exports = t;

t.create('Connection error')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.name&label=Package Name')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.name&label=Package Name&style=_shields_test')
.networkOff()
.expectJSON({ name: 'Package Name', value: 'inaccessible' });
.expectJSON({ name: 'Package Name', value: 'inaccessible', colorB: colorsB.lightgrey });

t.create('No URI specified')
.get('.json?query=$.name&label=Package Name')
.expectJSON({ name: 'Package Name', value: 'no uri specified' });
.get('.json?query=$.name&label=Package Name&style=_shields_test')
.expectJSON({ name: 'Package Name', value: 'no uri specified', colorB: colorsB.red });

t.create('JSON from uri')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.name')
.expectJSON({ name: 'custom badge', value: 'gh-badges'});
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.name&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'gh-badges', colorB: colorsB.brightgreen });

t.create('JSON from uri | caching with new query params')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.version')
Expand All @@ -34,9 +38,21 @@ t.create('JSON from uri | with prefix & suffix & label')
}));

t.create('JSON from uri | object doesnt exist')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.does_not_exist')
.expectJSON({ name: 'custom badge', value: '' });
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.does_not_exist&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'no result', colorB: colorsB.lightgrey });

t.create('JSON from uri | invalid uri')
.get('.json?uri=https://github.com/badges/shields/raw/master/notafile.json&query=$.version')
.expectJSON({ name: 'custom badge', value: 'invalid resource' });
.get('.json?uri=https://github.com/badges/shields/raw/master/notafile.json&query=$.version&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'invalid resource', colorB: colorsB.lightgrey });

t.create('JSON from uri | user color overrides default')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.name&colorB=10ADED&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'gh-badges', colorB: '#10ADED' });

t.create('JSON from uri | error color overrides default')
.get('.json?uri=https://github.com/badges/shields/raw/master/notafile.json&query=$.version&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'invalid resource', colorB: colorsB.lightgrey });

t.create('JSON from uri | error color overrides user specified')
.get('.json?query=$.version&colorB=10ADED&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'no uri specified', colorB: colorsB.red });