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

The Great [Wercker] Rewrite #1920

Merged
merged 5 commits into from
Aug 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
4 changes: 0 additions & 4 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ const allBadgeExamples = [
previewUri: '/travis/com/ivandelabeldad/rackian-gateway/master.svg',
exampleUri: '/travis/com/USER/REPO/BRANCH.svg',
},
{
title: 'Wercker',
previewUri: '/wercker/ci/wercker/docs.svg',
},
{
title: 'TeamCity CodeBetter',
previewUri: '/teamcity/codebetter/bt428.svg',
Expand Down
81 changes: 0 additions & 81 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,87 +576,6 @@ cache(function (data, match, sendBadge, request) {
});
}));

// Wercker integration
camp.route(/^\/wercker\/ci\/([a-fA-F0-9]+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var projectId = match[1]; // eg, `54330318b4ce963d50020750`
var format = match[2];
var options = {
method: 'GET',
json: true,
uri: 'https://app.wercker.com/getbuilds/' + projectId + '?limit=1',
};
var badgeData = getBadgeData('build', data);
request(options, function(err, res, json) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
var build = json[0];

if (build.status === 'finished') {
if (build.result === 'passed') {
badgeData.colorscheme = 'brightgreen';
badgeData.text[1] = 'passing';
} else {
badgeData.colorscheme = 'red';
badgeData.text[1] = build.result;
}
} else {
badgeData.text[1] = build.status;
}
sendBadge(format, badgeData);

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

// Wercker V3 integration
camp.route(/^\/wercker\/ci\/(.+)\/(.+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var owner = match[1];
var application = match[2];
var format = match[3];
var options = {
method: 'GET',
json: true,
uri: 'https://app.wercker.com/api/v3/applications/' + owner + '/' + application + '/builds?limit=1',
};
var badgeData = getBadgeData('build', data);
request(options, function(err, res, json) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
var build = json[0];

if (build.status === 'finished') {
if (build.result === 'passed') {
badgeData.colorscheme = 'brightgreen';
badgeData.text[1] = 'passing';
} else {
badgeData.colorscheme = 'red';
badgeData.text[1] = build.result;
}
} else {
badgeData.text[1] = build.status;
}
sendBadge(format, badgeData);

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

// Rust download and version integration
camp.route(/^\/crates\/(d|v|dv|l)\/([A-Za-z0-9_-]+)(?:\/([0-9.]+))?\.(svg|png|gif|jpg|json)$/,
cache(function (data, match, sendBadge, request) {
Expand Down
73 changes: 73 additions & 0 deletions services/wercker/wercker.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const Joi = require('joi')
const BaseJsonService = require('../base-json')

const werckerSchema = Joi.array()
.items(
Joi.object({
status: Joi.string().required(),
result: Joi.string().required(),
})
)
.min(1)
.required()

module.exports = class Wercker extends BaseJsonService {
async fetch({ applicationName, projectId, branch }) {
const url = applicationName
? `https://app.wercker.com/api/v3/applications/${applicationName}/builds?limit=1`
: `https://app.wercker.com/getbuilds/${projectId}?limit=1`
return this._requestJson({
schema: werckerSchema,
url,
options: { qs: { branch } },
errorMessages: {
401: 'private application not supported',
404: 'application not found',
},
})
}

static render({ status, result }) {
if (status === 'finished') {
if (result === 'passed') {
return { message: 'passing', color: 'brightgreen' }
Copy link
Member

Choose a reason for hiding this comment

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

If we are translating passed to passing, why does the build status regex need to be updated?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm... I must have added this part way through rewriting the service, didn't think about removing it later.

} else {
return { message: result, color: 'red' }
}
}
return { message: status }
}

async handle({ applicationName, projectId, branch }) {
const json = await this.fetch({ applicationName, projectId, branch })
const { status, result } = json[0]
return this.constructor.render({ status, result })
}

// Metadata
static get category() {
return 'build'
}

static get url() {
return {
base: 'wercker/ci',
format: '(?:([^/]+/[^/]+)|([a-fA-F0-9]+))(?:/(.+))?',
capture: ['applicationName', 'projectId', 'branch'],
}
}

static get examples() {
return [
{
previewUrl: 'wercker/go-wercker-api',
},
{
title: `${this.name} branch`,
previewUrl: 'wercker/go-wercker-api/master',
},
]
}
}
29 changes: 29 additions & 0 deletions services/wercker/wercker.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const Joi = require('joi')
const ServiceTester = require('../service-tester')

const { isBuildStatus } = require('../test-validators')

const t = new ServiceTester({ id: 'wercker', title: 'Wercker' })
module.exports = t

t.create('CI build status')
.get('/ci/wercker/go-wercker-api.json')
.expectJSONTypes(Joi.object().keys({ name: 'build', value: isBuildStatus }))

t.create('CI build status (branch)')
.get('/ci/wercker/go-wercker-api/master.json')
.expectJSONTypes(Joi.object().keys({ name: 'build', value: isBuildStatus }))

t.create('CI build status (old v1 API)')
.get('/ci/559e33c8e982fc615500b357.json')
.expectJSONTypes(Joi.object().keys({ name: 'build', value: isBuildStatus }))

t.create('CI application not found')
.get('/ci/somerandomproject/thatdoesntexits.json')
.expectJSON({ name: 'build', value: 'application not found' })

t.create('CI private application')
.get('/ci/wercker/blueprint.json')
.expectJSON({ name: 'build', value: 'private application not supported' })
Copy link
Member

Choose a reason for hiding this comment

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

😍

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm happy about how this tied into my previous piece of work, #1888, which made things really easy here. 😄