<commit_before>(ns runbld.publish.email
  (:refer-clojure :exclude [send])
  (:require [clojure.string :as str]
            [postal.core :as mail]
            [runbld.opts :refer [Opts]]
            [schema.core :as s]
            [stencil.core :as mustache]))

(def Ctx
  {
   (s/required-key :commit) s/Str
   (s/required-key :commit-desc) s/Str
   (s/required-key :commit-email) s/Str
   (s/required-key :commit-msg) s/Str
   (s/required-key :commit-name) s/Str
   (s/required-key :commit-time)  s/Str
   (s/required-key :env) {s/Str s/Any}
   (s/required-key :github-name) s/Str
   (s/required-key :id) s/Str
   (s/required-key :job-name) s/Str
   (s/required-key :mail-from) s/Str
   (s/required-key :org) s/Str
   (s/required-key :project) s/Str
   (s/required-key :rcpt-to) (s/cond-pre s/Str [s/Str])
   (s/required-key :scriptfile) s/Str
   (s/required-key :start-millis) s/Num
   (s/required-key :time-end) s/Str
   (s/required-key :time-start) s/Str

   (s/optional-key :args) [s/Str]
   (s/optional-key :branch) s/Str
   (s/optional-key :cmd) [s/Str]
   (s/optional-key :cmd-source) s/Str
   (s/optional-key :cwd) s/Str
   (s/optional-key :end-millis) s/Num
   (s/optional-key :github-page) s/Str
   (s/optional-key :jenkins-executor) (s/maybe s/Str)
   (s/optional-key :jenkins-labels) (s/maybe s/Str)
   (s/optional-key :jenkins-node) (s/maybe s/Str)
   (s/optional-key :jenkins-number) (s/maybe s/Str)
   (s/optional-key :job-name-extra) s/Str
   (s/optional-key :profile-name) s/Str
   (s/optional-key :program) s/Str
   (s/optional-key :status) s/Num
   (s/optional-key :took) s/Num
   (s/optional-key :url) (s/maybe s/Str)
   (s/optional-key :workspace) s/Str
   })

(defn maybe-split-addr [s]
  (if (and (string? s) (.contains s ","))
    (->> (str/split s #",")
         (map #(.trim %)))
    s))

(defn send* [conn from to subject msg]
  (mail/send-message
   conn
   {:from from
    :to to
    :subject subject
    :body msg}))

(s/defn send :- clojure.lang.IPersistentVector
  [opts :- Opts
   ctx :- Ctx]
  (send* (opts :email)
         (ctx :mail-from)
         (ctx :rcpt-to)
         (format "%s %s"
                 (ctx :github-name)
                 (ctx :commit))
         (mustache/render-string
          (slurp (-> opts :email :template))
          ctx)))<commit_msg>Align syntax<commit_after>(ns runbld.publish.email
  (:refer-clojure :exclude [send])
  (:require [clojure.string :as str]
            [postal.core :as mail]
            [runbld.opts :refer [Opts]]
            [schema.core :as s]
            [stencil.core :as mustache]))

(def Ctx
  {
   (s/required-key :commit           ) s/Str
   (s/required-key :commit-desc      ) s/Str
   (s/required-key :commit-email     ) s/Str
   (s/required-key :commit-msg       ) s/Str
   (s/required-key :commit-name      ) s/Str
   (s/required-key :commit-time      ) s/Str
   (s/required-key :env              ) {s/Str s/Any}
   (s/required-key :github-name      ) s/Str
   (s/required-key :id               ) s/Str
   (s/required-key :job-name         ) s/Str
   (s/required-key :mail-from        ) s/Str
   (s/required-key :org              ) s/Str
   (s/required-key :project          ) s/Str
   (s/required-key :rcpt-to          ) (s/cond-pre s/Str [s/Str])
   (s/required-key :scriptfile       ) s/Str
   (s/required-key :start-millis     ) s/Num
   (s/required-key :time-end         ) s/Str
   (s/required-key :time-start       ) s/Str

   (s/optional-key :args             ) [s/Str]
   (s/optional-key :branch           ) s/Str
   (s/optional-key :cmd              ) [s/Str]
   (s/optional-key :cmd-source       ) s/Str
   (s/optional-key :cwd              ) s/Str
   (s/optional-key :end-millis       ) s/Num
   (s/optional-key :github-page      ) s/Str
   (s/optional-key :jenkins-executor ) (s/maybe s/Str)
   (s/optional-key :jenkins-labels   ) (s/maybe s/Str)
   (s/optional-key :jenkins-node     ) (s/maybe s/Str)
   (s/optional-key :jenkins-number   ) (s/maybe s/Str)
   (s/optional-key :job-name-extra   ) s/Str
   (s/optional-key :profile-name     ) s/Str
   (s/optional-key :program          ) s/Str
   (s/optional-key :status           ) s/Num
   (s/optional-key :took             ) s/Num
   (s/optional-key :url              ) (s/maybe s/Str)
   (s/optional-key :workspace        ) s/Str
   })

(defn maybe-split-addr [s]
  (if (and (string? s) (.contains s ","))
    (->> (str/split s #",")
         (map #(.trim %)))
    s))

(defn send* [conn from to subject msg]
  (mail/send-message
   conn
   {:from from
    :to to
    :subject subject
    :body msg}))

(s/defn send :- clojure.lang.IPersistentVector
  [opts :- Opts
   ctx :- Ctx]
  (send* (opts :email)
         (ctx :mail-from)
         (ctx :rcpt-to)
         (format "%s %s"
                 (ctx :github-name)
                 (ctx :commit))
         (mustache/render-string
          (slurp (-> opts :email :template))
          ctx)))
{'fraction_non_alphanumeric': 0.16935319322587228, 'fraction_numerical': 0.0, 'mean_word_length': 1.9459134615384615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3075308', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1885, 'n_words': 394}