Skip to content

Commit

Permalink
Rewrite [appveyor] tests badge (#1940)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow authored Aug 20, 2018
1 parent 77061fe commit 656326d
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 127 deletions.
8 changes: 0 additions & 8 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,6 @@ const allBadgeExamples = [
keywords: ['teamcity'],
exampleUri: '/teamcity/http/teamcity.jetbrains.com/e/bt345.svg',
},
{
title: 'AppVeyor tests',
previewUri: '/appveyor/tests/NZSmartie/coap-net-iu0to.svg',
},
{
title: 'AppVeyor tests branch',
previewUri: '/appveyor/tests/NZSmartie/coap-net-iu0to/master.svg',
},
{
title: 'Buildkite',
previewUri:
Expand Down
53 changes: 0 additions & 53 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,59 +647,6 @@ cache(function (data, match, sendBadge, request) {
});
}));

// AppVeyor test status integration.
camp.route(/^\/appveyor\/tests\/([^/]+\/[^/]+)(?:\/(.+))?\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var repo = match[1]; // eg, `gruntjs/grunt`.
var branch = match[2];
var format = match[3];
var apiUrl = 'https://ci.appveyor.com/api/projects/' + repo;
if (branch != null) {
apiUrl += '/branch/' + branch;
}
var badgeData = getBadgeData('tests', data);
request(apiUrl, { headers: { 'Accept': 'application/json' } }, function(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
if (res.statusCode === 404) {
badgeData.text[1] = 'project not found or access denied';
sendBadge(format, badgeData);
return;
}
var data = JSON.parse(buffer);
var testsTotal = data.build.jobs.reduce((currentValue, job) => currentValue + job.testsCount, 0);
var testsPassed = data.build.jobs.reduce((currentValue, job) => currentValue + job.passedTestsCount, 0);
var testsFailed = data.build.jobs.reduce((currentValue, job) => currentValue + job.failedTestsCount, 0);
var testsSkipped = testsTotal - testsPassed - testsFailed;

if (testsPassed == testsTotal) {
badgeData.colorscheme = 'brightgreen';
} else if (testsFailed == 0 ) {
badgeData.colorscheme = 'green';
} else if (testsPassed == 0 ) {
badgeData.colorscheme = 'red';
} else{
badgeData.colorscheme = 'orange';
}

badgeData.text[1] = testsPassed + ' passed';
if (testsFailed > 0)
badgeData.text[1] += ', ' + testsFailed + ' failed';
if (testsSkipped > 0)
badgeData.text[1] += ', ' + testsSkipped + ' skipped';

sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// Old url for CodeBetter TeamCity instance.
camp.route(/^\/teamcity\/codebetter\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
Expand Down
44 changes: 44 additions & 0 deletions services/appveyor/appveyor-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const Joi = require('joi')
const BaseJsonService = require('../base-json')
const { nonNegativeInteger } = require('../validators')

const schema = Joi.object({
build: Joi.object({
status: Joi.string().required(),
jobs: Joi.array()
.items({
testsCount: nonNegativeInteger,
passedTestsCount: nonNegativeInteger,
failedTestsCount: nonNegativeInteger,
})
.required(),
}),
}).required()

module.exports = class AppVeyorBase extends BaseJsonService {
static get category() {
return 'build'
}

async fetch({ repo, branch }) {
let url = `https://ci.appveyor.com/api/projects/${repo}`
if (branch != null) {
url += `/branch/${branch}`
}
return this._requestJson({
schema,
url,
errorMessages: { 404: 'project not found or access denied' },
})
}

static buildUrl(base) {
return {
base,
format: '([^/]+/[^/]+)(?:/(.+))?',
capture: ['repo', 'branch'],
}
}
}
39 changes: 39 additions & 0 deletions services/appveyor/appveyor-ci.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

const AppVeyorBase = require('./appveyor-base')

module.exports = class AppVeyorCi extends AppVeyorBase {
static get url() {
return this.buildUrl('appveyor/ci')
}

static get examples() {
return [
{
title: 'AppVeyor',
previewUrl: 'gruntjs/grunt',
},
{
title: 'AppVeyor branch',
previewUrl: 'gruntjs/grunt/master',
},
]
}

static render({ status }) {
if (status === 'success') {
return { message: 'passing', color: 'brightgreen' }
} else if (status !== 'running' && status !== 'queued') {
return { message: 'failing', color: 'red' }
} else {
return { message: status }
}
}

async handle({ repo, branch }) {
const {
build: { status },
} = await this.fetch({ repo, branch })
return this.constructor.render({ status })
}
}
69 changes: 69 additions & 0 deletions services/appveyor/appveyor-tests.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'

const AppVeyorBase = require('./appveyor-base')

module.exports = class AppVeyorTests extends AppVeyorBase {
static get url() {
return this.buildUrl('appveyor/tests')
}

static get defaultBadgeData() {
return {
label: 'tests',
}
}

static get examples() {
return [
{
title: 'AppVeyor tests',
previewUrl: 'NZSmartie/coap-net-iu0to',
},
{
title: 'AppVeyor tests branch',
previewUrl: 'NZSmartie/coap-net-iu0to/master',
},
]
}

static render({ passed, failed, skipped, total }) {
let message = `${passed} passed`
if (failed > 0) {
message += `, ${failed} failed`
}
if (skipped > 0) {
message += `, ${skipped} skipped`
}

let color
if (passed === total) {
color = 'brightgreen'
} else if (failed === 0) {
color = 'green'
} else if (passed === 0) {
color = 'red'
} else {
color = 'orange'
}

return { message, color }
}

async handle({ repo, branch }) {
const {
build: { jobs },
} = await this.fetch({ repo, branch })

let total = 0,
passed = 0,
failed = 0
jobs.forEach(job => {
total += job.testsCount
passed += job.passedTestsCount
failed += job.failedTestsCount
})
const skipped = total - passed - failed

return this.constructor.render({ passed, failed, skipped, total })
}
}
66 changes: 0 additions & 66 deletions services/appveyor/appveyor.service.js

This file was deleted.

0 comments on commit 656326d

Please sign in to comment.