Use a HttpClient service to get the shield.io badge.

This commit is contained in:
Marco Vito Moscaritolo 2015-05-31 16:56:42 +02:00
parent e977360083
commit f125047951
3 changed files with 41 additions and 20 deletions

View file

@ -13,8 +13,10 @@ use b8;
use b8\Exception\HttpException\NotFoundException; use b8\Exception\HttpException\NotFoundException;
use b8\Exception\HttpException\NotAuthorizedException; use b8\Exception\HttpException\NotAuthorizedException;
use b8\Store; use b8\Store;
use b8\HttpClient;
use b8\Http\Request; use b8\Http\Request;
use b8\Http\Response; use b8\Http\Response;
use Exception;
use PHPCI\Config; use PHPCI\Config;
use PHPCI\BuildFactory; use PHPCI\BuildFactory;
use PHPCI\Helper\Lang; use PHPCI\Helper\Lang;
@ -47,19 +49,25 @@ class BuildStatusController extends \PHPCI\Controller
Request $request, Request $request,
Response $response, Response $response,
BuildStore $buildStore, BuildStore $buildStore,
ProjectStore $projectStore ProjectStore $projectStore,
HttpClient $shieldsClient
) )
{ {
parent::__construct($config, $request, $response); parent::__construct($config, $request, $response);
$this->buildStore = $buildStore; $this->buildStore = $buildStore;
$this->projectStore = $projectStore; $this->projectStore = $projectStore;
$this->shieldsClient = $shieldsClient;
} }
/** /**
* Returns status of the last build * Returns status of the last build
* @param $projectId *
* @param int $projectId
*
* @return string * @return string
*
* @throws Exception
*/ */
protected function getStatus($projectId) protected function getStatus($projectId)
{ {
@ -79,7 +87,7 @@ class BuildStatusController extends \PHPCI\Controller
$status = 'failed'; $status = 'failed';
} }
} }
} catch (\Exception $e) { } catch (Exception $e) {
$status = 'error'; $status = 'error';
} }
@ -89,10 +97,12 @@ class BuildStatusController extends \PHPCI\Controller
/** /**
* Displays projects information in ccmenu format * Displays projects information in ccmenu format
* *
* @param $projectId * @param int $projectId
* @return bool *
* @throws \Exception * @return Response
* @throws b8\Exception\HttpException *
* @throws Exception
* @throws HttpException
*/ */
public function ccxml($projectId) public function ccxml($projectId)
{ {
@ -132,8 +142,11 @@ class BuildStatusController extends \PHPCI\Controller
} }
/** /**
* Render the XML object
*
* @param \SimpleXMLElement $xml * @param \SimpleXMLElement $xml
* @return bool *
* @return Response
*/ */
protected function renderXml(\SimpleXMLElement $xml = null) protected function renderXml(\SimpleXMLElement $xml = null)
{ {
@ -146,6 +159,10 @@ class BuildStatusController extends \PHPCI\Controller
/** /**
* Returns the appropriate build status image in SVG format for a given project. * Returns the appropriate build status image in SVG format for a given project.
*
* @param int $projectId
*
* @return Response
*/ */
public function image($projectId) public function image($projectId)
{ {
@ -161,17 +178,14 @@ class BuildStatusController extends \PHPCI\Controller
} }
$color = ($status == 'passing') ? 'green' : 'red'; $color = ($status == 'passing') ? 'green' : 'red';
$image = file_get_contents(sprintf( $image = $this->shieldsClient->get(
'http://img.shields.io/badge/%s-%s-%s.svg?style=%s', sprintf('/badge/%s-%s-%s.svg', $label, $status, $color),
$label, array('style' => $style)
$status, );
$color,
$style
));
$this->response->disableLayout(); $this->response->disableLayout();
$this->response->setHeader('Content-Type', 'image/svg+xml'); $this->response->setHeader('Content-Type', 'image/svg+xml');
$this->response->setContent($image); $this->response->setContent($image['body']);
return $this->response; return $this->response;
} }
@ -187,11 +201,11 @@ class BuildStatusController extends \PHPCI\Controller
$project = $this->projectStore->getById($projectId); $project = $this->projectStore->getById($projectId);
if (empty($project)) { if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found'); throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
} }
if (!$project->getAllowPublicStatus()) { if (!$project->getAllowPublicStatus()) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found'); throw new NotAuthorizedException();
} }
$builds = $this->getLatestBuilds($projectId); $builds = $this->getLatestBuilds($projectId);

View file

@ -35,7 +35,8 @@ class BuildStatusControllerTest extends \PHPUnit_Framework_TestCase
$this->prophesize('b8\Http\Request')->reveal(), $this->prophesize('b8\Http\Request')->reveal(),
new \b8\Http\Response(), new \b8\Http\Response(),
$buildStore->reveal(), $buildStore->reveal(),
$projectStore->reveal() $projectStore->reveal(),
$this->prophesize('b8\HttpClient')->reveal()
); );
$result = $webController->handleAction('ccxml', [1]); $result = $webController->handleAction('ccxml', [1]);
@ -57,7 +58,8 @@ class BuildStatusControllerTest extends \PHPUnit_Framework_TestCase
$this->prophesize('b8\Http\Request')->reveal(), $this->prophesize('b8\Http\Request')->reveal(),
new \b8\Http\Response(), new \b8\Http\Response(),
$buildStore->reveal(), $buildStore->reveal(),
$projectStore->reveal() $projectStore->reveal(),
$this->prophesize('b8\HttpClient')->reveal()
); );
$result = $webController->handleAction('ccxml', [1]); $result = $webController->handleAction('ccxml', [1]);

View file

@ -35,6 +35,10 @@ services:
class: b8\HttpClient class: b8\HttpClient
arguments: arguments:
- https://api.github.com - https://api.github.com
http_client.shields:
class: b8\HttpClient
arguments:
- http://img.shields.io
config: config:
class: PHPCI\Config class: PHPCI\Config
arguments: [%config_file%] arguments: [%config_file%]
@ -81,6 +85,7 @@ services:
- @http.response - @http.response
- @storage.build - @storage.build
- @storage.project - @storage.project
- @http_client.shields
application.controller.user: application.controller.user:
class: PHPCI\Controller\UserController class: PHPCI\Controller\UserController
arguments: arguments: