-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBitBucketProcessor.php
79 lines (60 loc) · 2.09 KB
/
BitBucketProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace ThirdParty\BitBucket\Webhook\Incoming\Processor;
use Symfony\Component\HttpFoundation\Request;
use Component\Webhook\Incoming\ProcessorInterface;
class BitBucketProcessor implements ProcessorInterface
{
/** @var string */
protected $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function process(Request $request): void
{
if (!$request->headers->has('X-Event-Key')) {
return;
}
$event = $request->headers->get('X-Event-Key');
$contents = $request->getContent(false);
$payload = json_decode($contents, true, 32);
if (null === $payload) {
throw new \InvalidArgumentException('Could not decode json payload.');
}
if ('repo:push' === $event) {
list($action, $username) = $this->processPushHook($event, $payload);
$request->request->set('action', $action);
$request->request->set('username', $username);
}
if (preg_match('/^pullrequest:/', $event)) {
list($action, $username) = $this->processMergeRequestHook($event, $payload);
$request->request->set('action', $action);
$request->request->set('username', $username);
}
}
public function processPushHook(string $event, array $payload): array
{
$action = 'push';
$username = '';
if (isset($payload['actor']['username'])) {
$username = $payload['actor']['username'];
}
return [$action, $username];
}
public function processMergeRequestHook(string $event, array $payload): array
{
$action = '';
$username = '';
if (isset($payload['actor']['username'])) {
$username = $payload['actor']['username'];
}
if (preg_match('/^pullrequest:(?P<action>[A-Za-z]+)$/', $event, $matches) > 0) {
$action = sprintf('pull_request.%s', $matches['action']);
}
return [$action, $username];
}
}