From 7dfb6c3ed77dfc89fff1f5e6fab64e53075f56b9 Mon Sep 17 00:00:00 2001
From: Stefano Rivera <stefanor@ubuntu.com>
Date: Tue, 22 Nov 2011 23:48:30 +0200
Subject: [PATCH] requestbackport: Check for existing backport bugs first.

---
 debian/changelog |  1 +
 requestbackport  | 30 +++++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/debian/changelog b/debian/changelog
index b4e9138..1b0552b 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -4,6 +4,7 @@ ubuntu-dev-tools (0.136ubuntu1) UNRELEASED; urgency=low
   * Remove dgetlp. No longer needed.
   * Use httplib2 everywhere that we do https. The python stdlib doesn't do
     certificate verification.
+  * requestbackport: Check for existing backport bugs first.
 
  -- Stefano Rivera <stefanor@debian.org>  Mon, 21 Nov 2011 09:47:00 +0200
 
diff --git a/requestbackport b/requestbackport
index 3189b6e..4e4348f 100755
--- a/requestbackport
+++ b/requestbackport
@@ -16,6 +16,7 @@
 
 from collections import defaultdict
 import optparse
+import re
 import sys
 
 import apt
@@ -26,7 +27,8 @@ from ubuntutools.lp.lpapicache import Launchpad, Distribution
 from ubuntutools.lp.udtexceptions import PackageNotFoundException
 from ubuntutools.config import UDTConfig
 from ubuntutools.rdepends import query_rdepends, RDependsException
-from ubuntutools.question import YesNoQuestion, EditBugReport
+from ubuntutools.question import (YesNoQuestion, EditBugReport,
+                                  confirmation_prompt)
 
 
 class DestinationException(Exception):
@@ -72,6 +74,30 @@ def determine_destinations(source, destination):
     return destinations
 
 
+def check_existing(package, destinations):
+    """Search for possible existing bug reports"""
+    # The LP bug search is indexed, not substring:
+    query = re.findall(r'[a-z]+', package)
+    bugs = []
+    for release in destinations:
+        project = Launchpad.projects[release + '-backports']
+        bugs += project.searchTasks(omit_duplicates=True,
+                                    search_text=query,
+                                    status=["Incomplete", "New", "Confirmed",
+                                            "Triaged", "In Progress",
+                                            "Fix Committed"])
+    if not bugs:
+        return
+
+    Logger.normal("There are existing bug reports that look similar to your "
+                  "request. Please check before continuing:")
+
+    for bug in sorted(set(bug_task.bug for bug_task in bugs)):
+        Logger.normal(" * LP: #%-7i: %s  %s", bug.id, bug.title, bug.web_link)
+
+    confirmation_prompt()
+
+
 def find_rdepends(releases, published_binaries):
     intermediate = defaultdict(lambda: defaultdict(list))
 
@@ -250,6 +276,8 @@ def main():
         Logger.error(str(e))
         sys.exit(1)
 
+    check_existing(package, destinations)
+
     package_spph = locate_package(package, options.source)
     request_backport(package_spph, options.source, destinations)