1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CompoLab\Application\Http\Controller; |
4
|
|
|
|
5
|
|
|
use CompoLab\Application\GitlabRepositoryManager; |
6
|
|
|
use Gitlab\Client as Gitlab; |
7
|
|
|
use Gitlab\Model\Project; |
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
12
|
|
|
|
13
|
|
|
final class GitlabController |
14
|
|
|
{ |
15
|
|
|
/** @var Gitlab */ |
16
|
|
|
private $gitlab; |
17
|
|
|
|
18
|
|
|
/** @var GitlabRepositoryManager */ |
19
|
|
|
private $repositoryManager; |
20
|
|
|
|
21
|
|
|
public function __construct(Gitlab $gitlab, GitlabRepositoryManager $repositoryManager) |
22
|
|
|
{ |
23
|
|
|
$this->gitlab = $gitlab; |
24
|
|
|
$this->repositoryManager = $repositoryManager; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function handle(Request $request): Response |
28
|
|
|
{ |
29
|
|
|
$event = json_decode($request->getContent(), true); |
30
|
|
|
|
31
|
|
|
if (!isset($event['project_id'])) { |
32
|
|
|
throw new BadRequestHttpException('Missing project_id from body'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (!isset($event['event_name'])) { |
36
|
|
|
throw new BadRequestHttpException('Missing event_name from body'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!isset($event['project_id']) or !$project = Project::fromArray( |
40
|
|
|
$this->gitlab, |
41
|
|
|
$this->gitlab->projects()->show($event['project_id'])) |
|
|
|
|
42
|
|
|
){ |
43
|
|
|
throw new BadRequestHttpException('Impossible te retrieve a Gitlab project from the request'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
switch ($event['event_name']) { |
47
|
|
|
case 'project_destroy': |
48
|
|
|
$this->repositoryManager->deleteProject($project); |
49
|
|
|
break; |
50
|
|
|
|
51
|
|
|
case 'push': |
52
|
|
|
case 'tag_push': |
53
|
|
|
$this->repositoryManager->registerProject($project); |
54
|
|
|
break; |
55
|
|
|
|
56
|
|
|
default: return new JsonResponse([ |
57
|
|
|
'status' => 200, |
58
|
|
|
'message' => 'CompoLab has NOT handled the Gitlab event', |
59
|
|
|
'project_id' => $event['project_id'], |
60
|
|
|
'event_name' => $event['event_name'], |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->repositoryManager->save(); |
65
|
|
|
|
66
|
|
|
return new JsonResponse([ |
67
|
|
|
'status' => 200, |
68
|
|
|
'message' => 'CompoLab has successfully handled the Gitlab event', |
69
|
|
|
'project_id' => $event['project_id'], |
70
|
|
|
'event_name' => $event['event_name'], |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|