Fix badges should be accessible by branch
@@ -147,8 +147,37 @@ router.get("/:project_id/job/:job_id/output", pv, token_auth, checkProjectAccess
router.get("/:project_id/build/state.svg", pv, (req: Request, res: Response, next) => {
const project_id = req.params['project_id'];
+ return db.any(`
+ SELECT type FROM project WHERE id = $1
+ `, [project_id])
+ .then((result: any[]) => {
+ if (result.length !== 1) {
+ throw new NotFound();
+ }
- db.any(`SELECT state FROM job j
+ const t = result[0].type
+
+ if (req.query.branch && (t === 'github' || t === 'gerrit')) {
+ return db.any(`
+ SELECT state FROM job j
+ WHERE j.project_id = $1
+ AND j.build_id = (
+ SELECT b.id
+ FROM build b
+ INNER JOIN job j
+ ON b.id = j.build_id
+ AND b.project_id = $1
+ AND j.project_id = $1
+ INNER JOIN "commit" c
+ ON c.id = b.commit_id
+ AND c.project_id = $1
+ AND c.branch = $2
+ ORDER BY j.created_at DESC
+ LIMIT 1
+ )`, [project_id, req.query.branch]);
+ } else {
+ return db.any(`
+ SELECT state FROM job j
WHERE j.project_id = $1
AND j.build_id = (
SELECT b.id
@@ -159,7 +188,9 @@ router.get("/:project_id/build/state.svg", pv, (req: Request, res: Response, nex
AND j.project_id = $1
ORDER BY j.created_at DESC
LIMIT 1
- )`, [project_id]).then((rows: any[]) => {
+ )`, [project_id]);
+ }
+ }).then((rows: any[]) => {
let status = 'finished';
let color = 'brightgreen';
