/Vendor/Github/Api/Repo.php

https://github.com/alfrekjv/ppi-framework · PHP · 400 lines · 150 code · 52 blank · 198 comment · 3 complexity · 5c5f1cac928949626f934c9576591a52 MD5 · raw file

  1. <?php
  2. /**
  3. * Searching repositories, getting repository information
  4. * and managing repository information for authenticated users.
  5. *
  6. * @link http://develop.github.com/p/repos.html
  7. * @author Thibault Duplessis <thibault.duplessis at gmail dot com>
  8. * @license MIT License
  9. */
  10. class Github_Api_Repo extends Github_Api
  11. {
  12. /**
  13. * Search repos by keyword
  14. * http://develop.github.com/p/repo.html
  15. *
  16. * @param string $query the search query
  17. * @param string $language takes the same values as the language drop down on http://github.com/search
  18. * @param int $startPage the page number
  19. * @return array list of repos found
  20. */
  21. public function search($query, $language = '', $startPage = 1)
  22. {
  23. $response = $this->get('repos/search/'.urlencode($query), array(
  24. 'language' => strtolower($language),
  25. 'start_page' => $startPage
  26. ));
  27. return $response['repositories'];
  28. }
  29. /**
  30. * Get the repositories of a user
  31. * http://develop.github.com/p/repo.html
  32. *
  33. * @param string $username the username
  34. * @return array list of the user repos
  35. */
  36. public function getUserRepos($username)
  37. {
  38. $response = $this->get('repos/show/'.urlencode($username));
  39. return $response['repositories'];
  40. }
  41. /**
  42. * Get a list of the repositories that the authenticated user can push to
  43. *
  44. * @return array list of repositories
  45. */
  46. public function getPushableRepos()
  47. {
  48. $response = $this->get('repos/pushable');
  49. return $response['repositories'];
  50. }
  51. /**
  52. * Get extended information about a repository by its username and repo name
  53. * http://develop.github.com/p/repo.html
  54. *
  55. * @param string $username the user who owns the repo
  56. * @param string $repo the name of the repo
  57. * @return array informations about the repo
  58. */
  59. public function show($username, $repo)
  60. {
  61. $response = $this->get('repos/show/'.urlencode($username).'/'.urlencode($repo));
  62. return $response['repository'];
  63. }
  64. /**
  65. * create repo
  66. * http://develop.github.com/p/repo.html
  67. *
  68. * @param string $name name of the repository
  69. * @param string $description repo description
  70. * @param string $homepage homepage url
  71. * @param bool $public 1 for public, 0 for private
  72. * @return array returns repo data
  73. */
  74. public function create($name, $description = '', $homepage = '', $public = true)
  75. {
  76. $response = $this->post('repos/create', array(
  77. 'name' => $name,
  78. 'description' => $description,
  79. 'homepage' => $homepage,
  80. 'public' => $public
  81. ));
  82. return $response['repository'];
  83. }
  84. /**
  85. * delete repo
  86. * http://develop.github.com/p/repo.html
  87. *
  88. * @param string $name name of the repository
  89. * @param string $token delete token
  90. * @param string $force force repository deletion
  91. *
  92. * @return string|array returns delete_token or repo status
  93. */
  94. public function delete($name, $token = null, $force = false)
  95. {
  96. if ($token === null) {
  97. $response = $this->post('repos/delete/'.urlencode($name));
  98. $token = $response['delete_token'];
  99. if (!$force) {
  100. return $token;
  101. }
  102. }
  103. $response = $this->post('repos/delete/'.urlencode($name), array(
  104. 'delete_token' => $token,
  105. ));
  106. return $response;
  107. }
  108. /**
  109. * Set information of a repository
  110. * http://develop.github.com/p/repo.html
  111. *
  112. * @param string $username the user who owns the repo
  113. * @param string $repo the name of the repo
  114. * @param array $values the key => value pairs to post
  115. * @return array informations about the repo
  116. */
  117. public function setRepoInfo($username, $repo, $values)
  118. {
  119. $response = $this->post('repos/show/'.urlencode($username).'/'.urlencode($repo), array('values' => $values));
  120. return $response['repository'];
  121. }
  122. /**
  123. * Set the visibility of a repostory to public
  124. * http://develop.github.com/p/repo.html
  125. *
  126. * @param string $repo the name of the repo
  127. * @return array informations about the repo
  128. */
  129. public function setPublic($repo)
  130. {
  131. $response = $this->get('repos/set/public/'.urlencode($repo));
  132. return $response['repository'];
  133. }
  134. /**
  135. * Set the visibility of a repostory to private
  136. * http://develop.github.com/p/repo.html
  137. *
  138. * @param string $repo the name of the repo
  139. * @return array informations about the repo
  140. */
  141. public function setPrivate($repo)
  142. {
  143. $response = $this->get('repos/set/private/'.urlencode($repo));
  144. return $response['repository'];
  145. }
  146. /**
  147. * Get the list of deploy keys for a repository
  148. *
  149. * @param string $repo the name of the repo
  150. * @return array the list of deploy keys
  151. */
  152. public function getDeployKeys($repo)
  153. {
  154. $response = $this->get('repos/keys/'.urlencode($repo));
  155. return $response['public_keys'];
  156. }
  157. /**
  158. * Add a deploy key for a repository
  159. *
  160. * @param string $repo the name of the repo
  161. * @param string $title the title of the key
  162. * @param string $key the public key data
  163. * @return array the list of deploy keys
  164. */
  165. public function addDeployKey($repo, $title, $key)
  166. {
  167. $response = $this->post('repos/key/'.urlencode($repo).'/add', array(
  168. 'title' => $title,
  169. 'key' => $key
  170. ));
  171. return $response['public_keys'];
  172. }
  173. /**
  174. * Delete a deploy key from a repository
  175. *
  176. * @param string $repo the name of the repo
  177. * @param string $id the the id of the key to remove
  178. * @return array the list of deploy keys
  179. */
  180. public function removeDeployKey($repo, $id)
  181. {
  182. $response = $this->post('repos/key/'.urlencode($repo).'/remove', array(
  183. 'id' => $id,
  184. ));
  185. return $response['public_keys'];
  186. }
  187. /**
  188. * Get the collaborators of a repository
  189. * http://develop.github.com/p/repo.html
  190. *
  191. * @param string $username the user who owns the repo
  192. * @param string $repo the name of the repo
  193. * @return array list of the repo collaborators
  194. */
  195. public function getRepoCollaborators($username, $repo)
  196. {
  197. $response = $this->get('repos/show/'.urlencode($username).'/'.urlencode($repo).'/collaborators');
  198. return $response['collaborators'];
  199. }
  200. /**
  201. * Add a collaborator to a repository
  202. * http://develop.github.com/p/repo.html
  203. *
  204. * @param string $repo the name of the repo
  205. * @param string $username the user who should be added as a collaborator
  206. * @return array list of the repo collaborators
  207. */
  208. public function addRepoCollaborator($repo, $username)
  209. {
  210. $response = $this->post('repos/collaborators/'.urlencode($repo).'/add/'.urlencode($username));
  211. return $response['collaborators'];
  212. }
  213. /**
  214. * Delete a collaborator from a repository
  215. * http://develop.github.com/p/repo.html
  216. *
  217. * @param string $repo the name of the repo
  218. * @param string $username the user who should be removed as a collaborator
  219. * @return array list of the repo collaborators
  220. */
  221. public function removeRepoCollaborator($repo, $username)
  222. {
  223. $response = $this->post('repos/collaborators/'.urlencode($repo).'/remove/'.urlencode($username));
  224. return $response['collaborators'];
  225. }
  226. /**
  227. * Make the authenticated user watch a repository
  228. * http://develop.github.com/p/repo.html
  229. *
  230. * @param string $username the user who owns the repo
  231. * @param string $repo the name of the repo
  232. * @return array informations about the repo
  233. */
  234. public function watch($username, $repo)
  235. {
  236. $response = $this->get('repos/watch/'.urlencode($username).'/'.urlencode($repo));
  237. return $response['repository'];
  238. }
  239. /**
  240. * Make the authenticated user unwatch a repository
  241. * http://develop.github.com/p/repo.html
  242. *
  243. * @param string $username the user who owns the repo
  244. * @param string $repo the name of the repo
  245. * @return array informations about the repo
  246. */
  247. public function unwatch($username, $repo)
  248. {
  249. $response = $this->get('repos/unwatch/'.urlencode($username).'/'.urlencode($repo));
  250. return $response['repository'];
  251. }
  252. /**
  253. * Make the authenticated user fork a repository
  254. * http://develop.github.com/p/repo.html
  255. *
  256. * @param string $username the user who owns the repo
  257. * @param string $repo the name of the repo
  258. * @return array informations about the newly forked repo
  259. */
  260. public function fork($username, $repo)
  261. {
  262. $response = $this->get('repos/fork/'.urlencode($username).'/'.urlencode($repo));
  263. return $response['repository'];
  264. }
  265. /**
  266. * Get the tags of a repository
  267. * http://develop.github.com/p/repo.html
  268. *
  269. * @param string $username the user who owns the repo
  270. * @param string $repo the name of the repo
  271. * @return array list of the repo tags
  272. */
  273. public function getRepoTags($username, $repo)
  274. {
  275. $response = $this->get('repos/show/'.urlencode($username).'/'.urlencode($repo).'/tags');
  276. return $response['tags'];
  277. }
  278. /**
  279. * Get the branches of a repository
  280. * http://develop.github.com/p/repo.html
  281. *
  282. * @param string $username the username
  283. * @param string $repo the name of the repo
  284. * @return array list of the repo branches
  285. */
  286. public function getRepoBranches($username, $repo)
  287. {
  288. $response = $this->get('repos/show/'.urlencode($username).'/'.urlencode($repo).'/branches');
  289. return $response['branches'];
  290. }
  291. /**
  292. * Get the watchers of a repository
  293. * http://develop.github.com/p/repo.html
  294. *
  295. * @param string $username the user who owns the repo
  296. * @param string $repo the name of the repo
  297. * @return array list of the repo watchers
  298. */
  299. public function getRepoWatchers($username, $repo)
  300. {
  301. $response = $this->get('repos/show/'.urlencode($username).'/'.urlencode($repo).'/watchers');
  302. return $response['watchers'];
  303. }
  304. /**
  305. * Get the network (a list of forks) of a repository
  306. * http://develop.github.com/p/repo.html
  307. *
  308. * @param string $username the user who owns the repo
  309. * @param string $repo the name of the repo
  310. * @return array list of the repo forks
  311. */
  312. public function getRepoNetwork($username, $repo)
  313. {
  314. $response = $this->get('repos/show/'.urlencode($username).'/'.urlencode($repo).'/network');
  315. return $response['network'];
  316. }
  317. /**
  318. * Get the language breakdown of a repository
  319. * http://develop.github.com/p/repo.html
  320. *
  321. * @param string $username the user who owns the repo
  322. * @param string $repo the name of the repo
  323. * @return array list of the languages
  324. */
  325. public function getRepoLanguages($username, $repo)
  326. {
  327. $response = $this->get('repos/show/'.urlencode($username).'/'.urlencode($repo).'/languages');
  328. return $response['languages'];
  329. }
  330. /**
  331. * Get the contributors of a repository
  332. * http://develop.github.com/p/repo.html
  333. *
  334. * @param string $username the user who owns the repo
  335. * @param string $repo the name of the repo
  336. * @param boolean $includingNonGithubUsers by default, the list only shows GitHub users. You can include non-users too by setting this to true
  337. * @return array list of the repo contributors
  338. */
  339. public function getRepoContributors($username, $repo, $includingNonGithubUsers = false)
  340. {
  341. $url = 'repos/show/'.urlencode($username).'/'.urlencode($repo).'/contributors';
  342. if ($includingNonGithubUsers) {
  343. $url .= '/anon';
  344. }
  345. $response = $this->get($url);
  346. return $response['contributors'];
  347. }
  348. }