/libraries/joomla/github/commits.php

https://gitlab.com/vitaliylukin91/text · PHP · 375 lines · 132 code · 44 blank · 199 comment · 20 complexity · b91c2eb37bb4ac6cd321359b7657a831 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage GitHub
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 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. * @since 12.1
  14. */
  15. class JGithubCommits extends JGithubObject
  16. {
  17. /**
  18. * Method to create a commit.
  19. *
  20. * @param string $user The name of the owner of the GitHub repository.
  21. * @param string $repo The name of the GitHub repository.
  22. * @param string $message The commit message.
  23. * @param string $tree SHA of the tree object this commit points to.
  24. * @param array $parents Array of the SHAs of the commits that were the parents of this commit.
  25. * If omitted or empty, the commit will be written as a root commit.
  26. * For a single parent, an array of one SHA should be provided.
  27. * For a merge commit, an array of more than one should be provided.
  28. *
  29. * @deprecated use data->commits->create()
  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. * @deprecated use repositories->comments->create()
  65. *
  66. * @return object
  67. *
  68. * @since 12.1
  69. */
  70. public function createCommitComment($user, $repo, $sha, $comment, $line, $filepath, $position)
  71. {
  72. // Build the request path.
  73. $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
  74. $data = json_encode(
  75. array(
  76. 'body' => $comment,
  77. 'commit_id' => $sha,
  78. 'line' => (int) $line,
  79. 'path' => $filepath,
  80. 'position' => (int) $position
  81. )
  82. );
  83. // Send the request.
  84. $response = $this->client->post($this->fetchUrl($path), $data);
  85. // Validate the response code.
  86. if ($response->code != 201)
  87. {
  88. // Decode the error response and throw an exception.
  89. $error = json_decode($response->body);
  90. throw new DomainException($error->message, $response->code);
  91. }
  92. return json_decode($response->body);
  93. }
  94. /**
  95. * Method to delete a comment on a commit.
  96. *
  97. * @param string $user The name of the owner of the GitHub repository.
  98. * @param string $repo The name of the GitHub repository.
  99. * @param string $id The ID of the comment to edit.
  100. *
  101. * @deprecated use repositories->comments->delete()
  102. *
  103. * @return object
  104. *
  105. * @since 12.1
  106. */
  107. public function deleteCommitComment($user, $repo, $id)
  108. {
  109. // Build the request path.
  110. $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
  111. // Send the request.
  112. $response = $this->client->delete($this->fetchUrl($path));
  113. // Validate the response code.
  114. if ($response->code != 204)
  115. {
  116. // Decode the error response and throw an exception.
  117. $error = json_decode($response->body);
  118. throw new DomainException($error->message, $response->code);
  119. }
  120. return json_decode($response->body);
  121. }
  122. /**
  123. * Method to edit a comment on a commit.
  124. *
  125. * @param string $user The name of the owner of the GitHub repository.
  126. * @param string $repo The name of the GitHub repository.
  127. * @param string $id The ID of the comment to edit.
  128. * @param string $comment The text of the comment.
  129. *
  130. * @deprecated use repositories->comments->edit()
  131. *
  132. * @return object
  133. *
  134. * @since 12.1
  135. */
  136. public function editCommitComment($user, $repo, $id, $comment)
  137. {
  138. // Build the request path.
  139. $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
  140. $data = json_encode(
  141. array(
  142. 'body' => $comment
  143. )
  144. );
  145. // Send the request.
  146. $response = $this->client->patch($this->fetchUrl($path), $data);
  147. // Validate the response code.
  148. if ($response->code != 200)
  149. {
  150. // Decode the error response and throw an exception.
  151. $error = json_decode($response->body);
  152. throw new DomainException($error->message, $response->code);
  153. }
  154. return json_decode($response->body);
  155. }
  156. /**
  157. * Method to get a single commit for a repository.
  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 $sha The SHA of the commit to retrieve.
  162. * @param integer $page Page to request
  163. * @param integer $limit Number of results to return per page
  164. *
  165. * @deprecated use repositories->commits->get()
  166. *
  167. * @return array
  168. *
  169. * @since 12.1
  170. */
  171. public function getCommit($user, $repo, $sha, $page = 0, $limit = 0)
  172. {
  173. // Build the request path.
  174. $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha;
  175. // Send the request.
  176. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  177. // Validate the response code.
  178. if ($response->code != 200)
  179. {
  180. // Decode the error response and throw an exception.
  181. $error = json_decode($response->body);
  182. throw new DomainException($error->message, $response->code);
  183. }
  184. return json_decode($response->body);
  185. }
  186. /**
  187. * Method to get a single comment on a commit.
  188. *
  189. * @param string $user The name of the owner of the GitHub repository.
  190. * @param string $repo The name of the GitHub repository.
  191. * @param integer $id ID of the comment to retrieve
  192. *
  193. * @deprecated use repositories->comments->get()
  194. *
  195. * @return array
  196. *
  197. * @since 12.1
  198. */
  199. public function getCommitComment($user, $repo, $id)
  200. {
  201. // Build the request path.
  202. $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
  203. // Send the request.
  204. $response = $this->client->get($this->fetchUrl($path));
  205. // Validate the response code.
  206. if ($response->code != 200)
  207. {
  208. // Decode the error response and throw an exception.
  209. $error = json_decode($response->body);
  210. throw new DomainException($error->message, $response->code);
  211. }
  212. return json_decode($response->body);
  213. }
  214. /**
  215. * Method to get a list of comments for a single commit for a repository.
  216. *
  217. * @param string $user The name of the owner of the GitHub repository.
  218. * @param string $repo The name of the GitHub repository.
  219. * @param string $sha The SHA of the commit to retrieve.
  220. * @param integer $page Page to request
  221. * @param integer $limit Number of results to return per page
  222. *
  223. * @deprecated use repositories->comments->getList()
  224. *
  225. * @return array
  226. *
  227. * @since 12.1
  228. */
  229. public function getCommitComments($user, $repo, $sha, $page = 0, $limit = 0)
  230. {
  231. // Build the request path.
  232. $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
  233. // Send the request.
  234. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  235. // Validate the response code.
  236. if ($response->code != 200)
  237. {
  238. // Decode the error response and throw an exception.
  239. $error = json_decode($response->body);
  240. throw new DomainException($error->message, $response->code);
  241. }
  242. return json_decode($response->body);
  243. }
  244. /**
  245. * Method to get a diff for two commits.
  246. *
  247. * @param string $user The name of the owner of the GitHub repository.
  248. * @param string $repo The name of the GitHub repository.
  249. * @param string $base The base of the diff, either a commit SHA or branch.
  250. * @param string $head The head of the diff, either a commit SHA or branch.
  251. *
  252. * @deprecated use repositories->commits->compare()
  253. *
  254. * @return array
  255. *
  256. * @since 12.1
  257. */
  258. public function getDiff($user, $repo, $base, $head)
  259. {
  260. // Build the request path.
  261. $path = '/repos/' . $user . '/' . $repo . '/compare/' . $base . '...' . $head;
  262. // Send the request.
  263. $response = $this->client->get($this->fetchUrl($path));
  264. // Validate the response code.
  265. if ($response->code != 200)
  266. {
  267. // Decode the error response and throw an exception.
  268. $error = json_decode($response->body);
  269. throw new DomainException($error->message, $response->code);
  270. }
  271. return json_decode($response->body);
  272. }
  273. /**
  274. * Method to list commits for a repository.
  275. *
  276. * @param string $user The name of the owner of the GitHub repository.
  277. * @param string $repo The name of the GitHub repository.
  278. * @param integer $page Page to request
  279. * @param integer $limit Number of results to return per page
  280. *
  281. * @deprecated use repositories->commits->getList()
  282. *
  283. * @return array
  284. *
  285. * @since 12.1
  286. */
  287. public function getList($user, $repo, $page = 0, $limit = 0)
  288. {
  289. // Build the request path.
  290. $path = '/repos/' . $user . '/' . $repo . '/commits';
  291. // Send the request.
  292. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  293. // Validate the response code.
  294. if ($response->code != 200)
  295. {
  296. // Decode the error response and throw an exception.
  297. $error = json_decode($response->body);
  298. throw new DomainException($error->message, $response->code);
  299. }
  300. return json_decode($response->body);
  301. }
  302. /**
  303. * Method to get a list of commit comments for a repository.
  304. *
  305. * @param string $user The name of the owner of the GitHub repository.
  306. * @param string $repo The name of the GitHub repository.
  307. * @param integer $page Page to request
  308. * @param integer $limit Number of results to return per page
  309. *
  310. * @deprecated use repositories->comments->getListRepository()
  311. *
  312. * @return array
  313. *
  314. * @since 12.1
  315. */
  316. public function getListComments($user, $repo, $page = 0, $limit = 0)
  317. {
  318. // Build the request path.
  319. $path = '/repos/' . $user . '/' . $repo . '/comments';
  320. // Send the request.
  321. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  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. }