def post(self, request, project, plugin_id):
        """
        Enable plugin, Test plugin or Reset plugin values
        """
        plugin = self._get_plugin(plugin_id)

        if request.DATA.get('test') and plugin.is_testable():
            try:
                test_results = plugin.test_configuration(project)
            except Exception as exc:
                if isinstance(exc, HTTPError):
                    test_results = '%s\n%s' % (exc, exc.response.text[:256])
                elif hasattr(exc, 'read') and callable(exc.read):
                    test_results = '%s\n%s' % (exc, exc.read()[:256])
                else:
                    logging.exception('Plugin(%s) raised an error during test',
                                      plugin_id)
                    test_results = 'There was an internal error with the Plugin'
            if not test_results:
                test_results = 'No errors returned'
            return Response({'detail': test_results}, status=200)

        if request.DATA.get('reset'):
            plugin = self._get_plugin(plugin_id)
            plugin.reset_options(project=project)
            context = serialize(plugin, request.user,
                                PluginWithConfigSerializer(project))
            return Response(context, status=200)

        if not plugin.can_disable:
            return Response({'detail': ERR_ALWAYS_ENABLED}, status=400)

        plugin.enable(project)

        return Response(status=201)
Beispiel #2
0
    def get(self, request, project, plugin_id):
        plugin = self._get_plugin(plugin_id)

        context = serialize(plugin, request.user,
                            PluginWithConfigSerializer(project))

        return Response(context)
Beispiel #3
0
    def put(self, request, project, plugin_id):
        plugin = self._get_plugin(plugin_id)

        config = plugin.get_config(project=project, )

        cleaned = {}
        errors = {}
        for field in config:
            key = field['name']
            value = request.DATA.get(key)

            if field.get('required') and not value:
                errors[key] = ERR_FIELD_REQUIRED

            try:
                value = plugin.validate_config_field(
                    project=project,
                    name=key,
                    value=value,
                    actor=request.user,
                )
            except (forms.ValidationError, serializers.ValidationError,
                    PluginError) as e:
                errors[key] = e.message

            if not errors.get(key):
                cleaned[key] = value

        if not errors:
            try:
                cleaned = plugin.validate_config(
                    project=project,
                    config=cleaned,
                    actor=request.user,
                )
            except PluginError as e:
                errors['__all__'] = e.message

        if errors:
            return Response({
                'errors': errors,
            }, status=400)

        for key, value in six.iteritems(cleaned):
            if not value:
                plugin.unset_option(
                    project=project,
                    key=key,
                )
            else:
                plugin.set_option(
                    project=project,
                    key=key,
                    value=value,
                )

        context = serialize(plugin, request.user,
                            PluginWithConfigSerializer(project))

        return Response(context)
Beispiel #4
0
    def post(self, request, project, plugin_id):
        """
        Enable plugin, Test plugin or Reset plugin values
        """
        plugin = self._get_plugin(plugin_id)

        if request.data.get("test") and plugin.is_testable():
            try:
                test_results = plugin.test_configuration(project)
            except Exception as exc:
                if isinstance(exc, HTTPError):
                    test_results = "%s\n%s" % (exc, exc.response.text[:256])
                elif hasattr(exc, "read") and callable(exc.read):
                    test_results = "%s\n%s" % (exc, exc.read()[:256])
                else:
                    logging.exception("Plugin(%s) raised an error during test",
                                      plugin_id)
                    test_results = "There was an internal error with the Plugin"
            if not test_results:
                test_results = "No errors returned"
            return Response({"detail": test_results}, status=200)

        if request.data.get("reset"):
            plugin = self._get_plugin(plugin_id)
            plugin.reset_options(project=project)
            context = serialize(plugin, request.user,
                                PluginWithConfigSerializer(project))

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=project.id,
                event=AuditLogEntryEvent.INTEGRATION_EDIT,
                data={
                    "integration": plugin_id,
                    "project": project.slug
                },
            )

            return Response(context, status=200)

        if not plugin.can_disable:
            return Response({"detail": ERR_ALWAYS_ENABLED}, status=400)

        plugin.enable(project)

        self.create_audit_entry(
            request=request,
            organization=project.organization,
            target_object=project.id,
            event=AuditLogEntryEvent.INTEGRATION_ADD,
            data={
                "integration": plugin_id,
                "project": project.slug
            },
        )

        return Response(status=201)
