Skip to content

Commit 0fa209b

Browse files
committed
Fix #312
1 parent c00d2f3 commit 0fa209b

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

docs/job/badges.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Template | Meaning | Optional
2828
`<subject>` | Badge subject | False
2929
`<job_name>` | Fully-qualified job name | False
3030
`<branch>` | Git branch | True
31+
`<build_number>` | Build number i.e. `432` | True
32+
`<build_restart_counter>` | Restart counter, default `1` | True
3133

3234

3335
Additionally, there are also two job-independent badges:
@@ -36,4 +38,3 @@ Additionally, there are also two job-independent badges:
3638
- `<infrabox-url>/api/v1/projects/<project-id>/tests.svg?branch=<branch>&job_name=<job_name>` showing the amount of passing jobs of given job for the build
3739

3840
Markdown code suitable for embedding can be retrieved from the "Settings" tab of your project (requires administrator privileges).
39-

src/api/handlers/project.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def get(self, project_id):
8080

8181
project_type = p['type']
8282

83+
build_number = request.args.get('build_number', None)
84+
build_restart_count = request.args.get('build_restart_counter', 1)
85+
8386
rows = None
8487
if request.args.get('branch', None) and project_type in ('github', 'gerrit'):
8588
rows = g.db.execute_many_dict('''
@@ -97,6 +100,18 @@ def get(self, project_id):
97100
LIMIT 1
98101
)
99102
''', [project_id, project_id, request.args['branch']])
103+
elif build_number and build_restart_count:
104+
rows = g.db.execute_many_dict('''
105+
SELECT state FROM job j
106+
WHERE j.project_id = %s
107+
AND j.build_id = (
108+
SELECT id
109+
FROM build
110+
WHERE project_id = %s
111+
AND build_number = %s
112+
AND restart_counter = %s
113+
)
114+
''', [project_id, project_id, build_number, build_restart_count])
100115
else:
101116
rows = g.db.execute_many_dict('''
102117
SELECT state FROM job j
@@ -127,6 +142,10 @@ def get(self, project_id):
127142
status = state
128143
color = 'red'
129144

145+
if state == 'unstable':
146+
status = state
147+
color = 'yellow'
148+
130149
return get_badge('infrabox', status, color)
131150

132151
@ns.route('/tests.svg')
@@ -140,6 +159,8 @@ def get(self, project_id):
140159
'''
141160
branch = request.args.get('branch', None)
142161
job_name = request.args.get('job_name', '%')
162+
build_number = request.args.get('build_number', None)
163+
build_restart_count = request.args.get('build_restart_counter', 1)
143164

144165
p = g.db.execute_one_dict('''
145166
SELECT type FROM project WHERE id = %s
@@ -180,6 +201,35 @@ def get(self, project_id):
180201
AND j.name LIKE %s
181202
)
182203
''', [project_id, project_id, project_id, project_id, branch, job_name])
204+
elif build_number and build_restart_count:
205+
r = g.db.execute_one_dict('''
206+
SELECT
207+
count(CASE WHEN tr.state = 'ok' THEN 1 END) success,
208+
count(CASE WHEN tr.state = 'failure' THEN 1 END) failure,
209+
count(CASE WHEN tr.state = 'error' THEN 1 END) error,
210+
count(CASE WHEN tr.state = 'skipped' THEN 1 END) skipped
211+
FROM test_run tr
212+
WHERE tr.project_id = %s
213+
AND tr.job_id IN (
214+
SELECT j.id
215+
FROM job j
216+
WHERE j.project_id = %s
217+
AND j.build_id = (
218+
SELECT b.id
219+
FROM build b
220+
INNER JOIN job j
221+
ON b.id = j.build_id
222+
AND j.state in ('finished', 'unstable')
223+
AND b.project_id = %s
224+
AND j.project_id = %s
225+
AND j.name LIKE %s
226+
AND b.build_number = %s
227+
AND b.restart_counter = %s
228+
LIMIT 1
229+
)
230+
AND j.name LIKE %s
231+
)
232+
''', [project_id, project_id, project_id, project_id, job_name, build_number, build_restart_count, job_name])
183233
else:
184234
r = g.db.execute_one_dict('''
185235
SELECT
@@ -226,6 +276,9 @@ def get(self, project_id):
226276
job_name = request.args.get('job_name', None)
227277
subject = request.args.get('subject', None)
228278
branch = request.args.get('branch', None)
279+
build_number = request.args.get('build_number', None)
280+
build_restart_count = request.args.get('build_restart_counter', 1)
281+
229282
p = g.db.execute_one_dict('''
230283
SELECT type FROM project WHERE id = %s
231284
''', [project_id])
@@ -252,6 +305,22 @@ def get(self, project_id):
252305
ORDER BY j.end_date desc
253306
LIMIT 1
254307
''', [project_id, job_name, subject, project_id, branch])
308+
elif build_number and build_restart_count:
309+
badge = g.db.execute_one_dict('''
310+
SELECT status, color
311+
FROM job_badge jb
312+
JOIN job j
313+
ON j.id = jb.job_id
314+
AND j.project_id = %s
315+
AND j.state in ('finished', 'unstable')
316+
AND j.name = %s
317+
AND jb.subject = %s
318+
JOIN build b
319+
ON j.build_id = b.id
320+
AND b.project_id = %s
321+
AND b.build_number = %s
322+
AND b.restart_counter = %s
323+
''', [project_id, job_name, subject, project_id, build_number, build_number])
255324
else:
256325
badge = g.db.execute_one_dict('''
257326
SELECT status, color

0 commit comments

Comments
 (0)