The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
(ns metabase-enterprise.custom-viz-plugin.test-util
"Shared helpers for custom-viz-plugin tests: building in-memory tar+gzip
archives that match the format `validate-bundle!` expects."
(:require
[buddy.core.codecs :as codecs]
[buddy.core.hash :as buddy-hash]
[metabase.util.json :as json])
(:import
(java.io ByteArrayOutputStream)
(org.apache.commons.compress.archivers.tar TarArchiveEntry TarArchiveOutputStream)
(org.apache.commons.compress.compressors.gzip GzipCompressorOutputStream)))
(set! *warn-on-reflection* true)
(defn make-tgz-bytes
"Build a tar.gz archive in memory from a seq of `[name content]` pairs.
`content` may be a String or a byte array."
^bytes [entries]
;; The gzip/tar streams must be closed (not just finished) before the byte
;; array is final — gzip only emits its trailer on close. Keep `baos` out
;; of `with-open` so we can still read from it after the streams close.
(let [baos (ByteArrayOutputStream.)]
(with-open [gz (GzipCompressorOutputStream. baos)
tar (TarArchiveOutputStream. gz)]
(doseq [[name content] entries
:let [^bytes bs (if (bytes? content) content (.getBytes (str content) "UTF-8"))
entry (doto (TarArchiveEntry. ^String name)
(.setSize (alength bs)))]]
(.putArchiveEntry tar entry)
(.write tar bs 0 (alength bs))
(.closeArchiveEntry tar))
(.finish tar))
(.toByteArray baos)))
(defn valid-bundle-bytes
"Build a minimal valid plugin tar.gz archive: manifest at the root with the
given `identifier` as `name`, a trivial `dist/index.js`, and optional
`:icon` / `:metabase-version` / `:sdk-version` overrides merged into the
manifest."
^bytes [identifier & [{:keys [icon metabase-version sdk-version]}]]
(let [manifest (cond-> {:name identifier}
icon (assoc :icon icon)
metabase-version (assoc-in [:metabase :version] metabase-version)
sdk-version (assoc-in [:sdk :version] sdk-version))]
(make-tgz-bytes
[["metabase-plugin.json" (json/encode manifest)]
["dist/index.js" "console.log('hi')"]])))
(defn bundle-hash
"The sha256 hex of `bundle-bytes`, as stored in `custom_viz_plugin.bundle_hash`."
^String [^bytes bundle-bytes]
(-> bundle-bytes buddy-hash/sha256 codecs/bytes->hex))