Skip to content

Commit

Permalink
implement #48 & #53
Browse files Browse the repository at this point in the history
added /repos/{repos_key}/branches (#48)
added /repos/{repo_key}/branches/{branch_name} (#53)
includes tests by @cvrebert
  • Loading branch information
rajivm authored and cvrebert committed Dec 20, 2013
1 parent d8f44ff commit 030c2a3
Showing 2 changed files with 151 additions and 4 deletions.
74 changes: 70 additions & 4 deletions restfulgit/__init__.py
Original file line number Diff line number Diff line change
@@ -54,6 +54,8 @@ class DefaultConfig(object):
restfulgit = Blueprint('restfulgit', __name__) # pylint: disable=C0103


### PLUMBING RETRIEVE OBJECTS ###

def _get_repo(repo_key):
path = safe_join(current_app.config['RESTFULGIT_REPO_BASE_PATH'], repo_key)
try:
@@ -121,6 +123,16 @@ def _lookup_ref(repo, ref_name):
return None


#### PORCELAIN RETRIEVE OBJECTS ####


def _get_branch(repo, branch_name):
branch = repo.lookup_branch(branch_name)
if branch is None:
raise NotFound("branch not found")
return branch


def _get_commit_for_refspec(repo, branch_or_tag_or_sha):
# Precedence order GitHub uses (& that we copy):
# 1. Branch 2. Tag 3. Commit SHA
@@ -187,12 +199,15 @@ def _get_repo_description(repo_key):
return description


#### PLUMBING OBJECT CONVERTERS ####


def _convert_repo(repo_key):
description = _get_repo_description(repo_key)
return {
"name": repo_key,
"description": description,
"url": url_for('.get_repo', _external=True, repo_key=repo_key)
"url": url_for('.get_repo', _external=True, repo_key=repo_key),
}


@@ -411,6 +426,23 @@ def _convert_ref(repo_key, ref, obj):
}


#### PORCELAIN DATA CONVERTERS ####


def _convert_branch(repo_key, repo, branch):
url = url_for('.get_branch', _external=True, repo_key=repo_key, branch_name=branch.branch_name)
return {
"name": branch.branch_name,
"commit": _repos_convert_commit(repo_key, repo, branch.get_object()),
"url": url,
"_links": {
# For some reason GitHub API for branch does the self-link like this
# instead of with "url" as everywhere else.
"self": url,
}
}


def jsonify(func):
def dthandler(obj):
if hasattr(obj, 'isoformat'):
@@ -423,6 +455,9 @@ def wrapped(*args, **kwargs):
return wrapped


#### VIEW UTILS ####


def corsify(func):
# based on http://flask.pocoo.org/snippets/56/
func.provide_automatic_options = False
@@ -468,9 +503,6 @@ def dst(self, dt): # pylint: disable=W0613

UTC = FixedOffset(0)

##### VIEWS #####


OCTET_STREAM = 'application/octet-stream'


@@ -508,6 +540,9 @@ class SHAConverter(BaseConverter): # pylint: disable=W0232
register_converter(restfulgit, 'sha', SHAConverter)


##### PLUMBING API VIEWS #####


@restfulgit.route('/repos/<repo_key>/git/commits/')
@corsify
@jsonify
@@ -597,6 +632,9 @@ def get_tag(repo_key, sha):
return _convert_tag(repo_key, repo, tag)


##### PORCELAIN API VIEWS #####


@restfulgit.route('/repos/<repo_key>/')
@corsify
@jsonify
@@ -621,6 +659,34 @@ def get_repo_list():
return [_convert_repo(repo_key) for repo_key in repositories]


@restfulgit.route('/repos/<repo_key>/branches/')
@corsify
@jsonify
def get_branches(repo_key):
repo = _get_repo(repo_key)
branches = [repo.lookup_branch(branch_name) for branch_name in repo.listall_branches()]
return [
{
"name": branch.branch_name,
"commit": {
"sha": branch.target.hex,
"url": url_for('.get_repos_commit', _external=True,
repo_key=repo_key, branch_or_tag_or_sha=branch.target.hex),
},
}
for branch in branches
]


@restfulgit.route('/repos/<repo_key>/branches/<branch_name>/')
@corsify
@jsonify
def get_branch(repo_key, branch_name):
repo = _get_repo(repo_key)
branch = _get_branch(repo, branch_name)
return _convert_branch(repo_key, repo, branch)


@restfulgit.route('/repos/<repo_key>/git/refs/')
@restfulgit.route('/repos/<repo_key>/git/refs/<path:ref_path>')
@corsify
81 changes: 81 additions & 0 deletions tests/test_restfulgit.py
Original file line number Diff line number Diff line change
@@ -596,6 +596,87 @@ def test_get_tag_works(self):
}
)

def test_get_repo_branches_works(self):
# From https://api.github.com/repos/hulu/restfulgit/branches with necessary adjustments
reference_branch = {
"name": "ambiguous",
"commit": {
"sha": "1f51b91ac383806df9d322ae67bbad3364f50811",
"url": "http://localhost/repos/restfulgit/commits/1f51b91ac383806df9d322ae67bbad3364f50811/"
}
}
resp = self.client.get('/repos/restfulgit/branches/')
self.assert200(resp)
json = resp.json
self.assertIsInstance(json, list)
for branch in json:
self.assertIsInstance(branch, dict)
self.assertIn('name', branch)
ambiguous_branches = [branch for branch in json if branch['name'] == 'ambiguous']
self.assertEqual(len(ambiguous_branches), 1)
ambiguous_branch = ambiguous_branches[0]
self.assertEqual(reference_branch, ambiguous_branch)

def test_get_repo_branch_works(self):
# From https://api.github.com/repos/hulu/restfulgit/branches/ambiguous with necessary adjustments
reference = {
"name": "ambiguous",
"commit": {
"sha": "1f51b91ac383806df9d322ae67bbad3364f50811",
"commit": {
"author": {
"name": "Rajiv Makhijani",
"email": "rajiv@hulu.com",
"date": "2013-02-25T12:35:29Z"
},
"committer": {
"name": "Rajiv Makhijani",
"email": "rajiv@hulu.com",
"date": "2013-02-25T12:35:29Z"
},
"message": "Support submodule in tree-listings",
"tree": {
"sha": "1404e1766a3269f5a73b3d2ec8c81b7ea3ad6e09",
"url": "http://localhost/repos/restfulgit/git/trees/1404e1766a3269f5a73b3d2ec8c81b7ea3ad6e09/"
},
"url": "http://localhost/repos/restfulgit/git/commits/1f51b91ac383806df9d322ae67bbad3364f50811/",
"sha": "1f51b91ac383806df9d322ae67bbad3364f50811", # NOTE: RestfulGit extension
"parents": [ # NOTE: RestfulGit extension
{
"sha": "ff6405b71273b5c2c50d5c33d5cf962af5390542",
"url": "http://localhost/repos/restfulgit/commits/ff6405b71273b5c2c50d5c33d5cf962af5390542/",
}
]
},
"url": "http://localhost/repos/restfulgit/commits/1f51b91ac383806df9d322ae67bbad3364f50811/",
"author": {
"name": "Rajiv Makhijani",
"email": "rajiv@hulu.com",
"date": "2013-02-25T12:35:29Z"
},
"committer": {
"name": "Rajiv Makhijani",
"email": "rajiv@hulu.com",
"date": "2013-02-25T12:35:29Z"
},
"parents": [
{
"sha": "ff6405b71273b5c2c50d5c33d5cf962af5390542",
"url": "http://localhost/repos/restfulgit/commits/ff6405b71273b5c2c50d5c33d5cf962af5390542/",
}
]
},
"_links": {
"self": "http://localhost/repos/restfulgit/branches/ambiguous/",
},
'url': 'http://localhost/repos/restfulgit/branches/ambiguous/'
}
resp = self.client.get('/repos/restfulgit/branches/ambiguous/')
self.assert200(resp)
json = resp.json
self.maxDiff = None
self.assertEqual(reference, json)

def test_get_repo_commit(self):
# From https://api.github.com/repos/hulu/restfulgit/commits/d408fc2428bc6444cabd7f7b46edbe70b6992b16 with necessary adjustments
reference = {

0 comments on commit 030c2a3

Please sign in to comment.