コード例 #1
0
        def getRawHash(self, content):
            if content is None:
                raise HookError('Cannot get hash of empty content')

            hasher = self._getHasher()
            hasher.update(content)
            return hasher.hexdigest()
コード例 #2
0
        def _getHasher(self):
            if self._request_id is None:
                raise HookError('Cannot get hash without request id')

            hasher = hmac.new(self._key, digestmod=hashlib.sha256)
            hasher.update(six.text_type(self._request_id).encode('utf-8'))
            return hasher
コード例 #3
0
    def _set_root(self):
        """Set API root object."""
        cmd = MercurialGitHookCmd()
        try:
            server_url = cmd.get_server_url(None, None)
        except Exception:
            self.log(
                'Trying .reviewboardrc (RBTOOLS_CONFIG_PATH) file "'
                'in "%s" and "%s"', os.environ.get('HOME'),
                os.environ.get('RBTOOLS_CONFIG_PATH'))
            raise

        self.log('Review Board: %s', server_url)

        try:
            api_client, self.root = cmd.get_api(server_url)
        except Exception:
            self.log('Cannot fetch data from RB. Is ALLOWED_HOST correct?')
            raise

        session = get_authenticated_session(api_client,
                                            self.root,
                                            auth_required=True,
                                            num_retries=0)
        if session is None or not session.authenticated:
            raise HookError('Please add an USERNAME and a PASSWORD or '
                            'API_TOKEN to .reviewboardrc')

        self._differ = self.review_differ_class(self.root)
コード例 #4
0
    def _get_request(self):
        """Find a review request in the given repo for the given changeset.

        Returns:
            complex:
            The corresponding review request on review board if exist,
            otherwise None.
        """
        fields = ('summary,approved,approval_failure,id,commit_id,'
                  'branch,description,extra_data')
        links = 'submitter,update,latest_diff,draft,file_attachments'

        reqs = self.root.get_review_requests(repository=self.repo,
                                             status='pending',
                                             show_all_unpublished=True,
                                             only_fields=fields,
                                             only_links=links,
                                             commit_id=self.commit_id)

        count = len(reqs)
        if count == 0:
            reqs = self.root.get_review_requests(repository=self.repo,
                                                 status='pending',
                                                 show_all_unpublished=True,
                                                 only_fields=fields,
                                                 only_links=links,
                                                 from_user=self.submitter)
            found = None
            for r in reqs.all_items:
                if r.summary == self.summary():
                    if found is not None:
                        raise HookError('Multiple review requests: %s' %
                                        self.summary())
                    found = r

            return found

        elif count == 1:
            r = reqs[0]
            if r.links.submitter.title.lower() != self.submitter.lower():
                raise HookError('Owner of review request (%d): %s' %
                                (r.id, r.links.submitter.title))
            return r

        return None
コード例 #5
0
    def push_to_reviewboard(self, node):
        """Run the hook.

        Returns:
            int:
            Return code of execution. 0 on success, otherwise non-zero.
        """
        self.log('Push as user "%s" to "%s"...', self.submitter,
                 self.repo_name)

        if node is None or len(node) == 0:
            raise HookError('Initial changeset is undefined.')

        if self.submitter is None or self.repo_name is None:
            raise HookError('Cannot detect submitter or repository.')

        self._set_root()
        self._set_repo_id()
        return self._handle_changeset_list(node)
コード例 #6
0
    def __init__(self, tool):
        self.tool = tool
        envKey = 'HOOK_HMAC_KEY'
        self._key = os.environ.get(envKey)
        if self._key is None:
            try:
                with open('/etc/machine-id', 'r') as content_file:
                    self._key = content_file.read().strip()
            except Exception:
                raise HookError('You need to define %s' % envKey)

        if not six.PY2:
            self._key = bytes(self._key, 'ascii')
コード例 #7
0
        def getHash(self, diffset_id):
            if self._diff is None:
                raise HookError('Cannot get hash of empty diff')

            if diffset_id is None:
                raise HookError('Cannot get hash without diffset id')

            if diffset_id in self._hashes:
                return self._hashes[diffset_id]

            hasher = self._getHasher()
            hasher.update(six.text_type(diffset_id).encode('utf-8'))

            prefixes = (b'diff', b'@@', b'#', b'index')

            for line in self._diff.splitlines():
                if len(line) > 0 and not line.startswith(prefixes):
                    hasher.update(line)

            h = hasher.hexdigest()
            self._hashes[diffset_id] = h
            return h
コード例 #8
0
    def _set_repo_id(self):
        """Set ID of repository."""
        fields = 'path,mirror_path,id'

        repos = self.root.get_repositories(name=self.repo_name,
                                           tool=self.name,
                                           only_fields=fields,
                                           only_links='')

        if repos.num_items < 1:
            repos = self.root.get_repositories(path=self.repo_name,
                                               tool=self.name,
                                               only_fields=fields,
                                               only_links='')
            if repos.num_items < 1:
                raise HookError('Could not open Review Board repository:'
                                '\n%s\n'
                                'Repository is not registered or you do '
                                'not have permissions to access this '
                                'repository.' % self.repo_name)

        r = repos[0]
        self.repo_id = r.id
        return r