github_lib.php 5.2 KB

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