Skip to content

Commit e05949d

Browse files
committed
[WIP] Addition of the Repository provider and the Repository object
1 parent 454394a commit e05949d

File tree

6 files changed

+170
-19
lines changed

6 files changed

+170
-19
lines changed

app/config/github.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ services:
66
- '@app.status_api.default'
77

88
parameters:
9-
# defines symfony repository as default
10-
default_repository: symfony/symfony
11-
129
# point to the main symfony repositories
1310
repositories:
1411
symfony/symfony:

app/config/services.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ services:
3232

3333
app.github.request_handler:
3434
class: AppBundle\Issues\GitHubRequestHandler
35-
arguments: ['@app.github.labels_api', '@event_dispatcher', '%repositories%', '@service_container']
35+
arguments: ['@app.github.labels_api', '@event_dispatcher', '@app.repository_provider', '@service_container']
3636

3737
app.github.exception_listener:
3838
class: AppBundle\Listener\GitHubExceptionListener
3939
arguments: ['%kernel.debug%']
4040
tags:
4141
- { name: kernel.event_subscriber }
42+
43+
app.repository_provider:
44+
class: AppBundle\Repository\Provider\InMemoryRepositoryProvider
45+
arguments: [ '%repositories%' ]

src/AppBundle/Issues/GitHubRequestHandler.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use AppBundle\Exception\GitHubRuntimeException;
1212
use AppBundle\Issues\GitHub\CachedLabelsApi;
1313
use AppBundle\Issues\GitHub\GitHubStatusApi;
14+
use AppBundle\Repository\Provider\RepositoryProviderInterface;
15+
use AppBundle\Repository\RepositoryRegistry;
1416
use Github\Api\Issue\Labels;
1517
use Symfony\Component\DependencyInjection\ContainerInterface;
1618
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -25,17 +27,17 @@ class GitHubRequestHandler
2527
{
2628
private $labelsApi;
2729
private $dispatcher;
28-
private $repositories;
30+
private $repositoryProvider;
2931
/**
3032
* @var ContainerInterface
3133
*/
3234
private $container;
3335

34-
public function __construct(Labels $labelsApi, EventDispatcherInterface $dispatcher, array $repositories, ContainerInterface $container)
36+
public function __construct(Labels $labelsApi, EventDispatcherInterface $dispatcher, RepositoryProviderInterface $repositoryProvider, ContainerInterface $container)
3537
{
3638
$this->labelsApi = $labelsApi;
3739
$this->dispatcher = $dispatcher;
38-
$this->repositories = $repositories;
40+
$this->repositoryProvider = $repositoryProvider;
3941
$this->container = $container;
4042
}
4143

@@ -52,33 +54,31 @@ public function handle(Request $request)
5254
throw new GitHubBadRequestException('Invalid JSON body!');
5355
}
5456

