Skip to content

Commit db34b0e

Browse files
Izak88jkuri
authored andcommitted
fix(): status badges
1 parent fdcb60f commit db34b0e

File tree

3 files changed

+90
-64
lines changed

3 files changed

+90
-64
lines changed

src/api/db/repository.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export function getRepositoryBadge(id: number): Promise<string> {
4242
.fetch({
4343
withRelated: [
4444
{ 'builds': query => query.where('pr', null).orderBy('id', 'desc').limit(1) },
45-
'builds.jobs'
45+
{ 'builds.runs': query => query.orderBy('id', 'desc').limit(1) },
46+
'builds.runs.job_runs'
4647
]
4748
} as any)
4849
.then(repo => {
@@ -52,17 +53,17 @@ export function getRepositoryBadge(id: number): Promise<string> {
5253
repo = repo.toJSON();
5354
let status = 'queued';
5455

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

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

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

108+
export function getRepositoryId(owner: string, repository: string): Promise<any> {
109+
return new Promise((resolve, reject) => {
110+
new Repository().query(q => q.where('full_name', `${owner}/${repository}`))
111+
.fetch().then(repo => !repo ? reject() : resolve(repo.toJSON().id));
112+
});
113+
}
114+
107115
export function addRepository(data: any): Promise<any> {
108116
return new Promise((resolve, reject) => {
109117
new Repository().save(data, { method: 'insert' }).then(result => {

src/api/server-routes.ts

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as utils from './utils';
55
import { resolve } from 'path';
66
import { Observable } from 'rxjs';
77
import { exists } from './fs';
8-
import { getFilePath } from './utils';
8+
import { getFilePath, generateBadgeHtml } from './utils';
99
import { reinitializeDatabase } from './db/migrations';
1010
import {
1111
usersExists,
@@ -16,7 +16,13 @@ import {
1616
updateUserPassword,
1717
getUsers
1818
} from './db/user';
19-
import { addRepository, getRepositories, getRepository, getRepositoryBadge } from './db/repository';
19+
import {
20+
addRepository,
21+
getRepositories,
22+
getRepository,
23+
getRepositoryBadge,
24+
getRepositoryId
25+
} from './db/repository';
2026
import { getBuilds, getBuild } from './db/build';
2127
import { getJob } from './db/job';
2228
import { imageExists } from './docker';
@@ -146,64 +152,18 @@ export function badgeRoutes(): express.Router {
146152

147153
router.get('/:id', (req: express.Request, res: express.Response) => {
148154
getRepositoryBadge(req.params.id).then(status => {
149-
let background = null;
150-
if (status === 'failing') {
151-
background = '#f03e3e';
152-
} else if (status === 'running') {
153-
background = '#ffd43b';
154-
} else if (status === 'queued') {
155-
background = '#ffd43b';
156-
} else {
157-
background = '#39B54A';
158-
}
159-
160-
let html = `
161-
<svg xmlns="http://www.w3.org/2000/svg" width="97" height="20" style="shape-rendering:
162-
geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd;
163-
clip-rule:evenodd">
164-
<linearGradient id="b" x2="0" y2="100%">
165-
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
166-
<stop offset="1" stop-opacity=".1"/>
167-
</linearGradient>
168-
<mask id="a">
169-
<rect width="97" height="20" rx="3" fill="#fff"/>
170-
</mask>
171-
<g mask="url(#a)">
172-
<path fill="#333" d="M0 0h53v20H0z"/>
173-
<path fill="` + background + `" d="M53 0h75v20H53z"/>
174-
<path fill="url(#b)" d="M0 0h97v20H0z"/>
175-
</g>
176-
<g transform="matrix(0.27,0,0,0.27,3,2.7)">
177-
<circle fill-rule="evenodd" clip-rule="evenodd" fill="none" stroke="#FFFFFF"
178-
stroke-width="3px" stroke-linecap="round" stroke-linejoin="round"
179-
stroke-miterlimit="10" cx="27" cy="27" r="26"/>
180-
<path fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF"
181-
d="M24.83,25.84c0.01,1.54,0.01,3.08,0.01,4.63
182-
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,
183-
2.4,0.07,3.44-0.53c1-0.57,1.53-1.7,
184-
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
185-
M19.18,11.57c1.1-1.1,2.99-1.24,4.25-0.33c0.88,0.6,
186-
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,
187-
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,
188-
1.71,1.04,3.61,1.01,5.49c0,2.09-0.32,4.24-1.3,6.12c-0.82,
189-
1.65-2.19,3.01-3.84,3.84c-1.78,0.92-3.78,1.29-5.77,
190-
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
191-
c-0.02-0.32,0.12-0.66-0.02-0.98c-0.75-0.04-1.5,
192-
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
193-
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,
194-
0-3.2-0.01-4.81C18.09,13.33,18.42,12.27,19.18,11.57z"/>
195-
</g>
196-
<g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="9">
197-
<text x="22" y="15" fill="#010101" fill-opacity=".3">build</text>
198-
<text x="22" y="14">build</text>
199-
<text x="58" y="15" fill="#010101" fill-opacity=".3">` + status + `</text>
200-
<text x="58" y="14">` + status + `</text>
201-
</g>
202-
</svg>
203-
`;
155+
res.writeHead(200, {'Content-Type': 'image/svg+xml'});
156+
res.write(generateBadgeHtml(status));
157+
res.end();
158+
});
159+
});
204160

161+
router.get('/:owner/:repository', (req: express.Request, res: express.Response) => {
162+
getRepositoryId(req.params.owner, req.params.repository)
163+
.then(id => getRepositoryBadge(id))
164+
.then(status => {
205165
res.writeHead(200, {'Content-Type': 'image/svg+xml'});
206-
res.write(html);
166+
res.write(generateBadgeHtml(status));
207167
res.end();
208168
});
209169
});

src/api/utils.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,61 @@ export function getHttpJsonResponse(url: string): Promise<any> {
122122
});
123123
});
124124
}
125+
126+
export function generateBadgeHtml(status: string): string {
127+
let background = null;
128+
if (status === 'failing') {
129+
background = '#f03e3e';
130+
} else if (status === 'running') {
131+
background = '#ffd43b';
132+
} else if (status === 'queued') {
133+
background = '#ffd43b';
134+
} else {
135+
background = '#39B54A';
136+
}
137+
138+
return `
139+
<svg xmlns="http://www.w3.org/2000/svg" width="97" height="20" style="shape-rendering:
140+
geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd;
141+
clip-rule:evenodd">
142+
<linearGradient id="b" x2="0" y2="100%">
143+
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
144+
<stop offset="1" stop-opacity=".1"/>
145+
</linearGradient>
146+
<mask id="a">
147+
<rect width="97" height="20" rx="3" fill="#fff"/>
148+
</mask>
149+
<g mask="url(#a)">
150+
<path fill="#333" d="M0 0h53v20H0z"/>
151+
<path fill="` + background + `" d="M53 0h75v20H53z"/>
152+
<path fill="url(#b)" d="M0 0h97v20H0z"/>
153+
</g>
154+
<g transform="matrix(0.27,0,0,0.27,3,2.7)">
155+
<circle fill-rule="evenodd" clip-rule="evenodd" fill="none" stroke="#FFFFFF"
156+
stroke-width="3px" stroke-linecap="round" stroke-linejoin="round"
157+
stroke-miterlimit="10" cx="27" cy="27" r="26"/>
158+
<path fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF"
159+
d="M24.83,25.84c0.01,1.54,0.01,3.08,0.01,4.63
160+
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,
161+
2.4,0.07,3.44-0.53c1-0.57,1.53-1.7,
162+
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
163+
M19.18,11.57c1.1-1.1,2.99-1.24,4.25-0.33c0.88,0.6,
164+
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,
165+
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,
166+
1.71,1.04,3.61,1.01,5.49c0,2.09-0.32,4.24-1.3,6.12c-0.82,
167+
1.65-2.19,3.01-3.84,3.84c-1.78,0.92-3.78,1.29-5.77,
168+
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
169+
c-0.02-0.32,0.12-0.66-0.02-0.98c-0.75-0.04-1.5,
170+
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
171+
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,
172+
0-3.2-0.01-4.81C18.09,13.33,18.42,12.27,19.18,11.57z"/>
173+
</g>
174+
<g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="9">
175+
<text x="22" y="15" fill="#010101" fill-opacity=".3">build</text>
176+
<text x="22" y="14">build</text>
177+
<text x="58" y="15" fill="#010101" fill-opacity=".3">` + status + `</text>
178+
<text x="58" y="14">` + status + `</text>
179+
</g>
180+
</svg>
181+
`;
182+
}

0 commit comments

Comments
 (0)