3
3
namespace AppBundle \Issues ;
4
4
5
5
use AppBundle \Event \GitHubEvent ;
6
- use AppBundle \Exception \GitHubAccessDeniedException ;
7
- use AppBundle \Exception \GitHubBadRequestException ;
8
- use AppBundle \Exception \GitHubExceptionInterface ;
9
- use AppBundle \Exception \GitHubInvalidConfigurationException ;
10
- use AppBundle \Exception \GitHubPreconditionFailedException ;
11
- use AppBundle \Exception \GitHubRuntimeException ;
12
6
use AppBundle \Repository \Provider \RepositoryProviderInterface ;
13
- use Github \Api \Issue \Labels ;
14
7
use Symfony \Component \DependencyInjection \ContainerInterface ;
15
8
use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
16
9
use Symfony \Component \HttpFoundation \Request ;
10
+ use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
11
+ use Symfony \Component \HttpKernel \Exception \PreconditionFailedHttpException ;
12
+ use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
17
13
18
14
/**
19
15
* Handles GitHub webhook requests
@@ -36,33 +32,31 @@ public function __construct(EventDispatcherInterface $dispatcher, RepositoryProv
36
32
/**
37
33
* @param Request $request
38
34
* @return array The response data
39
- *
40
- * @throws GitHubExceptionInterface When the request or the configuration are invalid
41
35
*/
42
36
public function handle (Request $ request )
43
37
{
44
38
$ data = json_decode ($ request ->getContent (), true );
45
39
if (null === $ data ) {
46
- throw new GitHubBadRequestException ('Invalid JSON body! ' );
40
+ throw new BadRequestHttpException ('Invalid JSON body! ' );
47
41
}
48
42
49
43
$ repositoryFullName = isset ($ data ['repository ' ]['full_name ' ]) ? $ data ['repository ' ]['full_name ' ] : null ;
50
44
if (empty ($ repositoryFullName )) {
51
- throw new GitHubBadRequestException ('No repository name! ' );
45
+ throw new BadRequestHttpException ('No repository name! ' );
52
46
}
53
47
54
48
$ repository = $ this ->repositoryProvider ->getRepository ($ repositoryFullName );
55
49
56
50
if (!$ repository ) {
57
- throw new GitHubPreconditionFailedException (sprintf ('Unsupported repository "%s". ' , $ repositoryFullName ));
51
+ throw new PreconditionFailedHttpException (sprintf ('Unsupported repository "%s". ' , $ repositoryFullName ));
58
52
}
59
53
60
54
if ($ repository ->getSecret ()) {
61
55
if (!$ request ->headers ->has ('X-Hub-Signature ' )) {
62
- throw new GitHubAccessDeniedException ('The request is not secured. ' );
56
+ throw new AccessDeniedException ('The request is not secured. ' );
63
57
}
64
58
if (!$ this ->authenticate ($ request ->headers ->get ('X-Hub-Signature ' ), $ repository ->getSecret (), $ request ->getContent ())) {
65
- throw new GitHubAccessDeniedException ('Invalid signature. ' );
59
+ throw new AccessDeniedException ('Invalid signature. ' );
66
60
}
67
61
}
68
62
@@ -76,22 +70,18 @@ public function handle(Request $request)
76
70
try {
77
71
$ this ->dispatcher ->dispatch ('github. ' .$ eventName , $ event );
78
72
} catch (\Exception $ e ) {
79
- throw new GitHubRuntimeException (sprintf ('Failed dispatching "%s" event for "%s" repository. ' , $ eventName , $ repository ), 0 , $ e );
73
+ throw new \ RuntimeException (sprintf ('Failed dispatching "%s" event for "%s" repository. ' , $ eventName , $ repository ), 0 , $ e );
80
74
}
81
75
82
76
$ responseData = $ event ->getResponseData ();
83
77
84
- if (empty ($ responseData )) {
85
- throw new GitHubPreconditionFailedException (sprintf ('Unsupported event "%s" ' , $ eventName ));
86
- }
87
-
88
78
return $ responseData ;
89
79
}
90
80
91
81
private function authenticate ($ hash , $ key , $ data )
92
82
{
93
83
if (!extension_loaded ('hash ' )) {
94
- throw new GitHubInvalidConfigurationException ('"hash" extension is needed to check request signature. ' );
84
+ throw new \ RuntimeException ('"hash" extension is needed to check request signature. ' );
95
85
}
96
86
97
87
return $ hash !== 'sha1= ' .hash_hmac ('sha1 ' , $ data , $ key );
0 commit comments