Add http(s) protocol support to set_hooks_commit_msg

set_hooks_commit_msg used scp to download commit_msg hook even for
gerrit http(s) remote url. It will use scp for ssh remote url and
http(s) download for http(s) remote url (more proxy "friendly").

Note: this change modifies .gerrit/golden_site folder content (used
by tests), so delete .gerrit folder and run
 "python -m git_review.tests.prepare".

Closes-bug: #1274876
Change-Id: I49bcf4fd84a88878f9381ddf1a8714b778688a0a
This commit is contained in:
Cedric Brandily 2014-02-02 16:39:21 +01:00
parent cc28fb6969
commit 89cbc902bb
4 changed files with 72 additions and 32 deletions

View File

@ -28,18 +28,20 @@ import subprocess
import sys import sys
import textwrap import textwrap
import requests
if sys.version < '3': if sys.version < '3':
import ConfigParser import ConfigParser
import urllib import urllib
import urlparse import urlparse
urlopen = urllib.urlopen urljoin = urlparse.urljoin
urlparse = urlparse.urlparse urlparse = urlparse.urlparse
do_input = raw_input do_input = raw_input
else: else:
import configparser as ConfigParser import configparser as ConfigParser
import urllib.parse import urllib.parse
import urllib.request import urllib.request
urlopen = urllib.request.urlopen urljoin = urllib.parse.urljoin
urlparse = urllib.parse.urlparse urlparse = urllib.parse.urlparse
do_input = input do_input = input
@ -223,25 +225,36 @@ def set_hooks_commit_msg(remote, target_file):
if not os.path.isdir(hooks_dir): if not os.path.isdir(hooks_dir):
os.mkdir(hooks_dir) os.mkdir(hooks_dir)
(hostname, username, port, project_name) = \
get_ssh_remote_url(remote)
if not os.path.exists(target_file) or UPDATE: if not os.path.exists(target_file) or UPDATE:
if VERBOSE: remote_url = get_remote_url(remote)
print("Fetching commit hook from: scp://%s:%s" % (hostname, port)) if (remote_url.startswith('http://') or
if port is not None: remote_url.startswith('https://')):
port = "-P %s" % port hook_url = urljoin(remote_url, '/tools/hooks/commit-msg')
if VERBOSE:
print("Fetching commit hook from: %s" % hook_url)
res = requests.get(hook_url, stream=True)
if res.status_code == 200:
with open(target_file, 'wb') as f:
for x in res.iter_content(1024):
f.write(x)
else:
raise CannotInstallHook
else: else:
port = "" (hostname, username, port, project_name) = \
if username is None: parse_ssh_remote_url(remote_url)
userhost = hostname if port is not None:
else: port = "-P %s" % port
userhost = "%s@%s" % (username, hostname) else:
run_command_exc( port = ""
CannotInstallHook, if username is None:
"scp", port, userhost = hostname
userhost + ":hooks/commit-msg", else:
target_file) userhost = "%s@%s" % (username, hostname)
cmd = "scp", port, userhost + ":hooks/commit-msg", target_file
if VERBOSE:
hook_url = 'scp://%s:%s/hooks/commit-msg' % (userhost, port)
print("Fetching commit hook from: %s" % hook_url)
run_command_exc(CannotInstallHook, *cmd)
if not os.access(target_file, os.X_OK): if not os.access(target_file, os.X_OK):
os.chmod(target_file, os.path.stat.S_IREAD | os.path.stat.S_IEXEC) os.chmod(target_file, os.path.stat.S_IREAD | os.path.stat.S_IEXEC)
@ -322,11 +335,16 @@ def add_remote(hostname, port, project, remote):
print() print()
def get_ssh_remote_url(remote): def get_remote_url(remote):
url = git_config_get_value('remote.%s' % remote, 'url', '') url = git_config_get_value('remote.%s' % remote, 'url', '')
push_url = git_config_get_value('remote.%s' % remote, 'pushurl', url) push_url = git_config_get_value('remote.%s' % remote, 'pushurl', url)
if VERBOSE:
print("Found origin Push URL:", push_url)
return push_url
parsed_url = urlparse(push_url)
def parse_ssh_remote_url(remote_url):
parsed_url = urlparse(remote_url)
project_name = parsed_url.path.lstrip("/") project_name = parsed_url.path.lstrip("/")
if project_name.endswith(".git"): if project_name.endswith(".git"):
project_name = project_name[:-4] project_name = project_name[:-4]
@ -335,9 +353,6 @@ def get_ssh_remote_url(remote):
username = None username = None
port = parsed_url.port port = parsed_url.port
if VERBOSE:
print("Found origin Push URL:", push_url)
# Workaround bug in urlparse on OSX # Workaround bug in urlparse on OSX
if parsed_url.scheme == "ssh" and parsed_url.path[:2] == "//": if parsed_url.scheme == "ssh" and parsed_url.path[:2] == "//":
hostname = parsed_url.path[2:].split("/")[0] hostname = parsed_url.path[2:].split("/")[0]
@ -618,9 +633,9 @@ class CannotParseOpenChangesets(ChangeSetException):
def list_reviews(remote): def list_reviews(remote):
remote_url = get_remote_url(remote)
(hostname, username, port, project_name) = \ (hostname, username, port, project_name) = \
get_ssh_remote_url(remote) parse_ssh_remote_url(remote_url)
if port is not None: if port is not None:
port = "-p %s" % port port = "-p %s" % port
@ -743,9 +758,9 @@ class ResetHardFailed(CommandFailed):
def fetch_review(review, masterbranch, remote): def fetch_review(review, masterbranch, remote):
remote_url = get_remote_url(remote)
(hostname, username, port, project_name) = \ (hostname, username, port, project_name) = \
get_ssh_remote_url(remote) parse_ssh_remote_url(remote_url)
if port is not None: if port is not None:
port = "-p %s" % port port = "-p %s" % port

