Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from raphaelstolt/addition-gitattributes-badge
Adds a .gitattributes badge
- Loading branch information
Showing
7 changed files
with
245 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the badge-poser package. | ||
* | ||
* (c) PUGX <http://pugx.github.io/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PUGX\Badge\Model\UseCase; | ||
|
||
use Guzzle\Http\Client; | ||
use PUGX\Badge\Model\PackageRepositoryInterface; | ||
|
||
/** | ||
* Create the 'gitattributes' image using a generator `Poser` | ||
*/ | ||
class CreateGitattributesBadge extends BaseCreatePackagistImage | ||
{ | ||
const COLOR_COMMITTED = '96d490'; | ||
const COLOR_UNCOMMITTED = 'ad6c4b'; | ||
const COLOR_ERROR = 'aa0000'; | ||
const GITATTRIBUTES_COMMITTED = 'committed'; | ||
const GITATTRIBUTES_UNCOMMITTED = 'uncommitted'; | ||
const GITATTRIBUTES_ERROR = 'checking'; | ||
const SUBJECT = '.gitattributes'; | ||
const SUBJECT_ERROR = 'Error'; | ||
|
||
protected $text = self::GITATTRIBUTES_ERROR; | ||
|
||
/** @var Client */ | ||
protected $client; | ||
|
||
/** | ||
* @param PackageRepositoryInterface $packageRepository | ||
* @param Client $client | ||
*/ | ||
public function __construct(PackageRepositoryInterface $packageRepository, Client $client) | ||
{ | ||
$this->packageRepository = $packageRepository; | ||
$this->client = $client; | ||
} | ||
/** | ||
* @param string $repository | ||
* @param string $format | ||
* | ||
* @return \PUGX\Badge\Model\Badge | ||
*/ | ||
public function createGitattributesBadge($repository, $format = 'svg') | ||
{ | ||
$repo = str_replace('.git', '', $this->packageRepository | ||
->fetchByRepository($repository) | ||
->getOriginalObject() | ||
->getRepository() | ||
); | ||
|
||
$request = $this->client->head( | ||
$repo . '/blob/master/.gitattributes', | ||
array(), | ||
array( | ||
'timeout' => 2, | ||
'connect_timeout' => 1, | ||
'exceptions' => false, | ||
) | ||
); | ||
|
||
$response = $this->client->send($request); | ||
$status = 500; | ||
if ($request) { | ||
$status = $response->getStatusCode(); | ||
} | ||
|
||
$this->text = self::GITATTRIBUTES_ERROR; | ||
$color = self::COLOR_ERROR; | ||
$subject = self::SUBJECT_ERROR; | ||
if (200 === $status) { | ||
$this->text = self::GITATTRIBUTES_COMMITTED; | ||
$color = self::COLOR_COMMITTED; | ||
$subject = self::SUBJECT; | ||
} elseif (404 === $status) { | ||
$this->text = self::GITATTRIBUTES_UNCOMMITTED; | ||
$color = self::COLOR_UNCOMMITTED; | ||
$subject = self::SUBJECT; | ||
} | ||
|
||
return $this->createBadgeFromRepository( | ||
$repository, | ||
$subject, | ||
$color, | ||
$format | ||
); | ||
} | ||
|
||
protected function prepareText($package, $context = null) | ||
{ | ||
return $this->text; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/PUGX/BadgeBundle/Controller/Badge/GitattributesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the badge-poser package. | ||
* | ||
* (c) PUGX <http://pugx.github.io/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace PUGX\BadgeBundle\Controller\Badge; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerAware; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; | ||
|
||
use PUGX\Badge\Infrastructure\ResponseFactory; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Class GitattributesController. | ||
* | ||
* @author Raphael Stolt <raphael.stolt@gmail.com> | ||
*/ | ||
class GitattributesController extends ContainerAware | ||
{ | ||
/** | ||
* .gitattributes action. | ||
* | ||
* @param string $repository repository | ||
* | ||
* @Route("/{repository}/gitattributes", | ||
* name="pugx_badge_gitattributes", | ||
* requirements={"repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?"} | ||
* ) | ||
* @Method({"GET"}) | ||
* @Cache(maxage="3600", smaxage="3600", public=true) | ||
* | ||
* @return Response | ||
*/ | ||
public function gitattributesAction(Request $request, $repository, $format='svg') | ||
{ | ||
if ($request->query->get('format') == 'plastic') { | ||
$format = 'plastic'; | ||
} | ||
|
||
$this->useCase = $this->container->get('use_case_badge_gitattributes'); | ||
$this->imageFactory = $this->container->get('image_factory'); | ||
|
||
|
||
$badge = $this->useCase->createGitattributesBadge($repository, $format); | ||
$image = $this->imageFactory->createFromBadge($badge); | ||
|
||
return ResponseFactory::createFromImage($image, 200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/PUGX/BadgeBundle/Tests/Controller/GitattributesBadgeControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the badge-poser package. | ||
* | ||
* (c) PUGX <http://pugx.github.io/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace PUGX\BadgeBundle\Tests\Controller; | ||
|
||
use Packagist\Api\Client; | ||
|
||
class GitattributesBadgeControllerTest extends PackagistWebTestCase | ||
{ | ||
/** | ||
* @group gitattributes | ||
*/ | ||
public function testGitattributesResponseUncommitted() | ||
{ | ||
$client = static::createClient(); | ||
static::$kernel->getContainer()->set('packagist_client', $this->packagistClient); | ||
$client->request('GET','/pugx/badge-poser/gitattributes'); | ||
|
||
$this->assertTrue($client->getResponse()->isSuccessful()); | ||
$this->assertRegExp('/s-maxage=3600/', $client->getResponse()->headers->get('Cache-Control')); | ||
|
||
$svgContent = $client->getResponse()->getContent(); | ||
|
||
$this->assertRegExp('/.gitattributes/', $svgContent); | ||
$this->assertRegExp('/uncommitted/', $svgContent); | ||
} | ||
|
||
/** | ||
* @group gitattributes | ||
*/ | ||
public function testGitattributesResponseCommitted() | ||
{ | ||
$client = static::createClient(); | ||
static::$kernel->getContainer()->set('packagist_client', $this->packagistClient); | ||
$client->request('GET','/stolt/lean-package-validator/gitattributes'); | ||
|
||
$this->assertTrue($client->getResponse()->isSuccessful()); | ||
$this->assertRegExp('/s-maxage=3600/', $client->getResponse()->headers->get('Cache-Control')); | ||
|
||
$svgContent = $client->getResponse()->getContent(); | ||
|
||
$this->assertRegExp('/.gitattributes/', $svgContent); | ||
$this->assertRegExp('/committed/', $svgContent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters