PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/github/commits.php

http://github.com/joomla/joomla-platform
PHP | 357 lines | 132 code | 44 blank | 181 comment | 20 complexity | 812da9584900037ad83b5bc2249c55f6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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 Commits class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage GitHub
  15. * @since 12.1
  16. */
  17. class JGithubCommits extends JGithubObject
  18. {
  19. /**
  20. * Method to create a commit.
  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 $message The commit message.
  25. * @param string $tree SHA of the tree object this commit points to.
  26. * @param array $parents Array of the SHAs of the commits that were the parents of this commit.
  27. * If omitted or empty, the commit will be written as a root commit.
  28. * For a single parent, an array of one SHA should be provided.
  29. * For a merge commit, an array of more than one should be provided.
  30. *
  31. * @return object
  32. *
  33. * @since 12.1
  34. */
  35. public function create($user, $repo, $message, $tree, array $parents = array())
  36. {
  37. // Build the request path.
  38. $path = '/repos/' . $user . '/' . $repo . '/git/commits';
  39. $data = json_encode(
  40. array('message' => $message, 'tree' => $tree, 'parents' => $parents)
  41. );
  42. // Send the request.
  43. $response = $this->client->post($this->fetchUrl($path), $data);
  44. // Validate the response code.
  45. if ($response->code != 201)
  46. {
  47. // Decode the error response and throw an exception.
  48. $error = json_decode($response->body);
  49. throw new DomainException($error->message, $response->code);
  50. }
  51. return json_decode($response->body);
  52. }
  53. /**
  54. * Method to create a comment on a commit.
  55. *
  56. * @param string $user The name of the owner of the GitHub repository.
  57. * @param string $repo The name of the GitHub repository.
  58. * @param string $sha The SHA of the commit to comment on.
  59. * @param string $comment The text of the comment.
  60. * @param integer $line The line number of the commit to comment on.
  61. * @param string $filepath A relative path to the file to comment on within the commit.
  62. * @param integer $position Line index in the diff to comment on.
  63. *
  64. * @return object
  65. *
  66. * @since 12.1
  67. */
  68. public function createCommitComment($user, $repo, $sha, $comment, $line, $filepath, $position)
  69. {
  70. // Build the request path.
  71. $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
  72. $data = json_encode(
  73. array(
  74. 'body' => $comment,
  75. 'commit_id' => $sha,
  76. 'line' => (int) $line,
  77. 'path' => $filepath,
  78. 'position' => (int) $position
  79. )
  80. );
  81. // Send the request.
  82. $response = $this->client->post($this->fetchUrl($path), $data);
  83. // Validate the response code.
  84. if ($response->code != 201)
  85. {
  86. // Decode the error response and throw an exception.
  87. $error = json_decode($response->body);
  88. throw new DomainException($error->message, $response->code);
  89. }
  90. return json_decode($response->body);
  91. }
  92. /**
  93. * Method to delete a comment on a commit.
  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 string $id The ID of the comment to edit.
  98. *
  99. * @return object
  100. *
  101. * @since 12.1
  102. */
  103. public function deleteCommitComment($user, $repo, $id)
  104. {
  105. // Build the request path.
  106. $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
  107. // Send the request.
  108. $response = $this->client->delete($this->fetchUrl($path));
  109. // Validate the response code.
  110. if ($response->code != 204)
  111. {
  112. // Decode the error response and throw an exception.
  113. $error = json_decode($response->body);
  114. throw new DomainException($error->message, $response->code);
  115. }
  116. return json_decode($response->body);
  117. }
  118. /**
  119. * Method to edit a comment on a commit.
  120. *
  121. * @param string $user The name of the owner of the GitHub repository.
  122. * @param string $repo The name of the GitHub repository.
  123. * @param string $id The ID of the comment to edit.
  124. * @param string $comment The text of the comment.
  125. *
  126. * @return object
  127. *
  128. * @since 12.1
  129. */
  130. public function editCommitComment($user, $repo, $id, $comment)
  131. {
  132. // Build the request path.
  133. $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
  134. $data = json_encode(
  135. array(
  136. 'body' => $comment
  137. )
  138. );
  139. // Send the request.
  140. $response = $this->client->patch($this->fetchUrl($path), $data);
  141. // Validate the response code.
  142. if ($response->code != 200)
  143. {
  144. // Decode the error response and throw an exception.
  145. $error = json_decode($response->body);
  146. throw new DomainException($error->message, $response->code);
  147. }
  148. return json_decode($response->body);
  149. }
  150. /**
  151. * Method to get a single commit for a repository.
  152. *
  153. * @param string $user The name of the owner of the GitHub repository.
  154. * @param string $repo The name of the GitHub repository.
  155. * @param string $sha The SHA of the commit to retrieve.
  156. * @param integer $page Page to request
  157. * @param integer $limit Number of results to return per page
  158. *
  159. * @return array
  160. *
  161. * @since 12.1
  162. */
  163. public function getCommit($user, $repo, $sha, $page = 0, $limit = 0)
  164. {
  165. // Build the request path.
  166. $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha;
  167. // Send the request.
  168. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  169. // Validate the response code.
  170. if ($response->code != 200)
  171. {
  172. // Decode the error response and throw an exception.
  173. $error = json_decode($response->body);
  174. throw new DomainException($error->message, $response->code);
  175. }
  176. return json_decode($response->body);
  177. }
  178. /**
  179. * Method to get a single comment on a commit.
  180. *
  181. * @param string $user The name of the owner of the GitHub repository.
  182. * @param string $repo The name of the GitHub repository.
  183. * @param integer $id ID of the comment to retrieve
  184. *
  185. * @return array
  186. *
  187. * @since 12.1
  188. */
  189. public function getCommitComment($user, $repo, $id)
  190. {
  191. // Build the request path.
  192. $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
  193. // Send the request.
  194. $response = $this->client->get($this->fetchUrl($path));
  195. // Validate the response code.
  196. if ($response->code != 200)
  197. {
  198. // Decode the error response and throw an exception.
  199. $error = json_decode($response->body);
  200. throw new DomainException($error->message, $response->code);
  201. }
  202. return json_decode($response->body);
  203. }
  204. /**
  205. * Method to get a list of comments for a single commit for a repository.
  206. *
  207. * @param string $user The name of the owner of the GitHub repository.
  208. * @param string $repo The name of the GitHub repository.
  209. * @param string $sha The SHA of the commit to retrieve.
  210. * @param integer $page Page to request
  211. * @param integer $limit Number of results to return per page
  212. *
  213. * @return array
  214. *
  215. * @since 12.1
  216. */
  217. public function getCommitComments($user, $repo, $sha, $page = 0, $limit = 0)
  218. {
  219. // Build the request path.
  220. $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
  221. // Send the request.
  222. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  223. // Validate the response code.
  224. if ($response->code != 200)
  225. {
  226. // Decode the error response and throw an exception.
  227. $error = json_decode($response->body);
  228. throw new DomainException($error->message, $response->code);
  229. }
  230. return json_decode($response->body);
  231. }
  232. /**
  233. * Method to get a diff for two commits.
  234. *
  235. * @param string $user The name of the owner of the GitHub repository.
  236. * @param string $repo The name of the GitHub repository.
  237. * @param string $base The base of the diff, either a commit SHA or branch.
  238. * @param string $head The head of the diff, either a commit SHA or branch.
  239. *
  240. * @return array
  241. *
  242. * @since 12.1
  243. */
  244. public function getDiff($user, $repo, $base, $head)
  245. {
  246. // Build the request path.
  247. $path = '/repos/' . $user . '/' . $repo . '/compare/' . $base . '...' . $head;
  248. // Send the request.
  249. $response = $this->client->get($this->fetchUrl($path));
  250. // Validate the response code.
  251. if ($response->code != 200)
  252. {
  253. // Decode the error response and throw an exception.
  254. $error = json_decode($response->body);
  255. throw new DomainException($error->message, $response->code);
  256. }
  257. return json_decode($response->body);
  258. }
  259. /**
  260. * Method to list commits for a repository.
  261. *
  262. * @param string $user The name of the owner of the GitHub repository.
  263. * @param string $repo The name of the GitHub repository.
  264. * @param integer $page Page to request
  265. * @param integer $limit Number of results to return per page
  266. *
  267. * @return array
  268. *
  269. * @since 12.1
  270. */
  271. public function getList($user, $repo, $page = 0, $limit = 0)
  272. {
  273. // Build the request path.
  274. $path = '/repos/' . $user . '/' . $repo . '/commits';
  275. // Send the request.
  276. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  277. // Validate the response code.
  278. if ($response->code != 200)
  279. {
  280. // Decode the error response and throw an exception.
  281. $error = json_decode($response->body);
  282. throw new DomainException($error->message, $response->code);
  283. }
  284. return json_decode($response->body);
  285. }
  286. /**
  287. * Method to get a list of commit comments for a repository.
  288. *
  289. * @param string $user The name of the owner of the GitHub repository.
  290. * @param string $repo The name of the GitHub repository.
  291. * @param integer $page Page to request
  292. * @param integer $limit Number of results to return per page
  293. *
  294. * @return array
  295. *
  296. * @since 12.1
  297. */
  298. public function getListComments($user, $repo, $page = 0, $limit = 0)
  299. {
  300. // Build the request path.
  301. $path = '/repos/' . $user . '/' . $repo . '/comments';
  302. // Send the request.
  303. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  304. // Validate the response code.
  305. if ($response->code != 200)
  306. {
  307. // Decode the error response and throw an exception.
  308. $error = json_decode($response->body);
  309. throw new DomainException($error->message, $response->code);
  310. }
  311. return json_decode($response->body);
  312. }
  313. }