Merge "New headers argument for github.Issue.Issue"
diff --git a/jeepyb/cmd/fetch_remotes.py b/jeepyb/cmd/fetch_remotes.py
deleted file mode 100644
index 7829097..0000000
--- a/jeepyb/cmd/fetch_remotes.py
+++ /dev/null
@@ -1,82 +0,0 @@
-#! /usr/bin/env python
-# Copyright (C) 2011 OpenStack, LLC.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-# Fetch remotes reads a project config file called projects.yaml
-# It should look like:
-
-# - homepage: http://openstack.org
-#   team-id: 153703
-#   has-wiki: False
-#   has-issues: False
-#   has-downloads: False
-# ---
-# - project: PROJECT_NAME
-#   options:
-#   - remote: https://gerrit.googlesource.com/gerrit
-
-
-import logging
-import os
-import shlex
-import subprocess
-import yaml
-
-
-def run_command(cmd, status=False, env={}):
-    cmd_list = shlex.split(str(cmd))
-    newenv = os.environ
-    newenv.update(env)
-    p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
-                         stderr=subprocess.STDOUT, env=newenv)
-    (out, nothing) = p.communicate()
-    if status:
-        return (p.returncode, out.strip())
-    return out.strip()
-
-
-def run_command_status(cmd, env={}):
-    return run_command(cmd, True, env)
-
-
-def main():
-    logging.basicConfig(level=logging.ERROR)
-
-    REPO_ROOT = os.environ.get('REPO_ROOT',
-                               '/home/gerrit2/review_site/git')
-    PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
-                                   '/home/gerrit2/projects.yaml')
-
-    (defaults, config) = [config for config in
-                          yaml.load_all(open(PROJECTS_YAML))]
-
-    for section in config:
-        project = section['project']
-
-        if 'remote' not in section:
-            continue
-
-        project_git = "%s.git" % project
-        os.chdir(os.path.join(REPO_ROOT, project_git))
-
-        # Make sure that the specified remote exists
-        remote_url = section['remote']
-        # We could check if it exists first, but we're ignoring output anyway
-        # So just try to make it, and it'll either make a new one or do nothing
-        run_command("git remote add -f upstream %s" % remote_url)
-        # Fetch new revs from it
-        run_command("git remote update upstream")
-
-if __name__ == "__main__":
-    main()
diff --git a/jeepyb/cmd/manage_projects.py b/jeepyb/cmd/manage_projects.py
index da9ec15..853dde2 100644
--- a/jeepyb/cmd/manage_projects.py
+++ b/jeepyb/cmd/manage_projects.py
@@ -356,6 +356,18 @@
             project_git = "%s.git" % project
             remote_url = "ssh://localhost:%s/%s" % (GERRIT_PORT, project)
 
+            # Create the project in Gerrit first, since it will fail
+            # spectacularly if its project directory or local replica
+            # already exist on disk
+            project_created = False
+            if project not in project_list:
+                try:
+                    gerrit.createProject(project)
+                    project_created = True
+                except Exception:
+                    log.exception(
+                        "Exception creating %s in Gerrit." % project)
+
             # Create the repo for the local git mirror
             git_mirror_path = os.path.join(LOCAL_GIT_DIR, project_git)
             if not os.path.exists(git_mirror_path):
@@ -371,7 +383,7 @@
                             remote_url=remote_url)
 
             # If we don't have a local copy already, get one
-            if not os.path.exists(repo_path):
+            if not os.path.exists(repo_path) or project_created:
                 if not os.path.exists(os.path.dirname(repo_path)):
                     os.makedirs(os.path.dirname(repo_path))
                 # Three choices
@@ -416,7 +428,7 @@
                     run_command("git init %s" % repo_path)
                     git_command(
                         repo_path,
-                        "remote add origin %(remote_url)" % git_opts)
+                        "remote add origin %(remote_url)s" % git_opts)
                     with open(os.path.join(repo_path,
                                            ".gitreview"),
                               'w') as gitreview:
@@ -469,10 +481,8 @@
                 create_github_project(defaults, options, project,
                                       description, homepage)
 
-            if project not in project_list:
+            if project_created:
                 try:
-                    gerrit.createProject(project)
-
                     git_command(repo_path,
                                 push_string % remote_url,
                                 env=ssh_env)
@@ -480,7 +490,7 @@
                                 "push --tags %s" % remote_url, env=ssh_env)
                 except Exception:
                     log.exception(
-                        "Exception creating %s in Gerrit." % project)
+                        "Error pushing %s to Gerrit." % project)
 
             # If we're configured to track upstream, make sure we have
             # upstream's refs, and then push them to the appropriate
diff --git a/setup.cfg b/setup.cfg
index 7f0114a..a257d7e 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -21,7 +21,6 @@
     close-pull-requests = jeepyb.cmd.close_pull_requests:main
     create-cgitrepos = jeepyb.cmd.create_cgitrepos:main
     expire-old-reviews = jeepyb.cmd.expire_old_reviews:main
-    fetch-remotes = jeepyb.cmd.fetch_remotes:main
     manage-projects = jeepyb.cmd.manage_projects:main
     notify-impact = jeepyb.cmd.notify_impact:main
     openstackwatch = jeepyb.cmd.openstackwatch:main