From e3c23f0bedb83a4b758f048ba9c01368883712dd Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 26 2016 17:32:51 +0000
Subject: [PATCH 1/3] reworking cli import a bit. support importing over a failed/canceled nvr


---

diff --git a/cli/koji b/cli/koji
index 4eb7697..0b389b8 100755
--- a/cli/koji
+++ b/cli/koji
@@ -1558,35 +1558,64 @@ def handle_import(options, session, args):
         sys.stdout.flush()
 
     for nvr in nvrs:
-        got_build = False
-        #srpms first, if any
+        # check for existing build
+        need_build = True
+        binfo = session.getBuild(nvr)
+        if binfo:
+            b_state = koji.BUILD_STATES[binfo['state']]
+            if b_state == 'COMPLETE':
+                need_build = False
+            elif b_state in ['FAILED', 'CANCELED']:
+                if not options.create_build:
+                    print _("Build %s state is %s. Skipping import") % (nvr, b_state)
+                    continue
+            else:
+                print _("Build %s exists with state=%s. Skipping import") % (nvr, b_state)
+                continue
+
+        # import srpms first, if any
         for path, data in to_import[nvr]:
             if data['sourcepackage']:
+                if binfo and b_state != 'COMPLETE':
+                    # need to fix the state
+                    print _("Creating empty build: %s") % nvr
+                    binfo = koji.util.dslice(binfo, ['name', 'version', 'release', 'epoch'])
+                    session.createEmptyBuild(**binfo)
+                    binfo = session.getBuild(nvr)
                 do_import(path, data)
-                got_build = True
+                need_build = False
+
+        if need_build:
+            # if we're doing this here, we weren't given the matching srpm
+            if not options.create_build:
+                if binfo:
+                    # should have caught this earlier, but just in case...
+                    b_state = koji.BUILD_STATES[binfo['state']]
+                    print _("Build %s state is %s. Skipping import") % (nvr, b_state)
+                    continue
+                else:
+                    print _("No such build: %s (include matching srpm or use "
+                            "--create-build option to add it)") % nvr
+                    continue
+            else:
+                # let's make a new build
+                b_data = koji.parse_NVR(nvr)
+                if options.src_epoch:
+                    b_data['epoch'] = options.src_epoch
+                else:
+                    # pull epoch from first rpm
+                    data = to_import[nvr][0][1]
+                    b_data['epoch'] = data['epoch']
+                if options.test:
+                    print _("Test mode -- would have created empty build: %s") % nvr
+                else:
+                    print _("Creating empty build: %s") % nvr
+                    session.createEmptyBuild(**b_data)
+                    binfo = session.getBuild(nvr)
+
         for path, data in to_import[nvr]:
             if data['sourcepackage']:
                 continue
-            if not got_build:
-                binfo = session.getBuild(nvr)
-                if binfo:
-                    got_build = True
-                elif options.create_build:
-                    binfo = koji.parse_NVR(nvr)
-                    if options.src_epoch:
-                        binfo['epoch'] = options.src_epoch
-                    else:
-                        binfo['epoch'] = data['epoch']
-                    if options.test:
-                        print _("Test mode -- would have created empty build: %s") % nvr
-                        got_build = True  #avoid duplicate notices
-                    else:
-                        print _("Creating empty build: %s") % nvr
-                        session.createEmptyBuild(**binfo)
-                else:
-                    #shouldn't happen
-                    print _("Build missing: %s") % nvr
-                    break
             do_import(path, data)
 
 

From a5fc700ab2beb202eefc81fc1057b47973f74f21 Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 26 2016 17:32:51 +0000
Subject: [PATCH 2/3] update epoch when recycling a build


---

diff --git a/hub/kojihub.py b/hub/kojihub.py
index 6622ea4..32b0c28 100644
--- a/hub/kojihub.py
+++ b/hub/kojihub.py
@@ -4474,7 +4474,7 @@ def new_build(data):
         if st_desc in ('FAILED','CANCELED'):
             #should be ok to replace
             update = UpdateProcessor('build', clauses=['id=%(id)s'], values=data)
-            update.set(**dslice(data, ['state', 'task_id', 'owner', 'start_time', 'completion_time']))
+            update.set(**dslice(data, ['state', 'task_id', 'owner', 'start_time', 'completion_time', 'epoch']))
             update.rawset(create_event='get_event()')
             update.execute()
             builddir = koji.pathinfo.build(data)

From 0f5c155784e63f4e81cc277900d77a9dbd245f86 Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 26 2016 17:32:52 +0000
Subject: [PATCH 3/3] use correct epoch when recycling nvr


---

diff --git a/cli/koji b/cli/koji
index 0b389b8..3744db4 100755
--- a/cli/koji
+++ b/cli/koji
@@ -1579,8 +1579,9 @@ def handle_import(options, session, args):
                 if binfo and b_state != 'COMPLETE':
                     # need to fix the state
                     print _("Creating empty build: %s") % nvr
-                    binfo = koji.util.dslice(binfo, ['name', 'version', 'release', 'epoch'])
-                    session.createEmptyBuild(**binfo)
+                    b_data = koji.util.dslice(binfo, ['name', 'version', 'release'])
+                    b_data['epoch'] = data['epoch']
+                    session.createEmptyBuild(**b_data)
                     binfo = session.getBuild(nvr)
                 do_import(path, data)
                 need_build = False