Beispiel #5
0
    def get(self, request, project, plugin_id):
        plugin = self._get_plugin(plugin_id)

        try:
            context = serialize(plugin, request.user, PluginWithConfigSerializer(project))
        except PluginIdentityRequired as e:
            context = serialize(plugin, request.user, PluginSerializer(project))
            context["config_error"] = six.text_type(e)
            context["auth_url"] = reverse("socialauth_associate", args=[plugin.slug])

        return Response(context)
    def get(self, request, project, plugin_id):
        plugin = self._get_plugin(plugin_id)

        try:
            context = serialize(plugin, request.user,
                                PluginWithConfigSerializer(project))
        except PluginIdentityRequired as e:
            context = serialize(plugin, request.user,
                                PluginSerializer(project))
            context['config_error'] = e.message
            context['auth_url'] = reverse('socialauth_associate',
                                          args=[plugin.slug])

        return Response(context)
    def get(self, request: Request, project, plugin_id) -> Response:
        plugin = self._get_plugin(plugin_id)

        try:
            context = serialize(plugin, request.user,
                                PluginWithConfigSerializer(project))
        except PluginIdentityRequired as e:
            context = serialize(plugin, request.user,
                                PluginSerializer(project))
            context["config_error"] = str(e)
            context["auth_url"] = reverse("socialauth_associate",
                                          args=[plugin.slug])

        if context["isDeprecated"]:
            raise Http404
        return Response(context)
    def post(self, request: Request, project, plugin_id) -> Response:
        """
        Enable plugin, Test plugin or Reset plugin values
        """
        plugin = self._get_plugin(plugin_id)

        if request.data.get("test") and plugin.is_testable():
            test_results = plugin.test_configuration_and_get_test_results(
                project)
            return Response({"detail": test_results}, status=200)

        if request.data.get("reset"):
            plugin = self._get_plugin(plugin_id)
            plugin.reset_options(project=project)
            context = serialize(plugin, request.user,
                                PluginWithConfigSerializer(project))

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=project.id,
                event=AuditLogEntryEvent.INTEGRATION_EDIT,
                data={
                    "integration": plugin_id,
                    "project": project.slug
                },
            )

            return Response(context, status=200)

        if not plugin.can_disable:
            return Response({"detail": ERR_ALWAYS_ENABLED}, status=400)

        plugin.enable(project)

        self.create_audit_entry(
            request=request,
            organization=project.organization,
            target_object=project.id,
            event=AuditLogEntryEvent.INTEGRATION_ADD,
            data={
                "integration": plugin_id,
                "project": project.slug
            },
        )

        return Response(status=201)
    def put(self, request: Request, project, plugin_id) -> Response:
        plugin = self._get_plugin(plugin_id)

        config = [
            serialize_field(project, plugin, c) for c in plugin.get_config(
                project=project, user=request.user, initial=request.data)
        ]

        cleaned = {}
        errors = {}
        for field in config:
            key = field["name"]
            value = request.data.get(key)

            if field.get("required") and not value:
                errors[key] = ERR_FIELD_REQUIRED

            try:
                value = plugin.validate_config_field(project=project,
                                                     name=key,
                                                     value=value,
                                                     actor=request.user)
            except (
                    forms.ValidationError,
                    serializers.ValidationError,
                    InvalidIdentity,
                    PluginError,
            ) as e:
                errors[key] = str(e)

            if not errors.get(key):
                cleaned[key] = value

        if not errors:
            try:
                cleaned = plugin.validate_config(project=project,
                                                 config=cleaned,
                                                 actor=request.user)
            except (InvalidIdentity, PluginError) as e:
                errors["__all__"] = str(e)

        if errors:
            return Response({"errors": errors}, status=400)

        for key, value in cleaned.items():
            if value is None:
                plugin.unset_option(project=project, key=key)
            else:
                plugin.set_option(project=project, key=key, value=value)

        context = serialize(plugin, request.user,
                            PluginWithConfigSerializer(project))

        plugin_enabled.send(plugin=plugin,
                            project=project,
                            user=request.user,
                            sender=self)

        self.create_audit_entry(
            request=request,
            organization=project.organization,
            target_object=project.id,
            event=AuditLogEntryEvent.INTEGRATION_EDIT,
            data={
                "integration": plugin_id,
                "project": project.slug
            },
        )

        return Response(context)
    def put(self, request, project, plugin_id):
        plugin = self._get_plugin(plugin_id)

        config = [
            serialize_field(project, plugin, c) for c in plugin.get_config(
                project=project,
                user=request.user,
                initial=request.DATA,
            )
        ]

        cleaned = {}
        errors = {}
        for field in config:
            key = field['name']
            value = request.DATA.get(key)

            if field.get('required') and not value:
                errors[key] = ERR_FIELD_REQUIRED

            try:
                value = plugin.validate_config_field(
                    project=project,
                    name=key,
                    value=value,
                    actor=request.user,
                )
            except (forms.ValidationError, serializers.ValidationError,
                    InvalidIdentity, PluginError) as e:
                errors[key] = e.message

            if not errors.get(key):
                cleaned[key] = value

        if not errors:
            try:
                cleaned = plugin.validate_config(
                    project=project,
                    config=cleaned,
                    actor=request.user,
                )
            except (InvalidIdentity, PluginError) as e:
                errors['__all__'] = e.message

        if errors:
            return Response({
                'errors': errors,
            }, status=400)

        for key, value in six.iteritems(cleaned):
            if value is None:
                plugin.unset_option(
                    project=project,
                    key=key,
                )
            else:
                plugin.set_option(
                    project=project,
                    key=key,
                    value=value,
                )

        context = serialize(plugin, request.user,
                            PluginWithConfigSerializer(project))

        plugin_enabled.send(plugin=plugin,
                            project=project,
                            user=request.user,
                            sender=self)

        self.create_audit_entry(request=request,
                                organization=project.organization,
                                target_object=project.id,
                                event=AuditLogEntryEvent.INTEGRATION_EDIT,
                                data={
                                    'integration': plugin_id,
                                    'project': project.slug
                                })

        return Response(context)