PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/github/milestones.php

https://bitbucket.org/biojazzard/joomla-eboracast
PHP | 230 lines | 96 code | 32 blank | 102 comment | 17 complexity | 21f4eee9fea747c570c20b57bbb82bd6 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, MIT, BSD-3-Clause
  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. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * GitHub API Milestones class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage GitHub
  15. * @since 12.3
  16. */
  17. class JGithubMilestones extends JGithubObject
  18. {
  19. /**
  20. * Method to get the list of milestones for a repo.
  21. *
  22. * @param string $user The name of the owner of the GitHub repository.
  23. * @param string $repo The name of the GitHub repository.
  24. * @param string $state The milestone state to retrieved. Open (default) or closed.
  25. * @param string $sort Sort can be due_date (default) or completeness.
  26. * @param string $direction Direction is asc or desc (default).
  27. * @param integer $page The page number from which to get items.
  28. * @param integer $limit The number of items on a page.
  29. *
  30. * @return array
  31. *
  32. * @since 12.3
  33. */
  34. public function getList($user, $repo, $state = 'open', $sort = 'due_date', $direction = 'desc', $page = 0, $limit = 0)
  35. {
  36. // Build the request path.
  37. $path = '/repos/' . $user . '/' . $repo . '/milestones?';
  38. $path .= 'state=' . $state;
  39. $path .= '&sort=' . $sort;
  40. $path .= '&direction=' . $direction;
  41. // Send the request.
  42. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  43. // Validate the response code.
  44. if ($response->code != 200)
  45. {
  46. // Decode the error response and throw an exception.
  47. $error = json_decode($response->body);
  48. throw new DomainException($error->message, $response->code);
  49. }
  50. return json_decode($response->body);
  51. }
  52. /**
  53. * Method to get a specific milestone.
  54. *
  55. * @param string $user The name of the owner of the GitHub repository.
  56. * @param string $repo The name of the GitHub repository.
  57. * @param integer $milestoneId The milestone id to get.
  58. *
  59. * @return object
  60. *
  61. * @since 12.3
  62. */
  63. public function get($user, $repo, $milestoneId)
  64. {
  65. // Build the request path.
  66. $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
  67. // Send the request.
  68. $response = $this->client->get($this->fetchUrl($path));
  69. // Validate the response code.
  70. if ($response->code != 200)
  71. {
  72. // Decode the error response and throw an exception.
  73. $error = json_decode($response->body);
  74. throw new DomainException($error->message, $response->code);
  75. }
  76. return json_decode($response->body);
  77. }
  78. /**
  79. * Method to create a milestone for a repository.
  80. *
  81. * @param string $user The name of the owner of the GitHub repository.
  82. * @param string $repo The name of the GitHub repository.
  83. * @param integer $title The title of the milestone.
  84. * @param string $state Can be open (default) or closed.
  85. * @param string $description Optional description for milestone.
  86. * @param string $due_on Optional ISO 8601 time.
  87. *
  88. * @return object
  89. *
  90. * @since 12.3
  91. */
  92. public function create($user, $repo, $title, $state = null, $description = null, $due_on = null)
  93. {
  94. // Build the request path.
  95. $path = '/repos/' . $user . '/' . $repo . '/milestones';
  96. // Build the request data.
  97. $data = array(
  98. 'title' => $title
  99. );
  100. if (!is_null($state))
  101. {
  102. $data['state'] = $state;
  103. }
  104. if (!is_null($description))
  105. {
  106. $data['description'] = $description;
  107. }
  108. if (!is_null($due_on))
  109. {
  110. $data['due_on'] = $due_on;
  111. }
  112. $data = json_encode($data);
  113. // Send the request.
  114. $response = $this->client->post($this->fetchUrl($path), $data);
  115. // Validate the response code.
  116. if ($response->code != 201)
  117. {
  118. // Decode the error response and throw an exception.
  119. $error = json_decode($response->body);
  120. throw new DomainException($error->message, $response->code);
  121. }
  122. return json_decode($response->body);
  123. }
  124. /**
  125. * Method to update a milestone.
  126. *
  127. * @param string $user The name of the owner of the GitHub repository.
  128. * @param string $repo The name of the GitHub repository.
  129. * @param integer $milestoneId The id of the comment to update.
  130. * @param integer $title Optional title of the milestone.
  131. * @param string $state Can be open (default) or closed.
  132. * @param string $description Optional description for milestone.
  133. * @param string $due_on Optional ISO 8601 time.
  134. *
  135. * @return object
  136. *
  137. * @since 12.3
  138. */
  139. public function edit($user, $repo, $milestoneId, $title = null, $state = null, $description = null, $due_on = null)
  140. {
  141. // Build the request path.
  142. $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
  143. // Build the request data.
  144. $data = array();
  145. if (!is_null($title))
  146. {
  147. $data['title'] = $title;
  148. }
  149. if (!is_null($state))
  150. {
  151. $data['state'] = $state;
  152. }
  153. if (!is_null($description))
  154. {
  155. $data['description'] = $description;
  156. }
  157. if (!is_null($due_on))
  158. {
  159. $data['due_on'] = $due_on;
  160. }
  161. $data = json_encode($data);
  162. // Send the request.
  163. $response = $this->client->patch($this->fetchUrl($path), $data);
  164. // Validate the response code.
  165. if ($response->code != 200)
  166. {
  167. // Decode the error response and throw an exception.
  168. $error = json_decode($response->body);
  169. throw new DomainException($error->message, $response->code);
  170. }
  171. return json_decode($response->body);
  172. }
  173. /**
  174. * Method to delete a milestone.
  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 $milestoneId The id of the milestone to delete.
  179. *
  180. * @return void
  181. *
  182. * @since 12.3
  183. */
  184. public function delete($user, $repo, $milestoneId)
  185. {
  186. // Build the request path.
  187. $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
  188. // Send the request.
  189. $response = $this->client->delete($this->fetchUrl($path));
  190. // Validate the response code.
  191. if ($response->code != 204)
  192. {
  193. // Decode the error response and throw an exception.
  194. $error = json_decode($response->body);
  195. throw new DomainException($error->message, $response->code);
  196. }
  197. }
  198. }