예제 #1
0
    def test_get_json_from_archive(self, mock_get):
        plugin_json_zip = get_json_from_archive(
            base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1]), "plugin.json")
        self.assertEqual(plugin_json_zip["name"], "helloworldplugin")
        self.assertEqual(plugin_json_zip["url"],
                         "https://github.com/PostHog/helloworldplugin")
        self.assertEqual(plugin_json_zip["description"],
                         "Greet the World and Foo a Bar, JS edition!")

        plugin_json_tgz = get_json_from_archive(
            base64.b64decode(HELLO_WORLD_PLUGIN_NPM_TGZ[1]), "plugin.json")
        self.assertEqual(plugin_json_tgz["name"], "helloworldplugin")
        self.assertEqual(plugin_json_tgz["url"],
                         "https://github.com/PostHog/helloworldplugin")
        self.assertEqual(plugin_json_tgz["description"],
                         "Greet the World and Foo a Bar, JS edition!")
예제 #2
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
예제 #3
0
    def test_put_json_into_zip_archive(self, mock_get):
        archive = base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1])
        plugin_json = get_json_from_archive(archive, "plugin.json")
        plugin_json["posthogVersion"] = "0.0.0"

        # check that we can override files
        new_archive = put_json_into_zip_archive(archive, plugin_json,
                                                "plugin.json")
        new_plugin_json = get_json_from_zip_archive(new_archive, "plugin.json")
        self.assertEqual(new_plugin_json["posthogVersion"], "0.0.0")

        # check that new the file is there
        new_archive_2 = put_json_into_zip_archive(archive, plugin_json,
                                                  "plugin2.json")
        new_plugin_json_2 = get_json_from_archive(new_archive_2,
                                                  "plugin2.json")
        self.assertEqual(new_plugin_json_2["posthogVersion"], "0.0.0")

        # check that old files are intact
        old_plugin_json_2 = get_json_from_archive(new_archive_2, "plugin.json")
        self.assertEqual(old_plugin_json_2["name"], "helloworldplugin")