1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  GitHub
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 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  * @since       12.3
 16  * @deprecated  4.0  Use the `joomla/github` package via Composer instead
 17  */
 18 class JGithubHooks extends JGithubObject
 19 {
 20     /**
 21      * Array containing the allowed hook events
 22      *
 23      * @var    array
 24      * @since  12.3
 25      */
 26     protected $events = array(
 27         'push',
 28         'issues',
 29         'issue_comment',
 30         'commit_comment',
 31         'pull_request',
 32         'gollum',
 33         'watch',
 34         'download',
 35         'fork',
 36         'fork_apply',
 37         'member',
 38         'public',
 39         'status',
 40     );
 41 
 42     /**
 43      * Method to create a hook on a repository.
 44      *
 45      * @param   string   $user    The name of the owner of the GitHub repository.
 46      * @param   string   $repo    The name of the GitHub repository.
 47      * @param   string   $name    The name of the service being called.
 48      * @param   array    $config  Array containing the config for the service.
 49      * @param   array    $events  The events the hook will be triggered for.
 50      * @param   boolean  $active  Flag to determine if the hook is active
 51      *
 52      * @deprecated  use repositories->hooks->create()
 53      *
 54      * @return  object
 55      *
 56      * @since   12.3
 57      * @throws  DomainException
 58      * @throws  RuntimeException
 59      */
 60     public function create($user, $repo, $name, array $config, array $events = array('push'), $active = true)
 61     {
 62         // Build the request path.
 63         $path = '/repos/' . $user . '/' . $repo . '/hooks';
 64 
 65         // Check to ensure all events are in the allowed list
 66         foreach ($events as $event)
 67         {
 68             if (!in_array($event, $this->events))
 69             {
 70                 throw new RuntimeException('Your events array contains an unauthorized event.');
 71             }
 72         }
 73 
 74         $data = json_encode(
 75             array('name' => $name, 'config' => $config, 'events' => $events, 'active' => $active)
 76         );
 77 
 78         return $this->processResponse(
 79             $this->client->post($this->fetchUrl($path), $data),
 80             201
 81         );
 82     }
 83 
 84     /**
 85      * Method to delete a hook
 86      *
 87      * @param   string   $user  The name of the owner of the GitHub repository.
 88      * @param   string   $repo  The name of the GitHub repository.
 89      * @param   integer  $id    ID of the hook to delete.
 90      *
 91      * @deprecated  use repositories->hooks->delete()
 92      *
 93      * @return  object
 94      *
 95      * @since   12.3
 96      * @throws  DomainException
 97      */
 98     public function delete($user, $repo, $id)
 99     {
100         // Build the request path.
101         $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
102 
103         return $this->processResponse(
104             $this->client->delete($this->fetchUrl($path)),
105             204
106         );
107     }
108 
109     /**
110      * Method to edit a hook.
111      *
112      * @param   string   $user          The name of the owner of the GitHub repository.
113      * @param   string   $repo          The name of the GitHub repository.
114      * @param   integer  $id            ID of the hook to edit.
115      * @param   string   $name          The name of the service being called.
116      * @param   array    $config        Array containing the config for the service.
117      * @param   array    $events        The events the hook will be triggered for.  This resets the currently set list
118      * @param   array    $addEvents     Events to add to the hook.
119      * @param   array    $removeEvents  Events to remove from the hook.
120      * @param   boolean  $active        Flag to determine if the hook is active
121      *
122      * @deprecated  use repositories->hooks->edit()
123      *
124      * @return  object
125      *
126      * @since   12.3
127      * @throws  DomainException
128      * @throws  RuntimeException
129      */
130     public function edit($user, $repo, $id, $name, array $config, array $events = array('push'), array $addEvents = array(),
131         array $removeEvents = array(), $active = true)
132     {
133         // Check to ensure all events are in the allowed list
134         foreach ($events as $event)
135         {
136             if (!in_array($event, $this->events))
137             {
138                 throw new RuntimeException('Your events array contains an unauthorized event.');
139             }
140         }
141 
142         foreach ($addEvents as $event)
143         {
144             if (!in_array($event, $this->events))
145             {
146                 throw new RuntimeException('Your active_events array contains an unauthorized event.');
147             }
148         }
149 
150         foreach ($removeEvents as $event)
151         {
152             if (!in_array($event, $this->events))
153             {
154                 throw new RuntimeException('Your remove_events array contains an unauthorized event.');
155             }
156         }
157 
158         // Build the request path.
159         $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
160 
161         $data = json_encode(
162             array(
163                 'name' => $name,
164                 'config' => $config,
165                 'events' => $events,
166                 'add_events' => $addEvents,
167                 'remove_events' => $removeEvents,
168                 'active' => $active,
169             )
170         );
171 
172         return $this->processResponse(
173             $this->client->patch($this->fetchUrl($path), $data)
174         );
175     }
176 
177     /**
178      * Method to get details about a single hook for the repository.
179      *
180      * @param   string   $user  The name of the owner of the GitHub repository.
181      * @param   string   $repo  The name of the GitHub repository.
182      * @param   integer  $id    ID of the hook to retrieve
183      *
184      * @deprecated  use repositories->hooks->get()
185      *
186      * @return  object
187      *
188      * @since   12.3
189      * @throws  DomainException
190      */
191     public function get($user, $repo, $id)
192     {
193         // Build the request path.
194         $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
195 
196         return $this->processResponse(
197             $this->client->get($this->fetchUrl($path))
198         );
199     }
200 
201     /**
202      * Method to list hooks for a repository.
203      *
204      * @param   string   $user   The name of the owner of the GitHub repository.
205      * @param   string   $repo   The name of the GitHub repository.
206      * @param   integer  $page   Page to request
207      * @param   integer  $limit  Number of results to return per page
208      *
209      * @deprecated  use repositories->hooks->getList()
210      *
211      * @return  object
212      *
213      * @since   12.3
214      * @throws  DomainException
215      */
216     public function getList($user, $repo, $page = 0, $limit = 0)
217     {
218         // Build the request path.
219         $path = '/repos/' . $user . '/' . $repo . '/hooks';
220 
221         return $this->processResponse(
222             $this->client->get($this->fetchUrl($path))
223         );
224     }
225 
226     /**
227      * Method to test a hook against the latest repository commit
228      *
229      * @param   string   $user  The name of the owner of the GitHub repository.
230      * @param   string   $repo  The name of the GitHub repository.
231      * @param   integer  $id    ID of the hook to delete
232      *
233      * @deprecated  use repositories->hooks->test()
234      *
235      * @return  object
236      *
237      * @since   12.3
238      * @throws  DomainException
239      */
240     public function test($user, $repo, $id)
241     {
242         // Build the request path.
243         $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id . '/test';
244 
245         return $this->processResponse(
246             $this->client->post($this->fetchUrl($path), json_encode('')),
247             204
248         );
249     }
250 }
251