Skip to content

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

Show what automated ratchet PRs shrink (#81807)

Chris Truter94d7dd520c04
diff --git a/.github/workflows/kondo-ratchets-shrink.yml b/.github/workflows/kondo-ratchets-shrink.yml
index b9a269d445a..20a2d6804a1 100644
--- a/.github/workflows/kondo-ratchets-shrink.yml
+++ b/.github/workflows/kondo-ratchets-shrink.yml
@@ -57,7 +57,9 @@ jobs:
distribution: temurin
java-version: 25
- name: Shrink ignore limits
- run: ./bin/mage kondo-ratchets-shrink
+ run: |
+ cp "$RATCHETS_FILE" "$RUNNER_TEMP/ratchets-before.edn"
+ ./bin/mage kondo-ratchets-shrink
# Two independent outputs: `push` says the branch needs a new commit, `pr` names the open automation
# PR if there is one. Approval and auto-merge key on `pr` alone, so a run that failed after opening
# the PR is repaired by the next run instead of leaving the PR stranded.
@@ -113,6 +115,12 @@ jobs:
GH_TOKEN: ${{ secrets.METABASE_AUTOMATION_USER_TOKEN }}
OPEN_PR: ${{ steps.compare.outputs.pr }}
run: |
+ ./bin/mage -kondo-ratchets-pr-body \
+ "$RUNNER_TEMP/ratchets-before.edn" \
+ "$RATCHETS_FILE" \
+ "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" \
+ > "$RUNNER_TEMP/ratchets-pr-body.md"
+
git config user.name github-actions
git config user.email [email protected]
git add -- "$RATCHETS_FILE"
@@ -123,6 +131,7 @@ jobs:
echo "head=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
if [ -n "$OPEN_PR" ]; then
+ gh pr edit "$OPEN_PR" --body-file "$RUNNER_TEMP/ratchets-pr-body.md"
echo "Updated the automation PR #$OPEN_PR"
echo "pr=$OPEN_PR" >> "$GITHUB_OUTPUT"
exit 0
@@ -133,10 +142,7 @@ jobs:
--head "$AUTOMATION_BRANCH" \
--title "Tighten ratchets" \
--label "no-backport" \
- --body "\`./bin/mage kondo-ratchets-shrink\` lowered \`$RATCHETS_FILE\` to match master.
- The shrink workflow force-updates this branch from master whenever the result changes, so the
- diff is always one commit on top of a recent master. It approves and merges itself once the
- ratchet check passes; nothing is raised.")
+ --body-file "$RUNNER_TEMP/ratchets-pr-body.md")
echo "Opened the automation PR $url"
echo "pr=${url##*/}" >> "$GITHUB_OUTPUT"
- name: Approve the automation PR
diff --git a/bb.edn b/bb.edn
index cf668d7f988..5358474ba16 100644
--- a/bb.edn
+++ b/bb.edn
@@ -438,6 +438,22 @@
(dev.kondo-ratchet/render merged)))
(println (str "merged ratchets into " output-path))))}
+ -kondo-ratchets-pr-body
+ {:doc "Render the body for an automated ratchet-shrink PR."
+ :arg-schema [:tuple
+ [:string {:name "before" :description "Ratchets file before shrinking"}]
+ [:string {:name "after" :description "Ratchets file after shrinking"}]
+ [:string {:name "workflow-url" :description "URL of the workflow run"}]]
+ :requires [[dev.kondo-ratchet]]
+ :task (task!
+ (let [[before-path after-path workflow-url] (:arguments parsed)
+ read-ratchets (fn [path]
+ (binding [dev.kondo-ratchet/*ratchets-file* path]
+ (dev.kondo-ratchet/read-ratchets)))]
+ (print (dev.kondo-ratchet/shrink-pr-body (read-ratchets before-path)
+ (read-ratchets after-path)
+ workflow-url))))}
+
-driver-decisions
{:doc "Determine which driver tests should run based on PR context. Outputs decisions for all drivers."
:examples [["./bin/mage -driver-decisions --git-ref=master --force-run=false --pr-labels= --skip=false" "Determine driver decisions for a PR"]
diff --git a/dev/src/dev/kondo_ratchet.clj b/dev/src/dev/kondo_ratchet.clj
index 1ebf0d884f8..dc92e90c184 100644
--- a/dev/src/dev/kondo_ratchet.clj
+++ b/dev/src/dev/kondo_ratchet.clj
@@ -756,6 +756,78 @@
linter recorded actual)))
(some-> (stale-exemptions-warning comment-exempt occurrences) vector))))
+(def ^:private count-fields
+ [:ignore-counts :config-counts])
+
+(defn- display-name
+ [field linter]
+ (if (= field :config-counts)
+ (str ":config/" (subs (str linter) 1))
+ (str linter)))
+
+(defn- shrink-changes
+ [before after]
+ (->> count-fields
+ (into []
+ (comp
+ (mapcat (fn [field]
+ (map (fn [[linter n]] [field linter n])
+ (get before field))))
+ (keep (fn [[field linter before-count]]
+ (let [after-count (get-in after [field linter] 0)]
+ (when (and (integer? before-count)
+ (integer? after-count)
+ (< after-count before-count))
+ {:name (display-name field linter)
+ :from before-count
+ :to after-count}))))))
+ (sort-by :name)))
+
+(defn shrink-summary
+ "A compact, sorted before/after listing of numeric budgets lowered between two ratchet maps.
+ Config-level budgets carry a `:config/` prefix."
+ [before after]
+ (let [changes (shrink-changes before after)]
+ (if (empty? changes)
+ "{}"
+ (let [column-width (fn [k]
+ (transduce (map (comp count str k)) max 0 changes))
+ [name-width from-width to-width] (mapv column-width [:name :from :to])]
+ (str "{"
+ (str/join "\n "
+ (for [{:keys [name from to]} changes]
+ (format (str "%-" name-width "s %" from-width "d => %" to-width "d")
+ name from to)))
+ "}")))))
+
+(def ^:private pie-legends
+ [["# still our problem" ". fixed, so no longer our problem"]
+ ["# stubborn lint" ". lint successfully rolled"]
+ ["# TODO" ". TODONE"]])
+
+(def ^:private pie-chart-resource
+ "dev/kondo_ratchet_pie.txt")
+
+(defn- pie-chart
+ []
+ (let [template (some-> pie-chart-resource io/resource slurp)
+ legend (rand-nth pie-legends)]
+ (when-not template
+ (throw (ex-info (str "missing classpath resource " pie-chart-resource)
+ {:resource pie-chart-resource})))
+ (apply format template legend)))
+
+(defn shrink-pr-body
+ "Markdown for an automated shrink PR."
+ [before after workflow-url]
+ (str "## Debt repaid, locking in progress\n\n"
+ "### What changed\n\n"
+ "```edn\n" (shrink-summary before after) "\n```\n\n"
+ "### Executive dashboard\n\n"
+ "```text\n" (pie-chart) "```\n\n"
+ "Generated by [`./bin/mage kondo-ratchets-shrink`](" workflow-url
+ ")\n"))
+
(defn fix!
"Rewrite [[*ratchets-file*]]: lower budgets and normalize formatting.
Refuses to touch a file containing an unknown linter or to seed one, rather than silently dropping it.
diff --git a/dev/src/dev/kondo_ratchet_pie.txt b/dev/src/dev/kondo_ratchet_pie.txt
new file mode 100644
index 00000000000..275bbd791c6
--- /dev/null
+++ b/dev/src/dev/kondo_ratchet_pie.txt
@@ -0,0 +1,12 @@
+ BEFORE AFTER
+ .-""""""""-. .-""""""""-.
+ .'############'. .'...........#'.
+ /################\ /.........../####\
+ |##################| |.........+########|
+ |##################| |..........\#######|
+ \################/ \...........\####/
+ '.############.' '............#.'
+ '-.______.-' '-.______.-'
+
+ %s
+ %s
diff --git a/dev/test/metabase/core/kondo_ratchet_test.clj b/dev/test/metabase/core/kondo_ratchet_test.clj
index 12ad56b3f09..6156b8563a4 100644
--- a/dev/test/metabase/core/kondo_ratchet_test.clj
+++ b/dev/test/metabase/core/kondo_ratchet_test.clj
@@ -9,7 +9,8 @@
[clojure.java.shell :as sh]
[clojure.string :as str]
[clojure.test :refer :all]
- [dev.kondo-ratchet :as kondo-ratchet]))
+ [dev.kondo-ratchet :as kondo-ratchet]
+ [metabase.test :as mt]))
(set! *warn-on-reflection* true)
@@ -714,3 +715,32 @@
"untouched budgets (:same, :cfg-same), a used unlimited policy (:free), and a still-needed
exemption (:lower) earn no line; a hand-written 0 (:zero) is dropped like any bounded budget with no
ignores left; the empty unlimited policy (:empty) is kept and warned about")))
+
+(deftest ^:parallel shrink-summary-test
+ (is (= (str "{:a 2 => 1\n"
+ " :config/unused-import 4 => 0\n"
+ " :z 10 => 3}")
+ (kondo-ratchet/shrink-summary
+ {:ignore-counts {:a 2, :same 1, :unlimited :unlimited, :z 10}
+ :config-counts {:same 2, :unused-import 4}
+ :comment-exempt #{:a}}
+ {:ignore-counts {:a 1, :raised 2, :same 1, :unlimited :unlimited, :z 3}
+ :config-counts {:same 2}
+ :comment-exempt #{}})))
+ (is (= "{}" (kondo-ratchet/shrink-summary {:ignore-counts {:a 1}}
+ {:ignore-counts {:a 2}}))
+ "increases and non-count policy changes aren't reported"))
+
+(deftest ^:synchronized shrink-pr-body-test
+ (let [before {:ignore-counts {:case-symbol-test 2}}
+ after {:ignore-counts {:case-symbol-test 1}}
+ body #(mt/with-dynamic-fn-redefs [rand-nth (constantly %)]
+ (kondo-ratchet/shrink-pr-body before after "https://example.test/run/1"))]
+ (is (str/includes? (body ["# still our problem" ". fixed, so no longer our problem"])
+ "{:case-symbol-test 2 => 1}"))
+ (is (str/includes? (body ["# stubborn lint" ". lint successfully rolled"])
+ "# stubborn lint\n . lint successfully rolled"))
+ (is (str/includes? (body ["# TODO" ". TODONE"])
+ "# TODO\n . TODONE"))
+ (is (str/includes? (body ["# still our problem" ". fixed, so no longer our problem"])
+ "[`./bin/mage kondo-ratchets-shrink`](https://example.test/run/1)"))))