Blob Blame History Raw
From 0a8efdc2e54a1df534425bef6460de0eb5c4fb7f Mon Sep 17 00:00:00 2001
From: Jan Kaluza <jkaluza@redhat.com>
Date: Jun 05 2017 11:48:36 +0000
Subject: Tag the component to final tag in case it is built, but not tagged.


---

diff --git a/module_build_service/builder/KojiModuleBuilder.py b/module_build_service/builder/KojiModuleBuilder.py
index eede0ab..382d2ea 100644
--- a/module_build_service/builder/KojiModuleBuilder.py
+++ b/module_build_service/builder/KojiModuleBuilder.py
@@ -67,6 +67,7 @@ class KojiModuleBuilder(GenericBuilder):
         """
         self.owner = owner
         self.module_str = module.name
+        self.mmd = module.mmd()
         self.config = config
         self.tag_name = tag_name
         self.__prep = False
@@ -375,15 +376,15 @@ chmod 644 %buildroot/%_rpmconfigdir/macros.d/macros.modules
 
         return get_result()
 
-    def _get_task_by_artifact(self, artifact_name):
+    def _get_build_by_artifact(self, artifact_name):
         """
         :param artifact_name: e.g. bash
 
-        Searches for a tagged package inside module tag.
+        Searches for a complete build of artifact belonging to this module.
+        The returned build can be even untagged.
 
-        Returns task_id or None.
+        Returns koji_session.getBuild response or None.
 
-        TODO: handle builds with skip_tag (not tagged at all)
         """
         # yaml file can hold only one reference to a package name, so
         # I expect that we can have only one build of package within single module
@@ -404,6 +405,21 @@ chmod 644 %buildroot/%_rpmconfigdir/macros.d/macros.modules
             assert len(tagged) == 1, "Expected exactly one item in list. Got %s" % tagged
             return tagged[0]
 
+        # If the build cannot be found in tag, it may be untagged as a result
+        # of some earlier inconsistent situation. Let's find the task_info
+        # based on the list of untagged builds
+        release = module_build_service.utils.get_rpm_release_from_mmd(self.mmd)
+        opts = {'name': artifact_name}
+        untagged = self.koji_session.untaggedBuilds(**opts)
+        for build in untagged:
+            if build["release"].endswith(release):
+                build_info = self.koji_session.getBuild(build['id'])
+                if not build_info:
+                    log.error("Cannot get build info of build %r", build['id'])
+                    return None
+                self.tag_artifacts([build_info["nvr"]])
+                return build_info
+
         return None
 
     def build(self, artifact_name, source):
@@ -436,7 +452,7 @@ chmod 644 %buildroot/%_rpmconfigdir/macros.d/macros.modules
                 raise RuntimeError("Buildroot is not prep-ed")
 
             # Skip existing builds
-            task_info = self._get_task_by_artifact(artifact_name)
+            task_info = self._get_build_by_artifact(artifact_name)
             if task_info:
                 log.info("skipping build of %s. Build already exists (task_id=%s), via %s" % (
                     source, task_info['task_id'], self))
diff --git a/module_build_service/scheduler/handlers/modules.py b/module_build_service/scheduler/handlers/modules.py
index a48df6b..cc94b73 100644
--- a/module_build_service/scheduler/handlers/modules.py
+++ b/module_build_service/scheduler/handlers/modules.py
@@ -29,13 +29,13 @@ import module_build_service.pdc
 import module_build_service.utils
 import module_build_service.messaging
 from module_build_service.utils import (
-    start_next_batch_build, attempt_to_reuse_all_components)
+    start_next_batch_build, attempt_to_reuse_all_components,
+    get_rpm_release_from_mmd)
 from module_build_service.builder.KojiContentGenerator import KojiContentGenerator
 
 from requests.exceptions import ConnectionError
 
 import koji
-import hashlib
 
 import logging
 import os
@@ -43,15 +43,6 @@ import os
 logging.basicConfig(level=logging.DEBUG)
 
 
-def get_rpm_release_from_mmd(mmd):
-    """
-    Returns the dist tag based on the modulemd metadata and MBS configuration.
-    """
-
-    dist_str = '.'.join([mmd.name, mmd.stream, str(mmd.version)])
-    dist_hash = hashlib.sha1(dist_str).hexdigest()[:8]
-    return conf.default_dist_tag_prefix + dist_hash
-
 def get_artifact_from_srpm(srpm_path):
     return os.path.basename(srpm_path).replace(".src.rpm", "")
 
diff --git a/module_build_service/utils.py b/module_build_service/utils.py
index a47b080..dba95c5 100644
--- a/module_build_service/utils.py
+++ b/module_build_service/utils.py
@@ -33,6 +33,7 @@ import copy
 import kobo.rpmlib
 import inspect
 from six import iteritems
+import hashlib
 
 import modulemd
 
@@ -1131,3 +1132,12 @@ def validate_koji_tag(tag_arg_names, pre='', post='-', dict_key='name'):
         return wrapper
 
     return validation_decorator
+
+def get_rpm_release_from_mmd(mmd):
+    """
+    Returns the dist tag based on the modulemd metadata and MBS configuration.
+    """
+
+    dist_str = '.'.join([mmd.name, mmd.stream, str(mmd.version)])
+    dist_hash = hashlib.sha1(dist_str).hexdigest()[:8]
+    return conf.default_dist_tag_prefix + dist_hash