浏览代码

github lib

dereuromark 15 年之前
父节点
当前提交
f8b9b59d30
共有 3 个文件被更改,包括 316 次插入0 次删除
  1. 4 0
      TODOS
  2. 204 0
      libs/github_lib.php
  3. 108 0
      tests/cases/libs/github_lib.test.php

+ 4 - 0
TODOS

@@ -1,3 +1,7 @@
+TOOLS (shell)
+- write test cases
+- refactor + enhance
+
 CAPTCHA
 - write test cases
 - confirm security (is it really preventing all kinds of hacking attempts - or least slowing them down?)

+ 204 - 0
libs/github_lib.php

@@ -0,0 +1,204 @@
+<?php
+
+/**
+ * access to github
+ */
+class GithubLib {
+
+	const JSON_URL = 'http://github.com/api/v2/json/';
+
+	// First tries with curl, then cake, then php
+	var $use = array('curl' => true, 'cake'=> true, 'php' => true);
+
+
+  /**
+   * Grab all issues for a specific repository
+   *
+   * @access	public
+   * @param	string - a GitHub user
+   * @param	string - a repository name
+   * @param	string - the state of the issues to pull (open/closed)
+   * @return	object - an object with all the repository's issues
+   */
+	public function projectIssues($user = '', $repo = '', $state = 'open') {
+		$response = $this->_fetch('issues/list/'.$user.'/'.$repo.'/'.$state);
+
+		if(empty($response->issues)) {
+			return false;
+		}
+		return $response->issues;
+	}
+
+
+  /**
+   * Grab the info for a repository
+   *
+   * @access	public
+   * @param	string - a GitHub user
+   * @param	string - a repository name
+   * @return array with all the repository's info
+   */
+  public function repoInfo($user = '', $repo = '') {
+  	$response = $this->_fetch('repos/show/'.$user.'/'.$repo);
+
+  	if(empty($response->repository)) {
+  		return false;
+  	}
+  	return (array)$response->repository;
+  }
+
+
+  /**
+   * Grab all refs for a specific repository
+   *
+   * @access	public
+   * @param	string - a GitHub user
+   * @param	string - a repository name
+   * @param	string - the repository reference to pull (tags/branches)
+   * @return array with all the repository's references
+   */
+  public function repoRefs($user = '', $repo = '', $ref = 'tags') {
+  	$response = $this->_fetch('repos/show/'.$user.'/'.$repo.'/'.$ref);
+
+  	if(empty($response->$ref)) {
+  		return false;
+  	}
+  	return (array)$response->$ref;
+  }
+
+
+	/**
+   * Grab the info for a specific user
+   *
+   * @access	public
+   * @param	string - a GitHub user
+   * @return array with all infos (gravatar_id, name, company, location, blog, id, login, email, ...)
+   */
+  public function userInfo($user = '') {
+  	$response = $this->_fetch('user/show/'.$user);
+
+  	if(empty($response->user)) {
+  		return false;
+  	}
+  	return (array)$response->user;
+  }
+
+
+	/**
+   * Grab all commits by a user to a specific repository
+   *
+   * @access public
+   * @param	string - a GitHub user
+   * @param	string - a repository name
+   * @param	string - the branch name (master by default)
+   * @return object - an object with all the branch's commits (array[array[parents, author, url, id, comitter, ...]])
+   */
+  public function userTimeline($user, $repo, $branch = 'master') {
+  	$response = $this->_fetch('commits/list/'.$user.'/'.$repo.'/'.$branch);
+
+  	if(empty($response->commits)) {
+  		return false;
+  	}
+
+  	return $response->commits;
+  }
+
+
+	/**
+	 * get the last commits with message and date
+   * @access public
+   * @param	string - a GitHub user
+   * @param	string - a repository name
+   * @param	string - the branch name (master by default)
+	 * @return array (url, commited, message)
+	 * 2010-06-24 ms
+	 */
+	function lastCommits($user, $repo, $branch = 'master', $limit = 10) {
+		if (!($response = $this->userTimeline($user, $repo, $branch))) {
+			return false;
+		}
+		$result = array();
+		foreach ($response as $c) {
+			if ($limit-- == 0) {
+				break;
+			}
+			$result[] = array('url'=>$c->url, 'committed' =>$c->committed_date, 'message' => $c->message);
+		}
+		return $result;
+	}
+
+
+  /**
+   * Search GitHub
+   *
+   * @access	public
+   * @param	string - the term to search for
+   * @param	string - the language
+   * @return	array  - an array with all the search results
+   */
+  public function search($term, $language = null) {
+  	if(!empty($language) && is_string($language)) {
+  		$language = strtolower($language);
+  	}
+
+  	$response = $this->_fetch('search/'.$term);
+
+  	if(empty($response->repositories) or !is_array($response->repositories)) {
+  		return false;
+  	}
+
+  	$results = array();
+
+  	foreach($response->repositories as &$result) {
+  		if($language != strtolower($result->language)) {
+  			continue;
+  		}
+  		$results[] = $result;
+  	}
+  	return $results;
+  }
+
+
+	/**
+	 * fetches url with curl if available
+	 * fallbacks: cake and php
+	 * note: expects url with json encoded content
+	 * @access private
+	 **/
+	function _fetch($url) {
+		$url = self::JSON_URL.$url;
+
+		if ($this->use['curl'] && function_exists('curl_init')) {
+			$ch = curl_init();
+			curl_setopt($ch, CURLOPT_URL, $url);
+			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+			curl_setopt($ch, CURLOPT_USERAGENT, 'cakephp github lib');
+			$response = curl_exec($ch);
+			$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+			curl_close ($ch);
+			if ($status != '200') {
+				return false;
+			}
+			return json_decode($response);
+
+		} elseif($this->use['cake'] && App::import('Core', 'HttpSocket')) {
+			$HttpSocket = new HttpSocket(array('timeout' => 5));
+			$response = $HttpSocket->get($url);
+			if (empty($response)) { //TODO: status 200?
+				return false;
+			}
+			return json_decode($response);
+
+		} elseif($this->use['php'] || true) {
+			$response = file_get_contents($url, 'r');
+			//TODO: status 200?
+			if (empty($response)) {
+				return false;
+			}
+			return json_decode($response);
+		}
+	}
+
+}
+
+?>

