def test_download_plugin_archive_github(self, mock_get): plugin_github_zip_1 = download_plugin_archive( "https://www.github.com/PostHog/helloworldplugin/commit/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e", HELLO_WORLD_PLUGIN_GITHUB_ZIP[0], ) self.assertEqual(plugin_github_zip_1, base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1])) self.assertEqual(mock_get.call_count, 1) mock_get.assert_called_with( "https://github.com/PostHog/helloworldplugin/archive/d5aa1d2b8a534f37cd93be48b214f490ef9ee904.zip", headers={}, ) plugin_github_zip_2 = download_plugin_archive( "https://www.github.com/PostHog/helloworldplugin/commit/{}".format( HELLO_WORLD_PLUGIN_GITHUB_ZIP[0])) self.assertEqual(plugin_github_zip_2, base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1])) self.assertEqual(mock_get.call_count, 2) mock_get.assert_called_with( "https://github.com/PostHog/helloworldplugin/archive/d5aa1d2b8a534f37cd93be48b214f490ef9ee904.zip", headers={}, ) plugin_github_zip_3 = download_plugin_archive( "https://www.github.com/PostHog/helloworldplugin/commit/{}?private_token=TOKEN" .format(HELLO_WORLD_PLUGIN_GITHUB_ZIP[0])) self.assertEqual(plugin_github_zip_3, base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1])) self.assertEqual(mock_get.call_count, 3) mock_get.assert_called_with( "https://github.com/PostHog/helloworldplugin/archive/d5aa1d2b8a534f37cd93be48b214f490ef9ee904.zip", headers={"Authorization": "token TOKEN"}, ) with self.settings(GITHUB_TOKEN="MY_GITHUB_TOKEN"): plugin_github_zip_4 = download_plugin_archive( "https://www.github.com/PostHog/helloworldplugin/commit/{}?private_token=TOKEN" .format(HELLO_WORLD_PLUGIN_GITHUB_ZIP[0])) self.assertEqual( plugin_github_zip_4, base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1])) self.assertEqual(mock_get.call_count, 4) mock_get.assert_called_with( "https://github.com/PostHog/helloworldplugin/archive/d5aa1d2b8a534f37cd93be48b214f490ef9ee904.zip", headers={"Authorization": "token TOKEN"}, ) plugin_github_zip_5 = download_plugin_archive( "https://www.github.com/PostHog/helloworldplugin/commit/{}". format(HELLO_WORLD_PLUGIN_GITHUB_ZIP[0])) self.assertEqual( plugin_github_zip_5, base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1])) self.assertEqual(mock_get.call_count, 5) mock_get.assert_called_with( "https://github.com/PostHog/helloworldplugin/archive/d5aa1d2b8a534f37cd93be48b214f490ef9ee904.zip", headers={"Authorization": "token MY_GITHUB_TOKEN"}, )
def test_download_plugin_archive_github(self, mock_get): plugin_github_zip_1 = download_plugin_archive( "https://www.github.com/PostHog/helloworldplugin/commit/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e", HELLO_WORLD_PLUGIN_GITHUB_ZIP[0], ) self.assertEqual(plugin_github_zip_1, base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1])) plugin_github_zip_2 = download_plugin_archive( "https://www.github.com/PostHog/helloworldplugin/commit/{}".format( HELLO_WORLD_PLUGIN_GITHUB_ZIP[0])) self.assertEqual(plugin_github_zip_2, base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1]))
def test_download_plugin_archive_gitlab(self, mock_get): plugin_gitlab = download_plugin_archive( "https://www.gitlab.com/mariusandra/helloworldplugin/-/commit/ff78cbe1d70316055c610a962a8355a4616d874b", HELLO_WORLD_PLUGIN_GITLAB_ZIP[0], ) self.assertEqual(plugin_gitlab, base64.b64decode(HELLO_WORLD_PLUGIN_GITLAB_ZIP[1])) self.assertEqual(mock_get.call_count, 1) mock_get.assert_called_with( "https://gitlab.com/api/v4/projects/mariusandra%2Fhelloworldplugin/repository/archive.zip?sha=ff78cbe1d70316055c610a962a8355a4616d874b", headers={}, ) plugin_gitlab = download_plugin_archive( "https://www.gitlab.com/mariusandra/helloworldplugin/-/commit/ff78cbe1d70316055c610a962a8355a4616d874b?private_token=PRIVATE_TOKEN", HELLO_WORLD_PLUGIN_GITLAB_ZIP[0], ) self.assertEqual(plugin_gitlab, base64.b64decode(HELLO_WORLD_PLUGIN_GITLAB_ZIP[1])) self.assertEqual(mock_get.call_count, 2) mock_get.assert_called_with( "https://gitlab.com/api/v4/projects/mariusandra%2Fhelloworldplugin/repository/archive.zip?sha=ff78cbe1d70316055c610a962a8355a4616d874b", headers={"Authorization": "Bearer PRIVATE_TOKEN"}, ) with self.settings(GITLAB_TOKEN="MY_GITLAB_TOKEN"): plugin_gitlab = download_plugin_archive( "https://www.gitlab.com/mariusandra/helloworldplugin/-/commit/ff78cbe1d70316055c610a962a8355a4616d874b?private_token=PRIVATE_TOKEN", HELLO_WORLD_PLUGIN_GITLAB_ZIP[0], ) self.assertEqual( plugin_gitlab, base64.b64decode(HELLO_WORLD_PLUGIN_GITLAB_ZIP[1])) self.assertEqual(mock_get.call_count, 3) mock_get.assert_called_with( "https://gitlab.com/api/v4/projects/mariusandra%2Fhelloworldplugin/repository/archive.zip?sha=ff78cbe1d70316055c610a962a8355a4616d874b", headers={"Authorization": "Bearer PRIVATE_TOKEN"}, ) plugin_gitlab = download_plugin_archive( "https://www.gitlab.com/mariusandra/helloworldplugin/-/commit/ff78cbe1d70316055c610a962a8355a4616d874b", HELLO_WORLD_PLUGIN_GITLAB_ZIP[0], ) self.assertEqual( plugin_gitlab, base64.b64decode(HELLO_WORLD_PLUGIN_GITLAB_ZIP[1])) self.assertEqual(mock_get.call_count, 4) mock_get.assert_called_with( "https://gitlab.com/api/v4/projects/mariusandra%2Fhelloworldplugin/repository/archive.zip?sha=ff78cbe1d70316055c610a962a8355a4616d874b", headers={"Authorization": "Bearer MY_GITLAB_TOKEN"}, )
def test_download_plugin_archive_gitlab(self, mock_get): plugin_gitlab = download_plugin_archive( "https://www.gitlab.com/mariusandra/helloworldplugin/-/commit/ff78cbe1d70316055c610a962a8355a4616d874b", HELLO_WORLD_PLUGIN_GITLAB_ZIP[0], ) self.assertEqual(plugin_gitlab, base64.b64decode(HELLO_WORLD_PLUGIN_GITLAB_ZIP[1])) plugin_gitlab = download_plugin_archive( "https://www.gitlab.com/mariusandra/helloworldplugin/-/commit/ff78cbe1d70316055c610a962a8355a4616d874b?private_token=PRIVATE_TOKEN", HELLO_WORLD_PLUGIN_GITLAB_ZIP[0], ) self.assertEqual(plugin_gitlab, base64.b64decode(HELLO_WORLD_PLUGIN_GITLAB_ZIP[1]))
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
def test_download_plugin_archive_npm(self, mock_get): plugin_npm_tgz = download_plugin_archive( "https://www.npmjs.com/package/posthog-helloworld-plugin/v/0.0.0") self.assertEqual(plugin_npm_tgz, base64.b64decode(HELLO_WORLD_PLUGIN_NPM_TGZ[1])) self.assertEqual(mock_get.call_count, 1) mock_get.assert_called_with( "https://registry.npmjs.org/posthog-helloworld-plugin/-/posthog-helloworld-plugin-0.0.0.tgz", headers={}) plugin_npm_tgz = download_plugin_archive( "https://www.npmjs.com/package/@posthog/helloworldplugin/v/0.0.0?private_token=TOKEN" ) self.assertEqual(plugin_npm_tgz, base64.b64decode(HELLO_WORLD_PLUGIN_NPM_TGZ[1])) self.assertEqual(mock_get.call_count, 2) mock_get.assert_called_with( "https://registry.npmjs.org/@posthog/helloworldplugin/-/helloworldplugin-0.0.0.tgz", headers={"Authorization": "Bearer TOKEN"}, ) with self.settings(NPM_TOKEN="MY_NPM_TOKEN"): plugin_npm_tgz = download_plugin_archive( "https://www.npmjs.com/package/@posthog/helloworldplugin/v/0.0.0?private_token=TOKEN" ) self.assertEqual(plugin_npm_tgz, base64.b64decode(HELLO_WORLD_PLUGIN_NPM_TGZ[1])) self.assertEqual(mock_get.call_count, 3) mock_get.assert_called_with( "https://registry.npmjs.org/@posthog/helloworldplugin/-/helloworldplugin-0.0.0.tgz", headers={"Authorization": "Bearer TOKEN"}, ) plugin_npm_tgz = download_plugin_archive( "https://www.npmjs.com/package/@posthog/helloworldplugin/v/0.0.0" ) self.assertEqual(plugin_npm_tgz, base64.b64decode(HELLO_WORLD_PLUGIN_NPM_TGZ[1])) self.assertEqual(mock_get.call_count, 4) mock_get.assert_called_with( "https://registry.npmjs.org/@posthog/helloworldplugin/-/helloworldplugin-0.0.0.tgz", headers={"Authorization": "Bearer MY_NPM_TOKEN"}, )
def test_download_plugin_archive_npm(self, mock_get): plugin_npm_tgz = download_plugin_archive( "https://www.npmjs.com/package/posthog-helloworld-plugin/v/0.0.0") self.assertEqual(plugin_npm_tgz, base64.b64decode(HELLO_WORLD_PLUGIN_NPM_TGZ[1]))