Skip to content

Commit

Permalink
Assigns None instead of 'na' git_* properties
Browse files Browse the repository at this point in the history
If the workspace directory does not reside within a git repository,
assign 'None' to PopperConfig.git_* properties. This in turns results in
not defining GIT_* environment variables at all when executing a
workflow.

fixes #823
  • Loading branch information
ivotron committed May 7, 2020
1 parent a6dd9e9 commit 00fe66d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 4 additions & 2 deletions cli/popper/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def get_sha(repo, short=None):
first 7 characters, otherwise it returns the entire SHA1 string.
"""
if not repo:
return 'na'
return None

if short:
return repo.git.rev_parse(repo.head.object.hexsha, short=short)
Expand All @@ -178,7 +178,7 @@ def get_branch(repo):
GIT_BRANCH (Jenkins), CIRCLE_BRANCH and CI_COMMIT_REF_NAME (Gitlab)
"""
if not repo:
return 'na'
return None

if not repo.head.is_detached:
return repo.active_branch.name
Expand All @@ -195,3 +195,5 @@ def get_branch(repo):
branch = os.environ.get('CI_COMMIT_REF_NAME')
if branch:
return branch

return get_sha(repo)
6 changes: 3 additions & 3 deletions cli/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def test_config_non_defaults(self):

def test_config_without_git_repo(self):
conf = PopperConfig(workspace_dir='/tmp/foo')
self.assertEqual('na', conf.git_commit)
self.assertEqual('na', conf.git_branch)
self.assertEqual('na', conf.git_sha_short)
self.assertIsNone(conf.git_commit)
self.assertIsNone(conf.git_branch)
self.assertIsNone(conf.git_sha_short)

def test_config_with_git_repo(self):
r = self.mk_repo()
Expand Down
8 changes: 8 additions & 0 deletions cli/test/test_scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_get_remote_url(self):

def test_get_branch_in_detached_head_state(self):
repo = self.mk_repo()

repo.git.checkout('HEAD~1')

os.environ['TRAVIS_BRANCH'] = 'travis'
Expand All @@ -45,6 +46,13 @@ def test_get_branch_in_detached_head_state(self):
self.assertEqual('gitlab', scm.get_branch(repo))
os.environ.pop('CI_COMMIT_REF_NAME')

self.assertEqual(scm.get_sha(repo), scm.get_branch(repo))

# None given as arg
self.assertIsNone(scm.get_sha(None))
self.assertIsNone(scm.get_sha(None, short=8))
self.assertIsNone(scm.get_branch(None))

def test_clone(self):
tempdir = tempfile.mkdtemp()
tdir = os.path.join(tempdir, 'test_clone')
Expand Down

0 comments on commit 00fe66d

Please sign in to comment.