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  * @documentation https://developer.github.com/v3/repos/hooks
 16  *
 17  * @since       12.3
 18  * @deprecated  4.0  Use the `joomla/github` package via Composer instead
 19  */
 20 class JGithubPackageRepositoriesHooks extends JGithubPackage
 21 {
 22     /**
 23      * Array containing the allowed hook events
 24      *
 25      * @var    array
 26      * @since  12.3
 27      */
 28     protected $events = array(
 29         'push',
 30         'issues',
 31         'issue_comment',
 32         'commit_comment',
 33         'pull_request',
 34         'gollum',
 35         'watch',
 36         'download',
 37         'fork',
 38         'fork_apply',
 39         'member',
 40         'public',
 41         'status',
 42     );
 43 
 44     /**
 45      * Method to create a hook on a repository.
 46      *
 47      * @param   string   $user    The name of the owner of the GitHub repository.
 48      * @param   string   $repo    The name of the GitHub repository.
 49      * @param   string   $name    The name of the service being called.
 50      * @param   array    $config  Array containing the config for the service.
 51      * @param   array    $events  The events the hook will be triggered for.
 52      * @param   boolean  $active  Flag to determine if the hook is active
 53      *
 54      * @return  object
 55      *
 56      * @since   12.3
 57      * @throws  DomainException
 58      * @throws  RuntimeException
 59      */
 60     public function create($user, $repo, $name, $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      * @return  object
 92      *
 93      * @since   12.3
 94      * @throws  DomainException
 95      */
 96     public function delete($user, $repo, $id)
 97     {
 98         // Build the request path.
 99         $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
100 
101         return $this->processResponse(
102             $this->client->delete($this->fetchUrl($path)),
103             204
104         );
105     }
106 
107     /**
108      * Method to edit a hook.
109      *
110      * @param   string   $user          The name of the owner of the GitHub repository.
111      * @param   string   $repo          The name of the GitHub repository.
112      * @param   integer  $id            ID of the hook to edit.
113      * @param   string   $name          The name of the service being called.
114      * @param   array    $config        Array containing the config for the service.
115      * @param   array    $events        The events the hook will be triggered for.  This resets the currently set list
116      * @param   array    $addEvents     Events to add to the hook.
117      * @param   array    $removeEvents  Events to remove from the hook.
118      * @param   boolean  $active        Flag to determine if the hook is active
119      *
120      * @return  object
121      *
122      * @since   12.3
123      * @throws  DomainException
124      * @throws  RuntimeException
125      */
126     public function edit($user, $repo, $id, $name, $config, array $events = array('push'), array $addEvents = array(),
127         array $removeEvents = array(), $active = true)
128     {
129         // Check to ensure all events are in the allowed list
130         foreach ($events as $event)
131         {
132             if (!in_array($event, $this->events))
133             {
134                 throw new RuntimeException('Your events array contains an unauthorized event.');
135             }
136         }
137 
138         foreach ($addEvents as $event)
139         {
140             if (!in_array($event, $this->events))
141             {
142                 throw new RuntimeException('Your active_events array contains an unauthorized event.');
143             }
144         }
145 
146         foreach ($removeEvents as $event)
147         {
148             if (!in_array($event, $this->events))
149             {
150                 throw new RuntimeException('Your remove_events array contains an unauthorized event.');
151             }
152         }
153 
154         // Build the request path.
155         $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
156 
157         $data = json_encode(
158             array(
159                 'name'          => $name,
160                 'config'        => $config,
161                 'events'        => $events,
162                 'add_events'    => $addEvents,
163                 'remove_events' => $removeEvents,
164                 'active'        => $active,
165             )
166         );
167 
168         return $this->processResponse(
169             $this->client->patch($this->fetchUrl($path), $data)
170         );
171     }
172 
173     /**
174      * Method to get details about a single hook for the repository.
175      *
176      * @param   string   $user  The name of the owner of the GitHub repository.
177      * @param   string   $repo  The name of the GitHub repository.
178      * @param   integer  $id    ID of the hook to retrieve
179      *
180      * @return  object
181      *
182      * @since   12.3
183      * @throws  DomainException
184      */
185     public function get($user, $repo, $id)
186     {
187         // Build the request path.
188         $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id;
189 
190         return $this->processResponse(
191             $this->client->get($this->fetchUrl($path))
192         );
193     }
194 
195     /**
196      * Method to list hooks for a repository.
197      *
198      * @param   string  $user  The name of the owner of the GitHub repository.
199      * @param   string  $repo  The name of the GitHub repository.
200      *
201      * @return  object
202      *
203      * @since   12.3
204      * @throws  DomainException
205      */
206     public function getList($user, $repo)
207     {
208         // Build the request path.
209         $path = '/repos/' . $user . '/' . $repo . '/hooks';
210 
211         return $this->processResponse(
212             $this->client->get($this->fetchUrl($path))
213         );
214     }
215 
216     /**
217      * Method to test a hook against the latest repository commit
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 delete
222      *
223      * @return  object
224      *
225      * @since   12.3
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