github_lib.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * access to github
  4. */
  5. class GithubLib {
  6. const JSON_URL = 'http://github.com/api/v2/json/';
  7. // First tries with curl, then cake, then php
  8. var $use = array('curl' => true, 'cake'=> true, 'php' => true);
  9. /**
  10. * Grab all issues for a specific repository
  11. *
  12. * @access public
  13. * @param string - a GitHub user
  14. * @param string - a repository name
  15. * @param string - the state of the issues to pull (open/closed)
  16. * @return object - an object with all the repository's issues
  17. */
  18. public function projectIssues($user = '', $repo = '', $state = 'open') {
  19. $response = $this->_fetch('issues/list/'.$user.'/'.$repo.'/'.$state);
  20. if(empty($response->issues)) {
  21. return false;
  22. }
  23. return $response->issues;
  24. }
  25. /**
  26. * Grab the info for a repository
  27. *
  28. * @access public
  29. * @param string - a GitHub user
  30. * @param string - a repository name
  31. * @return array with all the repository's info
  32. */
  33. public function repoInfo($user = '', $repo = '') {
  34. $response = $this->_fetch('repos/show/'.$user.'/'.$repo);
  35. if(empty($response->repository)) {
  36. return false;
  37. }
  38. return (array)$response->repository;
  39. }
  40. /**
  41. * Grab all refs for a specific repository
  42. *
  43. * @access public
  44. * @param string - a GitHub user
  45. * @param string - a repository name
  46. * @param string - the repository reference to pull (tags/branches)
  47. * @return array with all the repository's references
  48. */
  49. public function repoRefs($user = '', $repo = '', $ref = 'tags') {
  50. $response = $this->_fetch('repos/show/'.$user.'/'.$repo.'/'.$ref);
  51. if(empty($response->$ref)) {
  52. return false;
  53. }
  54. return (array)$response->$ref;
  55. }
  56. /**
  57. * Grab the info for a specific user
  58. *
  59. * @access public
  60. * @param string - a GitHub user
  61. * @return array with all infos (gravatar_id, name, company, location, blog, id, login, email, ...)
  62. */
  63. public function userInfo($user = '') {
  64. $response = $this->_fetch('user/show/'.$user);
  65. if(empty($response->user)) {
  66. return false;
  67. }
  68. return (array)$response->user;
  69. }
  70. /**
  71. * Grab all commits by a user to a specific repository
  72. *
  73. * @access public
  74. * @param string - a GitHub user
  75. * @param string - a repository name
  76. * @param string - the branch name (master by default)
  77. * @return object - an object with all the branch's commits (array[array[parents, author, url, id, comitter, ...]])
  78. */
  79. public function userTimeline($user, $repo, $branch = 'master') {
  80. $response = $this->_fetch('commits/list/'.$user.'/'.$repo.'/'.$branch);
  81. if(empty($response->commits)) {
  82. return false;
  83. }
  84. return $response->commits;
  85. }
  86. /**
  87. * get the last commits with message and date
  88. * @access public
  89. * @param string - a GitHub user
  90. * @param string - a repository name
  91. * @param string - the branch name (master by default)
  92. * @return array (url, commited, message)
  93. * 2010-06-24 ms
  94. */
  95. function lastCommits($user, $repo, $branch = 'master', $limit = 10) {
  96. if (!($response = $this->userTimeline($user, $repo, $branch))) {
  97. return false;
  98. }
  99. $result = array();
  100. foreach ($response as $c) {
  101. if ($limit-- == 0) {
  102. break;
  103. }
  104. $result[] = array('url'=>$c->url, 'committed' =>$c->committed_date, 'message' => $c->message);
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Search GitHub
  110. *
  111. * @access public
  112. * @param string - the term to search for
  113. * @param string - the language
  114. * @return array - an array with all the search results
  115. */
  116. public function search($term, $language = null) {
  117. if(!empty($language) && is_string($language)) {
  118. $language = strtolower($language);
  119. }
  120. $response = $this->_fetch('search/'.$term);
  121. if(empty($response->repositories) or !is_array($response->repositories)) {
  122. return false;
  123. }
  124. $results = array();
  125. foreach($response->repositories as &$result) {
  126. if($language != strtolower($result->language)) {
  127. continue;
  128. }
  129. $results[] = $result;
  130. }
  131. return $results;
  132. }
  133. /**
  134. * fetches url with curl if available
  135. * fallbacks: cake and php
  136. * note: expects url with json encoded content
  137. * @access private
  138. **/
  139. function _fetch($url) {
  140. $url = self::JSON_URL.$url;
  141. if ($this->use['curl'] && function_exists('curl_init')) {
  142. $ch = curl_init();
  143. curl_setopt($ch, CURLOPT_URL, $url);
  144. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  145. curl_setopt($ch, CURLOPT_USERAGENT, 'cakephp github lib');
  146. $response = curl_exec($ch);
  147. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  148. curl_close ($ch);
  149. if ($status != '200') {
  150. return false;
  151. }
  152. return json_decode($response);
  153. } elseif($this->use['cake'] && App::import('Core', 'HttpSocket')) {
  154. $HttpSocket = new HttpSocket(array('timeout' => 5));
  155. $response = $HttpSocket->get($url);
  156. if (empty($response)) { //TODO: status 200?
  157. return false;
  158. }
  159. return json_decode($response);
  160. } elseif($this->use['php'] || true) {
  161. $response = file_get_contents($url, 'r');
  162. //TODO: status 200?
  163. if (empty($response)) {
  164. return false;
  165. }
  166. return json_decode($response);
  167. }
  168. }
  169. }
  170. ?>