/libraries/joomla/github/issues.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 636 lines · 267 code · 84 blank · 285 comment · 46 complexity · 7c6792c0865079fe286335a008743463 MD5 · raw file

  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 Issues class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage GitHub
  15. * @since 11.3
  16. */
  17. class JGithubIssues extends JGithubObject
  18. {
  19. /**
  20. * Method to create an issue.
  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 $title The title of the new issue.
  25. * @param string $body The body text for the new issue.
  26. * @param string $assignee The login for the GitHub user that this issue should be assigned to.
  27. * @param integer $milestone The milestone to associate this issue with.
  28. * @param array $labels The labels to associate with this issue.
  29. *
  30. * @return object
  31. *
  32. * @since 11.3
  33. */
  34. public function create($user, $repo, $title, $body = null, $assignee = null, $milestone = null, array $labels = null)
  35. {
  36. // Build the request path.
  37. $path = '/repos/' . $user . '/' . $repo . '/issues';
  38. // Ensure that we have a non-associative array.
  39. if (isset($labels))
  40. {
  41. $labels = array_values($labels);
  42. }
  43. // Build the request data.
  44. $data = json_encode(
  45. array(
  46. 'title' => $title,
  47. 'assignee' => $assignee,
  48. 'milestone' => $milestone,
  49. 'labels' => $labels,
  50. 'body' => $body
  51. )
  52. );
  53. // Send the request.
  54. $response = $this->client->post($this->fetchUrl($path), $data);
  55. // Validate the response code.
  56. if ($response->code != 201)
  57. {
  58. // Decode the error response and throw an exception.
  59. $error = json_decode($response->body);
  60. throw new DomainException($error->message, $response->code);
  61. }
  62. return json_decode($response->body);
  63. }
  64. /**
  65. * Method to create a comment on an issue.
  66. *
  67. * @param string $user The name of the owner of the GitHub repository.
  68. * @param string $repo The name of the GitHub repository.
  69. * @param integer $issueId The issue number.
  70. * @param string $body The comment body text.
  71. *
  72. * @return object
  73. *
  74. * @since 11.3
  75. */
  76. public function createComment($user, $repo, $issueId, $body)
  77. {
  78. // Build the request path.
  79. $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId . '/comments';
  80. // Build the request data.
  81. $data = json_encode(
  82. array(
  83. 'body' => $body,
  84. )
  85. );
  86. // Send the request.
  87. $response = $this->client->post($this->fetchUrl($path), $data);
  88. // Validate the response code.
  89. if ($response->code != 201)
  90. {
  91. // Decode the error response and throw an exception.
  92. $error = json_decode($response->body);
  93. throw new DomainException($error->message, $response->code);
  94. }
  95. return json_decode($response->body);
  96. }
  97. /**
  98. * Method to create a label on a repo.
  99. *
  100. * @param string $user The name of the owner of the GitHub repository.
  101. * @param string $repo The name of the GitHub repository.
  102. * @param string $name The label name.
  103. * @param string $color The label color.
  104. *
  105. * @return object
  106. *
  107. * @since 12.3
  108. */
  109. public function createLabel($user, $repo, $name, $color)
  110. {
  111. // Build the request path.
  112. $path = '/repos/' . $user . '/' . $repo . '/labels';
  113. // Build the request data.
  114. $data = json_encode(
  115. array(
  116. 'name' => $name,
  117. 'color' => $color
  118. )
  119. );
  120. // Send the request.
  121. $response = $this->client->post($this->fetchUrl($path), $data);
  122. // Validate the response code.
  123. if ($response->code != 201)
  124. {
  125. // Decode the error response and throw an exception.
  126. $error = json_decode($response->body);
  127. throw new DomainException($error->message, $response->code);
  128. }
  129. return json_decode($response->body);
  130. }
  131. /**
  132. * Method to delete a comment on an issue.
  133. *
  134. * @param string $user The name of the owner of the GitHub repository.
  135. * @param string $repo The name of the GitHub repository.
  136. * @param integer $commentId The id of the comment to delete.
  137. *
  138. * @return void
  139. *
  140. * @since 11.3
  141. */
  142. public function deleteComment($user, $repo, $commentId)
  143. {
  144. // Build the request path.
  145. $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId;
  146. // Send the request.
  147. $response = $this->client->delete($this->fetchUrl($path));
  148. // Validate the response code.
  149. if ($response->code != 204)
  150. {
  151. // Decode the error response and throw an exception.
  152. $error = json_decode($response->body);
  153. throw new DomainException($error->message, $response->code);
  154. }
  155. }
  156. /**
  157. * Method to delete a label on a repo.
  158. *
  159. * @param string $user The name of the owner of the GitHub repository.
  160. * @param string $repo The name of the GitHub repository.
  161. * @param string $label The label name.
  162. *
  163. * @return object
  164. *
  165. * @since 12.3
  166. */
  167. public function deleteLabel($user, $repo, $label)
  168. {
  169. // Build the request path.
  170. $path = '/repos/' . $user . '/' . $repo . '/labels/' . $label;
  171. // Send the request.
  172. $response = $this->client->delete($this->fetchUrl($path));
  173. // Validate the response code.
  174. if ($response->code != 204)
  175. {
  176. // Decode the error response and throw an exception.
  177. $error = json_decode($response->body);
  178. throw new DomainException($error->message, $response->code);
  179. }
  180. }
  181. /**
  182. * Method to update an issue.
  183. *
  184. * @param string $user The name of the owner of the GitHub repository.
  185. * @param string $repo The name of the GitHub repository.
  186. * @param integer $issueId The issue number.
  187. * @param string $state The optional new state for the issue. [open, closed]
  188. * @param string $title The title of the new issue.
  189. * @param string $body The body text for the new issue.
  190. * @param string $assignee The login for the GitHub user that this issue should be assigned to.
  191. * @param integer $milestone The milestone to associate this issue with.
  192. * @param array $labels The labels to associate with this issue.
  193. *
  194. * @return object
  195. *
  196. * @since 11.3
  197. */
  198. public function edit($user, $repo, $issueId, $state = null, $title = null, $body = null, $assignee = null, $milestone = null, array $labels = null)
  199. {
  200. // Build the request path.
  201. $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId;
  202. // Craete the data object.
  203. $data = new stdClass;
  204. // If a title is set add it to the data object.
  205. if (isset($title))
  206. {
  207. $data->title = $title;
  208. }
  209. // If a body is set add it to the data object.
  210. if (isset($body))
  211. {
  212. $data->body = $body;
  213. }
  214. // If a state is set add it to the data object.
  215. if (isset($state))
  216. {
  217. $data->state = $state;
  218. }
  219. // If an assignee is set add it to the data object.
  220. if (isset($assignee))
  221. {
  222. $data->assignee = $assignee;
  223. }
  224. // If a milestone is set add it to the data object.
  225. if (isset($milestone))
  226. {
  227. $data->milestone = $milestone;
  228. }
  229. // If labels are set add them to the data object.
  230. if (isset($labels))
  231. {
  232. // Ensure that we have a non-associative array.
  233. if (isset($labels))
  234. {
  235. $labels = array_values($labels);
  236. }
  237. $data->labels = $labels;
  238. }
  239. // Encode the request data.
  240. $data = json_encode($data);
  241. // Send the request.
  242. $response = $this->client->patch($this->fetchUrl($path), $data);
  243. // Validate the response code.
  244. if ($response->code != 200)
  245. {
  246. // Decode the error response and throw an exception.
  247. $error = json_decode($response->body);
  248. throw new DomainException($error->message, $response->code);
  249. }
  250. return json_decode($response->body);
  251. }
  252. /**
  253. * Method to update a comment on an issue.
  254. *
  255. * @param string $user The name of the owner of the GitHub repository.
  256. * @param string $repo The name of the GitHub repository.
  257. * @param integer $commentId The id of the comment to update.
  258. * @param string $body The new body text for the comment.
  259. *
  260. * @return object
  261. *
  262. * @since 11.3
  263. */
  264. public function editComment($user, $repo, $commentId, $body)
  265. {
  266. // Build the request path.
  267. $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId;
  268. // Build the request data.
  269. $data = json_encode(
  270. array(
  271. 'body' => $body
  272. )
  273. );
  274. // Send the request.
  275. $response = $this->client->patch($this->fetchUrl($path), $data);
  276. // Validate the response code.
  277. if ($response->code != 200)
  278. {
  279. // Decode the error response and throw an exception.
  280. $error = json_decode($response->body);
  281. throw new DomainException($error->message, $response->code);
  282. }
  283. return json_decode($response->body);
  284. }
  285. /**
  286. * Method to update a label on a repo.
  287. *
  288. * @param string $user The name of the owner of the GitHub repository.
  289. * @param string $repo The name of the GitHub repository.
  290. * @param string $label The label name.
  291. * @param string $name The label name.
  292. * @param string $color The label color.
  293. *
  294. * @return object
  295. *
  296. * @since 12.3
  297. */
  298. public function editLabel($user, $repo, $label, $name, $color)
  299. {
  300. // Build the request path.
  301. $path = '/repos/' . $user . '/' . $repo . '/labels/' . $label;
  302. // Build the request data.
  303. $data = json_encode(
  304. array(
  305. 'name' => $name,
  306. 'color' => $color
  307. )
  308. );
  309. // Send the request.
  310. $response = $this->client->patch($this->fetchUrl($path), $data);
  311. // Validate the response code.
  312. if ($response->code != 200)
  313. {
  314. // Decode the error response and throw an exception.
  315. $error = json_decode($response->body);
  316. throw new DomainException($error->message, $response->code);
  317. }
  318. return json_decode($response->body);
  319. }
  320. /**
  321. * Method to get a single issue.
  322. *
  323. * @param string $user The name of the owner of the GitHub repository.
  324. * @param string $repo The name of the GitHub repository.
  325. * @param integer $issueId The issue number.
  326. *
  327. * @return object
  328. *
  329. * @since 11.3
  330. */
  331. public function get($user, $repo, $issueId)
  332. {
  333. // Build the request path.
  334. $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId;
  335. // Send the request.
  336. $response = $this->client->get($this->fetchUrl($path));
  337. // Validate the response code.
  338. if ($response->code != 200)
  339. {
  340. // Decode the error response and throw an exception.
  341. $error = json_decode($response->body);
  342. throw new DomainException($error->message, $response->code);
  343. }
  344. return json_decode($response->body);
  345. }
  346. /**
  347. * Method to get a specific comment on an issue.
  348. *
  349. * @param string $user The name of the owner of the GitHub repository.
  350. * @param string $repo The name of the GitHub repository.
  351. * @param integer $commentId The comment id to get.
  352. *
  353. * @return object
  354. *
  355. * @since 11.3
  356. */
  357. public function getComment($user, $repo, $commentId)
  358. {
  359. // Build the request path.
  360. $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId;
  361. // Send the request.
  362. $response = $this->client->get($this->fetchUrl($path));
  363. // Validate the response code.
  364. if ($response->code != 200)
  365. {
  366. // Decode the error response and throw an exception.
  367. $error = json_decode($response->body);
  368. throw new DomainException($error->message, $response->code);
  369. }
  370. return json_decode($response->body);
  371. }
  372. /**
  373. * Method to get the list of comments on an issue.
  374. *
  375. * @param string $user The name of the owner of the GitHub repository.
  376. * @param string $repo The name of the GitHub repository.
  377. * @param integer $issueId The issue number.
  378. * @param integer $page The page number from which to get items.
  379. * @param integer $limit The number of items on a page.
  380. *
  381. * @return array
  382. *
  383. * @since 11.3
  384. */
  385. public function getComments($user, $repo, $issueId, $page = 0, $limit = 0)
  386. {
  387. // Build the request path.
  388. $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId . '/comments';
  389. // Send the request.
  390. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  391. // Validate the response code.
  392. if ($response->code != 200)
  393. {
  394. // Decode the error response and throw an exception.
  395. $error = json_decode($response->body);
  396. throw new DomainException($error->message, $response->code);
  397. }
  398. return json_decode($response->body);
  399. }
  400. /**
  401. * Method to get a specific label on a repo.
  402. *
  403. * @param string $user The name of the owner of the GitHub repository.
  404. * @param string $repo The name of the GitHub repository.
  405. * @param string $name The label name to get.
  406. *
  407. * @return object
  408. *
  409. * @since 12.3
  410. */
  411. public function getLabel($user, $repo, $name)
  412. {
  413. // Build the request path.
  414. $path = '/repos/' . $user . '/' . $repo . '/labels/' . $name;
  415. // Send the request.
  416. $response = $this->client->get($this->fetchUrl($path));
  417. // Validate the response code.
  418. if ($response->code != 200)
  419. {
  420. // Decode the error response and throw an exception.
  421. $error = json_decode($response->body);
  422. throw new DomainException($error->message, $response->code);
  423. }
  424. return json_decode($response->body);
  425. }
  426. /**
  427. * Method to get the list of labels on a repo.
  428. *
  429. * @param string $user The name of the owner of the GitHub repository.
  430. * @param string $repo The name of the GitHub repository.
  431. *
  432. * @return array
  433. *
  434. * @since 12.3
  435. */
  436. public function getLabels($user, $repo)
  437. {
  438. // Build the request path.
  439. $path = '/repos/' . $user . '/' . $repo . '/labels';
  440. // Send the request.
  441. $response = $this->client->get($this->fetchUrl($path));
  442. // Validate the response code.
  443. if ($response->code != 200)
  444. {
  445. // Decode the error response and throw an exception.
  446. $error = json_decode($response->body);
  447. throw new DomainException($error->message, $response->code);
  448. }
  449. return json_decode($response->body);
  450. }
  451. /**
  452. * Method to list an authenticated user's issues.
  453. *
  454. * @param string $filter The filter type: assigned, created, mentioned, subscribed.
  455. * @param string $state The optional state to filter requests by. [open, closed]
  456. * @param string $labels The list of comma separated Label names. Example: bug,ui,@high.
  457. * @param string $sort The sort order: created, updated, comments, default: created.
  458. * @param string $direction The list direction: asc or desc, default: desc.
  459. * @param JDate $since The date/time since when issues should be returned.
  460. * @param integer $page The page number from which to get items.
  461. * @param integer $limit The number of items on a page.
  462. *
  463. * @return array
  464. *
  465. * @since 11.3
  466. */
  467. public function getList($filter = null, $state = null, $labels = null, $sort = null, $direction = null, JDate $since = null, $page = 0, $limit = 0)
  468. {
  469. // Build the request path.
  470. $path = '/issues';
  471. // TODO Implement the filtering options.
  472. // Send the request.
  473. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  474. // Validate the response code.
  475. if ($response->code != 200)
  476. {
  477. // Decode the error response and throw an exception.
  478. $error = json_decode($response->body);
  479. throw new DomainException($error->message, $response->code);
  480. }
  481. return json_decode($response->body);
  482. }
  483. /**
  484. * Method to list issues.
  485. *
  486. * @param string $user The name of the owner of the GitHub repository.
  487. * @param string $repo The name of the GitHub repository.
  488. * @param string $milestone The milestone number, 'none', or *.
  489. * @param string $state The optional state to filter requests by. [open, closed]
  490. * @param string $assignee The assignee name, 'none', or *.
  491. * @param string $mentioned The GitHub user name.
  492. * @param string $labels The list of comma separated Label names. Example: bug,ui,@high.
  493. * @param string $sort The sort order: created, updated, comments, default: created.
  494. * @param string $direction The list direction: asc or desc, default: desc.
  495. * @param JDate $since The date/time since when issues should be returned.
  496. * @param integer $page The page number from which to get items.
  497. * @param integer $limit The number of items on a page.
  498. *
  499. * @return array
  500. *
  501. * @since 11.3
  502. */
  503. public function getListByRepository($user, $repo, $milestone = null, $state = null, $assignee = null, $mentioned = null, $labels = null,
  504. $sort = null, $direction = null, JDate $since = null, $page = 0, $limit = 0)
  505. {
  506. // Build the request path.
  507. $path = '/repos/' . $user . '/' . $repo . '/issues';
  508. $uri = new JUri($this->fetchUrl($path, $page, $limit));
  509. if ($milestone)
  510. {
  511. $uri->setVar('milestone', $milestone);
  512. }
  513. if ($state)
  514. {
  515. $uri->setVar('state', $state);
  516. }
  517. if ($assignee)
  518. {
  519. $uri->setVar('assignee', $assignee);
  520. }
  521. if ($mentioned)
  522. {
  523. $uri->setVar('mentioned', $mentioned);
  524. }
  525. if ($labels)
  526. {
  527. $uri->setVar('labels', $labels);
  528. }
  529. if ($sort)
  530. {
  531. $uri->setVar('sort', $sort);
  532. }
  533. if ($direction)
  534. {
  535. $uri->setVar('direction', $direction);
  536. }
  537. if ($since)
  538. {
  539. $uri->setVar('since', $since->toISO8601());
  540. }
  541. // Send the request.
  542. $response = $this->client->get((string) $uri);
  543. // Validate the response code.
  544. if ($response->code != 200)
  545. {
  546. // Decode the error response and throw an exception.
  547. $error = json_decode($response->body);
  548. throw new DomainException($error->message, $response->code);
  549. }
  550. return json_decode($response->body);
  551. }
  552. }