Permalink
Browse files

fix(): status badges

  • Loading branch information...
Izak88 authored and jkuri committed Aug 17, 2017
1 parent fdcb60f commit db34b0e9fede1e7f8023541f094528d2a595014b
Showing with 90 additions and 64 deletions.
  1. +14 −6 src/api/db/repository.ts
  2. +18 −58 src/api/server-routes.ts
  3. +58 −0 src/api/utils.ts
@@ -42,7 +42,8 @@ export function getRepositoryBadge(id: number): Promise<string> {
.fetch({
withRelated: [
{ 'builds': query => query.where('pr', null).orderBy('id', 'desc').limit(1) },
'builds.jobs'
{ 'builds.runs': query => query.orderBy('id', 'desc').limit(1) },
'builds.runs.job_runs'
]
} as any)
.then(repo => {
@@ -52,17 +53,17 @@ export function getRepositoryBadge(id: number): Promise<string> {
repo = repo.toJSON();
let status = 'queued';

if (repo.builds[0] && repo.builds[0].jobs) {
if (repo.builds[0].jobs.findIndex(job => job.status === 'failed') !== -1) {
if (repo.builds[0] && repo.builds[0].runs[0]) {
if (repo.builds[0].runs[0].job_runs.findIndex(run => run.status === 'failed') !== -1) {
status = 'failing';
}

if (repo.builds[0].jobs.findIndex(job => job.status === 'running') !== -1) {
if (repo.builds[0].runs[0].job_runs.findIndex(run => run.status === 'running') !== -1) {
status = 'running';
}

if (repo.builds[0].jobs.length ===
repo.builds[0].jobs.filter(job => job.status === 'success').length) {
if (repo.builds[0].runs[0].job_runs.length ===
repo.builds[0].runs[0].job_runs.filter(run => run.status === 'success').length) {
status = 'passing';
}
}
@@ -104,6 +105,13 @@ export function getRepositories(userId: string, keyword: string): Promise<any[]>
});
}

export function getRepositoryId(owner: string, repository: string): Promise<any> {
return new Promise((resolve, reject) => {
new Repository().query(q => q.where('full_name', `${owner}/${repository}`))
.fetch().then(repo => !repo ? reject() : resolve(repo.toJSON().id));
});
}

export function addRepository(data: any): Promise<any> {
return new Promise((resolve, reject) => {
new Repository().save(data, { method: 'insert' }).then(result => {
@@ -5,7 +5,7 @@ import * as utils from './utils';
import { resolve } from 'path';
import { Observable } from 'rxjs';
import { exists } from './fs';
import { getFilePath } from './utils';
import { getFilePath, generateBadgeHtml } from './utils';
import { reinitializeDatabase } from './db/migrations';
import {
usersExists,
@@ -16,7 +16,13 @@ import {
updateUserPassword,
getUsers
} from './db/user';
import { addRepository, getRepositories, getRepository, getRepositoryBadge } from './db/repository';
import {
addRepository,
getRepositories,
getRepository,
getRepositoryBadge,
getRepositoryId
} from './db/repository';
import { getBuilds, getBuild } from './db/build';
import { getJob } from './db/job';
import { imageExists } from './docker';
@@ -146,64 +152,18 @@ export function badgeRoutes(): express.Router {

router.get('/:id', (req: express.Request, res: express.Response) => {
getRepositoryBadge(req.params.id).then(status => {
let background = null;
if (status === 'failing') {
background = '#f03e3e';
} else if (status === 'running') {
background = '#ffd43b';
} else if (status === 'queued') {
background = '#ffd43b';
} else {
background = '#39B54A';
}

let html = `
<svg xmlns="http://www.w3.org/2000/svg" width="97" height="20" style="shape-rendering:
geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd;
clip-rule:evenodd">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="97" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#333" d="M0 0h53v20H0z"/>
<path fill="` + background + `" d="M53 0h75v20H53z"/>
<path fill="url(#b)" d="M0 0h97v20H0z"/>
</g>
<g transform="matrix(0.27,0,0,0.27,3,2.7)">
<circle fill-rule="evenodd" clip-rule="evenodd" fill="none" stroke="#FFFFFF"
stroke-width="3px" stroke-linecap="round" stroke-linejoin="round"
stroke-miterlimit="10" cx="27" cy="27" r="26"/>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF"
d="M24.83,25.84c0.01,1.54,0.01,3.08,0.01,4.63
c0.02,0.96,0.18,1.99,0.79,2.76c0.69,0.88,1.83,1.27,2.9,1.35c1.16,0.06,
2.4,0.07,3.44-0.53c1-0.57,1.53-1.7,
1.66-2.81c0.19-1.26,0.12-2.62-0.57-3.73c-0.69-1.1-2.03-1.66-3.28-1.67H24.83z
M19.18,11.57c1.1-1.1,2.99-1.24,4.25-0.33c0.88,0.6,
1.48,1.65,1.41,2.73v4.65c-0.01,0.19,0,0.53,0.1,0.53c1.74-0.01,3.48,0,
5.21-0.01c2.04-0.06,4.11,0.5,5.85,1.57c1.48,0.92,2.69,2.28,3.41,3.88c0.79,
1.71,1.04,3.61,1.01,5.49c0,2.09-0.32,4.24-1.3,6.12c-0.82,
1.65-2.19,3.01-3.84,3.84c-1.78,0.92-3.78,1.29-5.77,
1.36c-2.3-0.04-4.68-0.41-6.66-1.67c-1.92-1.19-3.2-3.18-3.85-5.29c-0.77-2.47-0.85-5.08-0.85-7.64
c-0.02-0.32,0.12-0.66-0.02-0.98c-0.75-0.04-1.5,
0.07-2.22-0.11c-1.04-0.27-1.93-1.1-2.29-2.12c-0.32-0.89-0.24-1.92,0.21-2.75
c0.46-0.82,1.25-1.45,2.16-1.67c0.7-0.06,1.41-0.01,2.1-0.02c-0.01-1.6,
0-3.2-0.01-4.81C18.09,13.33,18.42,12.27,19.18,11.57z"/>
</g>
<g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="9">
<text x="22" y="15" fill="#010101" fill-opacity=".3">build</text>
<text x="22" y="14">build</text>
<text x="58" y="15" fill="#010101" fill-opacity=".3">` + status + `</text>
<text x="58" y="14">` + status + `</text>
</g>
</svg>
`;
res.writeHead(200, {'Content-Type': 'image/svg+xml'});
res.write(generateBadgeHtml(status));
res.end();
});
});

router.get('/:owner/:repository', (req: express.Request, res: express.Response) => {
getRepositoryId(req.params.owner, req.params.repository)
.then(id => getRepositoryBadge(id))
.then(status => {
res.writeHead(200, {'Content-Type': 'image/svg+xml'});
res.write(html);
res.write(generateBadgeHtml(status));
res.end();
});
});
@@ -122,3 +122,61 @@ export function getHttpJsonResponse(url: string): Promise<any> {
});
});
}

export function generateBadgeHtml(status: string): string {
let background = null;
if (status === 'failing') {
background = '#f03e3e';
} else if (status === 'running') {
background = '#ffd43b';
} else if (status === 'queued') {
background = '#ffd43b';
} else {
background = '#39B54A';
}

return `
<svg xmlns="http://www.w3.org/2000/svg" width="97" height="20" style="shape-rendering:
geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd;
clip-rule:evenodd">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="97" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#333" d="M0 0h53v20H0z"/>
<path fill="` + background + `" d="M53 0h75v20H53z"/>
<path fill="url(#b)" d="M0 0h97v20H0z"/>
</g>
<g transform="matrix(0.27,0,0,0.27,3,2.7)">
<circle fill-rule="evenodd" clip-rule="evenodd" fill="none" stroke="#FFFFFF"
stroke-width="3px" stroke-linecap="round" stroke-linejoin="round"
stroke-miterlimit="10" cx="27" cy="27" r="26"/>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF"
d="M24.83,25.84c0.01,1.54,0.01,3.08,0.01,4.63
c0.02,0.96,0.18,1.99,0.79,2.76c0.69,0.88,1.83,1.27,2.9,1.35c1.16,0.06,
2.4,0.07,3.44-0.53c1-0.57,1.53-1.7,
1.66-2.81c0.19-1.26,0.12-2.62-0.57-3.73c-0.69-1.1-2.03-1.66-3.28-1.67H24.83z
M19.18,11.57c1.1-1.1,2.99-1.24,4.25-0.33c0.88,0.6,
1.48,1.65,1.41,2.73v4.65c-0.01,0.19,0,0.53,0.1,0.53c1.74-0.01,3.48,0,
5.21-0.01c2.04-0.06,4.11,0.5,5.85,1.57c1.48,0.92,2.69,2.28,3.41,3.88c0.79,
1.71,1.04,3.61,1.01,5.49c0,2.09-0.32,4.24-1.3,6.12c-0.82,
1.65-2.19,3.01-3.84,3.84c-1.78,0.92-3.78,1.29-5.77,
1.36c-2.3-0.04-4.68-0.41-6.66-1.67c-1.92-1.19-3.2-3.18-3.85-5.29c-0.77-2.47-0.85-5.08-0.85-7.64
c-0.02-0.32,0.12-0.66-0.02-0.98c-0.75-0.04-1.5,
0.07-2.22-0.11c-1.04-0.27-1.93-1.1-2.29-2.12c-0.32-0.89-0.24-1.92,0.21-2.75
c0.46-0.82,1.25-1.45,2.16-1.67c0.7-0.06,1.41-0.01,2.1-0.02c-0.01-1.6,
0-3.2-0.01-4.81C18.09,13.33,18.42,12.27,19.18,11.57z"/>
</g>
<g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="9">
<text x="22" y="15" fill="#010101" fill-opacity=".3">build</text>
<text x="22" y="14">build</text>
<text x="58" y="15" fill="#010101" fill-opacity=".3">` + status + `</text>
<text x="58" y="14">` + status + `</text>
</g>
</svg>
`;
}

0 comments on commit db34b0e

Please sign in to comment.