Skip to content

Commit

Permalink
Merge pull request #158 from raphaelstolt/addition-gitattributes-badge
Browse files Browse the repository at this point in the history
Adds a .gitattributes badge
  • Loading branch information
liuggio committed Oct 13, 2016
2 parents d637277 + 84c7044 commit 9095ac2
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/config/config.yml
Expand Up @@ -44,6 +44,7 @@ pugx_badge:
- { name: 'daily', label: 'Daily Downloads', route: "pugx_badge_download_type", type: 'daily'}
- { name: 'version', label: 'Version', route: "pugx_badge_version" }
- { name: 'composerlock', label: 'composer.lock', route: "pugx_badge_composerlock" }
- { name: 'gitattributes', label: '.gitattributes', route: "pugx_badge_gitattributes" }


snc_redis:
Expand Down
99 changes: 99 additions & 0 deletions src/Badge/src/Model/UseCase/CreateGitattributesBadge.php
@@ -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 src/PUGX/BadgeBundle/Controller/Badge/GitattributesController.php
@@ -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);
}
}
5 changes: 5 additions & 0 deletions src/PUGX/BadgeBundle/Resources/config/controllers.xml
Expand Up @@ -35,6 +35,11 @@
<argument type="service" id="http_client" />
</service>

<!-- .gitattributes -->
<service id="use_case_badge_gitattributes" class="PUGX\Badge\Model\UseCase\CreateGitattributesBadge">
<argument type="service" id="package_repository" />
<argument type="service" id="http_client" />
</service>

<!-- kernel Exception -->
<service id="use_case_badge_error" class="PUGX\Badge\Model\UseCase\CreateErrorBadge">
Expand Down
28 changes: 27 additions & 1 deletion src/PUGX/BadgeBundle/Resources/views/Page/home.html.twig
Expand Up @@ -68,13 +68,14 @@
<img class="latest_unstable_version_img" src="{{ path('pugx_badge_version_latest',{'repository': repository, 'latest':'unstable'}) }}">
<img class="license_img" src="{{ path('pugx_badge_license',{'repository': repository}) }}">
<img class="composer_img" src="{{ path('pugx_badge_composerlock',{'repository': repository}) }}">
<img class="gitattributes_img" src="{{ path('pugx_badge_gitattributes',{'repository': repository}) }}">
</a>

<a title="copy to clipboard" class="copy_on_clip btn btn-mini pull-right" data-clipboard-target="#clip_all_markdown" style="margin-top: 20px;">
<i class="icon-share"></i>
Copy to clipboard
</a>
<input id="clip_all_markdown" readonly="readonly" class="input-large hidden" type="text" value="[![Latest Stable Version]({{ url('pugx_badge_version',{'repository': repository}) }})]({{ url('pugx_badge_packagist',{'repository': repository}) }}) [![Latest Unstable Version]({{ url('pugx_badge_version_latest',{'repository': repository, 'latest':'unstable'}) }})]({{ path('pugx_badge_packagist',{'repository': repository}) }}) [![Total Downloads]({{ url('pugx_badge_download',{'repository': repository}) }})]({{ url('pugx_badge_packagist',{'repository': repository}) }}) [![composer.lock available]({{ url('pugx_badge_composerlock',{'repository': repository}) }})]
<input id="clip_all_markdown" readonly="readonly" class="input-large hidden" type="text" value="[![Latest Stable Version]({{ url('pugx_badge_version',{'repository': repository}) }})]({{ url('pugx_badge_packagist',{'repository': repository}) }}) [![Latest Unstable Version]({{ url('pugx_badge_version_latest',{'repository': repository, 'latest':'unstable'}) }})]({{ path('pugx_badge_packagist',{'repository': repository}) }}) [![Total Downloads]({{ url('pugx_badge_download',{'repository': repository}) }})]({{ url('pugx_badge_packagist',{'repository': repository}) }}) [![composer.lock available]({{ url('pugx_badge_composerlock',{'repository': repository}) }})] [![.gitattributes available]({{ url('pugx_badge_gitattributes',{'repository': repository}) }})]
({{ url('pugx_badge_packagist',{'repository': repository}) }})">
</div>
Expand Down Expand Up @@ -235,6 +236,31 @@
</a>
</div>
</div>

<div class="row-fluid">
<a name="gitattributes"></a>
<h4>.gitattributes available</h4>

