Skip to content

Commit

Permalink
Merge branch 'lukaszgryglicki-gerrit-ssh-key-patch' of 'https://githu…
Browse files Browse the repository at this point in the history
  • Loading branch information
sduenas committed Jul 29, 2020
2 parents bd09cc5 + 51f5ff4 commit 7a51f21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 12 additions & 3 deletions perceval/backends/core/gerrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Gerrit(Backend):
:param tag: label used to mark the data
:param archive: archive to store/retrieve items
:param blacklist_ids: exclude the reviews while fetching
:param id_filepath: path to SSH private key
"""
version = '0.13.1'

Expand All @@ -74,14 +75,15 @@ class Gerrit(Backend):

def __init__(self, hostname,
user=None, port=PORT, max_reviews=MAX_REVIEWS,
disable_host_key_check=False,
disable_host_key_check=False, id_filepath=None,
tag=None, archive=None, blacklist_ids=None):
origin = hostname

super().__init__(origin, tag=tag, archive=archive, blacklist_ids=blacklist_ids)
self.hostname = hostname
self.user = user
self.port = port
self.id_filepath = id_filepath
self.max_reviews = max(1, max_reviews)
self.blacklist_ids = blacklist_ids
self.disable_host_key_check = disable_host_key_check
Expand Down Expand Up @@ -189,7 +191,8 @@ def _init_client(self, from_archive=False):

return GerritClient(self.hostname, self.user, self.max_reviews,
self.blacklist_ids, self.disable_host_key_check,
self.port, self.archive, from_archive)
self.port, self.id_filepath, self.archive,
from_archive)

def _fetch_gerrit28(self, from_date=DEFAULT_DATETIME):
""" Specific fetch for gerrit 2.8 version.
Expand Down Expand Up @@ -296,6 +299,7 @@ class GerritClient():
:param blacklist_reviews: exclude the reviews of this list while fetching
:param disable_host_key_check: disable host key controls
:param port: SSH port
:param id_filepath: SSH private key path
:param archive: collect issues already retrieved from an archive
:param from_archive: it tells whether to write/read the archive
"""
Expand All @@ -306,7 +310,7 @@ class GerritClient():
RETRY_WAIT = 60 # number of seconds when retrying a ssh command

def __init__(self, repository, user=None, max_reviews=MAX_REVIEWS, blacklist_reviews=None,
disable_host_key_check=False, port=PORT,
disable_host_key_check=False, port=PORT, id_filepath=None,
archive=None, from_archive=False):
self.gerrit_user = user
self.max_reviews = max_reviews
Expand All @@ -316,13 +320,17 @@ def __init__(self, repository, user=None, max_reviews=MAX_REVIEWS, blacklist_rev
self.project = None
self._version = None
self.port = port
self.id_filepath = id_filepath
self.archive = archive
self.from_archive = from_archive

ssh_opts = ''
if disable_host_key_check:
ssh_opts += "-o StrictHostKeyChecking=no "

if self.id_filepath:
ssh_opts += "-i %s " % self.id_filepath

if self.port:
self.gerrit_cmd = "ssh %s -p %s %s@%s" % (ssh_opts, self.port,
self.gerrit_user, self.repository)
Expand Down Expand Up @@ -520,6 +528,7 @@ def setup_cmd_parser(cls):
group.add_argument('--ssh-port', dest='port',
default=PORT, type=int,
help="Set SSH port of the Gerrit server")
group.add_argument('--ssh-id-filepath', dest='id_filepath', help="Set SSH private key path")

# Required arguments
parser.parser.add_argument('hostname',
Expand Down
14 changes: 12 additions & 2 deletions tests/test_gerrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ def test_initialization(self):
self.assertIsNone(gerrit.client)
self.assertIsNone(gerrit.blacklist_ids)

gerrit = Gerrit(GERRIT_REPO, GERRIT_USER, port=1000, max_reviews=100)
gerrit = Gerrit(GERRIT_REPO, GERRIT_USER,
port=1000, max_reviews=100,
id_filepath='/tmp/.ssh-keys/id_rsa')
self.assertEqual(gerrit.hostname, GERRIT_REPO)
self.assertEqual(gerrit.port, 1000)
self.assertEqual(gerrit.max_reviews, 100)
self.assertEqual(gerrit.tag, GERRIT_REPO)
self.assertEqual(gerrit.user, GERRIT_USER)
self.assertIsNone(gerrit.client)
self.assertIsNone(gerrit.blacklist_ids)
self.assertEqual(gerrit.id_filepath, '/tmp/.ssh-keys/id_rsa')

gerrit = Gerrit(GERRIT_REPO, tag='test', blacklist_ids=['willy'])
self.assertEqual(gerrit.hostname, GERRIT_REPO)
Expand Down Expand Up @@ -367,12 +370,17 @@ def test_init(self):
self.assertFalse(client.from_archive)
self.assertIsNone(client.archive)

client = GerritClient(GERRIT_REPO, GERRIT_USER, port=1000, max_reviews=2, blacklist_reviews=["willy"])
client = GerritClient(
GERRIT_REPO, GERRIT_USER, port=1000, max_reviews=2,
blacklist_reviews=["willy"],
id_filepath='/tmp/.ssh-keys/id_rsa'
)
self.assertEqual(client.repository, GERRIT_REPO)
self.assertEqual(client.gerrit_user, GERRIT_USER)
self.assertEqual(client.max_reviews, 2)
self.assertEqual(client.blacklist_reviews, ["willy"])
self.assertEqual(client.port, 1000)
self.assertEqual(client.id_filepath, '/tmp/.ssh-keys/id_rsa')
self.assertFalse(client.from_archive)
self.assertIsNone(client.archive)

Expand Down Expand Up @@ -519,6 +527,7 @@ def test_setup_cmd_parser(self):
'--blacklist-ids', 'willy', 'wolly', 'wally',
'--disable-host-key-check',
'--ssh-port', '1000',
'--ssh-id-filepath', '/my/keys/id_rsa',
'--tag', 'test', '--no-archive']

parsed_args = parser.parse(*args)
Expand All @@ -529,6 +538,7 @@ def test_setup_cmd_parser(self):
self.assertEqual(parsed_args.from_date, DEFAULT_DATETIME)
self.assertEqual(parsed_args.no_archive, True)
self.assertEqual(parsed_args.port, 1000)
self.assertEqual(parsed_args.id_filepath, '/my/keys/id_rsa')
self.assertListEqual(parsed_args.blacklist_ids, ['willy', 'wolly', 'wally'])


Expand Down

0 comments on commit 7a51f21

Please sign in to comment.