Skip to content

Commit

Permalink
Add support for ip git repo urls (#827)
Browse files Browse the repository at this point in the history
* Add support for ip v4 git repo urls
* Add tests for git urls
  • Loading branch information
zhiltsov-max authored and nmanovic committed Nov 8, 2019
1 parent befe5ef commit 690ecf9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ before_script:
- docker-compose -f docker-compose.yml -f docker-compose.ci.yml up --build -d

script:
- docker exec -it cvat /bin/bash -c 'python3 manage.py test cvat/apps/engine utils/cli'
- docker exec -it cvat /bin/bash -c 'python3 manage.py test cvat/apps utils/cli'
- docker exec -it cvat /bin/bash -c 'cd cvat-core && npm install && npm run test && npm run coveralls'
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"test",
"--settings",
"cvat.settings.testing",
"cvat/apps/engine",
"cvat/apps",
"utils/cli"
],
"django": true,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ https://github.com/opencv/cvat/issues/750).

### Fixed
- [Mask problem on coco json style](https://github.com/opencv/cvat/issues/718)
- [Exception in Git plugin](https://github.com/opencv/cvat/issues/826)

### Security
-
Expand Down
17 changes: 11 additions & 6 deletions cvat/apps/git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ def __init__(self, db_git, tid, user):
# HTTP/HTTPS: [http://]github.com/proj/repos[.git]
def _parse_url(self):
try:
http_pattern = "([https|http]+)*[://]*([a-zA-Z0-9._-]+.[a-zA-Z]+)/([a-zA-Z0-9._-]+)/([a-zA-Z0-9._-]+)"
ssh_pattern = "([a-zA-Z0-9._-]+)@([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)/([a-zA-Z0-9._-]+)"
# Almost STD66 (RFC3986), but schema can include a leading digit
# Reference on URL formats accepted by Git:
# https://github.com/git/git/blob/77bd3ea9f54f1584147b594abc04c26ca516d987/url.c

host_pattern = r"((?:(?:(?:\d{1,3}\.){3}\d{1,3})|(?:[a-zA-Z0-9._-]+.[a-zA-Z]+))(?::\d+)?)"
http_pattern = r"(?:http[s]?://)?" + host_pattern + r"((?:/[a-zA-Z0-9._-]+){2})"
ssh_pattern = r"([a-zA-Z0-9._-]+)@" + host_pattern + r":([a-zA-Z0-9._-]+)/([a-zA-Z0-9._-]+)"

http_match = re.match(http_pattern, self._url)
ssh_match = re.match(ssh_pattern, self._url)
Expand All @@ -87,21 +92,21 @@ def _parse_url(self):
repos = None

if http_match:
host = http_match.group(2)
repos = "{}/{}".format(http_match.group(3), http_match.group(4))
host = http_match.group(1)
repos = http_match.group(2)[1:]
elif ssh_match:
user = ssh_match.group(1)
host = ssh_match.group(2)
repos = "{}/{}".format(ssh_match.group(3), ssh_match.group(4))
else:
raise Exception("Got URL doesn't sutisfy for regular expression")
raise Exception("Git repository URL does not satisfy pattern")

if not repos.endswith(".git"):
repos += ".git"

return user, host, repos
except Exception as ex:
slogger.glob.exception('URL parsing errors occured', exc_info = True)
slogger.glob.exception('URL parsing errors occurred', exc_info = True)
raise ex


Expand Down
52 changes: 52 additions & 0 deletions cvat/apps/git/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,58 @@
#
# SPDX-License-Identifier: MIT

from itertools import product

from django.test import TestCase

# Create your tests here.

from cvat.apps.git.git import Git


class GitUrlTest(TestCase):
class FakeGit:
def __init__(self, url):
self._url = url

def _check_correct_urls(self, samples):
for i, (expected, url) in enumerate(samples):
git = GitUrlTest.FakeGit(url)
try:
actual = Git._parse_url(git)
self.assertEqual(expected, actual, "URL #%s: '%s'" % (i, url))
except Exception:
self.assertFalse(True, "URL #%s: '%s'" % (i, url))

def test_correct_urls_can_be_parsed(self):
hosts = ['host.zone', '1.2.3.4']
ports = ['', ':42']
repo_groups = ['repo', 'r4p0']
repo_repos = ['nkjl23', 'hewj']
git_suffixes = ['', '.git']

samples = []

# http samples
protocols = ['', 'http://', 'https://']
for protocol, host, port, repo_group, repo, git in product(
protocols, hosts, ports, repo_groups, repo_repos, git_suffixes):
url = '{protocol}{host}{port}/{repo_group}/{repo}{git}'.format(
protocol=protocol, host=host, port=port,
repo_group=repo_group, repo=repo, git=git
)
expected = ('git', host + port, '%s/%s.git' % (repo_group, repo))
samples.append((expected, url))

# git samples
users = ['user', 'u123_.']
for user, host, port, repo_group, repo, git in product(
users, hosts, ports, repo_groups, repo_repos, git_suffixes):
url = '{user}@{host}{port}:{repo_group}/{repo}{git}'.format(
user=user, host=host, port=port,
repo_group=repo_group, repo=repo, git=git
)
expected = (user, host + port, '%s/%s.git' % (repo_group, repo))
samples.append((expected, url))

self._check_correct_urls(samples)

0 comments on commit 690ecf9

Please sign in to comment.