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