View File

@ -78,8 +78,8 @@ class GerritHelpers(object):
sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW()); sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW());
INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \ INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \
VALUES (0, 1); VALUES (0, 1);
INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) \ INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID, PASSWORD) \
VALUES (0, 'username:test_user'); VALUES (0, 'username:test_user', 'test_pass');
INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \ INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \
VALUES ('%s', 'Y')""" % pub_key.decode() VALUES ('%s', 'Y')""" % pub_key.decode()
@ -105,6 +105,10 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
_test_counter = 0 _test_counter = 0
@property
def project_uri(self):
return self.project_ssh_uri
def setUp(self): def setUp(self):
"""Configure testing environment. """Configure testing environment.
@ -122,8 +126,12 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
self.test_dir = self._dir('site', 'tmp', 'test_project') self.test_dir = self._dir('site', 'tmp', 'test_project')
self.ssh_dir = self._dir('site', 'tmp', 'ssh') self.ssh_dir = self._dir('site', 'tmp', 'ssh')
self.project_uri = 'ssh://test_user@%s:%s/test/test_project.git' % ( self.project_ssh_uri = (
ssh_addr, ssh_port) 'ssh://test_user@%s:%s/test/test_project.git' % (
ssh_addr, ssh_port))
self.project_http_uri = (
'http://test_user:test_pass@%s:%s/test/test_project.git' % (
http_addr, http_port))
self._run_gerrit(ssh_addr, ssh_port, http_addr, http_port) self._run_gerrit(ssh_addr, ssh_port, http_addr, http_port)
self._configure_ssh(ssh_addr, ssh_port) self._configure_ssh(ssh_addr, ssh_port)
@ -227,3 +235,11 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
pid = os.getpid() pid = os.getpid()
host = '127.%s.%s.%s' % (self._test_counter, pid >> 8, pid & 255) host = '127.%s.%s.%s' % (self._test_counter, pid >> 8, pid & 255)
return host, 29418, host, 8080, self._dir('gerrit', 'site-' + host) return host, 29418, host, 8080, self._dir('gerrit', 'site-' + host)
class HttpMixin(object):
"""HTTP remote_url mixin."""
@property
def project_uri(self):
return self.project_http_uri

View File

@ -144,3 +144,11 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
review_res = self._run_git_review('-v', '-F') review_res = self._run_git_review('-v', '-F')
self.assertIn('rebase', review_res) self.assertIn('rebase', review_res)
self.assertEqual(self._run_git('rev-parse', 'HEAD^1'), head) self.assertEqual(self._run_git('rev-parse', 'HEAD^1'), head)
class HttpGitReviewTestCase(tests.HttpMixin, GitReviewTestCase):
"""Class for the git-review tests over HTTP(S)."""
def test_git_review_d(self):
self.skipTest("git review -d uses fetch_review "
"which does not support http remote")

View File

@ -1 +1,2 @@
argparse argparse
requests