From 37e4c718d92d9526efaca4efdf17da472980491e Mon Sep 17 00:00:00 2001 From: mprahl Date: May 06 2019 15:13:24 +0000 Subject: Simplify format_mmd since scm.commit is always set or returns an exception --- diff --git a/module_build_service/utils/submit.py b/module_build_service/utils/submit.py index 976fa96..69b99c0 100644 --- a/module_build_service/utils/submit.py +++ b/module_build_service/utils/submit.py @@ -145,18 +145,13 @@ def format_mmd(mmd, scmurl, module=None, session=None): # If module build was submitted via yaml file, there is no scmurl if scmurl: scm = SCM(scmurl) - # If a commit hash is provided, add that information to the modulemd - if scm.commit: - # We want to make sure we have the full commit hash for consistency - if SCM.is_full_commit_hash(scm.scheme, scm.commit): - full_scm_hash = scm.commit - else: - full_scm_hash = scm.get_full_commit_hash() - - xmd["mbs"]["commit"] = full_scm_hash - # If a commit hash wasn't provided then just get the latest from master + # We want to make sure we have the full commit hash for consistency + if SCM.is_full_commit_hash(scm.scheme, scm.commit): + full_scm_hash = scm.commit else: - xmd["mbs"]["commit"] = scm.get_latest() + full_scm_hash = scm.get_full_commit_hash() + + xmd["mbs"]["commit"] = full_scm_hash if mmd.get_rpm_components() or mmd.get_module_components(): if "rpms" not in xmd["mbs"]: diff --git a/tests/test_build/test_build.py b/tests/test_build/test_build.py index 2d10b0d..6191695 100644 --- a/tests/test_build/test_build.py +++ b/tests/test_build/test_build.py @@ -28,6 +28,7 @@ from os.path import dirname from shutil import copyfile from datetime import datetime, timedelta from random import randint +import hashlib from module_build_service.utils import to_text_type import module_build_service.messaging @@ -73,6 +74,8 @@ class FakeSCM(object): self.mocked_scm.return_value.repository_root = "https://src.stg.fedoraproject.org/modules/" self.mocked_scm.return_value.sourcedir = self.sourcedir self.mocked_scm.return_value.get_module_yaml = self.get_module_yaml + self.mocked_scm.return_value.is_full_commit_hash.return_value = commit and len(commit) == 40 + self.mocked_scm.return_value.get_full_commit_hash.return_value = self.get_full_commit_hash def checkout(self, temp_dir): self.sourcedir = path.join(temp_dir, self.name) @@ -89,6 +92,12 @@ class FakeSCM(object): def get_module_yaml(self): return path.join(self.sourcedir, self.name + ".yaml") + def get_full_commit_hash(self, commit_hash=None): + if not commit_hash: + commit_hash = self.commit + sha1_hash = hashlib.sha1("random").hexdigest() + return commit_hash + sha1_hash[len(commit_hash):] + class FakeModuleBuilder(GenericBuilder): """ diff --git a/tests/test_utils/test_utils.py b/tests/test_utils/test_utils.py index f7919ff..9d37ad1 100644 --- a/tests/test_utils/test_utils.py +++ b/tests/test_utils/test_utils.py @@ -20,6 +20,7 @@ import io import tempfile +import hashlib from os import path, mkdir from shutil import copyfile, rmtree from datetime import datetime @@ -67,6 +68,8 @@ class FakeSCM(object): self.mocked_scm.return_value.repository_root = "https://src.stg.fedoraproject.org/modules/" self.mocked_scm.return_value.sourcedir = self.sourcedir self.mocked_scm.return_value.get_module_yaml = self.get_module_yaml + self.mocked_scm.return_value.is_full_commit_hash.return_value = commit and len(commit) == 40 + self.mocked_scm.return_value.get_full_commit_hash.return_value = self.get_full_commit_hash def checkout(self, temp_dir): self.sourcedir = path.join(temp_dir, self.name) @@ -83,6 +86,12 @@ class FakeSCM(object): def get_module_yaml(self): return path.join(self.sourcedir, self.name + ".yaml") + def get_full_commit_hash(self, commit_hash=None): + if not commit_hash: + commit_hash = self.commit + sha1_hash = hashlib.sha1("random").hexdigest() + return commit_hash + sha1_hash[len(commit_hash):] + class TestUtilsComponentReuse: def setup_method(self, test_method): diff --git a/tests/test_views/test_views.py b/tests/test_views/test_views.py index b483b81..f363e99 100644 --- a/tests/test_views/test_views.py +++ b/tests/test_views/test_views.py @@ -102,6 +102,8 @@ class FakeSCM(object): self.mocked_scm.return_value.branch = branch self.mocked_scm.return_value.sourcedir = self.sourcedir self.mocked_scm.return_value.get_module_yaml = self.get_module_yaml + self.mocked_scm.return_value.is_full_commit_hash.return_value = commit and len(commit) == 40 + self.mocked_scm.return_value.get_full_commit_hash.return_value = self.get_full_commit_hash def checkout(self, temp_dir): try: @@ -124,6 +126,12 @@ class FakeSCM(object): def get_module_yaml(self): return path.join(self.sourcedir, self.name + ".yaml") + def get_full_commit_hash(self, commit_hash=None): + if not commit_hash: + commit_hash = self.commit + sha1_hash = hashlib.sha1("random").hexdigest() + return commit_hash + sha1_hash[len(commit_hash):] + class TestViews: def setup_method(self, test_method):