<commit_before>(ns clojars.event
  (:refer-clojure :exclude [load])
  (:require [clojure.java.io :as io]
            [clojure.java.jdbc :as sql]
            [clojure.set :as set]
            [clojure.string :as string]
            [clojars.config :refer [config]]
            [clojars.search :as search]
            [clucy.core :as clucy])
  (:import (org.apache.commons.codec.digest DigestUtils)))

(defn- validate-regex [x re message]
  (when-not (re-matches re x)
    (throw (ex-info (str message " (" re ")")
             {:value x
              :regex re}))))

(defn snapshot-version? [version]
  (.endsWith version "-SNAPSHOT"))

(defn assert-non-redeploy [group-id artifact-id version filename]
  (when (and (not (snapshot-version? version))
          (.exists (io/file (config :repo) (string/replace group-id "." "/")
                     artifact-id version filename)))
    (throw (ex-info "redeploying non-snapshots is not allowed" {}))))

(defn validate-deploy [group-id artifact-id version filename]
  (try
    ;; We're on purpose *at least* as restrictive as the recommendations on
    ;; https://maven.apache.org/guides/mini/guide-naming-conventions.html
    ;; If you want loosen these please include in your proposal the
    ;; ramifications on usability, security and compatiblity with filesystems,
    ;; OSes, URLs and tools.
    (validate-regex artifact-id #"^[a-z0-9_.-]+$"
      (str "project names must consist solely of lowercase "
        "letters, numbers, hyphens and underscores"))
    (validate-regex group-id #"^[a-z0-9_.-]+$"
      (str "group names must consist solely of lowercase "
        "letters, numbers, hyphens and underscores"))
    ;; Maven's pretty accepting of version numbers, but so far in 2.5 years
    ;; bar one broken non-ascii exception only these characters have been used.
    ;; Even if we manage to support obscure characters some filesystems do not
    ;; and some tools fail to escape URLs properly.  So to keep things nice and
    ;; compatible for everyone let's lock it down.
    (validate-regex version #"^[a-zA-Z0-9_.+-]+$"
      (str "version strings must consist solely of letters, "
        "numbers, dots, pluses, hyphens and underscores"))
    (assert-non-redeploy group-id artifact-id version filename)
    (catch Exception e
      (throw (ex-info (.getMessage e)
               (assoc (ex-data e)
                 :status 403
                 :status-message (str "Forbidden - " (.getMessage e))
                 :group-id group-id
                 :artifact-id artifact-id
                 :version version
                 :file filename))))))

(defn event-log-file [type]
  (io/file (config :event-dir) (str (name type) ".clj")))

(defn record [type event]
  (let [filename (event-log-file type)
        content (prn-str (assoc event :at (:at event (java.util.Date.))))]
    (locking #'record
      (spit filename content :append true))))

(defn record-deploy [{:keys [group-id artifact-id version]} deployed-by file]
  (when (.endsWith (str file) ".pom")
    (with-open [index (clucy/disk-index (config :index-path))]<commit_msg>Include links to the FAQ in error messages (#367)<commit_after>(ns clojars.event
  (:refer-clojure :exclude [load])
  (:require [clojure.java.io :as io]
            [clojure.java.jdbc :as sql]
            [clojure.set :as set]
            [clojure.string :as string]
            [clojars.config :refer [config]]
            [clojars.search :as search]
            [clucy.core :as clucy])
  (:import (org.apache.commons.codec.digest DigestUtils)))

(defn- validate-regex [x re message]
  (when-not (re-matches re x)
    (throw (ex-info message {:value x
                             :regex re}))))

(defn snapshot-version? [version]
  (.endsWith version "-SNAPSHOT"))

(defn assert-non-redeploy [group-id artifact-id version filename]
  (when (and (not (snapshot-version? version))
          (.exists (io/file (config :repo) (string/replace group-id "." "/")
                     artifact-id version filename)))
    (throw (ex-info "redeploying non-snapshots is not allowed (see http://git.io/vO2Tg)"
             {}))))

(defn validate-deploy [group-id artifact-id version filename]
  (try
    ;; We're on purpose *at least* as restrictive as the recommendations on
    ;; https://maven.apache.org/guides/mini/guide-naming-conventions.html
    ;; If you want loosen these please include in your proposal the
    ;; ramifications on usability, security and compatiblity with filesystems,
    ;; OSes, URLs and tools.
    (validate-regex artifact-id #"^[a-z0-9_.-]+$"
      (str "project names must consist solely of lowercase "
        "letters, numbers, hyphens and underscores (see http://git.io/vO2Uy)"))
    (validate-regex group-id #"^[a-z0-9_.-]+$"
      (str "group names must consist solely of lowercase "
        "letters, numbers, hyphens and underscores (see http://git.io/vO2Uy)"))
    ;; Maven's pretty accepting of version numbers, but so far in 2.5 years
    ;; bar one broken non-ascii exception only these characters have been used.
    ;; Even if we manage to support obscure characters some filesystems do not
    ;; and some tools fail to escape URLs properly.  So to keep things nice and
    ;; compatible for everyone let's lock it down.
    (validate-regex version #"^[a-zA-Z0-9_.+-]+$"
      (str "version strings must consist solely of letters, "
        "numbers, dots, pluses, hyphens and underscores (see http://git.io/vO2TO)"))
    (assert-non-redeploy group-id artifact-id version filename)
    (catch Exception e
      (throw (ex-info (.getMessage e)
               (merge
                 {:status 403
                  :status-message (str "Forbidden - " (.getMessage e))
                  :group-id group-id
                  :artifact-id artifact-id
                  :version version
                  :file filename}
                 (ex-data e)))))))

(defn event-log-file [type]
  (io/file (config :event-dir) (str (name type) ".clj")))

(defn record [type event]
  (let [filename (event-log-file type)
        content (prn-str (assoc event :at (:at event (java.util.Date.))))]
    (locking #'record
      (spit filename content :append true))))

(defn record-deploy [{:keys [group-id artifact-id version]} deployed-by file]
  (when (.endsWith (str file) ".pom")
    (with-open [index (clucy/disk-index (config :index-path))]
{'fraction_non_alphanumeric': 0.13024351924587588, 'fraction_numerical': 0.004556166535742341, 'mean_word_length': 2.602716468590832, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5410081', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1902, 'n_words': 631}