55-
$repository = isset($data['repository']['full_name']) ? $data['repository']['full_name'] : null;
56-
if (empty($repository)) {
57+
$repositoryFullName = isset($data['repository']['full_name']) ? $data['repository']['full_name'] : null;
58+
if (empty($repositoryFullName)) {
5759
throw new GitHubBadRequestException('No repository name!');
5860
}
5961

60-
if (!isset($this->repositories[$repository])) {
61-
throw new GitHubPreconditionFailedException(sprintf('Unsupported repository "%s".', $repository));
62+
$repository = $this->repositoryProvider->getRepository($repositoryFullName);
63+
64+
if (!$repository) {
65+
throw new GitHubPreconditionFailedException(sprintf('Unsupported repository "%s".', $repositoryFullName));
6266
}
6367

64-
$config = $this->repositories[$repository];
65-
if (isset($config['secret'])) {
68+
if ($repository->getSecret()) {
6669
if (!$request->headers->has('X-Hub-Signature')) {
6770
throw new GitHubAccessDeniedException('The request is not secured.');
6871
}
69-
if (!$this->authenticate($request->headers->get('X-Hub-Signature'), $config['secret'], $request->getContent())) {
72+
if (!$this->authenticate($request->headers->get('X-Hub-Signature'), $repository->getSecret(), $request->getContent())) {
7073
throw new GitHubAccessDeniedException('Invalid signature.');
7174
}
7275
}
73-
if (empty($config['subscribers'])) {
74-
throw new GitHubInvalidConfigurationException('The repository "%s" has no subscribers configured.');
75-
}
7676

77-
foreach ($config['subscribers'] as $subscriberId) {
77+
foreach ($repository->getSubscribers() as $subscriberId) {
7878
$this->dispatcher->addSubscriber($this->container->get($subscriberId));
7979
}
8080

81-
$event = new GitHubEvent($data, $repository, $config['maintainers']);
81+
$event = new GitHubEvent($data, $repository);
8282
$eventName = $request->headers->get('X-Github-Event');
8383

8484
try {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace AppBundle\Repository\Provider;
4+
5+
use AppBundle\Repository\Repository;
6+
7+
/**
8+
* @author Ener-Getick <egetick@gmail.com>
9+
*/
10+
class InMemoryRepositoryProvider implements RepositoryProviderInterface
11+
{
12+
private $repositories = array();
13+
14+
public function __construct(array $repositories)
15+
{
16+
foreach ($repositories as $repositoryFullName => $repositoryData) {
17+
if (1 !== substr_count($repositoryFullName, '/')) {
18+
throw new \InvalidArgumentException(sprintf('The repository name %s is invalid: it must be the form "username/repo_name"', $repositoryFullName));
19+
}
20+
21+
list($vendorName, $repositoryName) = explode('/', $repositoryFullName);
22+
23+
if (!isset($repositoryData['subscribers'])) {
24+
throw new \InvalidArgumentException(sprintf('The repository %s is missing a subscribers key!', $repositoryFullName));
25+
}
26+
27+
if (empty($repositoryData['subscribers'])) {
28+
throw new \InvalidArgumentException(sprintf('The repository "%s" has no subscribers configured.', $repositoryFullName));
29+
}
30+
31+
$this->addRepository(new Repository(
32+
$vendorName,
33+
$repositoryName,
34+
$repositoryData['subscribers']
35+
));
36+
}
37+
}
38+
39+
public function getRepository($repositoryName)
40+
{
41+
return isset($this->repositories[$repositoryName]) ? $this->repositories[$repositoryName] : null;
42+
}
43+
44+
private function addRepository(Repository $repository)
45+
{
46+
$this->repositories[strtolower($repository->getVendor().'/'.$repository->getName())] = $repository;
47+
}
48+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace AppBundle\Repository\Provider;
4+
5+
use AppBundle\Repository\Repository;
6+
7+
/**
8+
* @author Ener-Getick <egetick@gmail.com>
9+
*/
10+
interface RepositoryProviderInterface
11+
{
12+
/**
13+
* @param string $repositoryName e.g. symfony/symfony-docs
14+
*
15+
* @return Repository
16+
*/
17+
public function getRepository($repositoryName);
18+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace AppBundle\Repository;
4+
5+
/**
6+
* @author Ener-Getick <egetick@gmail.com>
7+
*/
8+
class Repository
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $vendor;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $name;
19+
20+
/**
21+
* An array of subscriber service ids
22+
*
23+
* @var array
24+
*/
25+
private $subscribers;
26+
27+
public function __construct($vendor, $name, array $subscribers)
28+
{
29+
$this->vendor = $vendor;
30+
$this->name = $name;
31+
$this->subscribers = $subscribers;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getVendor()
38+
{
39+
return $this->vendor;
40+
}
41+
42+
/**
43+
* @param string $vendor
44+
*/
45+
public function setVendor($vendor)
46+
{
47+
$this->vendor = $vendor;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getName()
54+
{
55+
return $this->name;
56+
}
57+
58+
/**
59+
* @param string $name
60+
*/
61+
public function setName($name)
62+
{
63+
$this->name = $name;
64+
}
65+
66+
public function getSubscribers()
67+
{
68+
return $this->subscribers;
69+
}
70+
71+
/**
72+
* @return string
73+
*/
74+
public function getSecret()
75+
{
76+
// todo - implement setting a secret
77+
}
78+
79+
public function getMaintainers()
80+
{
81+
// todo - implement this
82+
return [];
83+
}
84+
}

0 commit comments

Comments
 (0)