/libraries/joomla/github/pulls.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 585 lines · 233 code · 71 blank · 281 comment · 36 complexity · 495d621b184ccb49ef971401a3578c82 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 Pull Requests class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage GitHub
  15. * @since 11.3
  16. */
  17. class JGithubPulls extends JGithubObject
  18. {
  19. /**
  20. * Method to create a pull request.
  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 pull request.
  25. * @param string $base The branch (or git ref) you want your changes pulled into. This
  26. * should be an existing branch on the current repository. You cannot
  27. * submit a pull request to one repo that requests a merge to a base
  28. * of another repo.
  29. * @param string $head The branch (or git ref) where your changes are implemented.
  30. * @param string $body The body text for the new pull request.
  31. *
  32. * @return object
  33. *
  34. * @since 11.3
  35. */
  36. public function create($user, $repo, $title, $base, $head, $body = '')
  37. {
  38. // Build the request path.
  39. $path = '/repos/' . $user . '/' . $repo . '/pulls';
  40. // Build the request data.
  41. $data = json_encode(
  42. array(
  43. 'title' => $title,
  44. 'base' => $base,
  45. 'head' => $head,
  46. 'body' => $body
  47. )
  48. );
  49. // Send the request.
  50. $response = $this->client->post($this->fetchUrl($path), $data);
  51. // Validate the response code.
  52. if ($response->code != 201)
  53. {
  54. // Decode the error response and throw an exception.
  55. $error = json_decode($response->body);
  56. throw new DomainException($error->message, $response->code);
  57. }
  58. return json_decode($response->body);
  59. }
  60. /**
  61. * Method to create a comment on a pull request.
  62. *
  63. * @param string $user The name of the owner of the GitHub repository.
  64. * @param string $repo The name of the GitHub repository.
  65. * @param integer $pullId The pull request number.
  66. * @param string $body The comment body text.
  67. * @param string $commitId The SHA1 hash of the commit to comment on.
  68. * @param string $filePath The Relative path of the file to comment on.
  69. * @param string $position The line index in the diff to comment on.
  70. *
  71. * @return object
  72. *
  73. * @since 11.3
  74. */
  75. public function createComment($user, $repo, $pullId, $body, $commitId, $filePath, $position)
  76. {
  77. // Build the request path.
  78. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/comments';
  79. // Build the request data.
  80. $data = json_encode(
  81. array(
  82. 'body' => $body,
  83. 'commit_id' => $commitId,
  84. 'path' => $filePath,
  85. 'position' => $position
  86. )
  87. );
  88. // Send the request.
  89. $response = $this->client->post($this->fetchUrl($path), $data);
  90. // Validate the response code.
  91. if ($response->code != 201)
  92. {
  93. // Decode the error response and throw an exception.
  94. $error = json_decode($response->body);
  95. throw new DomainException($error->message, $response->code);
  96. }
  97. return json_decode($response->body);
  98. }
  99. /**
  100. * Method to create a comment in reply to another comment.
  101. *
  102. * @param string $user The name of the owner of the GitHub repository.
  103. * @param string $repo The name of the GitHub repository.
  104. * @param integer $pullId The pull request number.
  105. * @param string $body The comment body text.
  106. * @param integer $inReplyTo The id of the comment to reply to.
  107. *
  108. * @return object
  109. *
  110. * @since 11.3
  111. */
  112. public function createCommentReply($user, $repo, $pullId, $body, $inReplyTo)
  113. {
  114. // Build the request path.
  115. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/comments';
  116. // Build the request data.
  117. $data = json_encode(
  118. array(
  119. 'body' => $body,
  120. 'in_reply_to' => (int) $inReplyTo
  121. )
  122. );
  123. // Send the request.
  124. $response = $this->client->post($this->fetchUrl($path), $data);
  125. // Validate the response code.
  126. if ($response->code != 201)
  127. {
  128. // Decode the error response and throw an exception.
  129. $error = json_decode($response->body);
  130. throw new DomainException($error->message, $response->code);
  131. }
  132. return json_decode($response->body);
  133. }
  134. /**
  135. * Method to create a pull request from an existing issue.
  136. *
  137. * @param string $user The name of the owner of the GitHub repository.
  138. * @param string $repo The name of the GitHub repository.
  139. * @param integer $issueId The issue number for which to attach the new pull request.
  140. * @param string $base The branch (or git ref) you want your changes pulled into. This
  141. * should be an existing branch on the current repository. You cannot
  142. * submit a pull request to one repo that requests a merge to a base
  143. * of another repo.
  144. * @param string $head The branch (or git ref) where your changes are implemented.
  145. *
  146. * @return object
  147. *
  148. * @since 11.3
  149. */
  150. public function createFromIssue($user, $repo, $issueId, $base, $head)
  151. {
  152. // Build the request path.
  153. $path = '/repos/' . $user . '/' . $repo . '/pulls';
  154. // Build the request data.
  155. $data = json_encode(
  156. array(
  157. 'issue' => (int) $issueId,
  158. 'base' => $base,
  159. 'head' => $head
  160. )
  161. );
  162. // Send the request.
  163. $response = $this->client->post($this->fetchUrl($path), $data);
  164. // Validate the response code.
  165. if ($response->code != 201)
  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 comment on a pull request.
  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 $commentId The id of the comment to delete.
  179. *
  180. * @return void
  181. *
  182. * @since 11.3
  183. */
  184. public function deleteComment($user, $repo, $commentId)
  185. {
  186. // Build the request path.
  187. $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId;
  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. /**
  199. * Method to update a pull request.
  200. *
  201. * @param string $user The name of the owner of the GitHub repository.
  202. * @param string $repo The name of the GitHub repository.
  203. * @param integer $pullId The pull request number.
  204. * @param string $title The optional new title for the pull request.
  205. * @param string $body The optional new body text for the pull request.
  206. * @param string $state The optional new state for the pull request. [open, closed]
  207. *
  208. * @return object
  209. *
  210. * @since 11.3
  211. */
  212. public function edit($user, $repo, $pullId, $title = null, $body = null, $state = null)
  213. {
  214. // Build the request path.
  215. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId;
  216. // Craete the data object.
  217. $data = new stdClass;
  218. // If a title is set add it to the data object.
  219. if (isset($title))
  220. {
  221. $data->title = $title;
  222. }
  223. // If a body is set add it to the data object.
  224. if (isset($body))
  225. {
  226. $data->body = $body;
  227. }
  228. // If a state is set add it to the data object.
  229. if (isset($state))
  230. {
  231. $data->state = $state;
  232. }
  233. // Encode the request data.
  234. $data = json_encode($data);
  235. // Send the request.
  236. $response = $this->client->patch($this->fetchUrl($path), $data);
  237. // Validate the response code.
  238. if ($response->code != 200)
  239. {
  240. // Decode the error response and throw an exception.
  241. $error = json_decode($response->body);
  242. throw new DomainException($error->message, $response->code);
  243. }
  244. return json_decode($response->body);
  245. }
  246. /**
  247. * Method to update a comment on a pull request.
  248. *
  249. * @param string $user The name of the owner of the GitHub repository.
  250. * @param string $repo The name of the GitHub repository.
  251. * @param integer $commentId The id of the comment to update.
  252. * @param string $body The new body text for the comment.
  253. *
  254. * @return object
  255. *
  256. * @since 11.3
  257. */
  258. public function editComment($user, $repo, $commentId, $body)
  259. {
  260. // Build the request path.
  261. $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId;
  262. // Build the request data.
  263. $data = json_encode(
  264. array(
  265. 'body' => $body
  266. )
  267. );
  268. // Send the request.
  269. $response = $this->client->patch($this->fetchUrl($path), $data);
  270. // Validate the response code.
  271. if ($response->code != 200)
  272. {
  273. // Decode the error response and throw an exception.
  274. $error = json_decode($response->body);
  275. throw new DomainException($error->message, $response->code);
  276. }
  277. return json_decode($response->body);
  278. }
  279. /**
  280. * Method to get a single pull request.
  281. *
  282. * @param string $user The name of the owner of the GitHub repository.
  283. * @param string $repo The name of the GitHub repository.
  284. * @param integer $pullId The pull request number.
  285. *
  286. * @return object
  287. *
  288. * @since 11.3
  289. */
  290. public function get($user, $repo, $pullId)
  291. {
  292. // Build the request path.
  293. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId;
  294. // Send the request.
  295. $response = $this->client->get($this->fetchUrl($path));
  296. // Validate the response code.
  297. if ($response->code != 200)
  298. {
  299. // Decode the error response and throw an exception.
  300. $error = json_decode($response->body);
  301. throw new DomainException($error->message, $response->code);
  302. }
  303. return json_decode($response->body);
  304. }
  305. /**
  306. * Method to get a specific comment on a pull request.
  307. *
  308. * @param string $user The name of the owner of the GitHub repository.
  309. * @param string $repo The name of the GitHub repository.
  310. * @param integer $commentId The comment id to get.
  311. *
  312. * @return object
  313. *
  314. * @since 11.3
  315. */
  316. public function getComment($user, $repo, $commentId)
  317. {
  318. // Build the request path.
  319. $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId;
  320. // Send the request.
  321. $response = $this->client->get($this->fetchUrl($path));
  322. // Validate the response code.
  323. if ($response->code != 200)
  324. {
  325. // Decode the error response and throw an exception.
  326. $error = json_decode($response->body);
  327. throw new DomainException($error->message, $response->code);
  328. }
  329. return json_decode($response->body);
  330. }
  331. /**
  332. * Method to get the list of comments on a pull request.
  333. *
  334. * @param string $user The name of the owner of the GitHub repository.
  335. * @param string $repo The name of the GitHub repository.
  336. * @param integer $pullId The pull request number.
  337. * @param integer $page The page number from which to get items.
  338. * @param integer $limit The number of items on a page.
  339. *
  340. * @return array
  341. *
  342. * @since 11.3
  343. */
  344. public function getComments($user, $repo, $pullId, $page = 0, $limit = 0)
  345. {
  346. // Build the request path.
  347. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/comments';
  348. // Send the request.
  349. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  350. // Validate the response code.
  351. if ($response->code != 200)
  352. {
  353. // Decode the error response and throw an exception.
  354. $error = json_decode($response->body);
  355. throw new DomainException($error->message, $response->code);
  356. }
  357. return json_decode($response->body);
  358. }
  359. /**
  360. * Method to get a list of commits for a pull request.
  361. *
  362. * @param string $user The name of the owner of the GitHub repository.
  363. * @param string $repo The name of the GitHub repository.
  364. * @param integer $pullId The pull request number.
  365. * @param integer $page The page number from which to get items.
  366. * @param integer $limit The number of items on a page.
  367. *
  368. * @return array
  369. *
  370. * @since 11.3
  371. */
  372. public function getCommits($user, $repo, $pullId, $page = 0, $limit = 0)
  373. {
  374. // Build the request path.
  375. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/commits';
  376. // Send the request.
  377. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  378. // Validate the response code.
  379. if ($response->code != 200)
  380. {
  381. // Decode the error response and throw an exception.
  382. $error = json_decode($response->body);
  383. throw new DomainException($error->message, $response->code);
  384. }
  385. return json_decode($response->body);
  386. }
  387. /**
  388. * Method to get a list of files for a pull request.
  389. *
  390. * @param string $user The name of the owner of the GitHub repository.
  391. * @param string $repo The name of the GitHub repository.
  392. * @param integer $pullId The pull request number.
  393. * @param integer $page The page number from which to get items.
  394. * @param integer $limit The number of items on a page.
  395. *
  396. * @return array
  397. *
  398. * @since 11.3
  399. */
  400. public function getFiles($user, $repo, $pullId, $page = 0, $limit = 0)
  401. {
  402. // Build the request path.
  403. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/files';
  404. // Send the request.
  405. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  406. // Validate the response code.
  407. if ($response->code != 200)
  408. {
  409. // Decode the error response and throw an exception.
  410. $error = json_decode($response->body);
  411. throw new DomainException($error->message, $response->code);
  412. }
  413. return json_decode($response->body);
  414. }
  415. /**
  416. * Method to list pull requests.
  417. *
  418. * @param string $user The name of the owner of the GitHub repository.
  419. * @param string $repo The name of the GitHub repository.
  420. * @param string $state The optional state to filter requests by. [open, closed]
  421. * @param integer $page The page number from which to get items.
  422. * @param integer $limit The number of items on a page.
  423. *
  424. * @return array
  425. *
  426. * @since 11.3
  427. */
  428. public function getList($user, $repo, $state = 'open', $page = 0, $limit = 0)
  429. {
  430. // Build the request path.
  431. $path = '/repos/' . $user . '/' . $repo . '/pulls';
  432. // If a state exists append it as an option.
  433. if ($state != 'open')
  434. {
  435. $path .= '?state=' . $state;
  436. }
  437. // Send the request.
  438. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  439. // Validate the response code.
  440. if ($response->code != 200)
  441. {
  442. // Decode the error response and throw an exception.
  443. $error = json_decode($response->body);
  444. throw new DomainException($error->message, $response->code);
  445. }
  446. return json_decode($response->body);
  447. }
  448. /**
  449. * Method to check if a pull request has been merged.
  450. *
  451. * @param string $user The name of the owner of the GitHub repository.
  452. * @param string $repo The name of the GitHub repository.
  453. * @param integer $pullId The pull request number. The pull request number.
  454. *
  455. * @return boolean True if the pull request has been merged.
  456. *
  457. * @since 11.3
  458. */
  459. public function isMerged($user, $repo, $pullId)
  460. {
  461. // Build the request path.
  462. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/merge';
  463. // Send the request.
  464. $response = $this->client->get($this->fetchUrl($path));
  465. // Validate the response code.
  466. if ($response->code == 204)
  467. {
  468. return true;
  469. }
  470. elseif ($response->code == 404)
  471. {
  472. return false;
  473. }
  474. else
  475. {
  476. // Decode the error response and throw an exception.
  477. $error = json_decode($response->body);
  478. throw new DomainException($error->message, $response->code);
  479. }
  480. }
  481. /**
  482. * Method to merge a pull request.
  483. *
  484. * @param string $user The name of the owner of the GitHub repository.
  485. * @param string $repo The name of the GitHub repository.
  486. * @param integer $pullId The pull request number.
  487. * @param string $message The message that will be used for the merge commit.
  488. *
  489. * @return object
  490. *
  491. * @since 11.3
  492. */
  493. public function merge($user, $repo, $pullId, $message = '')
  494. {
  495. // Build the request path.
  496. $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/merge';
  497. // Build the request data.
  498. $data = json_encode(
  499. array(
  500. 'commit_message' => $message
  501. )
  502. );
  503. // Send the request.
  504. $response = $this->client->put($this->fetchUrl($path), $data);
  505. // Validate the response code.
  506. if ($response->code != 200)
  507. {
  508. // Decode the error response and throw an exception.
  509. $error = json_decode($response->body);
  510. throw new DomainException($error->message, $response->code);
  511. }
  512. return json_decode($response->body);
  513. }
  514. }