+ 108 - 0
tests/cases/libs/github_lib.test.php

@@ -0,0 +1,108 @@
+<?php
+
+App::import('Lib', 'Tools.GithubLib');
+
+class GithubLibTestCase extends CakeTestCase {
+
+	function setUp() {
+		Configure::write('debug', 1);
+
+		$this->GithubLib = new GithubLib();
+		$this->assertTrue(is_object($this->GithubLib));
+	}
+
+	function TearDown() {
+		unset($this->GithubLib);
+	}
+
+	function testFetch() {
+		$url = 'http://github.com/api/v2/json/issues/list/';
+		$is = $this->GithubLib->_fetch($url);
+		echo returns($is);
+		$this->assertFalse($is); // 401
+		//$this->assertTrue(!empty($is));
+
+		$url = 'http://github.com/api/';
+		$is = $this->GithubLib->_fetch($url);
+		echo returns($is);
+		$this->assertFalse($is); // 404
+	}
+
+
+
+	function testUser() {
+		$username = 'dereuromark';
+
+		$is = $this->GithubLib->userInfo($username);
+		echo returns($is);
+		$this->assertTrue(!empty($is));
+	}
+
+
+	function testCommits() {
+		$username = 'dereuromark';
+		$project = 'tools';
+
+		$is = $this->GithubLib->userTimeline('philsturgeon', 'codeigniter-github');
+		echo returns($is);
+		$this->assertTrue(!empty($is));
+
+
+	}
+
+	function testLastCommits() {
+		$is = $this->GithubLib->lastCommits('philsturgeon', 'codeigniter-github');
+		echo returns($is);
+		$this->assertTrue(!empty($is));
+	}
+
+
+	function testSearch() {
+		$term = 'cakephp';
+		$language = 'php';
+
+		$is = $this->GithubLib->search($term, $language);
+		echo returns($is);
+		$this->assertFalse($is); // WHY?
+		//$this->assertTrue(!empty($is));
+	}
+
+
+	function testRepoInfo() {
+		$user = 'dereuromark';
+		$repo = 'tools';
+
+		$is = $this->GithubLib->repoInfo($user, $repo);
+		echo returns($is);
+		$this->assertTrue(!empty($is));
+	}
+
+
+	function testRepoRefs() {
+		$user = 'dereuromark';
+		$repo = 'tools';
+
+		$is = $this->GithubLib->repoRefs($user, $repo);
+		echo returns($is);
+		$this->assertTrue(empty($is));
+
+		$is = $this->GithubLib->repoRefs($user, $repo, 'branches');
+		echo returns($is);
+		$this->assertTrue(!empty($is));
+	}
+
+
+	function testProjectIssues() {
+		$user = 'dereuromark';
+		$repo = 'tools';
+
+		$is = $this->GithubLib->projectIssues($user, $repo);
+		echo returns($is);
+		$this->assertFalse($is);
+	}
+
+
+
+
+}
+?>