Skip to content

Commit

Permalink
Fixes and tests for BitBucket + GitHub (#1315)
Browse files Browse the repository at this point in the history
* Add test suite for BitBucket service integration
* Present BitBucket issues as a metric (for consistency with BitBucket PR endpoint and GitHub endpoints)
* Factor out a shared regex
* Fail cleanly if count is `undefined`
  • Loading branch information
chris48s authored and paulmelnikow committed Dec 5, 2017
1 parent 58437ec commit 433d69b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 8 deletions.
20 changes: 17 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4250,11 +4250,18 @@ cache(function(data, match, sendBadge, request) {
try {
var data = JSON.parse(buffer);
var issues = data.count;
badgeData.text[1] = issues + (isRaw? '': ' open');
if (res.statusCode !== 200) {
throw Error('Failed to count issues.');
}
badgeData.text[1] = metric(issues) + (isRaw? '': ' open');
badgeData.colorscheme = issues ? 'yellow' : 'brightgreen';
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
if (res.statusCode === 404) {
badgeData.text[1] = 'not found';
} else {
badgeData.text[1] = 'invalid';
}
sendBadge(format, badgeData);
}
});
Expand All @@ -4281,11 +4288,18 @@ cache(function(data, match, sendBadge, request) {
try {
var data = JSON.parse(buffer);
var pullrequests = data.size;
if (res.statusCode !== 200) {
throw Error('Failed to count pull requests.');
}
badgeData.text[1] = metric(pullrequests) + (isRaw? '': ' open');
badgeData.colorscheme = (pullrequests > 0)? 'yellow': 'brightgreen';
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
if (res.statusCode === 404) {
badgeData.text[1] = 'not found';
} else {
badgeData.text[1] = 'invalid';
}
sendBadge(format, badgeData);
}
});
Expand Down
97 changes: 97 additions & 0 deletions service-tests/bitbucket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const {
isMetric,
isMetricOpenIssues
} = require('./helpers/validators.js');

const t = new ServiceTester({ id: 'bitbucket', title: 'BitBucket badges' });
module.exports = t;


// tests for issues endpoints

t.create('issues-raw (valid)')
.get('/issues-raw/atlassian/python-bitbucket.json')
.expectJSONTypes(Joi.object().keys({
name: 'issues',
value: isMetric
}));

t.create('issues-raw (not found)')
.get('/issues-raw/atlassian/not-a-repo.json')
.expectJSON({ name: 'issues', value: 'not found' });

t.create('issues-raw (invalid)')
.get('/issues-raw/chris48s/example-private-repo.json')
.expectJSON({ name: 'issues', value: 'invalid' });

t.create('issues-raw (connection error)')
.get('/issues-raw/atlassian/python-bitbucket.json')
.networkOff()
.expectJSON({ name: 'issues', value: 'inaccessible' });

t.create('issues (valid)')
.get('/issues/atlassian/python-bitbucket.json')
.expectJSONTypes(Joi.object().keys({
name: 'issues',
value: isMetricOpenIssues
}));

t.create('issues (not found)')
.get('/issues/atlassian/not-a-repo.json')
.expectJSON({ name: 'issues', value: 'not found' });

t.create('issues (invalid)')
.get('/issues/chris48s/example-private-repo.json')
.expectJSON({ name: 'issues', value: 'invalid' });

t.create('issues (connection error)')
.get('/issues/atlassian/python-bitbucket.json')
.networkOff()
.expectJSON({ name: 'issues', value: 'inaccessible' });


// tests for pull requests endpoints

t.create('pr-raw (valid)')
.get('/pr-raw/atlassian/python-bitbucket.json')
.expectJSONTypes(Joi.object().keys({
name: 'pull requests',
value: isMetric
}));

t.create('pr-raw (not found)')
.get('/pr-raw/atlassian/not-a-repo.json')
.expectJSON({ name: 'pull requests', value: 'not found' });

t.create('pr-raw (invalid)')
.get('/pr-raw/chris48s/example-private-repo.json')
.expectJSON({ name: 'pull requests', value: 'invalid' });

t.create('pr-raw (connection error)')
.get('/pr-raw/atlassian/python-bitbucket.json')
.networkOff()
.expectJSON({ name: 'pull requests', value: 'inaccessible' });

t.create('pr (valid)')
.get('/pr/atlassian/python-bitbucket.json')
.expectJSONTypes(Joi.object().keys({
name: 'pull requests',
value: isMetricOpenIssues
}));

t.create('pr (not found)')
.get('/pr/atlassian/not-a-repo.json')
.expectJSON({ name: 'pull requests', value: 'not found' });

t.create('pr (invalid)')
.get('/pr/chris48s/example-private-repo.json')
.expectJSON({ name: 'pull requests', value: 'invalid' });

t.create('pr (connection error)')
.get('/pr/atlassian/python-bitbucket.json')
.networkOff()
.expectJSON({ name: 'pull requests', value: 'inaccessible' });
11 changes: 6 additions & 5 deletions service-tests/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const {
isMetric,
isMetricOpenIssues,
isMetricOverTimePeriod,
isFileSize,
isFormattedDate,
Expand Down Expand Up @@ -42,7 +43,7 @@ t.create('GitHub pull requests')
.get('/issues-pr/badges/shields.json')
.expectJSONTypes(Joi.object().keys({
name: 'pull requests',
value: Joi.string().regex(/^[0-9]+[kMGTPEZY]? open$/)
value: isMetricOpenIssues
}));

t.create('GitHub pull requests raw')
Expand Down Expand Up @@ -70,7 +71,7 @@ t.create('GitHub open issues')
.get('/issues/badges/shields.json')
.expectJSONTypes(Joi.object().keys({
name: 'issues',
value: Joi.string().regex(/^[0-9]+[kMGTPEZY]? open$/)
value: isMetricOpenIssues
}));

t.create('GitHub open issues raw')
Expand All @@ -81,14 +82,14 @@ t.create('GitHub open issues by label is > zero')
.get('/issues/badges/shields/service-badge.json')
.expectJSONTypes(Joi.object().keys({
name: 'service-badge issues',
value: Joi.string().regex(/^[1-9][0-9]*[kMGTPEZY]? open$/)
value: isMetricOpenIssues
}));

t.create('GitHub open issues by multi-word label is > zero')
.get('/issues/Cockatrice/Cockatrice/App%20-%20Cockatrice.json')
.expectJSONTypes(Joi.object().keys({
name: '"App - Cockatrice" issues',
value: Joi.string().regex(/^[1-9][0-9]*[kMGTPEZY]? open$/)
value: isMetricOpenIssues
}));

t.create('GitHub open issues by label (raw)')
Expand All @@ -102,7 +103,7 @@ t.create('GitHub open pull requests by label')
.get('/issues-pr/badges/shields/service-badge.json')
.expectJSONTypes(Joi.object().keys({
name: 'service-badge pull requests',
value: Joi.string().regex(/^[0-9]+[kMGTPEZY]? open$/)
value: isMetricOpenIssues
}));

t.create('GitHub open pull requests by label (raw)')
Expand Down
3 changes: 3 additions & 0 deletions service-tests/helpers/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const isStarRating = withRegex(/^(?=.{5}$)(\u2605{0,5}[\u00BC\u00BD\u00BE]?\u260
// Required to be > 0, beacuse accepting zero masks many problems.
const isMetric = withRegex(/^[1-9][0-9]*[kMGTPEZY]?$/);

const isMetricOpenIssues = withRegex(/^[1-9][0-9]*[kMGTPEZY]? open$/);

const isMetricOverTimePeriod = withRegex(/^[1-9][0-9]*[kMGTPEZY]?\/(year|month|4 weeks|week|day)$/);

const isPercentage = withRegex(/^[0-9]+%$/);
Expand All @@ -55,6 +57,7 @@ module.exports = {
isComposerVersion,
isStarRating,
isMetric,
isMetricOpenIssues,
isMetricOverTimePeriod,
isPercentage,
isFileSize,
Expand Down

0 comments on commit 433d69b

Please sign in to comment.