Beispiel #1
0
    def handle_refresh_error(self, req, payload):
        error_name = 'unknown_error'
        error_description = 'no description available'
        for name_key in ['error', 'Error']:
            if name_key in payload:
                error_name = payload.get(name_key)
                break

        for desc_key in ['error_description', 'ErrorDescription']:
            if desc_key in payload:
                error_description = payload.get(desc_key)
                break

        formatted_error = u'HTTP {} ({}): {}'.format(req.status_code, error_name, error_description)

        if req.status_code == 401:
            raise IdentityNotValid(formatted_error)

        if req.status_code == 400:
            # this may not be common, but at the very least Google will return
            # an invalid grant when a user is suspended
            if error_name == 'invalid_grant':
                raise IdentityNotValid(formatted_error)

        if req.status_code != 200:
            raise ApiError(formatted_error)
Beispiel #2
0
    def get_create_meta_for_project(self, project):
        params = {
            'expand': 'projects.issuetypes.fields',
            'projectIds': project
        }
        metas = self.get_cached(
            self.META_URL,
            params=params,
        )
        # We saw an empty JSON response come back from the API :(
        if not metas:
            logger.info('jira.get-create-meta.empty-response',
                        extra={
                            'base_url': self.base_url,
                            'project': project,
                        })
            return None

        # XXX(dcramer): document how this is possible, if it even is
        if len(metas['projects']) > 1:
            raise ApiError(
                u'More than one project found matching {}.'.format(project))

        try:
            return metas['projects'][0]
        except IndexError:
            logger.info('jira.get-create-meta.key-error',
                        extra={
                            'base_url': self.base_url,
                            'project': project,
                        })
            return None
Beispiel #3
0
    def get_create_meta_for_project(self, project):
        params = {
            "expand": "projects.issuetypes.fields",
            "projectIds": project
        }
        metas = self.get_cached(self.META_URL, params=params)
        # We saw an empty JSON response come back from the API :(
        if not metas:
            logger.info(
                "jira.get-create-meta.empty-response",
                extra={
                    "base_url": self.base_url,
                    "project": project
                },
            )
            return None

        # XXX(dcramer): document how this is possible, if it even is
        if len(metas["projects"]) > 1:
            raise ApiError(
                u"More than one project found matching {}.".format(project))

        try:
            return metas["projects"][0]
        except IndexError:
            logger.info(
                "jira.get-create-meta.key-error",
                extra={
                    "base_url": self.base_url,
                    "project": project
                },
            )
            return None
Beispiel #4
0
 def get_issue(self, project_id, issue_id):
     try:
         return self.get(
             GitLabApiClientPath.issue.format(project=project_id,
                                              issue=issue_id))
     except IndexError:
         raise ApiError('Issue not found with ID', 404)
Beispiel #5
0
    def handle_refresh_error(self, req, payload):
        error_name = "unknown_error"
        error_description = "no description available"
        for name_key in ["error", "Error"]:
            if name_key in payload:
                error_name = payload.get(name_key)
                break

        for desc_key in ["error_description", "ErrorDescription"]:
            if desc_key in payload:
                error_description = payload.get(desc_key)
                break

        formatted_error = u"HTTP {} ({}): {}".format(req.status_code,
                                                     error_name,
                                                     error_description)

        if req.status_code == 401:
            self.logger.info(
                "identity.oauth.refresh.identity-not-valid-error",
                extra={
                    "error_name": error_name,
                    "error_status_code": req.status_code,
                    "error_description": error_description,
                    "provider_key": self.key,
                },
            )
            raise IdentityNotValid(formatted_error)

        if req.status_code == 400:
            # this may not be common, but at the very least Google will return
            # an invalid grant when a user is suspended
            if error_name == "invalid_grant":
                self.logger.info(
                    "identity.oauth.refresh.identity-not-valid-error",
                    extra={
                        "error_name": error_name,
                        "error_status_code": req.status_code,
                        "error_description": error_description,
                        "provider_key": self.key,
                    },
                )
                raise IdentityNotValid(formatted_error)

        if req.status_code != 200:
            self.logger.info(
                "identity.oauth.refresh.api-error",
                extra={
                    "error_name": error_name,
                    "error_status_code": req.status_code,
                    "error_description": error_description,
                    "provider_key": self.key,
                },
            )
            raise ApiError(formatted_error)
