PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Github/Api/Repo.php

https://gitlab.com/scribe-inc/ScribeGitHubApiLibrary
PHP | 438 lines | 166 code | 34 blank | 238 comment | 3 complexity | 334d351215898139e6cb474b5a5586a7 MD5 | raw file
  1. <?php
  2. namespace Github\Api;
  3. use Github\Api\Repository\Collaborators;
  4. use Github\Api\Repository\Comments;
  5. use Github\Api\Repository\Commits;
  6. use Github\Api\Repository\Contents;
  7. use Github\Api\Repository\DeployKeys;
  8. use Github\Api\Repository\Downloads;
  9. use Github\Api\Repository\Releases;
  10. use Github\Api\Repository\Forks;
  11. use Github\Api\Repository\Hooks;
  12. use Github\Api\Repository\Labels;
  13. use Github\Api\Repository\Statuses;
  14. /**
  15. * Searching repositories, getting repository information
  16. * and managing repository information for authenticated users.
  17. *
  18. * @link http://developer.github.com/v3/repos/
  19. * @author Joseph Bielawski <stloyd@gmail.com>
  20. * @author Thibault Duplessis <thibault.duplessis at gmail dot com>
  21. */
  22. class Repo extends AbstractApi
  23. {
  24. /**
  25. * Search repositories by keyword:
  26. * @link http://developer.github.com/v3/search/#search-repositories
  27. *
  28. * @param string $keyword the search query
  29. * @param array $params
  30. *
  31. * @return array list of found repositories
  32. */
  33. public function find($keyword, array $params = array())
  34. {
  35. return $this->get('legacy/repos/search/'.rawurlencode($keyword), array_merge(array('start_page' => 1), $params));
  36. }
  37. /**
  38. * Get the last year of commit activity for a repository grouped by week
  39. * @link http://developer.github.com/v3/repos/statistics/#commit-activity
  40. *
  41. * @param string $username the user who owns the repository
  42. * @param string $repository the name of the repository
  43. *
  44. * @return array commit activity grouped by week
  45. */
  46. public function activity($username, $repository)
  47. {
  48. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/stats/commit_activity');
  49. }
  50. /**
  51. * Get contributor commit statistics for a repository
  52. * @link http://developer.github.com/v3/repos/statistics/#contributors
  53. *
  54. * @param string $username the user who owns the repository
  55. * @param string $repository the name of the repository
  56. *
  57. * @return array list of contributors and their commit statistics
  58. */
  59. public function statistics($username, $repository)
  60. {
  61. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/stats/contributors');
  62. }
  63. /**
  64. * List all repositories for an organization
  65. * @link http://developer.github.com/v3/repos/#list-organization-repositories
  66. *
  67. * @param string $organization the name of the organization
  68. * @param array $params
  69. *
  70. * @return array list of organization repositories
  71. */
  72. public function org($organization, array $params = array())
  73. {
  74. return $this->get('orgs/'.$organization.'/repos', array_merge(array('start_page' => 1), $params));
  75. }
  76. /**
  77. * Get extended information about a repository by its username and repository name
  78. * @link http://developer.github.com/v3/repos/
  79. *
  80. * @param string $username the user who owns the repository
  81. * @param string $repository the name of the repository
  82. *
  83. * @return array informations about the repository
  84. */
  85. public function show($username, $repository)
  86. {
  87. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository));
  88. }
  89. /**
  90. * Create repository
  91. * @link http://developer.github.com/v3/repos/
  92. *
  93. * @param string $name name of the repository
  94. * @param string $description repository description
  95. * @param string $homepage homepage url
  96. * @param boolean $public `true` for public, `false` for private
  97. * @param null|string $organization username of organization if applicable
  98. * @param boolean $hasIssues `true` to enable issues for this repository, `false` to disable them
  99. * @param boolean $hasWiki `true` to enable the wiki for this repository, `false` to disable it
  100. * @param boolean $hadDownloads `true` to enable downloads for this repository, `false` to disable them
  101. * @param integer $teamId The id of the team that will be granted access to this repository. This is only valid when creating a repo in an organization.
  102. * @param boolean $autoInit `true` to create an initial commit with empty README, `false` for no initial commit
  103. *
  104. * @return array returns repository data
  105. */
  106. public function create(
  107. $name,
  108. $description = '',
  109. $homepage = '',
  110. $public = true,
  111. $organization = null,
  112. $hasIssues = false,
  113. $hasWiki = false,
  114. $hasDownloads = false,
  115. $teamId = null,
  116. $autoInit = false
  117. ) {
  118. $path = null !== $organization ? 'orgs/'.$organization.'/repos' : 'user/repos';
  119. $parameters = array(
  120. 'name' => $name,
  121. 'description' => $description,
  122. 'homepage' => $homepage,
  123. 'private' => !$public,
  124. 'has_issues' => $hasIssues,
  125. 'has_wiki' => $hasWiki,
  126. 'has_downloads' => $hasDownloads,
  127. 'auto_init' => $autoInit
  128. );
  129. if ($organization && $teamId) {
  130. $parameters['team_id'] = $teamId;
  131. }
  132. return $this->post($path, $parameters);
  133. }
  134. /**
  135. * Set information of a repository
  136. * @link http://developer.github.com/v3/repos/
  137. *
  138. * @param string $username the user who owns the repository
  139. * @param string $repository the name of the repository
  140. * @param array $values the key => value pairs to post
  141. *
  142. * @return array informations about the repository
  143. */
  144. public function update($username, $repository, array $values)
  145. {
  146. return $this->patch('repos/'.rawurlencode($username).'/'.rawurlencode($repository), $values);
  147. }
  148. /**
  149. * Delete a repository
  150. * @link http://developer.github.com/v3/repos/
  151. *
  152. * @param string $username the user who owns the repository
  153. * @param string $repository the name of the repository
  154. *
  155. * @return mixed null on success, array on error with 'message'
  156. */
  157. public function remove($username, $repository)
  158. {
  159. return $this->delete('repos/'.rawurlencode($username).'/'.rawurlencode($repository));
  160. }
  161. /**
  162. * Get the readme content for a repository by its username and repository name
  163. * @link http://developer.github.com/v3/repos/contents/#get-the-readme
  164. *
  165. * @param string $username the user who owns the repository
  166. * @param string $repository the name of the repository
  167. *
  168. * @return array the readme content
  169. */
  170. public function readme($username, $repository)
  171. {
  172. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/readme');
  173. }
  174. /**
  175. * Manage the collaborators of a repository
  176. * @link http://developer.github.com/v3/repos/collaborators/
  177. *
  178. * @return Collaborators
  179. */
  180. public function collaborators()
  181. {
  182. return new Collaborators($this->client);
  183. }
  184. /**
  185. * Manage the comments of a repository
  186. * @link http://developer.github.com/v3/repos/comments/
  187. *
  188. * @return Comments
  189. */
  190. public function comments()
  191. {
  192. return new Comments($this->client);
  193. }
  194. /**
  195. * Manage the commits of a repository
  196. * @link http://developer.github.com/v3/repos/commits/
  197. *
  198. * @return Commits
  199. */
  200. public function commits()
  201. {
  202. return new Commits($this->client);
  203. }
  204. /**
  205. * Manage the content of a repository
  206. * @link http://developer.github.com/v3/repos/contents/
  207. *
  208. * @return Contents
  209. */
  210. public function contents()
  211. {
  212. return new Contents($this->client);
  213. }
  214. /**
  215. * Manage the content of a repository
  216. * @link http://developer.github.com/v3/repos/downloads/
  217. *
  218. * @return Downloads
  219. */
  220. public function downloads()
  221. {
  222. return new Downloads($this->client);
  223. }
  224. /**
  225. * Manage the releases of a repository (Currently Undocumented)
  226. * @link http://developer.github.com/v3/repos/
  227. *
  228. * @return Releases
  229. */
  230. public function releases()
  231. {
  232. return new Releases($this->client);
  233. }
  234. /**
  235. * Manage the deploy keys of a repository
  236. * @link http://developer.github.com/v3/repos/keys/
  237. *
  238. * @return DeployKeys
  239. */
  240. public function keys()
  241. {
  242. return new DeployKeys($this->client);
  243. }
  244. /**
  245. * Manage the forks of a repository
  246. * @link http://developer.github.com/v3/repos/forks/
  247. *
  248. * @return Forks
  249. */
  250. public function forks()
  251. {
  252. return new Forks($this->client);
  253. }
  254. /**
  255. * Manage the hooks of a repository
  256. * @link http://developer.github.com/v3/issues/jooks/
  257. *
  258. * @return Hooks
  259. */
  260. public function hooks()
  261. {
  262. return new Hooks($this->client);
  263. }
  264. /**
  265. * Manage the labels of a repository
  266. * @link http://developer.github.com/v3/issues/labels/
  267. *
  268. * @return Labels
  269. */
  270. public function labels()
  271. {
  272. return new Labels($this->client);
  273. }
  274. /**
  275. * Manage the statuses of a repository
  276. * @link http://developer.github.com/v3/repos/statuses/
  277. *
  278. * @return Statuses
  279. */
  280. public function statuses()
  281. {
  282. return new Statuses($this->client);
  283. }
  284. /**
  285. * Get the branch(es) of a repository
  286. * @link http://developer.github.com/v3/repos/
  287. *
  288. * @param string $username the username
  289. * @param string $repository the name of the repository
  290. * @param string $branch the name of the branch
  291. *
  292. * @return array list of the repository branches
  293. */
  294. public function branches($username, $repository, $branch = null)
  295. {
  296. $url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/branches';
  297. if (null !== $branch) {
  298. $url .= '/'.rawurlencode($branch);
  299. }
  300. return $this->get($url);
  301. }
  302. /**
  303. * Get the contributors of a repository
  304. * @link http://developer.github.com/v3/repos/
  305. *
  306. * @param string $username the user who owns the repository
  307. * @param string $repository the name of the repository
  308. * @param boolean $includingAnonymous by default, the list only shows GitHub users.
  309. * You can include non-users too by setting this to true
  310. * @return array list of the repo contributors
  311. */
  312. public function contributors($username, $repository, $includingAnonymous = false)
  313. {
  314. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contributors', array(
  315. 'anon' => $includingAnonymous ?: null
  316. ));
  317. }
  318. /**
  319. * Get the language breakdown of a repository
  320. * @link http://developer.github.com/v3/repos/
  321. *
  322. * @param string $username the user who owns the repository
  323. * @param string $repository the name of the repository
  324. *
  325. * @return array list of the languages
  326. */
  327. public function languages($username, $repository)
  328. {
  329. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/languages');
  330. }
  331. /**
  332. * Get the tags of a repository
  333. * @link http://developer.github.com/v3/repos/
  334. *
  335. * @param string $username the user who owns the repository
  336. * @param string $repository the name of the repository
  337. *
  338. * @return array list of the repository tags
  339. */
  340. public function tags($username, $repository)
  341. {
  342. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/tags');
  343. }
  344. /**
  345. * Get the teams of a repository
  346. * @link http://developer.github.com/v3/repos/
  347. *
  348. * @param string $username the user who owns the repo
  349. * @param string $repository the name of the repo
  350. *
  351. * @return array list of the languages
  352. */
  353. public function teams($username, $repository)
  354. {
  355. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/teams');
  356. }
  357. /**
  358. * @deprecated see subscribers method
  359. * @param string $username
  360. * @param string $repository
  361. * @param integer $page
  362. *
  363. * @return array
  364. */
  365. public function watchers($username, $repository, $page = 1)
  366. {
  367. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/watchers', array(
  368. 'page' => $page
  369. ));
  370. }
  371. /**
  372. * @param string $username
  373. * @param string $repository
  374. * @param integer $page
  375. *
  376. * @return array
  377. */
  378. public function subscribers($username, $repository, $page = 1)
  379. {
  380. return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/subscribers', array(
  381. 'page' => $page
  382. ));
  383. }
  384. /**
  385. * Perform a merge
  386. * @link http://developer.github.com/v3/repos/merging/
  387. *
  388. * @param string $username
  389. * @param string $repository
  390. * @param string $base The name of the base branch that the head will be merged into.
  391. * @param string $head The head to merge. This can be a branch name or a commit SHA1.
  392. * @param string $message Commit message to use for the merge commit. If omitted, a default message will be used.
  393. *
  394. * @return array|null
  395. */
  396. public function merge($username, $repository, $base, $head, $message = null)
  397. {
  398. return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/merges', array(
  399. 'base' => $base,
  400. 'head' => $head,
  401. 'commit_message' => $message
  402. ));
  403. }
  404. }