From 771fad6d50eaffe574e63629d9e9e5af83bc7322 Mon Sep 17 00:00:00 2001 From: mprahl Date: Mon, 29 Apr 2019 14:21:45 -0400 Subject: [PATCH] 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 --- module_build_service/scm.py | 5 +++-- tests/test_scm.py | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/module_build_service/scm.py b/module_build_service/scm.py index a241c95d..8444d25f 100644 --- a/module_build_service/scm.py +++ b/module_build_service/scm.py @@ -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 diff --git a/tests/test_scm.py b/tests/test_scm.py index a3f1bd80..38678a26 100644 --- a/tests/test_scm.py +++ b/tests/test_scm.py @@ -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"