1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Github Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Github\Package\Repositories; |
10
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* GitHub API Hooks class for the Joomla Framework. |
15
|
|
|
* |
16
|
|
|
* @documentation http://developer.github.com/v3/repos/hooks |
17
|
|
|
* |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class Hooks extends AbstractPackage |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Create a hook. |
24
|
|
|
* |
25
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
26
|
|
|
* @param string $repo The name of the GitHub repository. |
27
|
|
|
* @param string $name The name of the service being called. |
28
|
|
|
* @param array $config Array containing the config for the service. |
29
|
|
|
* @param array $events The events the hook will be triggered for. |
30
|
|
|
* @param boolean $active Flag to determine if the hook is active |
31
|
|
|
* |
32
|
|
|
* @return object |
33
|
|
|
* |
34
|
|
|
* @since 1.0 |
35
|
|
|
* @throws \DomainException |
36
|
|
|
* @throws \RuntimeException |
37
|
|
|
*/ |
38
|
|
|
public function create($user, $repo, $name, $config, array $events = array('push'), $active = true) |
39
|
|
|
{ |
40
|
|
|
// Build the request path. |
41
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/hooks'; |
42
|
|
|
|
43
|
|
|
// Check to ensure all events are in the allowed list |
44
|
|
|
foreach ($events as $event) |
45
|
|
|
{ |
46
|
|
|
if (!in_array($event, $this->hookEvents)) |
47
|
|
|
{ |
48
|
|
|
throw new \RuntimeException('Your events array contains an unauthorized event.'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$data = json_encode( |
53
|
|
|
array('name' => $name, 'config' => $config, 'events' => $events, 'active' => $active) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
return $this->processResponse( |
57
|
|
|
$this->client->post($this->fetchUrl($path), $data), |
58
|
|
|
201 |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Delete a hook |
64
|
|
|
* |
65
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
66
|
|
|
* @param string $repo The name of the GitHub repository. |
67
|
|
|
* @param integer $id ID of the hook to delete. |
68
|
|
|
* |
69
|
|
|
* @return object |
70
|
|
|
* |
71
|
|
|
* @since 1.0 |
72
|
|
|
* @throws \DomainException |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function delete($user, $repo, $id) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
// Build the request path. |
77
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; |
78
|
|
|
|
79
|
|
|
return $this->processResponse( |
80
|
|
|
$this->client->delete($this->fetchUrl($path)), |
81
|
|
|
204 |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Edit a hook. |
87
|
|
|
* |
88
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
89
|
|
|
* @param string $repo The name of the GitHub repository. |
90
|
|
|
* @param integer $id ID of the hook to edit. |
91
|
|
|
* @param string $name The name of the service being called. |
92
|
|
|
* @param array $config Array containing the config for the service. |
93
|
|
|
* @param array $events The events the hook will be triggered for. This resets the currently set list |
94
|
|
|
* @param array $addEvents Events to add to the hook. |
95
|
|
|
* @param array $removeEvents Events to remove from the hook. |
96
|
|
|
* @param boolean $active Flag to determine if the hook is active |
97
|
|
|
* |
98
|
|
|
* @return object |
99
|
|
|
* |
100
|
|
|
* @since 1.0 |
101
|
|
|
* @throws \DomainException |
102
|
|
|
* @throws \RuntimeException |
103
|
|
|
*/ |
104
|
|
|
public function edit($user, $repo, $id, $name, $config, array $events = array('push'), array $addEvents = array(), |
105
|
|
|
array $removeEvents = array(), $active = true) |
106
|
|
|
{ |
107
|
|
|
// Check to ensure all events are in the allowed list |
108
|
|
|
foreach ($events as $event) |
109
|
|
|
{ |
110
|
|
|
if (!in_array($event, $this->hookEvents)) |
111
|
|
|
{ |
112
|
|
|
throw new \RuntimeException('Your events array contains an unauthorized event.'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($addEvents as $event) |
117
|
|
|
{ |
118
|
|
|
if (!in_array($event, $this->hookEvents)) |
119
|
|
|
{ |
120
|
|
|
throw new \RuntimeException('Your active_events array contains an unauthorized event.'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
foreach ($removeEvents as $event) |
125
|
|
|
{ |
126
|
|
|
if (!in_array($event, $this->hookEvents)) |
127
|
|
|
{ |
128
|
|
|
throw new \RuntimeException('Your remove_events array contains an unauthorized event.'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Build the request path. |
133
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; |
134
|
|
|
|
135
|
|
|
$data = json_encode( |
136
|
|
|
array( |
137
|
|
|
'name' => $name, |
138
|
|
|
'config' => $config, |
139
|
|
|
'events' => $events, |
140
|
|
|
'add_events' => $addEvents, |
141
|
|
|
'remove_events' => $removeEvents, |
142
|
|
|
'active' => $active) |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
return $this->processResponse( |
146
|
|
|
$this->client->patch($this->fetchUrl($path), $data) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get single hook. |
152
|
|
|
* |
153
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
154
|
|
|
* @param string $repo The name of the GitHub repository. |
155
|
|
|
* @param integer $id ID of the hook to retrieve |
156
|
|
|
* |
157
|
|
|
* @return object |
158
|
|
|
* |
159
|
|
|
* @since 1.0 |
160
|
|
|
* @throws \DomainException |
161
|
|
|
*/ |
162
|
|
|
public function get($user, $repo, $id) |
163
|
|
|
{ |
164
|
|
|
// Build the request path. |
165
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; |
166
|
|
|
|
167
|
|
|
return $this->processResponse( |
168
|
|
|
$this->client->get($this->fetchUrl($path)) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* List hooks. |
174
|
|
|
* |
175
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
176
|
|
|
* @param string $repo The name of the GitHub repository. |
177
|
|
|
* |
178
|
|
|
* @return object |
179
|
|
|
* |
180
|
|
|
* @since 1.0 |
181
|
|
|
* @throws \DomainException |
182
|
|
|
*/ |
183
|
|
|
public function getList($user, $repo) |
184
|
|
|
{ |
185
|
|
|
// Build the request path. |
186
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/hooks'; |
187
|
|
|
|
188
|
|
|
return $this->processResponse( |
189
|
|
|
$this->client->get($this->fetchUrl($path)) |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Ping a hook. |
195
|
|
|
* |
196
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
197
|
|
|
* @param string $repo The name of the GitHub repository. |
198
|
|
|
* @param integer $id ID of the hook to ping |
199
|
|
|
* |
200
|
|
|
* @return object |
201
|
|
|
* |
202
|
|
|
* @since 1.4.0 |
203
|
|
|
* @throws \DomainException |
204
|
|
|
*/ |
205
|
|
|
public function ping($user, $repo, $id) |
206
|
|
|
{ |
207
|
|
|
// Build the request path. |
208
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id . '/pings'; |
209
|
|
|
|
210
|
|
|
return $this->processResponse( |
211
|
|
|
$this->client->post($this->fetchUrl($path), json_encode('')), |
212
|
|
|
204 |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Test a `push` hook. |
218
|
|
|
* |
219
|
|
|
* @param string $user The name of the owner of the GitHub repository. |
220
|
|
|
* @param string $repo The name of the GitHub repository. |
221
|
|
|
* @param integer $id ID of the hook to test |
222
|
|
|
* |
223
|
|
|
* @return object |
224
|
|
|
* |
225
|
|
|
* @since 1.0 |
226
|
|
|
* @throws \DomainException |
227
|
|
|
*/ |
228
|
|
|
public function test($user, $repo, $id) |
229
|
|
|
{ |
230
|
|
|
// Build the request path. |
231
|
|
|
$path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id . '/test'; |
232
|
|
|
|
233
|
|
|
return $this->processResponse( |
234
|
|
|
$this->client->post($this->fetchUrl($path), json_encode('')), |
235
|
|
|
204 |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.