1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once 'vendor/autoload.php'; |
4
|
|
|
|
5
|
|
|
$server = array( |
6
|
|
|
'HTTP_CONTENT_LENGTH' => '2830', |
7
|
|
|
'HTTP_CONTENT_TYPE' => 'application/json', |
8
|
|
|
'HTTP_X_REQUEST_UUID' => '07606ab4-601c-4ca4-8c93-b979d3c7b3a1', |
9
|
|
|
'HTTP_X_ATTEMPT_NUMBER' => '1', |
10
|
|
|
'HTTP_X_EVENT_KEY' => 'repo:push', |
11
|
|
|
'REMOTE_ADDR' => '131.103.20.165', |
12
|
|
|
// 'REMOTE_ADDR' => '8.8.8.8', |
|
|
|
|
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
$calls = array( |
16
|
|
|
'issue' => array( |
17
|
|
|
'comment_created' => array('actor', 'repository', 'issue', 'comment'), |
18
|
|
|
'created' => array('actor', 'repository', 'issue'), |
19
|
|
|
'updated' => array('actor', 'repository', 'issue', 'comment', 'changes'), |
20
|
|
|
), |
21
|
|
|
'pullrequest' => array( |
22
|
|
|
'comment_created' => array('actor', 'repository', 'pullrequest', 'comment'), |
23
|
|
|
'comment_deleted' => array('actor', 'repository', 'pullrequest', 'comment'), |
24
|
|
|
'comment_updated' => array('actor', 'repository', 'pullrequest', 'comment'), |
25
|
|
|
'created' => array('actor', 'repository', 'pullrequest'), |
26
|
|
|
'updated' => array('actor', 'repository', 'pullrequest'), |
27
|
|
|
'approved' => array('actor', 'repository', 'pullrequest', 'approval'), |
28
|
|
|
'unapproved' => array('actor', 'repository', 'pullrequest', 'approval'), |
29
|
|
|
'fulfilled' => array('actor', 'repository', 'pullrequest'), |
30
|
|
|
'rejected' => array('actor', 'repository', 'pullrequest'), |
31
|
|
|
), |
32
|
|
|
'repo' => array( |
33
|
|
|
'commit_comment_created' => array('actor', 'repository', 'comment', 'commit'), |
34
|
|
|
'commit_status_created' => array('actor', 'repository', 'commit_status'), |
35
|
|
|
'commit_status_updated' => array('actor', 'repository', 'commit_status'), |
36
|
|
|
'fork' => array('actor', 'repository', 'fork'), |
37
|
|
|
'push' => array('actor', 'repository', 'push'), |
38
|
|
|
), |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$webhook = new \Smalot\Bitbucket\Webhook\Webhook(); |
42
|
|
|
$map = $webhook->getEventMap(); |
43
|
|
|
|
44
|
|
|
foreach ($calls as $group => $types) { |
45
|
|
|
foreach ($types as $type => $properties) { |
46
|
|
|
echo 'check ' . $group . ':' . $type . "\n"; |
47
|
|
|
|
48
|
|
|
$server['HTTP_X_EVENT_KEY'] = $group . ':' . $type; |
49
|
|
|
$content = file_get_contents('./test/payloads/' . $group . '/' . $type . '.json'); |
50
|
|
|
|
51
|
|
|
$request = new \Symfony\Component\HttpFoundation\Request( |
52
|
|
|
array(), array(), array(), array(), array(), $server, $content |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$event = $webhook->parseRequest($request); |
57
|
|
|
$classname = get_class($event); |
58
|
|
|
|
59
|
|
|
echo " check event class name\n"; |
60
|
|
|
|
61
|
|
|
if ($classname != trim($map[$group . ':' . $type], '\\')) { |
62
|
|
|
var_dump($classname, $map[$group . ':' . $type]); |
63
|
|
|
throw new \Exception('Invalid class used'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$model = $event->getModel(); |
67
|
|
|
|
68
|
|
|
echo " check model class name\n"; |
69
|
|
|
|
70
|
|
|
if (get_class($model) != trim(preg_replace('/Event/', 'Model', $map[$group . ':' . $type]), '\\')) { |
71
|
|
|
var_dump(get_class($model), trim(preg_replace('/Event/', 'Model', $map[$group . ':' . $type]), '\\')); |
72
|
|
|
throw new \Exception('Invalid class used'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
echo " check actions\n"; |
76
|
|
|
|
77
|
|
|
foreach ($properties as $property) { |
78
|
|
|
$action = 'get' . str_replace(' ', '', ucfirst(str_replace('_', ' ', $property))); |
79
|
|
|
|
80
|
|
|
echo ' ' . $action . "\n"; |
81
|
|
|
|
82
|
|
|
if (!method_exists($model, $action)) { |
83
|
|
|
var_dump(get_class($model), $action); |
84
|
|
|
throw new \Exception('Missing method'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!is_array($model->$action())) { |
88
|
|
|
var_dump($model, $action); |
89
|
|
|
throw new \Exception('Json not mapped'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} catch (Exception $e) { |
94
|
|
|
var_dump($e->getMessage()); |
95
|
|
|
die("error\n"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
die("done with success\n"); |
101
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.