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:
parent
cc28fb6969
commit
89cbc902bb
@ -28,18 +28,20 @@ import subprocess
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
import requests
|
||||
|
||||
if sys.version < '3':
|
||||
import ConfigParser
|
||||
import urllib
|
||||
import urlparse
|
||||
urlopen = urllib.urlopen
|
||||
urljoin = urlparse.urljoin
|
||||
urlparse = urlparse.urlparse
|
||||
do_input = raw_input
|
||||
else:
|
||||
import configparser as ConfigParser
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
urlopen = urllib.request.urlopen
|
||||
urljoin = urllib.parse.urljoin
|
||||
urlparse = urllib.parse.urlparse
|
||||
do_input = input
|
||||
|
||||
@ -223,12 +225,23 @@ def set_hooks_commit_msg(remote, target_file):
|
||||
if not os.path.isdir(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:
|
||||
remote_url = get_remote_url(remote)
|
||||
if (remote_url.startswith('http://') or
|
||||
remote_url.startswith('https://')):
|
||||
hook_url = urljoin(remote_url, '/tools/hooks/commit-msg')
|
||||
if VERBOSE:
|
||||
print("Fetching commit hook from: scp://%s:%s" % (hostname, port))
|
||||
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:
|
||||
(hostname, username, port, project_name) = \
|
||||
parse_ssh_remote_url(remote_url)
|
||||
if port is not None:
|
||||
port = "-P %s" % port
|
||||
else:
|
||||
@ -237,11 +250,11 @@ def set_hooks_commit_msg(remote, target_file):
|
||||
userhost = hostname
|
||||
else:
|
||||
userhost = "%s@%s" % (username, hostname)
|
||||
run_command_exc(
|
||||
CannotInstallHook,
|
||||
"scp", port,
|
||||
userhost + ":hooks/commit-msg",
|
||||
target_file)
|
||||
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):
|
||||
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()
|
||||
|
||||
|
||||
def get_ssh_remote_url(remote):
|
||||
def get_remote_url(remote):
|
||||
url = git_config_get_value('remote.%s' % remote, '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("/")
|
||||
if project_name.endswith(".git"):
|
||||
project_name = project_name[:-4]
|
||||
@ -335,9 +353,6 @@ def get_ssh_remote_url(remote):
|
||||
username = None
|
||||
port = parsed_url.port
|
||||
|
||||
if VERBOSE:
|
||||
print("Found origin Push URL:", push_url)
|
||||
|
||||
# Workaround bug in urlparse on OSX
|
||||
if parsed_url.scheme == "ssh" and parsed_url.path[:2] == "//":
|
||||
hostname = parsed_url.path[2:].split("/")[0]
|
||||
@ -618,9 +633,9 @@ class CannotParseOpenChangesets(ChangeSetException):
|
||||
|
||||
|
||||
def list_reviews(remote):
|
||||
|
||||
remote_url = get_remote_url(remote)
|
||||
(hostname, username, port, project_name) = \
|
||||
get_ssh_remote_url(remote)
|
||||
parse_ssh_remote_url(remote_url)
|
||||
|
||||
if port is not None:
|
||||
port = "-p %s" % port
|
||||
@ -743,9 +758,9 @@ class ResetHardFailed(CommandFailed):
|
||||
|
||||
|
||||
def fetch_review(review, masterbranch, remote):
|
||||
|
||||
remote_url = get_remote_url(remote)
|
||||
(hostname, username, port, project_name) = \
|
||||
get_ssh_remote_url(remote)
|
||||
parse_ssh_remote_url(remote_url)
|
||||
|
||||
if port is not None:
|
||||
port = "-p %s" % port
|
||||
|
@ -78,8 +78,8 @@ class GerritHelpers(object):
|
||||
sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW());
|
||||
INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \
|
||||
VALUES (0, 1);
|
||||
INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) \
|
||||
VALUES (0, 'username:test_user');
|
||||
INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID, PASSWORD) \
|
||||
VALUES (0, 'username:test_user', 'test_pass');
|
||||
INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \
|
||||
VALUES ('%s', 'Y')""" % pub_key.decode()
|
||||
|
||||
@ -105,6 +105,10 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
|
||||
|
||||
_test_counter = 0
|
||||
|
||||
@property
|
||||
def project_uri(self):
|
||||
return self.project_ssh_uri
|
||||
|
||||
def setUp(self):
|
||||
"""Configure testing environment.
|
||||
|
||||
@ -122,8 +126,12 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
|
||||
|
||||
self.test_dir = self._dir('site', 'tmp', 'test_project')
|
||||
self.ssh_dir = self._dir('site', 'tmp', 'ssh')
|
||||
self.project_uri = 'ssh://test_user@%s:%s/test/test_project.git' % (
|
||||
ssh_addr, ssh_port)
|
||||
self.project_ssh_uri = (
|
||||
'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._configure_ssh(ssh_addr, ssh_port)
|
||||
@ -227,3 +235,11 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
|
||||
pid = os.getpid()
|
||||
host = '127.%s.%s.%s' % (self._test_counter, pid >> 8, pid & 255)
|
||||
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
|
||||
|
@ -144,3 +144,11 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
|
||||
review_res = self._run_git_review('-v', '-F')
|
||||
self.assertIn('rebase', review_res)
|
||||
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")
|
||||
|
@ -1 +1,2 @@
|
||||
argparse
|
||||
requests
|
||||
|
Loading…
x
Reference in New Issue
Block a user