From 6b2712fc7d75a84cdad4cd9f6e76250c15f6e74e Mon Sep 17 00:00:00 2001 From: Rick Bartra Date: Tue, 16 Oct 2018 15:43:38 -0400 Subject: [PATCH] Improve Pegleg repository name parsing If a repository remote ends with `.git` Pegleg is successfully able to return the repository name. The same is not true in Pegleg if the repository name does not end in `.git`. This commit allows Pegleg to support repository names that do and do not end in `.git`. Change-Id: I45fd15790677e003af7be584221903551022b7d7 --- pegleg/engine/util/git.py | 9 ++++++++- tests/unit/engine/util/test_git.py | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pegleg/engine/util/git.py b/pegleg/engine/util/git.py index 7dbbf8ed..ef5ad87c 100644 --- a/pegleg/engine/util/git.py +++ b/pegleg/engine/util/git.py @@ -376,7 +376,14 @@ def repo_name(repo_url_or_path): if config_reader.has_section(section): repo_url = config_reader.get_value(section, option) try: - return repo_url.split('/')[-1].split('.git')[0] + # Support repos that end with or without '.git' + if repo_url.endswith('.git'): + return repo_url.split('/')[-1].split('.git')[0] + else: + if repo_url.endswith('/'): + return repo_url.split('/')[-2] + else: + return repo_url.split('/')[-1] except Exception: raise exceptions.GitConfigException(repo_url=repo_url_or_path) diff --git a/tests/unit/engine/util/test_git.py b/tests/unit/engine/util/test_git.py index a4da2f9a..9086b95b 100644 --- a/tests/unit/engine/util/test_git.py +++ b/tests/unit/engine/util/test_git.py @@ -540,7 +540,18 @@ def test_is_repository_negative(): @pytest.mark.skipif( not is_connected(), reason='git clone requires network connectivity.') -def test_repo_name(): +def test_repo_name_ending_in_git(): + url = "http://github.com/openstack/airship-pegleg.git" + git_dir = git.git_handler(url, ref="master") + _validate_git_clone(git_dir) + + name = git.repo_name(git_dir) + expected = "airship-pegleg" + assert name == expected + +@pytest.mark.skipif( + not is_connected(), reason='git clone requires network connectivity.') +def test_repo_name_not_ending_in_git_and_no_fwd_slash_at_end(): url = "http://github.com/openstack/airship-pegleg" git_dir = git.git_handler(url, ref="master") _validate_git_clone(git_dir) @@ -549,6 +560,17 @@ def test_repo_name(): expected = "airship-pegleg" assert name == expected +@pytest.mark.skipif( + not is_connected(), reason='git clone requires network connectivity.') +def test_repo_name_not_ending_in_git_with_fwd_slash_at_end(): + url = "http://github.com/openstack/airship-pegleg/" + git_dir = git.git_handler(url, ref="master") + _validate_git_clone(git_dir) + + name = git.repo_name(git_dir) + expected = "airship-pegleg" + assert name == expected + @pytest.mark.skipif( not is_connected(), reason='git clone requires network connectivity.')