LCOV - code coverage report
Current view: top level - matchscore/telegram - changelog_candidates.clj Coverage Total Hit
Test: lcov.info Lines: 95.2 % 83 79
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.telegram.changelog-candidates
       2              :   "Loads changelog drafts generated from repository changes. Each bundled EDN
       3              :    file holds one candidate and is written by Codex or Claude; this namespace
       4              :    only validates and imports them."
       5              :   (:require
       6              :    [clojure.edn :as edn]
       7              :    [clojure.java.io :as io]
       8              :    [clojure.string :as str]
       9              :    [matchscore.db.postgres.telegram-publications :as publications]
      10              :    [matchscore.telegram.admin-posts :as admin-posts]
      11              :    [taoensso.telemere :as t])
      12              :   (:import
      13              :    (java.io       File)
      14              :    (java.net      JarURLConnection URL)
      15              :    (java.util.jar JarEntry)))
      16              : 
      17              : 
      18              : ;; One file per candidate, named `<source-id>.edn`, so parallel branches and
      19              : ;; worktrees never edit the same file.
      20            8 : (def ^:private candidates-resource-dir "changelog/candidates")
      21              : 
      22              : 
      23            8 : (def ^:private candidate-file-pattern #"([^/]+)\.edn")
      24              : 
      25              : 
      26            4 : (defn- invalid-candidate
      27              :   [message source-id]
      28            1 :   (throw
      29            2 :    (ex-info
      30            1 :     message
      31            5 :     {:type      :admin-telegram-validation
      32            1 :      :message   message
      33            1 :      :source-id source-id})))
      34              : 
      35              : 
      36            4 : (defn- validate-candidate
      37              :   [{:keys [source-id title source posts]
      38              :     :as   candidate}]
      39           26 :   (when-not (and (string? source-id) (not (str/blank? source-id)))
      40            0 :     (invalid-candidate "Generated changelog candidate needs a source-id." source-id))
      41           28 :   (when-not (and (string? title) (not (str/blank? title)))
      42            4 :     (invalid-candidate "Generated changelog candidate needs a title." source-id))
      43           10 :   (when-not (map? source)
      44            0 :     (invalid-candidate "Generated changelog candidate needs source evidence." source-id))
      45              :   ;; Reuse the production Rich Message validator. The destination is supplied by
      46              :   ;; the reviewer, so a valid placeholder is used only during import.
      47            6 :   (let [prepared (admin-posts/prepare
      48           10 :                   {:chat-id "@candidate_validation"
      49            2 :                    :posts   posts
      50           10 :                    :disable-notification (true? (:disable-notification candidate))})]
      51           12 :     {:source-id source-id
      52            2 :      :title     title
      53            2 :      :source    source
      54           12 :      :content   {:disable-notification (:disable-notification prepared)
      55           24 :                  :posts (mapv #(select-keys % [:markdown :image-url]) (:posts prepared))}}))
      56              : 
      57              : 
      58            4 : (defn- directory-files
      59              :   [^File directory]
      60           61 :   (for [^File file (.listFiles directory)
      61            2 :         :when      (.isFile file)]
      62           10 :     {:file-name (.getName file)
      63            6 :      :read      #(slurp file)}))
      64              : 
      65              : 
      66            4 : (defn- jar-files
      67              :   "A classpath directory inside the uberjar cannot be listed as a file, so its
      68              :    entries are enumerated instead. The JarFile is the JVM's shared cached
      69              :    instance and must stay open."
      70              :   [^JarURLConnection connection]
      71            2 :   (let [jar    (.getJarFile connection)
      72            8 :         prefix (str (str/replace (.getEntryName connection) #"/+$" "") "/")]
      73           34 :     (for [^JarEntry entry (enumeration-seq (.entries jar))
      74            1 :           :let  [entry-name (.getName entry)]
      75           11 :           :when (and (str/starts-with? entry-name prefix) (not (.isDirectory entry)))]
      76            9 :       {:file-name (subs entry-name (count prefix))
      77            9 :        :read      #(with-open [in (.getInputStream jar entry)] (slurp in))})))
      78              : 
      79              : 
      80            4 : (defn- candidate-files
      81              :   "Lists the `<source-id>.edn` files directly under the candidates directory."
      82              :   [^URL directory-url]
      83            4 :   (->>
      84            8 :     (if (= "jar" (.getProtocol directory-url))
      85            3 :       (jar-files (.openConnection directory-url))
      86           10 :       (directory-files (io/file directory-url)))
      87           18 :     (filter #(re-matches candidate-file-pattern (:file-name %)))
      88            2 :     (sort-by :file-name)))
      89              : 
      90              : 
      91           16 : (defn- file-source-id [file-name] (second (re-matches candidate-file-pattern file-name)))
      92              : 
      93              : 
      94            4 : (defn- load-candidate
      95              :   [{:keys [file-name read]}]
      96            8 :   (let [expected-id (file-source-id file-name)
      97            8 :         value       (edn/read-string (read))]
      98           11 :     (when-not (map? value)
      99            3 :       (invalid-candidate
     100              :        "A bundled Telegram changelog candidate file must hold one EDN map."
     101            1 :        expected-id))
     102           15 :     (when-not (= expected-id (:source-id value))
     103            3 :       (invalid-candidate
     104              :        "A bundled Telegram changelog candidate file must be named <source-id>.edn."
     105            3 :        (:source-id value)))
     106            6 :     (validate-candidate value)))
     107              : 
     108              : 
     109            4 : (defn load-bundled
     110              :  "Returns the valid bundled candidates under `:candidates` and the files that
     111              :    failed to load under `:invalid`, so one broken draft cannot hide the rest of
     112              :    the review queue."
     113              :  ([]
     114            8 :   (if-let [directory-url (io/resource candidates-resource-dir)]
     115            3 :     (load-bundled directory-url)
     116            0 :     {:candidates []
     117            0 :      :invalid    []}))
     118              :  ([directory-url]
     119            4 :   (reduce
     120            2 :    (fn
     121              :     [result
     122              :      {:keys [file-name]
     123              :       :as   file}]
     124            2 :     (try
     125           16 :       (update result :candidates conj (load-candidate file))
     126              :       (catch Exception e
     127            6 :         (update
     128            2 :          result
     129              :          :invalid
     130            2 :          conj
     131            8 :          {:file-name file-name
     132            6 :           :message   (ex-message e)}))))
     133            8 :    {:candidates []
     134            2 :     :invalid    []}
     135            6 :    (candidate-files directory-url))))
     136              : 
     137              : 
     138            4 : (defn sync-bundled!
     139              :  "Makes the review queue mirror the bundled files: unreviewed candidates whose
     140              :    file is gone are removed first, so a renamed file does not collide with its
     141              :    old row, then every valid file is imported. A file that fails validation
     142              :    still counts as bundled and keeps its existing candidate."
     143              :  [ds]
     144            3 :  (let [{:keys [candidates invalid]} (load-bundled)
     145              :        bundled-source-ids
     146           15 :        (concat (map :source-id candidates) (map #(file-source-id (:file-name %)) invalid))]
     147           26 :    (doseq [{:keys [file-name message]} invalid]
     148           98 :      (t/log! :error (str "Skipped invalid Telegram changelog candidate " file-name ": " message)))
     149           25 :    (doseq [{:keys [source-id]}
     150            4 :            (publications/delete-unbundled-changelog-candidates! ds bundled-source-ids)]
     151           96 :      (t/log! :info (str "Removed Telegram changelog candidate without a bundled file: " source-id)))
     152           30 :    (doseq [candidate candidates] (publications/upsert-changelog-candidate! ds candidate))))
        

Generated by: LCOV version 2.0-1