def _retrieve_project_list_from_gerrit(project_source):
    LOG.info('Retrieving project list from Gerrit')
    try:
        uri = project_source.get('uri') or CONF.review_uri
        gerrit_inst = rcs.Gerrit(uri)
        key_filename = (project_source.get('ssh_key_filename') or
                        CONF.ssh_key_filename)
        username = project_source.get('ssh_username') or CONF.ssh_username
        gerrit_inst.setup(key_filename=key_filename, username=username)

        project_list = gerrit_inst.get_project_list()
        gerrit_inst.close()
    except rcs.RcsException:
        LOG.error('Failed to retrieve list of projects')
        raise

    organization = project_source['organization']
    LOG.debug('Get list of projects for organization %s', organization)
    git_repos = [f for f in project_list if f.startswith(organization + "/")]

    git_base_uri = project_source.get('git_base_uri') or CONF.git_base_uri

    for repo in git_repos:
        (org, name) = repo.split('/')
        repo_uri = '%(git_base_uri)s/%(repo)s.git' % dict(
            git_base_uri=git_base_uri, repo=repo)
        yield {
            'branches': ['master'],
            'module': name,
            'organization': org,
            'uri': repo_uri,
            'releases': [],
            'has_gerrit': True,
        }
Example #2
0
    def test_log_error_fatal(self, mock_time, mock_client_cons):
        mock_client = mock.Mock()
        mock_client_cons.return_value = mock_client

        mock_exec = mock.Mock()
        mock_client.exec_command = mock_exec
        mock_exec.side_effect = [Exception] * rcs.SSH_ERRORS_LIMIT

        gerrit = rcs.Gerrit('uri')

        repo = dict(organization='openstack', module='nova')
        branch = 'master'
        last_retrieval_time = 1444000000
        mock_time.return_value = 1444333333

        try:
            list(gerrit.log(repo, branch, last_retrieval_time))
            self.fail('Gerrit.log should raise RcsException, but it did not')
        except rcs.RcsException:
            pass

        mock_client.exec_command.assert_has_calls([
            mock.call('gerrit query --all-approvals --patch-sets '
                      '--format JSON project:\'openstack/nova\' branch:master '
                      'limit:100 age:0s')] * rcs.SSH_ERRORS_LIMIT)
Example #3
0
    def test_log(self, mock_time, mock_client_cons):
        mock_client = mock.Mock()
        mock_client_cons.return_value = mock_client

        mock_exec = mock.Mock()
        mock_client.exec_command = mock_exec
        mock_exec.side_effect = [
            ('', [REVIEW_ONE, REVIEW_END_LINE], ''),  # one review and summary
            ('', [REVIEW_END_LINE], ''),  # only summary = no more reviews
        ]

        gerrit = rcs.Gerrit('uri')

        repo = dict(organization='openstack', module='nova')
        branch = 'master'
        last_retrieval_time = 1444000000
        mock_time.return_value = 1444333333
        records = list(gerrit.log(repo, branch, last_retrieval_time))

        self.assertEqual(1, len(records))
        self.assertEqual('229382', records[0]['number'])

        mock_client.exec_command.assert_has_calls([
            mock.call('gerrit query --all-approvals --patch-sets '
                      '--format JSON project:\'openstack/nova\' branch:master '
                      'limit:100 age:0s'),
            mock.call('gerrit query --all-approvals --patch-sets '
                      '--format JSON project:\'openstack/nova\' branch:master '
                      'limit:100 age:111111s'),
        ])
Example #4
0
    def test_log_old_reviews(self, mock_client_cons):
        mock_client = mock.Mock()
        mock_client_cons.return_value = mock_client

        mock_exec = mock.Mock()
        mock_client.exec_command = mock_exec
        mock_exec.side_effect = [
            ('', [REVIEW_ONE, REVIEW_END_LINE], ''),  # one review and summary
            ('', [REVIEW_END_LINE], ''),  # only summary = no more reviews
        ]

        gerrit = rcs.Gerrit('uri')

        repo = dict(organization='openstack', module='nova')
        branch = 'master'
        last_retrieval_time = 1445000000
        records = list(gerrit.log(repo, branch, last_retrieval_time,
                                  status='merged', grab_comments=True))

        self.assertEqual(0, len(records))

        mock_client.exec_command.assert_has_calls([
            mock.call('gerrit query --all-approvals --patch-sets '
                      '--format JSON project:\'openstack/nova\' branch:master '
                      'limit:100 age:0s status:merged --comments'),
        ])
