|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Slim\Http\Request; |
|
7
|
|
|
use Slim\Http\Response; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
final class HookController |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $secret; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
private $secured = FALSE; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var callable[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $router; |
|
27
|
|
|
|
|
28
|
8 |
|
public function __construct(ContainerInterface $ci, Handler $handler) |
|
29
|
|
|
{ |
|
30
|
8 |
|
$this->secret = $ci->get('settings')['secret']; |
|
31
|
|
|
|
|
32
|
|
|
$this->router['pipeline'] = function (array $event) use($handler) { |
|
33
|
1 |
|
foreach ($event['builds'] as $build) { |
|
34
|
1 |
|
if ($build['stage'] === 'deploy') { |
|
35
|
1 |
|
$handler->handleDeploy($event, $build); |
|
36
|
1 |
|
} |
|
37
|
1 |
|
} |
|
38
|
1 |
|
}; |
|
39
|
|
|
|
|
40
|
|
|
$this->router['push'] = function (array $event) use($handler) { |
|
41
|
1 |
|
$handler->handlePush($event); |
|
42
|
1 |
|
}; |
|
43
|
|
|
|
|
44
|
3 |
|
$this->router['tag_push'] = function (array $event) use($handler) { |
|
45
|
3 |
|
$handler->handleTag($event); |
|
46
|
3 |
|
}; |
|
47
|
8 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
8 |
|
public function __invoke(Request $request, Response $response, array $args) |
|
51
|
|
|
{ |
|
52
|
8 |
|
$body = $request->getParsedBody(); |
|
53
|
8 |
|
if ( ! isset($body['object_kind'])) { |
|
54
|
1 |
|
return $response->withStatus(500); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
7 |
|
foreach ($request->getHeader('X-Gitlab-Token') as $secret) { |
|
58
|
6 |
|
if ($secret == $this->secret) { // allow cast |
|
59
|
6 |
|
$this->secured = TRUE; |
|
60
|
6 |
|
} |
|
61
|
7 |
|
} |
|
62
|
|
|
|
|
63
|
7 |
|
if ($this->secret !== NULL && ! $this->secured) { |
|
64
|
1 |
|
return $response->withStatus(403); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
6 |
|
if (isset($this->router[$body['object_kind']])) { |
|
68
|
5 |
|
$this->router[$body['object_kind']]($body); |
|
69
|
5 |
|
return $response->withStatus(200); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
1 |
|
return $response->withStatus(404); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|