Exemplo n.º 1
0
def update_validated_data_from_url(validated_data: Dict[str, Any],
                                   url: str) -> Dict:
    """If remote plugin, download the archive and get up-to-date validated_data from there."""
    if url.startswith("file:"):
        plugin_path = url[5:]
        json_path = os.path.join(plugin_path, "plugin.json")
        json = load_json_file(json_path)
        if not json:
            raise ValidationError(
                "Could not load plugin.json from: {}".format(json_path))
        validated_data["plugin_type"] = "local"
        validated_data["url"] = url
        validated_data["tag"] = None
        validated_data["archive"] = None
        validated_data["name"] = json.get("name", json_path.split("/")[-2])
        validated_data["description"] = json.get("description", "")
        validated_data["config_schema"] = json.get("config", [])
        validated_data["source"] = None
        posthog_version = json.get("posthogVersion", None)
    else:
        parsed_url = parse_url(url, get_latest_if_none=True)
        if parsed_url:
            validated_data["url"] = parsed_url["root_url"]
            validated_data["tag"] = parsed_url.get("tag", None)
            validated_data["archive"] = download_plugin_archive(
                validated_data["url"], validated_data["tag"])
            plugin_json = get_json_from_archive(validated_data["archive"],
                                                "plugin.json")
            if not plugin_json:
                raise ValidationError(
                    "Could not find plugin.json in the plugin")
            validated_data["name"] = plugin_json["name"]
            validated_data["description"] = plugin_json.get("description", "")
            validated_data["config_schema"] = plugin_json.get("config", [])
            validated_data["source"] = None
            posthog_version = plugin_json.get("posthogVersion", None)
        else:
            raise ValidationError(
                "Must be a GitHub/GitLab repository or a npm package URL!")

        # Keep plugin type as "repository" or reset to "custom" if it was something else.
        if (validated_data.get("plugin_type", None) != Plugin.PluginType.CUSTOM
                and validated_data.get("plugin_type",
                                       None) != Plugin.PluginType.REPOSITORY):
            validated_data["plugin_type"] = Plugin.PluginType.CUSTOM

    if posthog_version and not settings.MULTI_TENANCY:
        try:
            spec = SimpleSpec(posthog_version.replace(" ", ""))
        except ValueError:
            raise ValidationError(
                f'Invalid PostHog semantic version requirement "{posthog_version}"!'
            )
        if not (Version(VERSION) in spec):
            raise ValidationError(
                f'Currently running PostHog version {VERSION} does not match this plugin\'s semantic version requirement "{posthog_version}".'
            )

    return validated_data
Exemplo n.º 2
0
    def __init__(self, service, supported_version):
        if service not in self.accepted_services:
            services_str = ", ".join(self.accepted_services)
            raise Exception(
                f"service {service} cannot be used to specify a version requirement. service should be one of {services_str}"
            )

        self.service = service

        try:
            self.supported_version = SimpleSpec(supported_version)
        except:
            raise Exception(
                f"The provided supported_version for service {service} is invalid. See the Docs for SimpleSpec: https://pypi.org/project/semantic-version/"
            )
Exemplo n.º 3
0
def is_posthog_version_compatible(posthog_min_version, posthog_max_version):
    return POSTHOG_VERSION in SimpleSpec(
        f">={posthog_min_version},<={posthog_max_version}")