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
Fix env var doc gen (#65230)
Jeff Bruemmere6a3974c9a70
diff --git a/src/metabase/cmd/common.clj b/src/metabase/cmd/common.cljnew file mode 100644index 00000000000..9d3672e4fd9--- /dev/null+++ b/src/metabase/cmd/common.clj@@ -0,0 +1,37 @@+(ns metabase.cmd.common+ "Common utilities for cmd namespace documentation generation."+ (:require+ [clojure.java.io :as io]))++(set! *warn-on-reflection* true)++(defn load-resource!+ "Loads a resource file with error handling.++ Returns the resource contents as a string. Throws an informative ex-info with the resource path+ if the resource is not found or cannot be read. This provides better error messages than+ bare `io/resource` which returns nil for missing resources."+ [path]+ (if-let [resource (io/resource path)]+ (try+ (slurp resource)+ (catch Exception e+ (throw (ex-info (str "Failed to read resource: " path)+ {:path path}+ e))))+ (throw (ex-info (str "Resource not found: " path)+ {:path path}))))++(defn write-doc-file!+ "Writes content to a file with error handling.++ Creates parent directories if they don't exist. Throws an informative exception if the write fails.+ Used to ensure documentation files can be written even when the directory structure doesn't exist yet."+ [path content]+ (try+ (io/make-parents path)+ (spit path content)+ (catch Exception e+ (throw (ex-info (str "Failed to write documentation file: " path)+ {:path path}+ e)))))diff --git a/src/metabase/cmd/config_file_gen.clj b/src/metabase/cmd/config_file_gen.cljindex 4a2816fa3cb..29a67fa18b9 100644--- a/src/metabase/cmd/config_file_gen.clj+++ b/src/metabase/cmd/config_file_gen.clj@@ -3,7 +3,7 @@The example config includes all configurable settings and their default values."(:require[clj-yaml.core :as yaml]- [clojure.java.io :as io]+ [metabase.cmd.common :as cmd.common][metabase.cmd.env-var-dox :as dox]));;;; Get settings and their default values@@ -65,9 +65,9 @@(defn- build-markdown-page"Take a YAML string and builds a Markdown page for docs on a configuration file."[config-yaml]- (str (slurp (io/resource markdown-intro))+ (str (cmd.common/load-resource! markdown-intro)config-yaml- (slurp (io/resource markdown-outro))))+ (cmd.common/load-resource! markdown-outro)))(defn- format-yaml"Takes configuration map that includes settings data and preps YAML for embedding in Markdown doc."@@ -79,8 +79,7 @@with settings and their default values."[yaml-template](-> yaml-template- (io/resource)- (slurp)+ (cmd.common/load-resource!)(yaml/parse-string)(add-settings)(format-yaml)@@ -98,5 +97,5 @@"Generates a configuration file doc with template and saves to docs directory."[](println "Creating config file doc with template...")- (spit (io/file config-file-path) (create-config-doc yaml-template))+ (cmd.common/write-doc-file! config-file-path (create-config-doc yaml-template))(println (str "Config doc file created: `" config-file-path "`.")))diff --git a/src/metabase/cmd/endpoint_dox.clj b/src/metabase/cmd/endpoint_dox.cljindex 9ddc61eda90..5213da8340b 100644--- a/src/metabase/cmd/endpoint_dox.clj+++ b/src/metabase/cmd/endpoint_dox.clj@@ -3,38 +3,63 @@clojure -M:ee:doc api-documentation"(:require- [clojure.java.io :as io][clojure.string :as str][metabase.api-routes.core][metabase.api.open-api]+ [metabase.cmd.common :as cmd.common][metabase.util.json :as json]))(set! *warn-on-reflection* true)+(defn- validate-single-placeholder+ "Validates that split-parts contains exactly 2 elements (indicating one placeholder was found).+ Throws an ex-info if validation fails."+ [template-path placeholder split-parts]+ (when-not (= 2 (count split-parts))+ (throw (ex-info (str "Template must contain exactly one " placeholder " placeholder")+ {:path template-path+ :placeholder placeholder+ :expected 1+ :found (dec (count split-parts))}))))++(defn- fill-template-placeholder+ "Loads a template resource and replaces a placeholder with content.+ Validates that exactly one instance of the placeholder exists in the template."+ [template-path placeholder content]+ (let [template (cmd.common/load-resource! template-path)+ split-parts (str/split template (re-pattern (str "\\Q" placeholder "\\E")))]+ (validate-single-placeholder template-path placeholder split-parts)+ (let [[before after] split-parts]+ (str before content after))))+(def ^:private scalar-config"See https://github.com/scalar/scalar/blob/main/documentation/configuration.md"{:servers [{:url "http://localhost:3000":description "Localhost"}]:info {:title "Metabase API documentation"- :description (slurp (io/resource "openapi/api-intro.md"))}})+ :description (cmd.common/load-resource! "openapi/api-intro.md")}})-(defn- openapi-object []+(defn- openapi-object+ "Generates the complete OpenAPI specification object by merging the core API routes with Scalar configuration."+ [](merge(metabase.api.open-api/root-open-api-object #'metabase.api-routes.core/routes)scalar-config))(defn generate-dox!- "Generates OpenAPI/Scalar documentation and write it to `docs/api.html`."+ "Generates OpenAPI/Scalar documentation and writes it to a file.++ With no arguments, writes to `docs/api.html`.+ With an output-file argument, writes to the specified path."([](generate-dox! "docs/api.html"))([^String output-file](printf "Generating OpenAPI+Scalar documentation in %s\n" output-file)- (let [index (slurp (io/resource "openapi/index_inline.html.mustache"))- [^String before ^String after] (str/split index #"\Q{{json}}\E")- openapi-object (openapi-object)]- (with-open [w (java.io.FileWriter. output-file)]- (.write w before)- (json/encode-to openapi-object w {:pretty true})- (.write w after)))+ (let [openapi-json (json/encode (openapi-object) {:pretty true})+ content (fill-template-placeholder+ "openapi/index_inline.html.mustache"+ "{{json}}"+ openapi-json)]+ (cmd.common/write-doc-file! output-file content))(println "Done.")))diff --git a/src/metabase/cmd/env_var_dox.clj b/src/metabase/cmd/env_var_dox.cljindex f9cdbcc2568..c89399b688b 100644--- a/src/metabase/cmd/env_var_dox.clj+++ b/src/metabase/cmd/env_var_dox.clj@@ -3,10 +3,10 @@docs by running: `clojure -M:ee:doc environment-variables-documentation`"(:require[clojure.java.classpath :as classpath]- [clojure.java.io :as io][clojure.string :as str][clojure.tools.namespace.find :as ns.find][clojure.tools.reader.edn :as edn]+ [metabase.cmd.common :as cmd.common][metabase.query-processor.middleware.constraints :as qp.constraints][metabase.settings.core :as setting][metabase.util :as u]))@@ -114,10 +114,14 @@""))(defn- format-doc- "Includes additional documentation for an environment variable, if it exists."+ "Includes additional documentation for an environment variable, if it exists.++ Returns the doc string if present, or nil otherwise. Filters out non-string values+ (like false) which are used as sentinel values to indicate settings should not be documented."[env-var]- (when-let [d (:doc env-var)]- d))+ (let [d (:doc env-var)]+ (when (string? d)+ d)))(defn- format-config-name"Formats the configuration file name for an environment variable."@@ -126,7 +130,7 @@""(str "[Configuration file name](./config-file.md): `" (:munged-name env-var) "`")))-(defn list-item+(defn- list-item"Create a list item for an entry, like `- Default: 100`."[entry](if (or (str/blank? entry)@@ -134,7 +138,7 @@""(str "- " entry)))-(defn format-list+(defn- format-list"Used to format metadata as a list."[entries](str/join "\n" (remove str/blank? (map list-item entries))))@@ -159,7 +163,7 @@(def env-vars-not-to-mess-with"Flamber advises that people avoid touching these environment variables."- (set (edn/read-string (slurp (io/resource "metabase/cmd/resources/env-vars-to-avoid.edn")))))+ (set (edn/read-string (cmd.common/load-resource! "metabase/cmd/resources/env-vars-to-avoid.edn"))))(defn- avoid?"Used to filter out environment variables with high foot-gun indices."@@ -201,12 +205,12 @@(defn- format-intro"Exists just so we can write the intro in Markdown."[]- (str (slurp "src/metabase/cmd/resources/env-var-intro.md") "\n\n"))+ (str (cmd.common/load-resource! "metabase/cmd/resources/env-var-intro.md") "\n\n"))(defn- non-defsetting-env-vars"Retrieves environment variables not specified via `defsetting`."[]- (str "\n\n" (slurp "src/metabase/cmd/resources/other-env-vars.md") "\n"))+ (str "\n\n" (cmd.common/load-resource! "metabase/cmd/resources/other-env-vars.md") "\n"))(defn prep-dox"Preps the environment variable docs for printing."@@ -219,5 +223,5 @@"Prints the generated environment variable docs to a file."[](println "Generating docs for environment variables...")- (spit (io/file "docs/configuring-metabase/environment-variables.md") (prep-dox))+ (cmd.common/write-doc-file! "docs/configuring-metabase/environment-variables.md" (prep-dox))(println "Done."))diff --git a/test/metabase/cmd/env_var_dox_test.clj b/test/metabase/cmd/env_var_dox_test.cljindex c9b862d277c..ea161910a38 100644--- a/test/metabase/cmd/env_var_dox_test.clj+++ b/test/metabase/cmd/env_var_dox_test.clj@@ -60,3 +60,29 @@(let [generated-docs (sut/format-env-var-docs settings-filtered)](is (= expected-docs(str/join "\n\n" generated-docs))))))++(deftest ^:parallel format-doc-test+ (testing "format-doc handles different doc values correctly"+ (testing "returns string doc values"+ (is (= "This is documentation"+ (#'sut/format-doc {:doc "This is documentation"}))))+ (testing "returns nil for false doc values (not a string)"+ (is (nil? (#'sut/format-doc {:doc false}))))+ (testing "returns nil for true doc values (not a string)"+ (is (nil? (#'sut/format-doc {:doc true}))))+ (testing "returns nil for nil doc values"+ (is (nil? (#'sut/format-doc {:doc nil}))))+ (testing "returns nil when doc key is missing"+ (is (nil? (#'sut/format-doc {}))))))++(deftest ^:parallel format-env-var-entry-with-false-doc-test+ (testing "format-env-var-entry doesn't crash when env-var has :doc false"+ (let [env-var-with-false-doc {:name :test-setting+ :munged-name "test-setting"+ :type :string+ :default "default-value"+ :description (constantly "Test description")+ :doc false+ :visibility :public}]+ (is (string? (#'sut/format-env-var-entry env-var-with-false-doc)))+ (is (str/includes? (#'sut/format-env-var-entry env-var-with-false-doc) "MB_TEST_SETTING")))))