Resolve the commit in the SCM.commit property method instead of the constructor

This will prevent the need to call `SCM.get_latest` in the constructor,
since not all SCM objects need the commit to the branch. It also fixes
the situation where a component's git repo doesn't have a "master" branch.

See https://pagure.io/fm-orchestrator/issue/1224
This commit is contained in:
mprahl
2019-04-29 14:21:45 -04:00
parent 02f00d93c1
commit 771fad6d50
2 changed files with 7 additions and 3 deletions

View File

@@ -98,8 +98,6 @@ class SCM(object):
self.name = self.name[:-4]
self.commit = match.group("commit")
self.branch = branch if branch else "master"
if not self.commit:
self.commit = self.get_latest(self.branch)
self.version = None
else:
raise ValidationError("Unhandled SCM scheme: %s" % self.scheme)
@@ -351,6 +349,9 @@ class SCM(object):
@property
def commit(self):
"""The commit ID, for example the git hash, or None."""
if not self._commit:
self._commit = self.get_latest(self.branch)
return self._commit
@commit.setter

View File

@@ -93,7 +93,10 @@ class TestSCMModule:
def test_verify_unknown_branch(self):
with pytest.raises(UnprocessableEntity):
module_build_service.scm.SCM(repo_url, "unknown")
scm = module_build_service.scm.SCM(repo_url, "unknown")
# Accessing the commit property will cause the commit to be resolved, causing an
# exception
scm.commit
def test_verify_commit_in_branch(self):
target = "7035bd33614972ac66559ac1fdd019ff6027ad21"