Makes Impact bug creation generic, ceases doc spam

This patch moves the bug creation code to its own method, so that it
can be used by other kinds of impacts.

It also changes the notification logic for DocImpact, so that emails
are now only sent if bug creation was not successful.

This patch should have no effect on other kinds of impacts.

patchset3 fixes use of sys and updated docstring

patchset4 rebases to master

Change-Id: I6b64beb6e8da45b166d17c9fad0347aa5370e689
Reviewed-on: https://review.openstack.org/34842
Approved: James E. Blair <corvus@inaugust.com>
Reviewed-by: James E. Blair <corvus@inaugust.com>
Tested-by: Jenkins
diff --git a/jeepyb/cmd/notify_impact.py b/jeepyb/cmd/notify_impact.py
index b089cbb..55e44c8 100644
--- a/jeepyb/cmd/notify_impact.py
+++ b/jeepyb/cmd/notify_impact.py
@@ -36,16 +36,7 @@
 Log:
 %s
 """
-DOC_EMAIL_TEMPLATE = """
-Hi, I'd like you to take a look at this patch for potential
-%s.
-%s
 
-Log:
-%s
-Automatically generated bug (please review):
-%s
-"""
 GERRIT_CACHE_DIR = os.path.expanduser(
     os.environ.get('GERRIT_CACHE_DIR',
                    '~/.launchpadlib/cache'))
@@ -54,41 +45,52 @@
                    '~/.launchpadlib/creds'))
 
 
+def create_bug(git_log, args, lp_project):
+    """Create a bug for a change.
+
+    Create a launchpad bug in lp_project, titled with the first line of
+    the git commit message, with the content of the git_log prepended
+    with the Gerrit review URL. Tag the bug with the name of the repository
+    it came from. Don't create a duplicate bug. Returns link to the bug.
+    """
+    lpconn = launchpad.Launchpad.login_with(
+        'Gerrit User Sync',
+        uris.LPNET_SERVICE_ROOT,
+        GERRIT_CACHE_DIR,
+        credentials_file=GERRIT_CREDENTIALS,
+        version='devel')
+
+    lines_in_log = git_log.split("\n")
+    bug_title = lines_in_log[4]
+    bug_descr = args.change_url + '\n' + git_log
+    project = lpconn.projects[lp_project]
+    # check for existing bugs by searching for the title, to avoid
+    # creating multiple bugs per review
+    potential_dupes = project.searchTasks(search_text=bug_title)
+    if len(potential_dupes) == 0:
+        buginfo = lpconn.bugs.createBug(
+            target=project, title=bug_title,
+            description=bug_descr, tags=args.project.split('/')[1])
+        buglink = buginfo.web_link
+
+    return buglink
+
+
 def process_impact(git_log, args):
     """Process DocImpact flag.
 
     If the 'DocImpact' flag is present, create a new documentation bug in
     the openstack-manuals launchpad project based on the git_log, then
-    (and for non-documentation impacts) notify the mailing list of impact
+    (and for non-documentation impacts) notify the mailing list of impact,
+    unless a bug was created.
     """
     if args.impact.lower() == 'docimpact':
-        lpconn = launchpad.Launchpad.login_with(
-            'Gerrit User Sync',
-            uris.LPNET_SERVICE_ROOT,
-            GERRIT_CACHE_DIR,
-            credentials_file=GERRIT_CREDENTIALS,
-            version='devel')
-        lines_in_log = git_log.split("\n")
-        bug_title = lines_in_log[4]
-        bug_descr = args.change_url + '\n' + git_log
-        project_name = 'openstack-manuals'
-        project = lpconn.projects[project_name]
+        buglink = create_bug(git_log, args, 'openstack-manuals')
+        if buglink is not None:
+            return
 
-        # check for existing bugs by searching for the title, to avoid
-        # creating multiple bugs per review
-        potential_dupes = project.searchTasks(search_text=bug_title)
-
-        if len(potential_dupes) == 0:
-            buginfo = lpconn.bugs.createBug(
-                target=project, title=bug_title,
-                description=bug_descr, tags=args.project.split('/')[1])
-            buglink = buginfo.web_link
-            email_content = DOC_EMAIL_TEMPLATE % (args.impact,
-                                                  args.change_url,
-                                                  git_log, buglink)
-    else:
-        email_content = EMAIL_TEMPLATE % (args.impact,
-                                          args.change_url, git_log)
+    email_content = EMAIL_TEMPLATE % (args.impact,
+                                      args.change_url, git_log)
 
     msg = text.MIMEText(email_content)
     msg['Subject'] = '[%s] %s review request change %s' % \