Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
hooks.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage GitHub
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * GitHub API Hooks class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage GitHub
17  * @since 12.3
18  */
20 {
21  /**
22  * Array containing the allowed hook events
23  *
24  * @var array
25  * @since 12.3
26  */
27  protected $events = array(
28  'push', 'issues', 'issue_comment', 'commit_comment', 'pull_request', 'gollum', 'watch', 'download', 'fork', 'fork_apply',
29  'member', 'public', 'status'
30  );
31 
32  /**
33  * Method to create a hook on a repository.
34  *
35  * @param string $user The name of the owner of the GitHub repository.
36  * @param string $repo The name of the GitHub repository.
37  * @param string $name The name of the service being called.
38  * @param array $config Array containing the config for the service.
39  * @param array $events The events the hook will be triggered for.
40  * @param boolean $active Flag to determine if the hook is active
41  *
42  * @return object
43  *
44  * @since 12.3
45  * @throws DomainException
46  * @throws RuntimeException
47  */
48  public function create($user, $repo, $name, array $config, array $events = array('push'), $active = true)
49  {
50  // Build the request path.
51  $path = '/repos/' . $user . '/' . $repo . '/hooks';
52 
53  // Check to ensure all events are in the allowed list
54  foreach ($events as $event)
55  {
56  if (!in_array($event, $this->events))
57  {
58  throw new RuntimeException('Your events array contains an unauthorized event.');
59  }
60  }
61 
62  $data = json_encode(
63  array('name' => $name, 'config' => $config, 'events' => $events, 'active' => $active)
64  );
65 
66  return $this->processResponse(
67  $this->client->post($this->fetchUrl($path), $data),
68  201
69  );
70  }
71 
72  /**
73  * Method to delete a hook
74  *
75  * @param string $user The name of the owner of the GitHub repository.
76  * @param string $repo The name of the GitHub repository.
77  * @param integer $id ID of the hook to delete.
78  *
79  * @return object
80  *
81  * @since 12.3
82  * @throws DomainException
83  */
84  public function delete($user, $repo, $id)
85  {
86  // Build the request path.
87  $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
88 
89  return $this->processResponse(
90  $this->client->delete($this->fetchUrl($path)),
91  204
92  );
93  }
94 
95  /**
96  * Method to edit a hook.
97  *
98  * @param string $user The name of the owner of the GitHub repository.
99  * @param string $repo The name of the GitHub repository.
100  * @param integer $id ID of the hook to edit.
101  * @param string $name The name of the service being called.
102  * @param array $config Array containing the config for the service.
103  * @param array $events The events the hook will be triggered for. This resets the currently set list
104  * @param array $addEvents Events to add to the hook.
105  * @param array $removeEvents Events to remove from the hook.
106  * @param boolean $active Flag to determine if the hook is active
107  *
108  * @return object
109  *
110  * @since 12.3
111  * @throws DomainException
112  * @throws RuntimeException
113  */
114  public function edit($user, $repo, $id, $name, array $config, array $events = array('push'), array $addEvents = array(),
115  array $removeEvents = array(), $active = true)
116  {
117  // Check to ensure all events are in the allowed list
118  foreach ($events as $event)
119  {
120  if (!in_array($event, $this->events))
121  {
122  throw new RuntimeException('Your events array contains an unauthorized event.');
123  }
124  }
125 
126  foreach ($addEvents as $event)
127  {
128  if (!in_array($event, $this->events))
129  {
130  throw new RuntimeException('Your active_events array contains an unauthorized event.');
131  }
132  }
133 
134  foreach ($removeEvents as $event)
135  {
136  if (!in_array($event, $this->events))
137  {
138  throw new RuntimeException('Your remove_events array contains an unauthorized event.');
139  }
140  }
141  // Build the request path.
142  $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
143 
144  $data = json_encode(
145  array(
146  'name' => $name,
147  'config' => $config,
148  'events' => $events,
149  'add_events' => $addEvents,
150  'remove_events' => $removeEvents,
151  'active' => $active)
152  );
153 
154  return $this->processResponse(
155  $this->client->patch($this->fetchUrl($path), $data)
156  );
157  }
158 
159  /**
160  * Method to get details about a single hook for the repository.
161  *
162  * @param string $user The name of the owner of the GitHub repository.
163  * @param string $repo The name of the GitHub repository.
164  * @param integer $id ID of the hook to retrieve
165  *
166  * @return object
167  *
168  * @since 12.3
169  * @throws DomainException
170  */
171  public function get($user, $repo, $id)
172  {
173  // Build the request path.
174  $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
175 
176  return $this->processResponse(
177  $this->client->get($this->fetchUrl($path))
178  );
179  }
180 
181  /**
182  * Method to list hooks for a repository.
183  *
184  * @param string $user The name of the owner of the GitHub repository.
185  * @param string $repo The name of the GitHub repository.
186  * @param integer $page Page to request
187  * @param integer $limit Number of results to return per page
188  *
189  * @return object
190  *
191  * @since 12.3
192  * @throws DomainException
193  */
194  public function getList($user, $repo, $page = 0, $limit = 0)
195  {
196  // Build the request path.
197  $path = '/repos/' . $user . '/' . $repo . '/hooks';
198 
199  return $this->processResponse(
200  $this->client->get($this->fetchUrl($path))
201  );
202  }
203 
204  /**
205  * Method to test a hook against the latest repository commit
206  *
207  * @param string $user The name of the owner of the GitHub repository.
208  * @param string $repo The name of the GitHub repository.
209  * @param integer $id ID of the hook to delete
210  *
211  * @return object
212  *
213  * @since 12.3
214  * @throws DomainException
215  */
216  public function test($user, $repo, $id)
217  {
218  // Build the request path.
219  $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id . '/test';
220 
221  return $this->processResponse(
222  $this->client->post($this->fetchUrl($path), json_encode('')),
223  204
224  );
225  }
226 }