def setup_project(project_path, project_name, review_url, branch): """ Setup a project for code review. * Figure out the user name * Add a remote called 'gerrit' * Add the hook :return: a boolean to tell wether it's worth trying for other projects """ git = qisrc.git.Git(project_path) # Extract server from url: # pylint: disable-msg=E1103 netloc = urlparse.urlsplit(review_url).netloc server = netloc.split(":")[0] # Get username qibuild_cfg = qibuild.config.QiBuildConfig() qibuild_cfg.read(create_if_missing=True) access = qibuild_cfg.get_server_access(server) if access: username = access.username else: username = ask_gerrit_username(server) if not username: return False # Add it to config so we ask only once qibuild_cfg.set_server_access(server, username) qibuild_cfg.write() # Set a remote named 'gerrit' remote_url = http_to_ssh(review_url, project_name, username) git.set_remote("gerrit", remote_url) # Configure review.remote in git/config so that # qisrc push knows what to do: git.set_config("review.remote", "gerrit") # Install the hook commit_hook = os.path.join(project_path, ".git", "hooks", "commit-msg") if os.path.exists(commit_hook): return True ui.info("Configuring project for code review ...", end="") (username, server, port) = parse_git_url(remote_url) fetch_gerrit_hook(project_path, username, server, port) ui.info(ui.green, "[OK]") return True
def test_push(tmpdir): foo_url = create_git_repo(tmpdir.strpath, "foo") work = tmpdir.mkdir("work") foo_src = work.mkdir("foo") foo_src = foo_src.strpath git = qisrc.git.Git(foo_src) git.clone(foo_url) # this should work: qisrc.review.push(foo_src, "master") (retcode, out) = git.call("ls-remote", "origin", raises=False) assert retcode == 0 assert "refs/for/master" not in out assert "refs/heads/master" in out gerrit_url = create_git_repo(tmpdir.strpath, "foo-gerrit") git.call("remote", "add", "gerrit", gerrit_url) git.set_config("review.remote", "gerrit") git.checkout("-b", "next") qisrc.review.push(foo_src, "next") (retcode, out) = git.call("ls-remote", "gerrit", raises=False) assert retcode == 0 assert "refs/for/next" in out assert "refs/heads/next" not in out