Do github last

github project creation is really only a convenience, but API errors
cause other things to get borked from time to time. Move the interaction
with github to the very end, after we've done all of the things that
exist in our own backyard. Additionally, if we create the project in
github, it's possible we did it later on, so go ahead and trigger a
replication

Change-Id: I51572afe41f7ec9977ea7c17a90bd4df49b9a0f1
Closes-bug: #1242569
diff --git a/jeepyb/cmd/manage_projects.py b/jeepyb/cmd/manage_projects.py
index d13191e..b9b8946 100644
--- a/jeepyb/cmd/manage_projects.py
+++ b/jeepyb/cmd/manage_projects.py
@@ -240,6 +240,7 @@
 
 
 def create_github_project(defaults, options, project, description, homepage):
+    created = False
     default_has_issues = defaults.get('has-issues', False)
     default_has_downloads = defaults.get('has-downloads', False)
     default_has_wiki = defaults.get('has-wiki', False)
@@ -272,7 +273,7 @@
         org = orgs_dict[org_name.lower()]
     except KeyError:
         # We do not have control of this github org ignore the project.
-        return
+        return False
     try:
         repo = org.get_repo(repo_name)
     except github.GithubException:
@@ -281,6 +282,7 @@
                                has_issues=has_issues,
                                has_downloads=has_downloads,
                                has_wiki=has_wiki)
+        created = True
     if description:
         repo.edit(repo_name, description=description)
     if homepage:
@@ -294,6 +296,7 @@
         teams = org.get_teams()
         teams_dict = dict(zip([t.name.lower() for t in teams], teams))
         teams_dict['gerrit'].add_to_repos(repo)
+    return created
 
 
 # TODO(mordred): Inspect repo_dir:master for a description
@@ -595,13 +598,10 @@
                 description = (
                     find_description_override(repo_path) or description)
 
-                if 'has-github' in options or default_has_github:
-                    create_github_project(defaults, options, project,
-                                          description, homepage)
-
                 if project_created:
                     push_to_gerrit(
                         repo_path, project, push_string, remote_url, ssh_env)
+                    gerrit.replicate(project)
 
                 # If we're configured to track upstream, make sure we have
                 # upstream's refs, and then push them to the appropriate
@@ -614,6 +614,12 @@
                         acl_config, project, ACL_DIR, section,
                         remote_url, repo_path, ssh_env, gerrit, GERRIT_GITID)
 
+                if 'has-github' in options or default_has_github:
+                    created = create_github_project(
+                        defaults, options, project, description, homepage)
+                    if created:
+                        gerrit.replicate(project)
+
             except Exception:
                 log.exception(
                     "Problems creating %s, moving on." % project)
diff --git a/requirements.txt b/requirements.txt
index 994ad98..f2b536e 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,5 +1,5 @@
 argparse
-gerritlib
+gerritlib>=0.3.0
 MySQL-python
 paramiko
 PyGithub