Beispiel #6
0
    def get_issue(self, project_id, issue_id):
        """Get an issue

        See https://docs.gitlab.com/ee/api/issues.html#single-issue
        """
        try:
            return self.get(
                GitLabApiClientPath.issue.format(project=project_id,
                                                 issue=issue_id))
        except IndexError:
            raise ApiError('Issue not found with ID', 404)
Beispiel #7
0
    def handle_refresh_error(self, req, payload):
        error_name = 'unknown_error'
        error_description = 'no description available'
        for name_key in ['error', 'Error']:
            if name_key in payload:
                error_name = payload.get(name_key)
                break

        for desc_key in ['error_description', 'ErrorDescription']:
            if desc_key in payload:
                error_description = payload.get(desc_key)
                break

        formatted_error = u'HTTP {} ({}): {}'.format(req.status_code, error_name, error_description)

        if req.status_code == 401:
            self.logger.info(
                'identity.oauth.refresh.identity-not-valid-error',
                extra={
                    'error_name': error_name,
                    'error_status_code': req.status_code,
                    'error_description': error_description,
                    'provider_key': self.key,
                }
            )
            raise IdentityNotValid(formatted_error)

        if req.status_code == 400:
            # this may not be common, but at the very least Google will return
            # an invalid grant when a user is suspended
            if error_name == 'invalid_grant':
                self.logger.info(
                    'identity.oauth.refresh.identity-not-valid-error',
                    extra={
                        'error_name': error_name,
                        'error_status_code': req.status_code,
                        'error_description': error_description,
                        'provider_key': self.key,
                    }
                )
                raise IdentityNotValid(formatted_error)

        if req.status_code != 200:
            self.logger.info(
                'identity.oauth.refresh.api-error',
                extra={
                    'error_name': error_name,
                    'error_status_code': req.status_code,
                    'error_description': error_description,
                    'provider_key': self.key,
                }
            )
            raise ApiError(formatted_error)
    def get_create_meta_for_project(self, project):
        metas = self.get_create_meta(project)
        # We saw an empty JSON response come back from the API :(
        if not metas:
            return None

        # XXX(dcramer): document how this is possible, if it even is
        if len(metas['projects']) > 1:
            raise ApiError('More than one project found.')

        try:
            return metas['projects'][0]
        except IndexError:
            return None
Beispiel #9
0
    def test_notify_failure(self):
        errors = (
            ApiError('The server is sad'),
            SSLError('[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:590)'),
            HTTPError('A bad response'),
        )
        for err in errors:
            n = NotificationPlugin()
            n.slug = 'slack'

            def hook(*a, **kw):
                raise err

            event = self.create_event()
            notification = Notification(event)

            n.notify_users = hook
            assert n.notify(notification) is False
Beispiel #10
0
    def test_notify_failure(self):
        errors = (
            ApiError("The server is sad"),
            SSLError("[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:590)"),
            HTTPError("A bad response"),
            PluginError("A plugin is sad"),
        )
        for err in errors:
            n = NotificationPlugin()
            n.slug = "slack"

            def hook(*a, **kw):
                raise err

            event = self.store_event(data={}, project_id=self.project.id)
            notification = Notification(event)

            n.notify_users = hook
            assert n.notify(notification) is False
Beispiel #11
0
    def get_create_meta_for_project(self, project):
        params = {
            'expand': 'projects.issuetypes.fields',
            'projectIds': project
        }
        metas = self.get_cached(
            self.META_URL,
            params=params,
        )
        # We saw an empty JSON response come back from the API :(
        if not metas:
            return None

        # XXX(dcramer): document how this is possible, if it even is
        if len(metas['projects']) > 1:
            raise ApiError('More than one project found.')

        try:
            return metas['projects'][0]
        except IndexError:
            return None
 def test_handle_exception_503(self):
     resp = IntegrationEndpoint().handle_exception(
         HttpRequest(), ApiError("This is an error", code=503)
     )
     assert resp.status_code == 503
     assert resp.exception is True