github_lib.php 5.1 KB

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