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 Milestones class for the Joomla Platform.
14 *
15 * @documentation https://developer.github.com/v3/issues/labels/
16 *
17 * @since 12.3
18 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
19 */
20 class JGithubPackageIssuesLabels extends JGithubPackage
21 {
22 /**
23 * Method to get the list of labels on a repo.
24 *
25 * @param string $owner The name of the owner of the GitHub repository.
26 * @param string $repo The name of the GitHub repository.
27 *
28 * @throws DomainException
29 * @since 12.3
30 *
31 * @return array
32 */
33 public function getList($owner, $repo)
34 {
35 // Build the request path.
36 $path = '/repos/' . $owner . '/' . $repo . '/labels';
37
38 // Send the request.
39 return $this->processResponse(
40 $response = $this->client->get($this->fetchUrl($path))
41 );
42 }
43
44 /**
45 * Method to get a specific label on a repo.
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 label name to get.
50 *
51 * @throws DomainException
52 * @since 12.3
53 *
54 * @return object
55 */
56 public function get($user, $repo, $name)
57 {
58 // Build the request path.
59 $path = '/repos/' . $user . '/' . $repo . '/labels/' . $name;
60
61 // Send the request.
62 return $this->processResponse(
63 $response = $this->client->get($this->fetchUrl($path))
64 );
65 }
66
67 /**
68 * Method to create a label on a repo.
69 *
70 * @param string $owner The name of the owner of the GitHub repository.
71 * @param string $repo The name of the GitHub repository.
72 * @param string $name The label name.
73 * @param string $color The label color.
74 *
75 * @throws DomainException
76 * @since 12.3
77 *
78 * @return object
79 */
80 public function create($owner, $repo, $name, $color)
81 {
82 // Build the request path.
83 $path = '/repos/' . $owner . '/' . $repo . '/labels';
84
85 // Build the request data.
86 $data = json_encode(
87 array(
88 'name' => $name,
89 'color' => $color,
90 )
91 );
92
93 // Send the request.
94 $response = $this->client->post($this->fetchUrl($path), $data);
95
96 // Validate the response code.
97 if ($response->code != 201)
98 {
99 // Decode the error response and throw an exception.
100 $error = json_decode($response->body);
101 throw new DomainException($error->message, $response->code);
102 }
103
104 return json_decode($response->body);
105 }
106
107 /**
108 * Method to update a label on a repo.
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 string $label The label name.
113 * @param string $name The new label name.
114 * @param string $color The new label color.
115 *
116 * @throws DomainException
117 * @since 12.3
118 *
119 * @return object
120 */
121 public function update($user, $repo, $label, $name, $color)
122 {
123 // Build the request path.
124 $path = '/repos/' . $user . '/' . $repo . '/labels/' . $label;
125
126 // Build the request data.
127 $data = json_encode(
128 array(
129 'name' => $name,
130 'color' => $color,
131 )
132 );
133
134 // Send the request.
135 return $this->processResponse(
136 $this->client->patch($this->fetchUrl($path), $data)
137 );
138 }
139
140 /**
141 * Method to delete a label on a repo.
142 *
143 * @param string $owner The name of the owner of the GitHub repository.
144 * @param string $repo The name of the GitHub repository.
145 * @param string $name The label name.
146 *
147 * @throws DomainException
148 * @return object
149 *
150 * @since 12.3
151 */
152 public function delete($owner, $repo, $name)
153 {
154 // Build the request path.
155 $path = '/repos/' . $owner . '/' . $repo . '/labels/' . $name;
156
157 // Send the request.
158 return $this->processResponse(
159 $this->client->delete($this->fetchUrl($path)),
160 204
161 );
162 }
163
164 /**
165 * List labels on an issue.
166 *
167 * @param string $owner The name of the owner of the GitHub repository.
168 * @param string $repo The name of the GitHub repository.
169 * @param integer $number The issue number.
170 *
171 * @since 3.3 (CMS)
172 *
173 * @return object
174 */
175 public function getListByIssue($owner, $repo, $number)
176 {
177 // Build the request path.
178 $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels';
179
180 // Send the request.
181 return $this->processResponse(
182 $this->client->get($this->fetchUrl($path))
183 );
184 }
185
186 /**
187 * Add labels to an issue.
188 *
189 * @param string $owner The name of the owner of the GitHub repository.
190 * @param string $repo The name of the GitHub repository.
191 * @param string $number The issue number.
192 * @param array $labels An array of labels to add.
193 *
194 * @since 3.3 (CMS)
195 *
196 * @return object
197 */
198 public function add($owner, $repo, $number, array $labels)
199 {
200 // Build the request path.
201 $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels';
202
203 // Send the request.
204 return $this->processResponse(
205 $this->client->post($this->fetchUrl($path), json_encode($labels))
206 );
207 }
208
209 /**
210 * Remove a label from an issue.
211 *
212 * @param string $owner The name of the owner of the GitHub repository.
213 * @param string $repo The name of the GitHub repository.
214 * @param string $number The issue number.
215 * @param string $name The name of the label to remove.
216 *
217 * @since 3.3 (CMS)
218 *
219 * @return object
220 */
221 public function removeFromIssue($owner, $repo, $number, $name)
222 {
223 // Build the request path.
224 $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels/' . $name;
225
226 // Send the request.
227 return $this->processResponse(
228 $this->client->delete($this->fetchUrl($path))
229 );
230 }
231
232 /**
233 * Replace all labels for an issue.
234 *
235 * Sending an empty array ([]) will remove all Labels from the Issue.
236 *
237 * @param string $owner The name of the owner of the GitHub repository.
238 * @param string $repo The name of the GitHub repository.
239 * @param string $number The issue number.
240 * @param array $labels New labels
241 *
242 * @since 3.3 (CMS)
243 *
244 * @return object
245 */
246 public function replace($owner, $repo, $number, array $labels)
247 {
248 // Build the request path.
249 $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels';
250
251 // Send the request.
252 return $this->processResponse(
253 $this->client->put($this->fetchUrl($path), json_encode($labels))
254 );
255 }
256
257 /**
258 .* Remove all labels from an issue.
259 *
260 * @param string $owner The name of the owner of the GitHub repository.
261 * @param string $repo The name of the GitHub repository.
262 * @param string $number The issue number.
263 *
264 * @since 3.3 (CMS)
265 *
266 * @return object
267 */
268 public function removeAllFromIssue($owner, $repo, $number)
269 {
270 // Build the request path.
271 $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels';
272
273 // Send the request.
274 return $this->processResponse(
275 $this->client->delete($this->fetchUrl($path)),
276 204
277 );
278 }
279
280 /**
281 * Get labels for every issue in a milestone.
282 *
283 * @param string $owner The name of the owner of the GitHub repository.
284 * @param string $repo The name of the GitHub repository.
285 * @param string $number The issue number.
286 *
287 * @since 3.3 (CMS)
288 *
289 * @return object
290 */
291 public function getListByMilestone($owner, $repo, $number)
292 {
293 // Build the request path.
294 $path = '/repos/' . $owner . '/' . $repo . '/milestones/' . $number . '/labels';
295
296 // Send the request.
297 return $this->processResponse(
298 $this->client->get($this->fetchUrl($path))
299 );
300 }
301 }
302