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

* [FIX] error colorscheme

* throw error if jsonpath query non existent

* fixup

* show brightgreen badge by default

* update test

* let -> var

* set lightgrey when no uri specified

* update tests

* red color for no uri specified

* dynamic badge use setBadgecolor()
This commit is contained in:
Danial
2018-01-16 13:53:30 +13:00
committed by GitHub
parent e1f0e2598d
commit 65e1d69f53
2 changed files with 35 additions and 12 deletions

View File

@@ -7612,6 +7612,7 @@ cache({
var badgeData = getBadgeData('custom badge', query);
if (!query.uri){
setBadgeColor(badgeData, 'red');
badgeData.text[1] = 'no uri specified';
sendBadge(format, badgeData);
return;
@@ -7626,14 +7627,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)
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);

View File

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