Skip to content

Commit c6c44ab

Browse files
committed
Refactoring using a GitHubRequestHandler
1 parent 93b734d commit c6c44ab

18 files changed

+233
-122
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/app/bootstrap.php.cache
22
/app/cache/*
33
!app/cache/.gitkeep
4+
/app/config/github.yml
45
/app/config/parameters.yml
56
/app/logs/*
67
!app/logs/.gitkeep

app/config/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ imports:
22
- { resource: parameters.yml }
33
- { resource: security.yml }
44
- { resource: services.yml }
5+
- { resource: github.yml }
56

67
# Put parameters here that don't need to change on each machine where the app is deployed
78
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration

app/config/github.yml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
# token used to update labels on the repository, etc
3+
github_token: XXX
4+
5+
# defines symfony repository as default
6+
default_repository: symfony/symfony
7+
8+
# point to the main symfony repositories
9+
repositories:
10+
symfony/symfony:
11+
listeners:
12+
- AppBundle\Listener\SymfonyIssueListener
13+
secret: change_me
14+
symfony/symfony-docs:
15+
listeners:
16+
- AppBundle\Listener\SymfonyDocsIssueListener

app/config/parameters.yml.dist

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,3 @@ parameters:
1717

1818
# A secret key that's used to generate certain security-related tokens
1919
secret: ThisTokenIsNotSoSecretChangeIt
20-
21-
# token used to update labels on the repository, etc
22-
github_token: XXXX
23-
# defines symfony repository as default
24-
default_repository: symfony/symfony
25-
# point to the main symfony repositories
26-
github_listeners:
27-
symfony/symfony: 'AppBundle\Listener\SymfonyIssueListener'
28-
symfony/symfony-docs: 'AppBundle\Listener\SymfonyDocsIssueListener'
29-
symfony/symfony-standard: 'AppBundle\Listener\SymfonyStandardIssueListener'

app/config/services.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ services:
3030
class: AppBundle\Issues\GitHub\GitHubStatusApi
3131
arguments: ['@app.github.cached_labels_api', '%default_repository%']
3232

33-
app.github.listener_factory:
34-
class: AppBundle\Issues\GitHubListenerFactory
35-
arguments: ['@app.github.labels_api', '%github_listeners%']
33+
app.github.request_handler:
34+
class: AppBundle\Issues\GitHubRequestHandler
35+
arguments: ['@app.github.labels_api', '@event_dispatcher', '%repositories%']
3636

3737
app.github.exception_listener:
38-
class: Appbundle\Listener\ExceptionListener
38+
class: Appbundle\Listener\GitHubExceptionListener
39+
arguments: ['%kernel.debug%']
3940
tags:
4041
- { name: kernel.event_subscriber }

src/AppBundle/Controller/WebhookController.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace AppBundle\Controller;
44

5-
use AppBundle\Event\GitHubEvent;
6-
use AppBundle\Exception\GitHubException;
75
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
86
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
97
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
@@ -18,42 +16,8 @@ class WebhookController extends Controller
1816
*/
1917
public function githubAction(Request $request)
2018
{
21-
$data = json_decode($request->getContent(), true);
22-
if (null === $data) {
23-
throw new GitHubException('Invalid JSON body!');
24-
}
25-
26-
$repository = isset($data['repository']['full_name']) ? $data['repository']['full_name'] : null;
27-
if (empty($repository)) {
28-
throw new GitHubException('No repository name!');
29-
}
30-
31-
$listener = $this->get('app.github.listener_factory')->createFromRepository($repository);
32-
33-
$dispatcher = $this->get('event_dispatcher');
34-
$dispatcher->addSubscriber($listener);
35-
36-
$event = new GitHubEvent($data);
37-
$eventName = $request->headers->get('X-Github-Event');
38-
39-
try {
40-
$dispatcher->dispatch('github.'.$eventName, $event);
41-
} catch (\Exception $e) {
42-
throw new GitHubException(sprintf('Failed dispatching "%s" event for "%s" repository.', $eventName, $repository), 0, $e);
43-
}
44-
45-
$responseData = $event->getResponseData();
46-
47-
if (empty($responseData)) {
48-
$responseData['unsupported_event'] = $eventName;
49-
}
19+
$responseData = $this->get('app.github.request_handler')->handle($request);
5020

5121
return new JsonResponse($responseData);
52-
53-
// 1 read in what event they have
54-
// 2 perform some action
55-
// 3 return JSON
56-
57-
// log something to the database?
5822
}
5923
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace AppBundle\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
6+
7+
/**
8+
* GitHubAccessDeniedException.
9+
*
10+
* @author Jules Pietri <jules@heahprod.com>
11+
*/
12+
class GitHubAccessDeniedException extends AccessDeniedHttpException implements GitHubExceptionInterface
13+
{
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace AppBundle\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
6+
7+
/**
8+
* GitHubBadRequestException.
9+
*
10+
* @author Jules Pietri <jules@heahprod.com>
11+
*/
12+
class GitHubBadRequestException extends BadRequestHttpException implements GitHubExceptionInterface
13+
{
14+
}

src/AppBundle/Exception/GitHubException.php

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace AppBundle\Exception;
4+
5+
/**
6+
* GitHubExceptionInterface.
7+
*
8+
* @author Jules Pietri <jules@heahprod.com>
9+
*/
10+
interface GitHubExceptionInterface
11+
{
12+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy