Skip to content

Commit

Permalink
Merge branch 'feature/issue-25-allow-a-name-to-be-specified-for-a-rep…
Browse files Browse the repository at this point in the history
…o' into develop
  • Loading branch information
Rizziepit committed Jul 1, 2015
2 parents a95f3d3 + 30841ff commit f89afac
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
9 changes: 6 additions & 3 deletions unicore/distribute/api/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ def collection_get(self):
def collection_post(self):
storage_path = self.config.get('repo.storage_path')
repo_url = self.request.validated['repo_url']
repo_url_info = urlparse(repo_url)
repo_name_dot_git = os.path.basename(repo_url_info.path)
repo_name = repo_name_dot_git.partition('.git')[0]
repo_name = self.request.validated['repo_name']
if not repo_name:
repo_url_info = urlparse(repo_url)
repo_name_dot_git = os.path.basename(repo_url_info.path)
repo_name = repo_name_dot_git.partition('.git')[0]

try:
repo = EG.clone_repo(
repo_url, os.path.join(storage_path, repo_name))
Expand Down
12 changes: 12 additions & 0 deletions unicore/distribute/api/tests/test_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ def test_collection_post_success(self):
request = testing.DummyRequest({})
request.validated = {
'repo_url': self.remote_workspace.working_dir,
'repo_name': None
}
# Cleanup the repo created by the API on tear down
self.addCleanup(
lambda: EG.workspace(
os.path.join(
self.WORKING_DIR, api_repo_name)).destroy())
self.addCleanup(
lambda: EG.workspace(
os.path.join(
self.WORKING_DIR, 'foo-bar')).destroy())
request.route_url = lambda route, name: (
'/repos/%s.json' % (api_repo_name,))
request.errors = Errors()
Expand All @@ -97,6 +102,12 @@ def test_collection_post_success(self):
self.assertEqual(
event.repo.working_dir,
os.path.abspath(os.path.join(self.WORKING_DIR, api_repo_name)))
# check that the repo can be cloned with a different name
request.validated['repo_name'] = 'foo-bar'
resource.collection_post()
self.assertEqual(request.response.status_code, 301)
self.assertTrue(
os.path.exists(os.path.join(self.WORKING_DIR, 'foo-bar')))

def test_initialize_repo_index(self):
im = self.workspace.im
Expand Down Expand Up @@ -144,6 +155,7 @@ def test_collection_post_error(self, mock_method):
request = testing.DummyRequest({})
request.validated = {
'repo_url': 'git://example.org/bar.git',
'repo_name': None
}
request.errors = Errors()
resource = RepositoryResource(request)
Expand Down
12 changes: 10 additions & 2 deletions unicore/distribute/api/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from cornice.errors import Errors

from unicore.distribute.api.validators import (
validate_schema, repo_url_type_schema_validator, CreateRepoColanderSchema)
validate_schema, repo_url_type_schema_validator, CreateRepoColanderSchema,
repo_name_validator)

from pyramid import testing

Expand Down Expand Up @@ -93,8 +94,15 @@ def test_repo_url_type_schema_validator(self):
self.assertEqual(
None, repo_url_type_schema_validator(None, 'git://foo/bar.git'))

def test_repo_name_validator(self):
self.assertRaises(Invalid, repo_name_validator, None, '/foo/bar.git')
self.assertRaises(Invalid, repo_name_validator, None, '.')
self.assertRaises(Invalid, repo_name_validator, None, '..')
self.assertEqual(None, repo_name_validator(None, 'abcd1234.-_'))

def test_create_repo_colander_schema(self):
schema = CreateRepoColanderSchema()
valid = schema.deserialize({'repo_url': 'http://example.org/foo.git'})
self.assertEqual(valid, {'repo_url': 'http://example.org/foo.git'})
self.assertEqual(valid, {'repo_url': 'http://example.org/foo.git',
'repo_name': None})
self.assertRaises(Invalid, schema.deserialize, {'repo_url': 'foo'})
14 changes: 14 additions & 0 deletions unicore/distribute/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from unicore.distribute.utils import get_config, get_repository, get_schema


REPO_NAME_CHARS = set('abcdefghijklmnopqrstuvwxyz0123456789.-_')


def validate_schema(request):
config = get_config(request)
storage_path = config.get('repo.storage_path')
Expand Down Expand Up @@ -43,6 +46,17 @@ def repo_url_type_schema_validator(node, value):
raise Invalid(node, '%r is not a valid repo_url' % (value,))


def repo_name_validator(node, value):
chars = set(value)
if not chars <= REPO_NAME_CHARS:
raise Invalid(node, '%r contains invalid characters %r' % (
value, chars - REPO_NAME_CHARS))
if value in ('.', '..'):
raise Invalid(node, '%r is not a valid repo_name' % (value,))


class CreateRepoColanderSchema(MappingSchema):
repo_url = SchemaNode(
String(), location='body', validator=repo_url_type_schema_validator)
repo_name = SchemaNode(
String(), location='body', validator=repo_name_validator, missing=None)

0 comments on commit f89afac

Please sign in to comment.