def get_user(user): """Retrieve the GitHub user object for the given User. Args: user: the User to retrieve the GitHub user for. Returns: GitHub AuthenticatedUser instance. """ github = Github(user.github_token) gh_user = memcache.get('gh:user:%s' % user.key.id()) if gh_user: return github.create_from_raw_data(AuthenticatedUser, gh_user) gh_user = github.get_user() memcache.set('gh:user:%s' % user.key.id(), gh_user.raw_data) return gh_user
class GithubAPIWrapper: def __init__(self, user, password): self.g = Github(user, password) def get_rate_limit(self): rate = self.g.get_rate_limit() return (rate.rate.remaining, rate.rate.limit) def get_api_status(self): return self.g.get_api_status().status def query_by_stars(self, low, high): return self.g.search_repositories("stars:%d..%d" % (low, high)) def issues_by_date(self, label, low, high, sort="created", order="asc"): return self.g.search_issues("created:%s..%s type:issue label:%s" % (low, high, label), sort, order) def load_repo(self, raw): return self.g.create_from_raw_data(Repository, raw) def sleep(self, secs): time.sleep(secs)
def get_repos(user, repo_type='owner'): """Retrieve the User's GitHub repos. Args: user: the User to retrieve GitHub repos for. repo_type: the type of repos to fetch (all, owner, public, private, member). Returns: list of Github Repository instances. """ github = Github(user.github_token) repos = memcache.get('gh:repos:%s' % user.key.id()) if repos: return [github.create_from_raw_data(Repository, r) for r in repos] gh_user = github.get_user() repos = gh_user.get_repos(type=repo_type) memcache.set('gh:repos:%s' % user.key.id(), [r.raw_data for r in repos]) return repos
def setUp(self): from github import Github from github.Commit import Commit from kaput.tests import KAPUT_QUEUES_PATH super(TestProcessGithubPush, self).setUp() self.testbed.init_taskqueue_stub(root_path=KAPUT_QUEUES_PATH) # Ensure each test looks like a new request. os.environ['REQUEST_ID_HASH'] = uuid.uuid4().hex self.queue_service = self.testbed.get_stub( testbed.TASKQUEUE_SERVICE_NAME) self.user = User(id='github_123', username='******', primary_email='*****@*****.**', emails=['*****@*****.**'], github_token='token') self.user.put() self.push_payload = """ { "ref": "refs/heads/master", "after": "0832d0f7deb197617c9cdafd318432fc13bee35d", "before": "41b5ac93f2b4bdf655050146e108af3c399753e5", "created": false, "deleted": false, "forced": false, "compare": "https://github.com/tylertreat/webhook-test/compare/41b5ac93f2b4...0832d0f7deb1", "commits": [ { "id": "0832d0f7deb197617c9cdafd318432fc13bee35d", "distinct": true, "message": "Delete another newline", "timestamp": "2014-05-26T00:12:09-05:00", "url": "https://github.com/tylertreat/webhook-test/commit/0832d0f7deb197617c9cdafd318432fc13bee35d", "author": { "name": "Tyler Treat", "email": "*****@*****.**", "username": "******" }, "committer": { "name": "Tyler Treat", "email": "*****@*****.**", "username": "******" }, "added": [], "removed": [], "modified": [ "main.py" ] } ], "head_commit": { "id": "0832d0f7deb197617c9cdafd318432fc13bee35d", "distinct": true, "message": "Delete another newline", "timestamp": "2014-05-26T00:12:09-05:00", "url": "https://github.com/tylertreat/webhook-test/commit/0832d0f7deb197617c9cdafd318432fc13bee35d", "author": { "name": "Tyler Treat", "email": "*****@*****.**", "username": "******" }, "committer": { "name": "Tyler Treat", "email": "*****@*****.**", "username": "******" }, "added": [], "removed": [], "modified": [ "main.py" ] }, "repository": { "id": 16896925, "name": "webhook-test", "url": "https://github.com/tylertreat/webhook-test", "description": "webhook test", "watchers": 0, "stargazers": 0, "forks": 0, "fork": false, "size": 1048, "owner": { "name": "tylertreat", "email": "*****@*****.**" }, "private": false, "open_issues": 0, "has_issues": true, "has_downloads": true, "has_wiki": true, "language": "Python", "created_at": 1392593853, "pushed_at": 1401081153, "master_branch": "master" }, "pusher": { "name": "tylertreat", "email": "*****@*****.**" } } """ github = Github() self.commit = github.create_from_raw_data(Commit, json.loads( r''' { "sha": "0832d0f7deb197617c9cdafd318432fc13bee35d", "commit": { "author": { "name": "Tyler Treat", "email": "*****@*****.**", "date": "2014-05-26T05:12:09Z" }, "committer": { "name": "Tyler Treat", "email": "*****@*****.**", "date": "2014-05-26T05:12:09Z" }, "message": "Delete another newline", "tree": { "sha": "60e97a797de99d0d8a8a1ca05399145f14f98a66", "url": "https://api.github.com/repos/tylertreat/webhook-test/git/trees/60e97a797de99d0d8a8a1ca05399145f14f98a66" }, "url": "https://api.github.com/repos/tylertreat/webhook-test/git/commits/0832d0f7deb197617c9cdafd318432fc13bee35d", "comment_count": 0 }, "url": "https://api.github.com/repos/tylertreat/webhook-test/commits/0832d0f7deb197617c9cdafd318432fc13bee35d", "html_url": "https://github.com/tylertreat/webhook-test/commit/0832d0f7deb197617c9cdafd318432fc13bee35d", "comments_url": "https://api.github.com/repos/tylertreat/webhook-test/commits/0832d0f7deb197617c9cdafd318432fc13bee35d/comments", "author": { "login": "******", "id": 552817, "avatar_url": "https://avatars.githubusercontent.com/u/552817?", "gravatar_id": "dcbf01e42178cd9698fb3d4806e33d84", "url": "https://api.github.com/users/tylertreat", "html_url": "https://github.com/tylertreat", "followers_url": "https://api.github.com/users/tylertreat/followers", "following_url": "https://api.github.com/users/tylertreat/following{/other_user}", "gists_url": "https://api.github.com/users/tylertreat/gists{/gist_id}", "starred_url": "https://api.github.com/users/tylertreat/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/tylertreat/subscriptions", "organizations_url": "https://api.github.com/users/tylertreat/orgs", "repos_url": "https://api.github.com/users/tylertreat/repos", "events_url": "https://api.github.com/users/tylertreat/events{/privacy}", "received_events_url": "https://api.github.com/users/tylertreat/received_events", "type": "User", "site_admin": false }, "committer": { "login": "******", "id": 552817, "avatar_url": "https://avatars.githubusercontent.com/u/552817?", "gravatar_id": "dcbf01e42178cd9698fb3d4806e33d84", "url": "https://api.github.com/users/tylertreat", "html_url": "https://github.com/tylertreat", "followers_url": "https://api.github.com/users/tylertreat/followers", "following_url": "https://api.github.com/users/tylertreat/following{/other_user}", "gists_url": "https://api.github.com/users/tylertreat/gists{/gist_id}", "starred_url": "https://api.github.com/users/tylertreat/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/tylertreat/subscriptions", "organizations_url": "https://api.github.com/users/tylertreat/orgs", "repos_url": "https://api.github.com/users/tylertreat/repos", "events_url": "https://api.github.com/users/tylertreat/events{/privacy}", "received_events_url": "https://api.github.com/users/tylertreat/received_events", "type": "User", "site_admin": false }, "parents": [ { "sha": "a4240c12ef6152503ab6be1d80f9fbae4609d572", "url": "https://api.github.com/repos/tylertreat/webhook-test/commits/a4240c12ef6152503ab6be1d80f9fbae4609d572", "html_url": "https://github.com/tylertreat/webhook-test/commit/a4240c12ef6152503ab6be1d80f9fbae4609d572" } ], "stats": { "total": 1, "additions": 0, "deletions": 1 }, "files": [ { "sha": "8e32958cd62fb81a7548c46327baead0e8816156", "filename": "main.py", "status": "modified", "additions": 0, "deletions": 1, "changes": 1, "blob_url": "https://github.com/tylertreat/webhook-test/blob/0832d0f7deb197617c9cdafd318432fc13bee35d/main.py", "raw_url": "https://github.com/tylertreat/webhook-test/raw/0832d0f7deb197617c9cdafd318432fc13bee35d/main.py", "contents_url": "https://api.github.com/repos/tylertreat/webhook-test/contents/main.py?ref=0832d0f7deb197617c9cdafd318432fc13bee35d", "patch": "@@ -35,4 +35,3 @@ def push():\n \n if __name__ == '__main__':\n app.run()\n-" } ] } ''' ))