Example #5
0
def _retrieve_project_list_from_gerrit(project_source):
    LOG.info('Retrieving project list from Gerrit')
    try:
        uri = project_source.get('uri') or cfg.CONF.review_uri
        gerrit = rcs.Gerrit(None, uri)
        key_filename = (project_source.get('ssh_key_filename')
                        or cfg.CONF.ssh_key_filename)
        username = project_source.get('ssh_username') or cfg.CONF.ssh_username
        gerrit.setup(key_filename=key_filename, username=username)

        project_list = gerrit.get_project_list()
    except Exception as e:
        LOG.exception(e)
        LOG.warn('Fail to retrieve list of projects. Keep it unmodified')
        return

    organization = project_source['organization']
    LOG.debug('Get list of projects for organization %s', organization)
    git_repos = [f for f in project_list if f.startswith(organization + "/")]

    git_base_uri = project_source.get('git_base_uri') or cfg.CONF.git_base_uri

    for repo in git_repos:
        (org, name) = repo.split('/')
        repo_uri = '%(git_base_uri)s/%(repo)s.git' % dict(
            git_base_uri=git_base_uri, repo=repo)
        yield {
            'branches': ['master'],
            'module': name,
            'organization': org,
            'uri': repo_uri,
            'releases': []
        }
Example #6
0
    def test_setup(self, mock_client_cons):
        mock_client = mock.Mock()
        mock_client_cons.return_value = mock_client

        mock_connect = mock.Mock()
        mock_client.connect = mock_connect

        gerrit = rcs.Gerrit('gerrit://review.openstack.org')
        gerrit.setup(username='******', key_filename='key')

        mock_connect.assert_called_once_with(
            'review.openstack.org', port=rcs.DEFAULT_PORT, key_filename='key',
            username='******')
Example #7
0
    def test_setup_error(self, mock_client_cons):
        mock_client = mock.Mock()
        mock_client_cons.return_value = mock_client

        mock_connect = mock.Mock()
        mock_client.connect = mock_connect
        mock_connect.side_effect = Exception

        gerrit = rcs.Gerrit('gerrit://review.openstack.org')
        self.assertRaises(rcs.RcsException, gerrit.setup,
                          username='******', key_filename='key')

        mock_connect.assert_called_once_with(
            'review.openstack.org', port=rcs.DEFAULT_PORT, key_filename='key',
            username='******')
def _retrieve_project_list_from_gerrit(project_source):
    organization = project_source['organization']
    LOG.info('Retrieving project list from Gerrit for %s', organization)
    pattern = project_source.get('pattern')
    if pattern is None:
        pattern = "^%s/.*" % organization
    try:
        uri = project_source.get('uri') or CONF.review_uri
        gerrit_inst = rcs.Gerrit(uri)
        key_filename = (project_source.get('ssh_key_filename') or
                        CONF.ssh_key_filename)
        username = project_source.get('ssh_username') or CONF.ssh_username
        gerrit_inst.setup(key_filename=key_filename, username=username)

        git_repos = gerrit_inst.get_project_list(pattern)
        gerrit_inst.close()
    except rcs.RcsException:
        LOG.error('Failed to retrieve list of projects')
        raise

    git_base_uri = project_source.get('git_base_uri') or CONF.git_base_uri
    use_launchpad_metrics = project_source.get('launchpad_metrics', True)

    for repo in git_repos:
        name = repo.split('/')[-1]
        launchpad_name = name if use_launchpad_metrics else None
        repo_uri = '%(git_base_uri)s/%(repo)s.git' % dict(
            git_base_uri=git_base_uri, repo=repo)
        yield {
            'branches': ['master'],
            'module': name,
            'organization': organization,
            'uri': repo_uri,
            'releases': [],
            'repo_name': repo,
            'gerrit_uri': uri,
            'ssh_username': username,
            'key_filename': key_filename,
            'launchpad_name': launchpad_name,
        }