Example #1
0
    def test_set_review(self):
        req_path = 'changes/%s/revisions/%s/review' % (CHANGE_ID, REVISION)
        labels = {'Verified': 1}
        client = GerritClient('chromium-review.googlesource.com')
        client._fetch = Mock(return_value={'labels': labels})
        client.set_review(CHANGE_ID, REVISION, message='Hi!', labels=labels)
        client._fetch.assert_called_with(req_path,
                                         method='POST',
                                         body={
                                             'message': 'Hi!',
                                             'labels': labels,
                                         })

        # Test with "notify" parameter.
        client.set_review(CHANGE_ID,
                          REVISION,
                          message='Hi!',
                          labels=labels,
                          notify='all')
        client._fetch.assert_called_with(req_path,
                                         method='POST',
                                         body={
                                             'message': 'Hi!',
                                             'labels': labels,
                                             'notify': 'ALL',
                                         })

        with self.assertRaises(AssertionError):
            client.set_review(CHANGE_ID, REVISION, notify='Argh!')
def create_gerrit_client(config):
    """Create an instance of an Gerrit client.
        Will prompt for credentials if not configured."""
    client = GerritClient(base_url=config.GERRIT_HOST,
                          username=config.GERRIT_USERNAME,
                          password=config.GERRIT_PASSWORD)
    client = validate_gerrit_config(config, client)
    return client
Example #3
0
    def __init__(self, cfg):
        self.gerrits = {}
        gerrit_configs = self.get_section(cfg, 'gerrits', [])
        for gerrit in gerrit_configs:
            name = gerrit.keys()[0]
            self.gerrits[name] = {
                'name': name,
                'port': gerrit[name].get('port', 29418)
            }

            to_copy = [
                'username', 'project-pattern', 'host', 'key_filename',
                'startup'
            ]
            for k in to_copy:
                self.gerrits[name][k] = gerrit[name].get(k)

            self.gerrits[name]['project_re'] = re.compile(
                gerrit[name]['project-pattern'])

            self.gerrits[name]['events'] = {}

            # client connection details
            username = self.gerrits[name].get('username')
            host = self.gerrits[name].get('host')
            key_filename = self.gerrits[name].get('key_filename')
            name = self.gerrits[name].get('name')
            port = self.gerrits[name].get('port', 29418)
            # this is the only time we construct a new GerritClient
            # the client does not have an active ssh connection at
            # this point
            self.gerrits[name]['client'] = GerritClient(
                username=username,
                host=host,
                key_filename=key_filename,
                port=port)

            # create a list of actions for each event type
            for event in gerrit[name]['events']:
                event_type = event['type']
                if event_type not in self.gerrits[name]['events']:
                    self.gerrits[name]['events'][event_type] = []

                # some actions may only be interested in certain branches,
                # so construct a regexp object here so the action can match
                if 'branch-pattern' in event:
                    event['branch_re'] = re.compile(event['branch-pattern'])

                self.gerrits[name]['events'][event_type].append(event.copy())

        self.plugins = self.get_section(cfg, 'plugins', [])
Example #4
0
    def test_get_change(self):
        req_path = 'changes/%s?o=ALL_REVISIONS' % CHANGE_ID
        change_reponse = {
            'id': CHANGE_ID,
            'project': 'project',
            'branch': 'master',
            'hashtags': [],
            'change_id': SHORT_CHANGE_ID,
            'subject': 'My change',
            'status': 'NEW',
            'created': '2014-10-17 18:24:39.193000000',
            'updated': '2014-10-17 20:44:48.338000000',
            'mergeable': True,
            'insertions': 10,
            'deletions': 11,
            '_sortkey': '0030833c0002bff9',
            '_number': 180217,
            'owner': {
                'name': 'John Doe'
            },
            'current_revision': REVISION,
            'revisions': {
                REVISION: {
                    '_number': 1,
                    'fetch': {
                        'http': {
                            'url':
                            'https://chromium.googlesource.com/html-office',
                            'ref': 'refs/changes/80/123/1'
                        }
                    }
                }
            }
        }
        client = GerritClient('chromium-review.googlesource.com')
        client._fetch = Mock(return_value=change_reponse)
        change = client.get_change(CHANGE_ID)
        client._fetch.assert_called_once_with(req_path)
        self.assertIsNotNone(change)
        self.assertEqual(change.change_id, SHORT_CHANGE_ID)
        self.assertEqual(change.branch, 'master')
        self.assertEqual(change.project, 'project')
        self.assertEqual(change.owner.name, 'John Doe')
        self.assertEqual(change.current_revision, REVISION)

        # smoke test for branch coverage
        change = client.get_change(CHANGE_ID,
                                   include_all_revisions=False,
                                   include_owner_details=True)
Example #5
0
 def test_get_nonexistent_change(self):
     client = GerritClient('chromium-review.googlesource.com')
     change = client.get_change(CHANGE_ID)
     self.assertIsNone(change)