<a title="copy to clipboard" class="copy_on_clip"
data-clipboard-target="#gitattributes_markdown"
>
<img class="spinned gitattributes_img" src="{{ path('pugx_badge_gitattributes',{'repository': repository}) }}">
</a>

<div class="input-prepend input-append pull-right combined-box">
<span class="add-on">markdown</span>
<input id="gitattributes_markdown" readonly="readonly"
class="input-large -input" type="text"
value="[![.gitattributes available]({{ url('pugx_badge_gitattributes',{'repository': repository}) }})]({{ url('pugx_badge_packagist',{'repository': repository}) }})"
>
<a title="copy to clipboard" class="add-on copy_on_clip btn gitattributes_clip"
data-clipboard-target="#gitattributes_markdown"
>
<i class="icon-share"></i>
</a>
</div>
</div>

</div>
</article>
</div>
Expand Down
@@ -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);
}
}
Expand Up @@ -31,7 +31,7 @@ public function setUp()

public function testAllAction()
{
$expectedData = '{"clip_all":{"markdown":"[![Latest Stable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/stable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![Total Downloads](http:\/\/localhost\/pugx\/badge-poser\/downloads)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![Latest Unstable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/unstable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![License](http:\/\/localhost\/pugx\/badge-poser\/license)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)"},"latest_stable_version":{"markdown":"[![Latest Stable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/stable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/v\/stable"},"total":{"markdown":"[![Total Downloads](http:\/\/localhost\/pugx\/badge-poser\/downloads)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/downloads"},"latest_unstable_version":{"markdown":"[![Latest Unstable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/unstable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/v\/unstable"},"license":{"markdown":"[![License](http:\/\/localhost\/pugx\/badge-poser\/license)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/license"},"monthly":{"markdown":"[![Monthly Downloads](http:\/\/localhost\/pugx\/badge-poser\/d\/monthly)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/d\/monthly"},"daily":{"markdown":"[![Daily Downloads](http:\/\/localhost\/pugx\/badge-poser\/d\/daily)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/d\/daily"},"version":{"markdown":"[![Version](http:\/\/localhost\/pugx\/badge-poser\/version)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/version"},"composerlock":{"markdown":"[![composer.lock](http:\/\/localhost\/pugx\/badge-poser\/composerlock)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/composerlock"},"repository":{"html":"pugx\/badge-poser"}}';
$expectedData = '{"clip_all":{"markdown":"[![Latest Stable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/stable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![Total Downloads](http:\/\/localhost\/pugx\/badge-poser\/downloads)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![Latest Unstable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/unstable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser) [![License](http:\/\/localhost\/pugx\/badge-poser\/license)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)"},"latest_stable_version":{"markdown":"[![Latest Stable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/stable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/v\/stable"},"total":{"markdown":"[![Total Downloads](http:\/\/localhost\/pugx\/badge-poser\/downloads)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/downloads"},"latest_unstable_version":{"markdown":"[![Latest Unstable Version](http:\/\/localhost\/pugx\/badge-poser\/v\/unstable)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/v\/unstable"},"license":{"markdown":"[![License](http:\/\/localhost\/pugx\/badge-poser\/license)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/license"},"monthly":{"markdown":"[![Monthly Downloads](http:\/\/localhost\/pugx\/badge-poser\/d\/monthly)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/d\/monthly"},"daily":{"markdown":"[![Daily Downloads](http:\/\/localhost\/pugx\/badge-poser\/d\/daily)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/d\/daily"},"version":{"markdown":"[![Version](http:\/\/localhost\/pugx\/badge-poser\/version)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/version"},"composerlock":{"markdown":"[![composer.lock](http:\/\/localhost\/pugx\/badge-poser\/composerlock)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/composerlock"},"gitattributes":{"markdown":"[![.gitattributes](http:\/\/localhost\/pugx\/badge-poser\/gitattributes)](https:\/\/packagist.org\/packages\/pugx\/badge-poser)","img":"http:\/\/localhost\/pugx\/badge-poser\/gitattributes"},"repository":{"html":"pugx\/badge-poser"}}';

$client = static::createClient();
static::$kernel->getContainer()->set('packagist_client', $this->packagistClient);
Expand Down

0 comments on commit 9095ac2

Please sign in to comment.