Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
milestones.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 Milestones class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage GitHub
17  * @since 12.3
18  */
20 {
21  /**
22  * Method to get the list of milestones for a repo.
23  *
24  * @param string $user The name of the owner of the GitHub repository.
25  * @param string $repo The name of the GitHub repository.
26  * @param string $state The milestone state to retrieved. Open (default) or closed.
27  * @param string $sort Sort can be due_date (default) or completeness.
28  * @param string $direction Direction is asc or desc (default).
29  * @param integer $page The page number from which to get items.
30  * @param integer $limit The number of items on a page.
31  *
32  * @return array
33  *
34  * @since 12.3
35  */
36  public function getList($user, $repo, $state = 'open', $sort = 'due_date', $direction = 'desc', $page = 0, $limit = 0)
37  {
38  // Build the request path.
39  $path = '/repos/' . $user . '/' . $repo . '/milestones?';
40 
41  $path .= 'state=' . $state;
42  $path .= '&sort=' . $sort;
43  $path .= '&direction=' . $direction;
44 
45  // Send the request.
46  $response = $this->client->get($this->fetchUrl($path, $page, $limit));
47 
48  // Validate the response code.
49  if ($response->code != 200)
50  {
51  // Decode the error response and throw an exception.
52  $error = json_decode($response->body);
53  throw new DomainException($error->message, $response->code);
54  }
55 
56  return json_decode($response->body);
57  }
58 
59  /**
60  * Method to get a specific milestone.
61  *
62  * @param string $user The name of the owner of the GitHub repository.
63  * @param string $repo The name of the GitHub repository.
64  * @param integer $milestoneId The milestone id to get.
65  *
66  * @return object
67  *
68  * @since 12.3
69  */
70  public function get($user, $repo, $milestoneId)
71  {
72  // Build the request path.
73  $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
74 
75  // Send the request.
76  $response = $this->client->get($this->fetchUrl($path));
77 
78  // Validate the response code.
79  if ($response->code != 200)
80  {
81  // Decode the error response and throw an exception.
82  $error = json_decode($response->body);
83  throw new DomainException($error->message, $response->code);
84  }
85 
86  return json_decode($response->body);
87  }
88 
89  /**
90  * Method to create a milestone for a repository.
91  *
92  * @param string $user The name of the owner of the GitHub repository.
93  * @param string $repo The name of the GitHub repository.
94  * @param integer $title The title of the milestone.
95  * @param string $state Can be open (default) or closed.
96  * @param string $description Optional description for milestone.
97  * @param string $due_on Optional ISO 8601 time.
98  *
99  * @return object
100  *
101  * @since 12.3
102  */
103  public function create($user, $repo, $title, $state = null, $description = null, $due_on = null)
104  {
105  // Build the request path.
106  $path = '/repos/' . $user . '/' . $repo . '/milestones';
107 
108  // Build the request data.
109  $data = array(
110  'title' => $title
111  );
112 
113  if (!is_null($state))
114  {
115  $data['state'] = $state;
116  }
117 
118  if (!is_null($description))
119  {
120  $data['description'] = $description;
121  }
122 
123  if (!is_null($due_on))
124  {
125  $data['due_on'] = $due_on;
126  }
127 
128  $data = json_encode($data);
129 
130  // Send the request.
131  $response = $this->client->post($this->fetchUrl($path), $data);
132 
133  // Validate the response code.
134  if ($response->code != 201)
135  {
136  // Decode the error response and throw an exception.
137  $error = json_decode($response->body);
138  throw new DomainException($error->message, $response->code);
139  }
140 
141  return json_decode($response->body);
142  }
143 
144  /**
145  * Method to update a milestone.
146  *
147  * @param string $user The name of the owner of the GitHub repository.
148  * @param string $repo The name of the GitHub repository.
149  * @param integer $milestoneId The id of the comment to update.
150  * @param integer $title Optional title of the milestone.
151  * @param string $state Can be open (default) or closed.
152  * @param string $description Optional description for milestone.
153  * @param string $due_on Optional ISO 8601 time.
154  *
155  * @return object
156  *
157  * @since 12.3
158  */
159  public function edit($user, $repo, $milestoneId, $title = null, $state = null, $description = null, $due_on = null)
160  {
161  // Build the request path.
162  $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
163 
164  // Build the request data.
165  $data = array();
166 
167  if (!is_null($title))
168  {
169  $data['title'] = $title;
170  }
171 
172  if (!is_null($state))
173  {
174  $data['state'] = $state;
175  }
176 
177  if (!is_null($description))
178  {
179  $data['description'] = $description;
180  }
181 
182  if (!is_null($due_on))
183  {
184  $data['due_on'] = $due_on;
185  }
186 
187  $data = json_encode($data);
188 
189  // Send the request.
190  $response = $this->client->patch($this->fetchUrl($path), $data);
191 
192  // Validate the response code.
193  if ($response->code != 200)
194  {
195  // Decode the error response and throw an exception.
196  $error = json_decode($response->body);
197  throw new DomainException($error->message, $response->code);
198  }
199 
200  return json_decode($response->body);
201  }
202 
203  /**
204  * Method to delete a milestone.
205  *
206  * @param string $user The name of the owner of the GitHub repository.
207  * @param string $repo The name of the GitHub repository.
208  * @param integer $milestoneId The id of the milestone to delete.
209  *
210  * @return void
211  *
212  * @since 12.3
213  */
214  public function delete($user, $repo, $milestoneId)
215  {
216  // Build the request path.
217  $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
218 
219  // Send the request.
220  $response = $this->client->delete($this->fetchUrl($path));
221 
222  // Validate the response code.
223  if ($response->code != 204)
224  {
225  // Decode the error response and throw an exception.
226  $error = json_decode($response->body);
227  throw new DomainException($error->message, $response->code);
228  }
229  }
230 }