1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Github Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2016 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\Orgs; |
10
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* GitHub API Orgs Hooks class for the Joomla Framework. |
15
|
|
|
* |
16
|
|
|
* All actions against organization webhooks require the authenticated user to be an admin of the organization being managed. |
17
|
|
|
* Additionally, OAuth tokens require the "admin:org_hook" scope. |
18
|
|
|
* |
19
|
|
|
* @documentation http://developer.github.com/v3/orgs/hooks/ |
20
|
|
|
* |
21
|
|
|
* @since 1.4.0 |
22
|
|
|
*/ |
23
|
|
|
class Hooks extends AbstractPackage |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* List hooks. |
27
|
|
|
* |
28
|
|
|
* @param string $org The name of the organization. |
29
|
|
|
* |
30
|
|
|
* @return object |
31
|
|
|
* |
32
|
|
|
* @since 1.4.0 |
33
|
|
|
*/ |
34
|
|
|
public function getList($org) |
35
|
|
|
{ |
36
|
|
|
// Build the request path. |
37
|
|
|
$path = "/orgs/$org/hooks"; |
38
|
|
|
|
39
|
|
|
return $this->processResponse( |
40
|
|
|
$this->client->get($this->fetchUrl($path)) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get single hook. |
46
|
|
|
* |
47
|
|
|
* @param string $org The name of the organization. |
48
|
|
|
* @param integer $id The hook id. |
49
|
|
|
* |
50
|
|
|
* @return object |
51
|
|
|
* |
52
|
|
|
* @since 1.4.0 |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function get($org, $id) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
// Build the request path. |
57
|
|
|
$path = "/orgs/$org/hooks/" . (int) $id; |
58
|
|
|
|
59
|
|
|
return $this->processResponse( |
60
|
|
|
$this->client->get($this->fetchUrl($path)) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a hook. |
66
|
|
|
* |
67
|
|
|
* @param string $org The name of the organization. |
68
|
|
|
* @param string $url The URL to which the payloads will be delivered. |
69
|
|
|
* @param string $contentType The media type used to serialize the payloads. Supported values include "json" and "form". |
70
|
|
|
* @param string $secret If provided, payloads will be delivered with an X-Hub-Signature header. |
71
|
|
|
* The value of this header is computed as the |
72
|
|
|
* [HMAC hex digest of the body, using the secret as the key][hub-signature]. |
73
|
|
|
* @param boolean $insecureSsl Determines whether the SSL certificate of the host for url will be verified when delivering payloads. |
74
|
|
|
* If false, verification is performed. If true, verification is not performed. |
75
|
|
|
* @param array $events Determines what events the hook is triggered for. |
76
|
|
|
* @param boolean $active Determines whether the hook is actually triggered on pushes. |
77
|
|
|
* |
78
|
|
|
* @return object |
79
|
|
|
* |
80
|
|
|
* @since 1.4.0 |
81
|
|
|
* @throws \UnexpectedValueException |
82
|
|
|
*/ |
83
|
|
|
public function create($org, $url, $contentType = 'form', $secret = null, $insecureSsl = false, array $events = array('push'), $active = true) |
84
|
|
|
{ |
85
|
|
|
// Build the request path. |
86
|
|
|
$path = "/orgs/$org/hooks"; |
87
|
|
|
|
88
|
|
View Code Duplication |
if (false == in_array($contentType, array('form', 'json'))) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
throw new \UnexpectedValueException('Content type must be either "form" or "json".'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$config = array( |
94
|
|
|
'url' => $url, |
95
|
|
|
'content_type' => $contentType, |
96
|
|
|
'insecure_ssl' => (int) $insecureSsl, |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
if ($secret) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$config['secret'] = $secret; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$data = array( |
105
|
|
|
'name' => 'web', |
106
|
|
|
'active' => $active, |
107
|
|
|
'config' => (object) $config, |
108
|
|
|
); |
109
|
|
|
|
110
|
|
View Code Duplication |
if (!empty($events)) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
// Check to ensure all events are in the allowed list |
113
|
|
|
foreach ($events as $event) |
114
|
|
|
{ |
115
|
|
|
if (!in_array($event, $this->hookEvents)) |
116
|
|
|
{ |
117
|
|
|
throw new \RuntimeException('Your events array contains an unauthorized event.'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$data['events'] = $events; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->processResponse( |
125
|
|
|
$this->client->post($this->fetchUrl($path), $data), |
126
|
|
|
201 |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Edit a hook. |
132
|
|
|
* |
133
|
|
|
* @param string $org The name of the organization. |
134
|
|
|
* @param string $url The URL to which the payloads will be delivered. |
135
|
|
|
* @param string $contentType The media type used to serialize the payloads. Supported values include "json" and "form". |
136
|
|
|
* @param string $secret If provided, payloads will be delivered with an X-Hub-Signature header. |
137
|
|
|
* The value of this header is computed as the |
138
|
|
|
* [HMAC hex digest of the body, using the secret as the key][hub-signature]. |
139
|
|
|
* @param boolean $insecureSsl Determines whether the SSL certificate of the host for url will be verified when delivering payloads. |
140
|
|
|
* If false, verification is performed. If true, verification is not performed. |
141
|
|
|
* @param array $events Determines what events the hook is triggered for. |
142
|
|
|
* @param boolean $active Determines whether the hook is actually triggered on pushes. |
143
|
|
|
* |
144
|
|
|
* @return object |
145
|
|
|
* |
146
|
|
|
* @since 1.4.0 |
147
|
|
|
* @throws \UnexpectedValueException |
148
|
|
|
*/ |
149
|
|
|
public function edit($org, $url, $contentType = null, $secret = null, $insecureSsl = null, array $events = array(), $active = null) |
150
|
|
|
{ |
151
|
|
|
// Build the request path. |
152
|
|
|
$path = "/orgs/$org/hooks"; |
153
|
|
|
|
154
|
|
|
$config = array( |
155
|
|
|
'url' => $url, |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
if ($contentType) |
|
|
|
|
159
|
|
|
{ |
160
|
|
View Code Duplication |
if (false == in_array($contentType, array('form', 'json'))) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
throw new \UnexpectedValueException('Content type must be either "form" or "json".'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$config['content_type'] = $contentType; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($insecureSsl !== null) |
169
|
|
|
{ |
170
|
|
|
$config['insecure_ssl'] = (int) $insecureSsl; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ($secret) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$config['secret'] = $secret; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$data = array( |
179
|
|
|
'config' => (object) $config, |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
if ($active !== null) |
183
|
|
|
{ |
184
|
|
|
$data['active'] = (bool) $active; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
View Code Duplication |
if (!empty($events)) |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
// Check to ensure all events are in the allowed list |
190
|
|
|
foreach ($events as $event) |
191
|
|
|
{ |
192
|
|
|
if (!in_array($event, $this->hookEvents)) |
193
|
|
|
{ |
194
|
|
|
throw new \RuntimeException('Your events array contains an unauthorized event.'); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$data['events'] = $events; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this->processResponse( |
202
|
|
|
$this->client->post($this->fetchUrl($path), $data), |
203
|
|
|
201 |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Ping a hook. |
209
|
|
|
* |
210
|
|
|
* @param string $org The name of the organization |
211
|
|
|
* @param integer $id ID of the hook to ping |
212
|
|
|
* |
213
|
|
|
* @return object |
214
|
|
|
* |
215
|
|
|
* @since 1.4.0 |
216
|
|
|
* @throws \DomainException |
217
|
|
|
*/ |
218
|
|
View Code Duplication |
public function ping($org, $id) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
// Build the request path. |
221
|
|
|
$path = "/orgs/$org/hooks/$id/pings"; |
222
|
|
|
|
223
|
|
|
return $this->processResponse( |
224
|
|
|
$this->client->post($this->fetchUrl($path), json_encode('')), |
225
|
|
|
204 |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Delete a hook. |
231
|
|
|
* |
232
|
|
|
* @param string $org The name of the organization |
233
|
|
|
* @param integer $id ID of the hook to delete |
234
|
|
|
* |
235
|
|
|
* @return object |
236
|
|
|
* |
237
|
|
|
* @since 1.4.0 |
238
|
|
|
*/ |
239
|
|
|
public function delete($org, $id) |
240
|
|
|
{ |
241
|
|
|
// Build the request path. |
242
|
|
|
$path = "/orgs/$org/hooks/$id"; |
243
|
|
|
|
244
|
|
|
return $this->processResponse( |
245
|
|
|
$this->client->delete($this->fetchUrl($path)), |
246
|
|
|
204 |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
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.