Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Generate backend CODEOWNERS from module ownership (#85953)

Chris Truterdf4fe58e396c
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index d677e7d341f..ac482923c37 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -12,6 +12,24 @@
;; The team that owns this module. This has to match one of the team names in `.github/team.json` EXACTLY. You can
;; run [[metabase.core.modules-test/all-modules-have-teams-test]] to make sure you did this correctly.
;;
+;; ## `codeowners`:
+;;
+;; Set it to `true` to make the module's team a required reviewer for the module's src and test code in
+;; `.github/CODEOWNERS`. Ownership goes to the team's `codeowner` handle in `.github/team.json`, which the team must
+;; have. Run `./bin/mage update-codeowners` after changing it. Leave it out for modules whose team has not agreed to
+;; review them.
+;;
+;; It is not inherited, but it covers the module's directories. A nested module inside them that does not set it keeps
+;; its own team if an opted-in module around it has that team, and is left unowned otherwise. A nested module with an
+;; `:ns-prefix` outside its parent's directories needs its own, and so does an `enterprise/X` module: it is `X`'s child
+;; but lives under `enterprise/backend`, as `enterprise/search` does beside `search`. Plugin code under
+;; `modules/<plugin>/src` is not supported: give it an owner by hand.
+;;
+;; search
+;; {:team "Metabot"
+;; :codeowners true
+;; ...}
+;;
;; ## `api`:
;;
;; Map of module name => the "core" external public-facing API namespace(s). You have three options here:
@@ -865,6 +883,8 @@
:model/Query
:model/User}}
+ ;; TODO (Chris 2026-10-30) -- CODEOWNERS gives its src to the @metabase/data-apps reviewer group by hand.
+ ;; Once :team and that group cover the same people, delete those lines and set :codeowners, which also owns tests.
data-apps
{:team "Embedding"
:api #{metabase.data-apps.core metabase.data-apps.schema}
@@ -901,6 +921,8 @@
:api #{metabase.database-routing.core}
:uses #{lib.schema premium-features}}
+ ;; TODO (Chris 2026-10-30) -- CODEOWNERS gives its src to the @metabase/data-apps reviewer group by hand.
+ ;; Once :team and that group cover the same people, delete those lines and set :codeowners, which also owns tests.
dependencies
{:team "Graphy"
:api #{metabase.dependencies.hooks}
@@ -2415,6 +2437,8 @@
:api #{metabase.pivot.core metabase.pivot.postprocess}
:uses #{models util}}
+ ;; TODO (Chris 2026-10-30) -- CODEOWNERS gives its src to the @metabase/data-apps reviewer group by hand.
+ ;; Once :team and that group cover the same people, delete those lines and set :codeowners, which also owns tests.
remote-sync
{:team "UX West"
:api #{metabase.remote-sync.core
@@ -2521,6 +2545,7 @@
search
{:team "Metabot"
+ :codeowners true
:module-exports #{search.embeddings
search.entity-retrieval}
:api #{metabase.search.api
@@ -3842,6 +3867,8 @@
util.config}
:model-exports #{:model/CustomVizPlugin}}
+ ;; TODO (Chris 2026-10-30) -- CODEOWNERS gives its src to the @metabase/data-apps reviewer group by hand.
+ ;; Once :team and that group cover the same people, delete those lines and set :codeowners, which also owns tests.
enterprise/data-apps
{:team "Embedding"
:api #{metabase-enterprise.data-apps.api metabase-enterprise.data-apps.init}
@@ -3919,6 +3946,8 @@
:model-exports #{:model/DatabaseRouter}
:model-imports #{:model/Database :model/Transform}}
+ ;; TODO (Chris 2026-10-30) -- CODEOWNERS gives its src to the @metabase/data-apps reviewer group by hand.
+ ;; Once :team and that group cover the same people, delete those lines and set :codeowners, which also owns tests.
enterprise/dependencies
{:team "Graphy"
:module-exports #{enterprise/dependencies.replacement}
@@ -4308,6 +4337,8 @@
:ns-prefix "metabase-enterprise.dashboard-subscription-filters"
:uses #{premium-features}}
+ ;; TODO (Chris 2026-10-30) -- CODEOWNERS gives its src to the @metabase/data-apps reviewer group by hand.
+ ;; Once :team and that group cover the same people, delete those lines and set :codeowners, which also owns tests.
enterprise/remote-sync
{:team "UX West"
:api #{metabase-enterprise.remote-sync.api
@@ -4355,6 +4386,7 @@
enterprise/search
{:team "Metabot"
+ :codeowners true
:module-exports #{enterprise/search.embeddings
enterprise/search.entity-retrieval
enterprise/search.semantic}
diff --git a/.clj-kondo/src/hooks/common/modules.clj b/.clj-kondo/src/hooks/common/modules.clj
index 13166af48a0..961387d6e92 100644
--- a/.clj-kondo/src/hooks/common/modules.clj
+++ b/.clj-kondo/src/hooks/common/modules.clj
@@ -34,6 +34,16 @@
(or (get-in modules [module :ns-prefix])
(default-ns-prefix module)))
+(defn module-dir
+ "Repo-relative directory holding `module`'s namespaces in `tree`, which is `\"src\"` or `\"test\"`.
+ Examples: `src/metabase/lib_be` for `lib-be`, `enterprise/backend/test/metabase_enterprise/sso` for `enterprise/sso`.
+ Plugin code under `modules/<plugin>/src` is not covered: the result there is a directory that does not exist."
+ [modules module tree]
+ (let [ns-prefix (module-ns-prefix modules module)]
+ (str (when (str/starts-with? ns-prefix "metabase-enterprise.") "enterprise/backend/")
+ tree "/"
+ (-> ns-prefix (str/replace "." "/") (str/replace "-" "_")))))
+
(defn build-prefix->module
"Map each declared namespace prefix to its module, for [[declared-module]]."
[modules]
diff --git a/.clj-kondo/test/hooks/common/modules_test.clj b/.clj-kondo/test/hooks/common/modules_test.clj
index cba7f15f10d..c12a8a4df29 100644
--- a/.clj-kondo/test/hooks/common/modules_test.clj
+++ b/.clj-kondo/test/hooks/common/modules_test.clj
@@ -8,3 +8,15 @@
(hooks.common.modules/module {} symb))
'metabase.qp.middleware.wow 'qp
'metabase-enterprise.whatever.core 'enterprise/whatever))
+
+(deftest ^:parallel module-dir-test
+ (testing "default prefixes, OSS and enterprise"
+ (are [module tree expected] (= expected (hooks.common.modules/module-dir {} module tree))
+ 'lib-be "src" "src/metabase/lib_be"
+ 'lib.schema "test" "test/metabase/lib/schema"
+ 'enterprise/sso "src" "enterprise/backend/src/metabase_enterprise/sso"))
+ (testing "a custom :ns-prefix"
+ (is (= "enterprise/backend/test/metabase_enterprise/y_z"
+ (hooks.common.modules/module-dir '{enterprise/x {:ns-prefix "metabase-enterprise.y-z"}}
+ 'enterprise/x
+ "test")))))
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index fa113d339f8..c9ed8e1e7c9 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -1,6 +1,9 @@
# Before changing this file, make sure you have read how we use it:
# https://www.notion.so/metabase/Pull-Request-Code-Review-Guide-374f05df889748b69d67af753f9bc9b8?pvs=4#36550c04ceab40b5a8b8bee6264b090c
+# TODO (Chris 2026-10-30) -- Frontend ownership below is hand-written.
+# Its module map in frontend/lint/module-boundaries.mjs could carry each element's team and feed a generated block.
+
# @metabase/querying-platform
# Not enforced: uncomment these lines once the team agrees to review these paths.
# src/metabase/lib @metabase/querying-platform
@@ -56,18 +59,13 @@ docs/embedding/eajs/snippets # auto-generated
# @metabase/cloud-ops
src/metabase/analytics/prometheus.clj @metabase/cloud-ops
-# @metabase/metabot-frontend
-enterprise/frontend/src/metabase-enterprise/metabot @metabase/metabot-frontend
-frontend/src/metabase/metabot @metabase/metabot-frontend
-
-# @metabase/metabot-backend
-enterprise/backend/src/metabase_enterprise/search @metabase/metabot-backend
-enterprise/backend/test/metabase_enterprise/search @metabase/metabot-backend
-frontend/src/metabase-types/api/search.ts @metabase/metabot-backend
-frontend/src/metabase/api/search.ts @metabase/metabot-backend
-modules/embedder @metabase/metabot-backend
-src/metabase/search @metabase/metabot-backend
-test/metabase/search @metabase/metabot-backend
+# @metabase/metabot
+# Backend modules are in the generated block below.
+enterprise/frontend/src/metabase-enterprise/metabot @metabase/metabot
+frontend/src/metabase/metabot @metabase/metabot
+frontend/src/metabase-types/api/search.ts @metabase/metabot
+frontend/src/metabase/api/search.ts @metabase/metabot
+modules/embedder @metabase/metabot
# @metabase/gadget
# Not enforced: uncomment these lines once the team agrees to review these paths.
@@ -83,14 +81,39 @@ test/metabase/search @metabase/metabot-backend
# .github/workflows/release-custom-viz.yml @metabase/devex @metabase/gadget
# @metabase/data-apps
-enterprise/backend/src/metabase_enterprise/data_apps @metabase/data-apps
-enterprise/backend/src/metabase_enterprise/dependencies @metabase/data-apps
-enterprise/backend/src/metabase_enterprise/remote_sync @metabase/data-apps
+# TODO (Chris 2026-10-30) -- This reviewer group spans teams, so these lines stay hand-written.
+# Each backend line names its module and the :team the module config gives it.
+# Once each module's :team and this group cover the same people, delete these lines and opt the modules in.
+enterprise/backend/src/metabase_enterprise/data_apps @metabase/data-apps # enterprise/data-apps (Embedding)
+enterprise/backend/src/metabase_enterprise/dependencies @metabase/data-apps # enterprise/dependencies (Graphy)
+enterprise/backend/src/metabase_enterprise/remote_sync @metabase/data-apps # enterprise/remote-sync (UX West)
enterprise/frontend/src/metabase-enterprise/data-apps @metabase/data-apps
enterprise/frontend/src/metabase-enterprise/dependencies @metabase/data-apps
enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics @metabase/data-apps
enterprise/frontend/src/metabase-enterprise/remote_sync @metabase/data-apps
frontend/src/metabase/monitor/dependency-diagnostics @metabase/data-apps
-src/metabase/data_apps @metabase/data-apps
-src/metabase/dependencies @metabase/data-apps
-src/metabase/remote_sync @metabase/data-apps
+src/metabase/data_apps @metabase/data-apps # data-apps (Embedding)
+src/metabase/dependencies @metabase/data-apps # dependencies (Graphy)
+src/metabase/remote_sync @metabase/data-apps # remote-sync (UX West)
+
+# ===== BEGIN generated module ownership (./bin/mage update-codeowners) =====
+#
+# Generated by `./bin/mage update-codeowners` from .clj-kondo/config/modules/config.edn. Do not edit by hand.
+# The `codeowners` section at the top of that file says which modules are owned, and by whom.
+# Each team's handle is its "codeowner" in .github/team.json.
+# Exceptions inside these paths go below the END marker: lines above this block lose to it.
+#
+
+# enterprise/search (Metabot)
+enterprise/backend/src/metabase_enterprise/search @metabase/metabot
+enterprise/backend/src/metabase_enterprise/search.* @metabase/metabot
+enterprise/backend/test/metabase_enterprise/search @metabase/metabot
+enterprise/backend/test/metabase_enterprise/search_test.* @metabase/metabot
+
+# search (Metabot)
+src/metabase/search @metabase/metabot
+src/metabase/search.* @metabase/metabot
+test/metabase/search @metabase/metabot
+test/metabase/search_test.* @metabase/metabot
+
+# ===== END generated module ownership =====
diff --git a/.github/file-paths.yaml b/.github/file-paths.yaml
index 595940c9dc6..af1b41e7ee3 100644
--- a/.github/file-paths.yaml
+++ b/.github/file-paths.yaml
@@ -217,6 +217,16 @@ project_ratchet_checks:
- ".clj-kondo/ratchets-test.edn"
- ".clj-kondo/config/modules/ratchets.edn"
+# Inputs of the generated CODEOWNERS block.
+# Mage Tests run on every PR and catch a change that stops mage.modules from loading.
+project_codeowners_checks:
+ - *project_tests_infra
+ - ".clj-kondo/config/modules/config.edn"
+ - ".clj-kondo/src/hooks/common/modules.clj"
+ - ".github/CODEOWNERS"
+ - ".github/team.json"
+ - "mage/src/mage/modules.clj"
+
project_migration_checks:
- *project_tests_infra
- "bin/lint-migrations-file/**"
diff --git a/.github/scripts/file-paths.unit.spec.ts b/.github/scripts/file-paths.unit.spec.ts
index 39a86fa8d8a..3fa6305b87a 100644
--- a/.github/scripts/file-paths.unit.spec.ts
+++ b/.github/scripts/file-paths.unit.spec.ts
@@ -216,6 +216,23 @@ describe("file-paths.yaml", () => {
).toBe(true);
});
+ it.each([
+ ".clj-kondo/config/modules/config.edn",
+ ".clj-kondo/src/hooks/common/modules.clj",
+ ".github/CODEOWNERS",
+ ".github/team.json",
+ "mage/src/mage/modules.clj",
+ ])("checks the generated CODEOWNERS block when %s changes", (path) => {
+ expect(matches("project_codeowners_checks", path)).toBe(true);
+ });
+
+ it.each(["src/metabase/search/core.clj", "dev/src/dev/module_explorer.clj"])(
+ "does not check the CODEOWNERS block when %s changes",
+ (path) => {
+ expect(matches("project_codeowners_checks", path)).toBe(false);
+ },
+ );
+
it.each([
"loki.config.js",
".storybook/story-files.cjs",
diff --git a/.github/team.json b/.github/team.json
index 335acb85989..5562628a816 100644
--- a/.github/team.json
+++ b/.github/team.json
@@ -55,6 +55,7 @@
{
"name": "Metabot",
"label": ".Team/Metabot",
+ "codeowner": "@metabase/metabot",
"members": [
"retro",
"sloansparger",
diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml
index 4269d3fc146..4963624cc58 100644
--- a/.github/workflows/backend.yml
+++ b/.github/workflows/backend.yml
@@ -18,7 +18,7 @@ jobs:
gate:
uses: ./.github/workflows/test.gate.yml
with:
- filters: backend_all project_backend_checks project_migration_checks project_ratchet_checks
+ filters: backend_all project_backend_checks project_codeowners_checks project_migration_checks project_ratchet_checks
# Generated static-viz output can shift without any backend source changing, so the suite follows
# it too. Only worth probing when nothing else has already decided the suite.
@@ -82,9 +82,21 @@ jobs:
|| contains(fromJSON(needs.gate.outputs.matched), 'project_ratchet_checks') }}
run: ./bin/mage kondo-ratchets
+ # Only a checkout is needed: bin/mage fetches babashka, and the runner's Java resolves bb.edn's deps.
+ codeowners-check:
+ name: 'Project tests: CODEOWNERS'
+ needs: gate
+ if: ${{ contains(fromJSON(needs.gate.outputs.matched), 'project_codeowners_checks') }}
+ runs-on: ${{ vars.DEFAULT_RUNNER_KEY }}
+ timeout-minutes: 10
+ steps:
+ - uses: actions/checkout@v7
+ - name: Check the generated CODEOWNERS block
+ run: ./bin/mage update-codeowners --check
+
project-tests:
name: Project tests
- needs: project-checks
+ needs: [project-checks, codeowners-check]
if: ${{ !cancelled() }}
runs-on: ${{ vars.SLIM_RUNNER_KEY }}
timeout-minutes: 5
diff --git a/bb.edn b/bb.edn
index 26d9a220c62..8be54cbaa91 100644
--- a/bb.edn
+++ b/bb.edn
@@ -315,6 +315,14 @@
:requires [[mage.modules]]
:task (task! (mage.modules/cli-print-affected-modules (:arguments parsed)))}
+ update-codeowners
+ {:doc "Rewrite the module-ownership block in .github/CODEOWNERS from the modules that set :codeowners"
+ :examples [["./bin/mage update-codeowners" "Rewrite the generated block in .github/CODEOWNERS"]
+ ["./bin/mage update-codeowners --check" "Exit non-zero if the block is out of date (for CI)"]]
+ :options [[nil "--check" "Exit non-zero if CODEOWNERS is out of date instead of rewriting it"]]
+ :requires [[mage.modules]]
+ :task (task! (mage.modules/cli-update-codeowners parsed))}
+
modules-tree
{:doc "Print the module hierarchy, nesting enterprise extensions under their OSS module"
:examples [["./bin/mage modules-tree" "Print the full tree; * marks a custom :ns-prefix"]
diff --git a/dev/src/dev/deps_graph.clj b/dev/src/dev/deps_graph.clj
index d2df258158b..9ae71ed856c 100644
--- a/dev/src/dev/deps_graph.clj
+++ b/dev/src/dev/deps_graph.clj
@@ -176,7 +176,8 @@
[:friends {:optional true} [:set :symbol]]
[:model-exports {:optional true} [:or [:= :any] [:set :keyword]]]
[:model-imports {:optional true} [:or [:= :bypass] [:set :keyword]]]
- [:module-exports {:optional true} [:set :symbol]]])
+ [:module-exports {:optional true} [:set :symbol]]
+ [:codeowners {:optional true} [:= true]]])
(def ^:private ModulesConfig
[:map-of :symbol ModuleConfig])
@@ -446,7 +447,8 @@
([deps]
(let [kondo-config (kondo-config)]
(-> (ddiff/diff
- (update-vals kondo-config #(dissoc % :team :friends :model-imports :model-exports :module-exports :ns-prefix))
+ (update-vals kondo-config
+ #(dissoc % :team :codeowners :friends :model-imports :model-exports :module-exports :ns-prefix))
(generate-config deps kondo-config))
ddiff/minimize
kondo-config-diff-ignore-any
@@ -695,12 +697,6 @@
(def ^:private test-source-file-extensions
[".clj" ".cljc" ".cljs" ".bb"])
-(defn- module->test-path-prefix [modules-config module]
- (let [ns-prefix (modules/module-ns-prefix modules-config module)]
- (str (when (str/starts-with? ns-prefix "metabase-enterprise.") "enterprise/backend/")
- "test/"
- (-> ns-prefix (str/replace "." "/") (str/replace "-" "_")))))
-
(defn- existing-test-file-paths [path-prefix]
(into (sorted-set)
(keep (fn [extension]
@@ -717,7 +713,7 @@
([modules-config :- ModulesConfig
prefix->module :- PrefixToModule
module-sym :- :symbol]
- (let [path-prefix (module->test-path-prefix modules-config module-sym)
+ (let [path-prefix (modules/module-dir modules-config module-sym "test")
test-dir (io/file path-prefix)
nested-tests (when (.isDirectory test-dir)
(into
diff --git a/dev/src/dev/module_explorer.clj b/dev/src/dev/module_explorer.clj
index 39b9bce4499..1ea6ef1b38a 100644
--- a/dev/src/dev/module_explorer.clj
+++ b/dev/src/dev/module_explorer.clj
@@ -38,13 +38,6 @@
(when (and prefix (not= prefix (modules/default-ns-prefix module)))
prefix)))
-(defn- ns-prefix->source-dir
- "Repo-relative source directory for a namespace prefix: `metabase.lib-be` -> `src/metabase/lib_be`."
- [ns-prefix]
- (str (when (str/starts-with? ns-prefix "metabase-enterprise.") "enterprise/backend/")
- "src/"
- (-> ns-prefix (str/replace "." "/") (str/replace "-" "_"))))
-
(defn- set->strings
"Sort and stringify a set-valued config field."
[x]
@@ -94,7 +87,7 @@
"One module's config as the plain data the page consumes."
[modules-config module->uses used-by module]
(let [{:keys [api uses friends] :as entry} (get modules-config module)
- source-dir (ns-prefix->source-dir (modules/module-ns-prefix modules-config module))]
+ source-dir (modules/module-dir modules-config module "src")]
{:id (str module)
:enterprise (= (namespace module) "enterprise")
:team (modules/module-team modules-config module)
diff --git a/mage/src/mage/modules.clj b/mage/src/mage/modules.clj
index 4570e866325..c18cb63776c 100644
--- a/mage/src/mage/modules.clj
+++ b/mage/src/mage/modules.clj
@@ -1,5 +1,6 @@
(ns mage.modules
(:require
+ [cheshire.core :as json]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.set :as set]
@@ -63,15 +64,9 @@
(def ^:private backend-test-source-file-extensions
[".clj" ".cljc"])
-(defn- module->test-path-prefix [modules-config module]
- (let [ns-prefix (modules/module-ns-prefix modules-config module)]
- (str (when (str/starts-with? ns-prefix "metabase-enterprise.") "enterprise/backend/")
- "test/"
- (-> ns-prefix (str/replace "." "/") (str/replace "-" "_")))))
-
(defn- module->test-paths [modules-config module]
(let [prefix->module (modules/build-prefix->module modules-config)
- path-prefix (module->test-path-prefix modules-config module)
+ path-prefix (modules/module-dir modules-config module "test")
test-dir (io/file path-prefix)
test-files (concat
(for [extension backend-test-source-file-extensions
@@ -270,6 +265,281 @@
(flush)
(u/exit 0)))
+;;;; =============================================================================
+;;;; CODEOWNERS
+;;;; =============================================================================
+
+;;;; The generated block in `.github/CODEOWNERS` sits between BEGIN/END markers.
+;;;; Its lines come from [[codeowners-entries]].
+;;;; Its text depends only on config.edn and team.json; the one disk read refuses an opted-in module with no code.
+;;;; Lines outside the markers are hand-maintained.
+
+(def ^:private codeowners-path ".github/CODEOWNERS")
+(def ^:private team-config-path ".github/team.json")
+
+(def ^:private codeowners-begin-marker
+ "# ===== BEGIN generated module ownership (./bin/mage update-codeowners) =====")
+
+(def ^:private codeowners-end-marker
+ "# ===== END generated module ownership =====")
+
+(def ^:private tree->root-file-suffix
+ "Each tree an opted-in module owns => the suffix added to the module's directory to match its root namespace file.
+ Examples: `src/metabase/driver.*` and `test/metabase/driver_test.*`."
+ (array-map "src" ".*"
+ "test" "_test.*"))
+
+(defn- read-team->codeowner
+ "Team name => the `codeowner` value in team.json, for each team that has the field, whatever its value."
+ []
+ (into {}
+ (keep (fn [{:keys [name] :as team}]
+ (when (contains? team :codeowner)
+ [name (:codeowner team)])))
+ (:teams (json/parse-string (slurp team-config-path) true))))
+
+(defn- valid-codeowner?
+ "Whether `value` is one GitHub handle, such as `@metabase/metabot` or `@someone`, and nothing else."
+ [value]
+ (and (string? value)
+ (some? (re-matches #"@[A-Za-z0-9-]+(?:/[A-Za-z0-9._-]+)?" value))))
+
+(defn- has-source?
+ "Whether `src-dir` exists as a directory, or a root namespace file sits next to where it would be."
+ [src-dir]
+ (or (.isDirectory (io/file ^String src-dir))
+ (some #(.isFile (io/file (str src-dir %))) [".clj" ".cljc"])))
+
+(defn- path-within?
+ "Whether `path` is `dir` or sits inside it."
+ [dir path]
+ (or (= dir path)
+ (str/starts-with? path (str dir "/"))))
+
+(defn- opts-in?
+ "Whether `module` sets `:codeowners true`."
+ [modules-config module]
+ (true? (get-in modules-config [module :codeowners])))
+
+(defn- codeowners-entries
+ "One entry per module the block must name, in block order.
+ Each is `{:module, :src-dir, :team, :handle, :opted-in?, :patterns}`.
+ A module's src and test code go to its team's handle when it, or an opted-in module around it, belongs to that team.
+ Inside an opted-in module's directory, a nested module of another team is otherwise unowned, with a `nil` handle.
+ An opted-in module always gets an entry.
+ Another module gets one only where the nearest entry around it would give its code a different owner.
+ Only `:codeowners true` opts in; [[codeowners-config-problems]] reports any other value."
+ [modules-config team->codeowner]
+ ;; config.edn's `codeowners` section states this rule for module authors; change the two together.
+ (reduce
+ (fn [entries module]
+ (let [team (modules/module-team modules-config module)
+ src-dir (modules/module-dir modules-config module "src")
+ ;; Entries come parent first, so the last one around `src-dir` is the nearest.
+ around (filter #(path-within? (:src-dir %) src-dir) entries)
+ owners (filter :opted-in? around)
+ opted-in? (opts-in? modules-config module)
+ handle (when (or opted-in? (some #(= team (:team %)) owners))
+ (get team->codeowner team))]
+ (cond-> entries
+ (or opted-in?
+ (and (seq owners) (not= handle (:handle (last around)))))
+ (conj {:module module
+ :src-dir src-dir
+ :team team
+ :handle handle
+ :opted-in? opted-in?
+ :patterns (for [[tree suffix] tree->root-file-suffix
+ :let [dir (modules/module-dir modules-config module tree)]
+ pattern [dir (str dir suffix)]]
+ pattern)}))))
+ []
+ ;; CODEOWNERS applies the last matching pattern, so a nested module's entry must follow its parent's. Sorting by
+ ;; source path does that, and the test tree mirrors the source tree.
+ (sort-by #(modules/module-dir modules-config % "src") (keys modules-config))))
+
+(defn- codeowners-config-problems
+ "Problems with the modules that set `:codeowners`, as maps keyed by `:type`, all of them in one run.
+ Each module problem has `:module`.
+ An `:unknown-value` adds `:value`, a `:no-codeowner` adds `:team`, and a `:no-source` adds `:src-dir`.
+ A team whose codeowner is not one GitHub handle gives a single `:bad-codeowner` (`:team`, `:value`, `:modules`).
+ The predicate `source?` takes a module's src directory and says whether its code is there."
+ [modules-config team->codeowner source?]
+ (let [modules (for [[module cfg] (sort-by key modules-config)
+ :when (contains? cfg :codeowners)]
+ module)]
+ (concat
+ (for [module modules
+ :let [team (modules/module-team modules-config module)
+ src-dir (modules/module-dir modules-config module "src")]
+ problem (cond-> []
+ (not (opts-in? modules-config module))
+ (conj {:type :unknown-value, :module module, :value (get-in modules-config [module :codeowners])})
+
+ (not (contains? team->codeowner team))
+ (conj {:type :no-codeowner, :module module, :team team})
+
+ (not (source? src-dir))
+ (conj {:type :no-source, :module module, :src-dir src-dir}))]
+ problem)
+ (for [[team team-modules] (sort-by key (group-by #(modules/module-team modules-config %) modules))
+ :let [handle (get team->codeowner team)]
+ :when (and (contains? team->codeowner team) (not (valid-codeowner? handle)))]
+ {:type :bad-codeowner, :team team, :value handle, :modules (vec team-modules)}))))
+
+(defn- pattern-covers?
+ "Whether CODEOWNERS `pattern`, a directory or a `<path>.*` root-file glob from the block, matches `path`."
+ [pattern path]
+ (if (str/ends-with? pattern ".*")
+ (let [stem (subs pattern 0 (dec (count pattern)))]
+ (and (str/starts-with? path stem)
+ (not (str/includes? (subs path (count stem)) "/"))))
+ (path-within? pattern path)))
+
+(defn- shadowed-lines
+ "Rule lines above the generated block in `existing` whose path falls inside one of `patterns`.
+ With no block, every line counts as above it.
+ Paths with wildcards are compared as literal text, so they are found only when the text matches."
+ [existing patterns]
+ (let [above (subs existing 0 (or (str/index-of existing codeowners-begin-marker) (count existing)))]
+ (for [line (str/split-lines above)
+ :let [path (some-> (re-find #"^\s*([^#\s]\S*)" line) second (str/replace #"^/" ""))]
+ :when (and path (some #(pattern-covers? % path) patterns))]
+ (str/trim line))))
+
+(defn- markers-problem
+ "A `:broken-markers` problem (`:begin`, `:end`), or nil when the markers are sound.
+ Sound means no marker at all, or each marker exactly once with BEGIN before END."
+ [existing]
+ (let [begin (str/index-of existing codeowners-begin-marker)
+ end (str/index-of existing codeowners-end-marker)]
+ (when-not (or (= nil begin end)
+ (and begin
+ end
+ ;; A marker is there once when its first and last positions match.
+ (= begin (str/last-index-of existing codeowners-begin-marker))
+ (= end (str/last-index-of existing codeowners-end-marker))
+ (< begin end)))
+ {:type :broken-markers, :begin begin, :end end})))
+
+(defn- codeowners-file-problems
+ "Problems with the `existing` CODEOWNERS text for the block's `entries`, as maps keyed by `:type`.
+ Broken markers give a [[markers-problem]].
+ Otherwise, lines above the block inside its paths give a `:shadowed-lines` problem listing them under `:lines`.
+ Lines are not checked while the markers are broken, since the block's position is then unknown."
+ [existing entries]
+ (if-let [markers (markers-problem existing)]
+ [markers]
+ (when-let [shadowed (seq (shadowed-lines existing (mapcat :patterns entries)))]
+ [{:type :shadowed-lines, :lines (vec shadowed)}])))
+
+(defn- codeowners-problem-message
+ "One problem from [[codeowners-config-problems]] or [[codeowners-file-problems]] as a sentence saying what to fix."
+ [{:keys [module] :as problem}]
+ (case (:type problem)
+ :unknown-value (format "Module %s sets :codeowners to %s. Use true, or leave it out."
+ module (pr-str (:value problem)))
+ :no-codeowner (format "Module %s sets :codeowners, but its team %s has no \"codeowner\" in %s."
+ module (pr-str (:team problem)) team-config-path)
+ :bad-codeowner (format (str "Team %s has \"codeowner\" %s in %s. Use one GitHub handle, such as"
+ " \"@metabase/metabot\", so that %s can opt in.")
+ (pr-str (:team problem)) (pr-str (:value problem)) team-config-path
+ (str/join ", " (:modules problem)))
+ :no-source (format (str "Module %s sets :codeowners, but has no code at %s. The generator does not handle"
+ " plugin code under modules/<plugin>/src; give that directory an owner by hand.")
+ module (:src-dir problem))
+ :shadowed-lines (str "These lines in " codeowners-path " sit above the generated block but inside paths it owns,"
+ " so the block overrides them. Move them below the END marker:\n "
+ (str/join "\n " (:lines problem)))
+ :broken-markers (str codeowners-path " has a broken generated block. It needs one BEGIN marker and one END"
+ " marker, in that order. Fix the markers by hand.")))
+
+(defn- codeowners-stanza-lines
+ "Lines for one entry: a `# module (team)` header, with `, not opted in` after the team when it did not opt in.
+ Then one `pattern handle` line per pattern, or the bare pattern when the entry is unowned."
+ [{:keys [module team handle opted-in? patterns]}]
+ (into [(format "# %s (%s%s)" module team (if opted-in? "" ", not opted in"))]
+ (map #(cond-> % handle (str " " handle)))
+ patterns))
+
+(defn- codeowners-block
+ "Generated block as one string, marker to marker, rendering `entries` in order."
+ [entries]
+ (str/join
+ "\n"
+ (concat
+ [codeowners-begin-marker
+ "#"
+ "# Generated by `./bin/mage update-codeowners` from .clj-kondo/config/modules/config.edn. Do not edit by hand."
+ "# The `codeowners` section at the top of that file says which modules are owned, and by whom."
+ "# Each team's handle is its \"codeowner\" in .github/team.json."
+ "# Exceptions inside these paths go below the END marker: lines above this block lose to it."
+ "#"]
+ (mapcat #(cons "" (codeowners-stanza-lines %)) entries)
+ [""
+ codeowners-end-marker])))
+
+(defn- splice-codeowners
+ "Replace the marked region (inclusive) in `existing` with `block`, or append `block` when no marker is there.
+ Returns text ending in one newline.
+ Throws on a [[markers-problem]]."
+ [existing block]
+ ;; Callers check the markers first, but splicing a bad file would put the block in the wrong place.
+ (when-let [problem (markers-problem existing)]
+ (throw (ex-info (codeowners-problem-message problem) problem)))
+ (if-let [begin (str/index-of existing codeowners-begin-marker)]
+ (let [end (str/index-of existing codeowners-end-marker)]
+ (str (str/trimr (subs existing 0 begin))
+ "\n\n" block
+ (str/trimr (subs existing (+ end (count codeowners-end-marker))))
+ "\n"))
+ (str (str/trimr existing) "\n\n" block "\n")))
+
+(defn- codeowners-outcome
+ "What `update-codeowners` should do with the `existing` CODEOWNERS text, as a map keyed by `:status`.
+ The statuses are `:problems`, `:up-to-date`, `:stale` (only with `check?`) and `:write`.
+ A `:problems` outcome carries `:problems`, and a `:write` outcome carries the `:desired` text."
+ [existing modules-config team->codeowner source? check?]
+ (let [entries (codeowners-entries modules-config team->codeowner)
+ problems (into (vec (codeowners-config-problems modules-config team->codeowner source?))
+ (codeowners-file-problems existing entries))
+ desired (when (empty? problems)
+ (splice-codeowners existing (codeowners-block entries)))]
+ (cond
+ (seq problems) {:status :problems, :problems problems}
+ (= existing desired) {:status :up-to-date}
+ check? {:status :stale}
+ :else {:status :write, :desired desired})))
+
+(defn cli-update-codeowners
+ "Regenerate the module-ownership block in `.github/CODEOWNERS`.
+ With `--check`, exit non-zero when the file is stale instead of writing it.
+ Exits non-zero, printing each problem, when [[codeowners-outcome]] finds any."
+ [cli-args]
+ (let [outcome (codeowners-outcome (slurp codeowners-path)
+ (read-modules-config)
+ (read-team->codeowner)
+ has-source?
+ (get-in cli-args [:options :check]))]
+ (case (:status outcome)
+ :problems
+ (do (doseq [problem (:problems outcome)]
+ (println (c/red (codeowners-problem-message problem))))
+ (u/exit 1))
+
+ :up-to-date
+ (do (println (c/green "CODEOWNERS module ownership is up to date."))
+ (u/exit 0))
+
+ :stale
+ (do (println (c/red "CODEOWNERS module ownership is out of date. Run ./bin/mage update-codeowners."))
+ (u/exit 1))
+
+ :write
+ (do (spit codeowners-path (:desired outcome))
+ (println (c/green (str "Updated " codeowners-path ".")))
+ (u/exit 0)))))
+
;;;; =============================================================================
;;;; Module tree
;;;; =============================================================================
diff --git a/mage/test/mage/modules_test.clj b/mage/test/mage/modules_test.clj
index 00f8f0cad17..1428e5fea71 100644
--- a/mage/test/mage/modules_test.clj
+++ b/mage/test/mage/modules_test.clj
@@ -2,10 +2,11 @@
"Tests for driver decision logic.
Run `mage -driver-decisions -h` to see the priority order."
(:require
+ [babashka.fs :as fs]
[clojure.java.io :as io]
[clojure.java.shell :as shell]
[clojure.string :as str]
- [clojure.test :refer [deftest is testing]]
+ [clojure.test :refer [are deftest is testing]]
[dev.module-cycle-names :as cycle-names]
[dev.module-explorer :as module-explorer]
[hooks.common.modules :as modules]
@@ -544,3 +545,299 @@
(is (= 'driver (#'mage.modules/file->module prefix->module "src/metabase/driver.clj")))
(is (nil? (#'mage.modules/file->module prefix->module "test/metabase/test_runner.clj")))
(is (nil? (#'mage.modules/file->module prefix->module "src/metabase/DO_NOT_ADD_NEW_FILES_HERE.txt"))))))
+
+;;; =============================================================================
+;;; CODEOWNERS generation
+;;; =============================================================================
+
+(def ^:private test-team->codeowner
+ {"Querying Platform" "@metabase/querying-platform"
+ "UX West" "@metabase/ux-west"})
+
+(defn- entries
+ "Block entries the generator builds for `modules-config`."
+ [modules-config]
+ (#'mage.modules/codeowners-entries modules-config test-team->codeowner))
+
+(defn- owners
+ "`[module handle]` for each block entry for `modules-config`, in block order."
+ [modules-config]
+ (mapv (juxt :module :handle) (entries modules-config)))
+
+(deftest codeowners-entries-test
+ (testing "only modules that set :codeowners true get entries, covering their directories and root files"
+ (is (= [{:module 'lib
+ :src-dir "src/metabase/lib"
+ :team "Querying Platform"
+ :handle "@metabase/querying-platform"
+ :opted-in? true
+ :patterns ["src/metabase/lib" "src/metabase/lib.*" "test/metabase/lib" "test/metabase/lib_test.*"]}]
+ (entries '{lib {:team "Querying Platform", :codeowners true}
+ query-processor {:team "Querying Platform"}}))))
+ (testing "enterprise modules own their enterprise/backend code"
+ (is (= ["enterprise/backend/src/metabase_enterprise/sso"
+ "enterprise/backend/src/metabase_enterprise/sso.*"
+ "enterprise/backend/test/metabase_enterprise/sso"
+ "enterprise/backend/test/metabase_enterprise/sso_test.*"]
+ (mapcat :patterns (entries '{enterprise/sso {:team "Querying Platform", :codeowners true}})))))
+ (testing "any value other than true does not opt in"
+ (is (= []
+ (owners '{lib {:team "Querying Platform", :codeowners :test}}))))
+ (testing "a nested module without its own :team takes its ancestor's"
+ (is (= [['lib.schema "@metabase/querying-platform"]]
+ (owners '{lib {:team "Querying Platform"}
+ lib.schema {:codeowners true}})))))
+
+(deftest codeowners-entries-nested-test
+ ;; CODEOWNERS applies the last matching pattern, so an entry inside another module's directory must follow it.
+ (testing "a nested module of the same team that opts in follows its parent"
+ (is (= [['lib "@metabase/querying-platform"]
+ ['lib.schema "@metabase/querying-platform"]]
+ (owners '{lib.schema {:team "Querying Platform", :codeowners true}
+ lib {:team "Querying Platform", :codeowners true}}))))
+ (testing "a nested module of the same team that does not opt in needs no entry"
+ (is (= [['lib "@metabase/querying-platform"]]
+ (owners '{lib {:team "Querying Platform", :codeowners true}
+ lib.schema {:team "Querying Platform"}}))))
+ (testing "a nested module of another team that does not opt in is left unowned"
+ (is (= [['lib "@metabase/querying-platform"]
+ ['lib.schema nil]]
+ (owners '{lib {:team "Querying Platform", :codeowners true}
+ lib.schema {:team "UX West"}}))))
+ (testing "a nested module of another team that opts in keeps its own team"
+ (is (= [['lib "@metabase/querying-platform"]
+ ['lib.schema "@metabase/ux-west"]]
+ (owners '{lib {:team "Querying Platform", :codeowners true}
+ lib.schema {:team "UX West", :codeowners true}})))))
+
+(deftest codeowners-entries-deep-nesting-test
+ (testing "a grandchild that inherits the team of an opted-in nested module needs no entry"
+ (is (= [['lib "@metabase/querying-platform"]
+ ['lib.schema "@metabase/ux-west"]]
+ (owners '{lib {:team "Querying Platform", :codeowners true}
+ lib.schema {:team "UX West", :codeowners true}
+ lib.schema.util {}}))))
+ (testing "a grandchild inside another team's opted-in module keeps an outer opted-in module of its team"
+ (is (= [['lib "@metabase/querying-platform"]
+ ['lib.schema "@metabase/ux-west"]
+ ['lib.schema.util "@metabase/querying-platform"]]
+ (owners '{lib {:team "Querying Platform", :codeowners true}
+ lib.schema {:team "UX West", :codeowners true}
+ lib.schema.util {:team "Querying Platform"}}))))
+ (testing "a grandchild of the owning team inside an unowned module gets that team back"
+ (is (= [['lib "@metabase/querying-platform"]
+ ['lib.schema nil]
+ ['lib.schema.util "@metabase/querying-platform"]]
+ (owners '{lib {:team "Querying Platform", :codeowners true}
+ lib.schema {:team "UX West"}
+ lib.schema.util {:team "Querying Platform"}}))))
+ (testing "a module inside an unowned module of its own team stays unowned, with no entry of its own"
+ (is (= [['lib "@metabase/querying-platform"]
+ ['lib.a nil]]
+ (owners '{lib {:team "Querying Platform", :codeowners true}
+ lib.a {:team "UX West"}
+ lib.a.b {:team "UX West"}}))))
+ (testing "a deep chain alternates between teams, with a team that never opted in left unowned"
+ (is (= [['lib "@metabase/querying-platform"]
+ ['lib.a "@metabase/ux-west"]
+ ['lib.a.b nil]
+ ['lib.a.b.c "@metabase/ux-west"]
+ ['lib.a.b.c.d "@metabase/querying-platform"]]
+ (owners '{lib {:team "Querying Platform", :codeowners true}
+ lib.a {:team "UX West", :codeowners true}
+ lib.a.b {:team "Gadget"}
+ lib.a.b.c {:team "UX West"}
+ lib.a.b.c.d {:team "Querying Platform"}})))))
+
+(deftest codeowners-config-problems-test
+ (let [problems (fn [config source? & [team->codeowner]]
+ (vec (#'mage.modules/codeowners-config-problems
+ config (or team->codeowner test-team->codeowner) source?)))]
+ (testing "a valid config has none"
+ (is (= []
+ (problems '{lib {:team "Querying Platform", :codeowners true}} (constantly true)))))
+ (testing "every problem of a module is reported in one run"
+ (is (= [{:type :unknown-value, :module 'lib, :value :test}
+ {:type :no-codeowner, :module 'lib, :team "Gadget"}
+ {:type :no-source, :module 'lib, :src-dir "src/metabase/lib"}]
+ (problems '{lib {:team "Gadget", :codeowners :test}} (constantly false)))))
+ (testing "a team whose codeowner is not one GitHub handle is reported once, naming its modules"
+ (doseq [value [""
+ "metabase/metabot"
+ "@"
+ "@metabase/"
+ "@metabase/metabot @metabase/other"
+ "@metabase/ux west"
+ "@metabase/metabot\n* @metabase/other"
+ 42]]
+ (is (= [{:type :bad-codeowner, :team "Gadget", :value value, :modules ['lib 'lib.schema]}]
+ (problems '{lib {:team "Gadget", :codeowners true}
+ lib.schema {:codeowners true}}
+ (constantly true)
+ {"Gadget" value})))))
+ (testing "user and team handles are valid"
+ (doseq [value ["@someone" "@metabase/metabot" "@metabase/ux.west_2"]]
+ (is (= []
+ (problems '{lib {:team "Gadget", :codeowners true}} (constantly true) {"Gadget" value})))))
+ (testing "modules that do not set :codeowners are not checked"
+ (is (= []
+ (problems '{lib {:team "Gadget"}} (constantly false)))))))
+
+(deftest codeowners-has-source-test
+ (let [has-source? #'mage.modules/has-source?
+ root (str (fs/create-temp-dir))]
+ (try
+ (fs/create-dirs (str root "/src/metabase/actions"))
+ (spit (str root "/src/metabase/args.clj") "(ns metabase.args)")
+ (testing "a module directory counts"
+ (is (has-source? (str root "/src/metabase/actions"))))
+ (testing "a root namespace file without a directory counts"
+ (is (has-source? (str root "/src/metabase/args"))))
+ (testing "neither does not"
+ (is (not (has-source? (str root "/src/metabase/no_such_module")))))
+ (finally
+ (fs/delete-tree root)))))
+
+(deftest codeowners-shadowed-lines-test
+ (let [begin @#'mage.modules/codeowners-begin-marker
+ shadowed #(#'mage.modules/shadowed-lines % ["src/metabase/lib" "src/metabase/lib.*"])]
+ (testing "rule lines above the block inside an owned path are reported"
+ (is (= ["src/metabase/lib/util.clj @metabase/other"
+ "/src/metabase/lib.cljc @metabase/other"]
+ (shadowed (str "src/metabase/lib/util.clj @metabase/other\n"
+ "/src/metabase/lib.cljc @metabase/other\n"
+ begin "\n")))))
+ (testing "broader, neighboring, commented and below-the-block lines are not"
+ (is (= []
+ (shadowed (str "src/metabase @metabase/other\n"
+ "src/metabase/lib_be @metabase/other\n"
+ "src/metabase/lib.cljc.bak/x @metabase/other\n"
+ "# src/metabase/lib/util.clj @metabase/other\n"
+ begin "\n"
+ "src/metabase/lib/util.clj @metabase/other\n")))))))
+
+(deftest codeowners-markers-problem-test
+ (let [begin @#'mage.modules/codeowners-begin-marker
+ end @#'mage.modules/codeowners-end-marker
+ problem #'mage.modules/markers-problem]
+ (testing "no markers, or one of each in order, is fine"
+ (is (nil? (problem "docs @writers\n")))
+ (is (nil? (problem (str begin "\n" end "\n")))))
+ (testing "a missing, repeated or misordered marker is a problem"
+ (doseq [existing [(str "# header\n" begin "\nsrc/a.clj @owner\n")
+ (str "src/a.clj @owner\n" end "\n")
+ (str end "\nsrc/a.clj @owner\n" begin "\n")
+ (str begin "\nnew/path @new\n" end "\n" begin "\nold/path @old\n" end "\n")]]
+ (is (= :broken-markers (:type (problem existing))))))))
+
+(deftest codeowners-file-problems-test
+ (let [begin @#'mage.modules/codeowners-begin-marker
+ end @#'mage.modules/codeowners-end-marker
+ problems #(#'mage.modules/codeowners-file-problems % (entries '{lib {:team "Querying Platform"
+ :codeowners true}}))]
+ (testing "a file with nothing above the block inside an owned path has none"
+ (is (empty? (problems "docs @writers\n"))))
+ (testing "a line above the block inside an owned path is a problem"
+ (is (= [{:type :shadowed-lines, :lines ["src/metabase/lib/x.clj @metabase/other"]}]
+ (problems "src/metabase/lib/x.clj @metabase/other\n"))))
+ (testing "lines are not checked while the markers are broken"
+ (is (= [:broken-markers]
+ (map :type (problems (str "src/metabase/lib @metabase/querying-platform\n" end "\n"))))))
+ (testing "a well-formed block's own lines are not shadowed lines"
+ (is (empty? (problems (str begin "\nsrc/metabase/lib @metabase/querying-platform\n" end "\n")))))))
+
+(deftest codeowners-outcome-test
+ (let [outcome (fn [existing config check?]
+ (#'mage.modules/codeowners-outcome existing config test-team->codeowner (constantly true) check?))
+ config '{lib {:team "Querying Platform", :codeowners true}}
+ written (str "docs @writers\n\n"
+ (#'mage.modules/codeowners-block
+ [{:module 'lib
+ :team "Querying Platform"
+ :handle "@metabase/querying-platform"
+ :opted-in? true
+ :patterns ["src/metabase/lib"
+ "src/metabase/lib.*"
+ "test/metabase/lib"
+ "test/metabase/lib_test.*"]}])
+ "\n")]
+ (testing "a file without the block gets it appended"
+ (is (= {:status :write, :desired written}
+ (outcome "docs @writers\n" config false))))
+ (testing "an up-to-date file is left alone, with or without --check"
+ (is (= {:status :up-to-date} (outcome written config false)))
+ (is (= {:status :up-to-date} (outcome written config true))))
+ (testing "--check reports a stale file instead of writing it"
+ (is (= {:status :stale} (outcome "docs @writers\n" config true))))
+ (testing "problems stop the write, with or without --check"
+ (doseq [check? [false true]]
+ (is (= {:status :problems
+ :problems [{:type :no-codeowner, :module 'lib, :team "Gadget"}]}
+ (outcome "docs @writers\n" '{lib {:team "Gadget", :codeowners true}} check?)))))
+ (testing "config and file problems are reported together"
+ (is (= [{:type :no-codeowner, :module 'lib, :team "Gadget"}
+ {:type :shadowed-lines, :lines ["src/metabase/lib/x.clj @metabase/other"]}]
+ (:problems (outcome "src/metabase/lib/x.clj @metabase/other\n"
+ '{lib {:team "Gadget", :codeowners true}}
+ false)))))
+ (testing "config problems and broken markers are reported together"
+ (is (= [:no-codeowner :broken-markers]
+ (map :type (:problems (outcome (str @#'mage.modules/codeowners-begin-marker "\n")
+ '{lib {:team "Gadget", :codeowners true}}
+ false))))))
+ (testing "a module with an unknown value is left out of the shadowed-line check"
+ (is (= [{:type :unknown-value, :module 'lib, :value :test}]
+ (:problems (outcome "src/metabase/lib/x.clj @metabase/other\n"
+ '{lib {:team "Querying Platform", :codeowners :test}}
+ false)))))))
+
+(deftest codeowners-problem-message-test
+ (testing "each problem names what is wrong and what to do"
+ (are [problem expected] (str/includes? (#'mage.modules/codeowners-problem-message problem) expected)
+ {:type :unknown-value, :module 'lib, :value :test} "lib sets :codeowners to :test. Use true"
+ {:type :no-codeowner, :module 'lib, :team "Gadget"} "its team \"Gadget\" has no \"codeowner\""
+ {:type :bad-codeowner, :team "Gadget", :value "", :modules ['lib]} "so that lib can opt in"
+ {:type :no-source, :module 'lib, :src-dir "src/metabase/lib"} "has no code at src/metabase/lib."
+ {:type :shadowed-lines, :lines ["a/b.clj @x"]} "Move them below the END marker:\n a/b.clj @x"
+ {:type :broken-markers, :begin 0, :end nil} "Fix the markers by hand.")))
+
+(deftest codeowners-stanza-lines-test
+ (testing "a header naming the module and team, then one owner line per pattern"
+ (is (= ["# lib (Querying Platform)"
+ "src/metabase/lib @metabase/qp"
+ "src/metabase/lib.* @metabase/qp"]
+ (#'mage.modules/codeowners-stanza-lines
+ {:module 'lib
+ :team "Querying Platform"
+ :handle "@metabase/qp"
+ :opted-in? true
+ :patterns ["src/metabase/lib" "src/metabase/lib.*"]}))))
+ (testing "an unowned entry has no handle, and its header says it did not opt in"
+ (is (= ["# lib.schema (UX West, not opted in)"
+ "src/metabase/lib/schema"
+ "src/metabase/lib/schema.*"]
+ (#'mage.modules/codeowners-stanza-lines
+ {:module 'lib.schema
+ :team "UX West"
+ :handle nil
+ :opted-in? false
+ :patterns ["src/metabase/lib/schema" "src/metabase/lib/schema.*"]})))))
+
+(deftest codeowners-splice-test
+ (let [begin @#'mage.modules/codeowners-begin-marker
+ end @#'mage.modules/codeowners-end-marker
+ splice @#'mage.modules/splice-codeowners
+ block (str begin "\nnew/path @new\n" end)]
+ (testing "a file without markers gets the block appended"
+ (is (= (str "docs @writers\n\n" block "\n")
+ (splice "docs @writers\n" block))))
+ (testing "the marked region is replaced, keeping the text on both sides"
+ (let [existing (str "# header\n\n" begin "\nold/path @old\n" end "\n\n# exceptions\nsrc/a.clj @owner\n")
+ out (splice existing block)]
+ (is (= (str "# header\n\n" block "\n\n# exceptions\nsrc/a.clj @owner\n")
+ out))
+ (is (= out (splice out block))
+ "splicing again changes nothing")))
+ (testing "broken markers throw instead of splicing at the wrong place"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"has a broken generated block"
+ (splice (str "src/a.clj @owner\n" end "\n") block))))))