Ejemplo n.º 1
0
    def add_reviewer(cls, change_id, account_id, force=False):
        """Add a reviewer

        Sends a POST request to Gerrit to add one user or all members of one
        group as reviewer to the change.

        :param change_id: any identification number for the change (UUID,
            Change-Id, or legacy numeric change ID)
        :type change_id: str
        :param account_id: any identification string for an account (name,
            username, email)
        :type account_id: str
        :param force: do not prompt the user for confirmation if gerrit needs a
            confirmation to add multiple reviewers at once (group).  Defaults
            to False
        :type force: bool
        :rtype: tuple(AccountInfo)
        :raise: PyCRError if the Gerrit server returns an error
        """

        cls.log.debug('Assign review to %s: %s', account_id, change_id)

        payload = {'reviewer': account_id}
        headers = {'content-type': 'application/json'}

        try:
            endpoint = changes.reviewers(change_id)
            _, response = RequestFactory.post(endpoint,
                                              data=json.dumps(payload),
                                              headers=headers)

        except RequestError as why:
            if why.status_code == 404:
                raise NoSuchChangeError(change_id)

            raise UnexpectedError(why)

        if 'confirm' in response:
            assert 'error' in response, 'missing "error" field in response'

            cls.log.debug('Assigning review: confirmation requested')

            do_add_reviewers = True if force else confirm(response['error'])

            if not do_add_reviewers:
                info('reviewer not added, aborting...')
                return None

            try:
                payload['confirmed'] = True
                _, response = RequestFactory.post(endpoint,
                                                  data=json.dumps(payload),
                                                  headers=headers)

            except RequestError as why:
                raise UnexpectedError(why)

        assert 'reviewers' in response, '"reviewers" not in HTTP response'
        return tuple([AccountInfo.parse(r) for r in response['reviewers']])
Ejemplo n.º 2
0
    def add_reviewer(cls, change_id, account_id, force=False):
        """Add a reviewer

        Sends a POST request to Gerrit to add one user or all members of one
        group as reviewer to the change.

        :param change_id: any identification number for the change (UUID,
            Change-Id, or legacy numeric change ID)
        :type change_id: str
        :param account_id: any identification string for an account (name,
            username, email)
        :type account_id: str
        :param force: do not prompt the user for confirmation if gerrit needs a
            confirmation to add multiple reviewers at once (group).  Defaults
            to False
        :type force: bool
        :rtype: tuple(AccountInfo)
        :raise: PyCRError if the Gerrit server returns an error
        """

        cls.log.debug('Assign review to %s: %s', account_id, change_id)

        payload = {'reviewer': account_id}
        headers = {'content-type': 'application/json'}

        try:
            endpoint = changes.reviewers(change_id)
            _, response = RequestFactory.post(endpoint,
                                              data=json.dumps(payload),
                                              headers=headers)

        except RequestError as why:
            if why.status_code == 404:
                raise NoSuchChangeError(change_id)

            raise UnexpectedError(why)

        if 'confirm' in response:
            assert 'error' in response, 'missing "error" field in response'

            cls.log.debug('Assigning review: confirmation requested')

            do_add_reviewers = True if force else confirm(response['error'])

            if not do_add_reviewers:
                info('reviewer not added, aborting...')
                return None

            try:
                payload['confirmed'] = True
                _, response = RequestFactory.post(endpoint,
                                                  data=json.dumps(payload),
                                                  headers=headers)

            except RequestError as why:
                raise UnexpectedError(why)

        assert 'reviewers' in response, '"reviewers" not in HTTP response'
        return tuple([AccountInfo.parse(r) for r in response['reviewers']])
Ejemplo n.º 3
0
    def get_reviews(cls, change_id):
        """Fetch the reviews for a change

        Sends a GET request to Gerrit to fetch the reviews for the given
        change.

        :param change_id: any identification number for the change (UUID,
            Change-Id, or legacy numeric change ID)
        :type change_id: str
        :rtype: tuple[ReviewerInfo]
        :raise: NoSuchChangeError if the change does not exists
        :raise: PyCRError on any other error
        """

        cls.log.debug('Reviews lookup: %s', change_id)

        try:
            endpoint = changes.reviewers(change_id)
            _, response = RequestFactory.get(endpoint)

        except RequestError as why:
            if why.status_code == 404:
                raise NoSuchChangeError(change_id)

            raise UnexpectedError(why)

        # If 'approvals' field is missing, then there is no reviewer

        # NOTE: This seems to be against the specifications for this method:
        # https://gerrit-review.googlesource.com/Documentation/
        #   rest-api-changes.html#list-reviewers
        # "As result a list of ReviewerInfo entries is returned."
        # A ReviewerInfo entry is expected to have an "approvals" field, but
        # experiences show that it's not always the case, and that the change
        # owner can also be in the list although not a reviewers.

        return tuple(
            [ReviewerInfo.parse(r) for r in response if 'approvals' in r])
Ejemplo n.º 4
0
    def get_reviews(cls, change_id):
        """Fetch the reviews for a change

        Sends a GET request to Gerrit to fetch the reviews for the given
        change.

        :param change_id: any identification number for the change (UUID,
            Change-Id, or legacy numeric change ID)
        :type change_id: str
        :rtype: tuple[ReviewerInfo]
        :raise: NoSuchChangeError if the change does not exists
        :raise: PyCRError on any other error
        """

        cls.log.debug('Reviews lookup: %s', change_id)

        try:
            endpoint = changes.reviewers(change_id)
            _, response = RequestFactory.get(endpoint)

        except RequestError as why:
            if why.status_code == 404:
                raise NoSuchChangeError(change_id)

            raise UnexpectedError(why)

        # If 'approvals' field is missing, then there is no reviewer

        # NOTE: This seems to be against the specifications for this method:
        # https://gerrit-review.googlesource.com/Documentation/
        #   rest-api-changes.html#list-reviewers
        # "As result a list of ReviewerInfo entries is returned."
        # A ReviewerInfo entry is expected to have an "approvals" field, but
        # experiences show that it's not always the case, and that the change
        # owner can also be in the list although not a reviewers.

        return tuple(
            [ReviewerInfo.parse(r) for r in response if 'approvals' in r])