|
1 | 1 | (ns clojars.verification |
2 | 2 | (:require |
| 3 | + [clj-http.client :as http] |
3 | 4 | [clojars.db :as db] |
4 | 5 | [clojars.web.common :as common] |
5 | 6 | [clojure.java.shell :as shell] |
| 7 | + [clojure.set :as set] |
6 | 8 | [clojure.string :as str])) |
7 | 9 |
|
8 | 10 | (defn- get-txt-records |
|
51 | 53 | [request msg] |
52 | 54 | (assoc request :error msg)) |
53 | 55 |
|
54 | | -(defn- verify-group |
55 | | - [db request username group] |
| 56 | +(defn- verify-group* |
| 57 | + [db username group] |
56 | 58 | ;; will only create the group if it doesn't already exist |
57 | 59 | (db/add-group db username group) |
58 | | - (db/verify-group! db username group) |
| 60 | + (db/verify-group! db username group)) |
| 61 | + |
| 62 | +(defn- verify-group |
| 63 | + [db request username group] |
| 64 | + (verify-group* db username group) |
59 | 65 | (assoc request :message (format "The group '%s' has been verified." group))) |
60 | 66 |
|
61 | 67 | (defn verify-group-by-TXT |
|
95 | 101 | (err request "No valid verification TXT record found.") |
96 | 102 |
|
97 | 103 | (not (= username username-from-txt)) |
98 | | - (err request (format "Validation TXT record is for user '%s', not '%s' (you)." |
| 104 | + (err request (format "The verification TXT record is for user '%s', not '%s' (you)." |
99 | 105 | username-from-txt username)) |
100 | 106 |
|
101 | 107 | group-verification |
|
136 | 142 | (db/find-group-verifications-for-users-groups db username))] |
137 | 143 | (verify-group db (assoc request :parent-group parent-group-name) username group) |
138 | 144 | (err request "The group is not a subgroup of a verified group."))))) |
| 145 | + |
| 146 | +(defn- parse-url |
| 147 | + [url] |
| 148 | + (when (string? url) |
| 149 | + (rest (re-find #"^https://(github|gitlab)\.com/([^/]+)/clojars-([^/]+)/?$" url)))) |
| 150 | + |
| 151 | +(defn- repo-exists? |
| 152 | + [url] |
| 153 | + (try |
| 154 | + (= 200 (:status (http/head url {:throw-exceptions false}))) |
| 155 | + (catch Exception _ |
| 156 | + false))) |
| 157 | + |
| 158 | +(defn- verify-groups |
| 159 | + [db request username groups] |
| 160 | + (if (= 1 (count groups)) |
| 161 | + (verify-group db request username (first groups)) |
| 162 | + (do |
| 163 | + (doseq [group groups] |
| 164 | + (verify-group* db username group)) |
| 165 | + ;; We will only have two groups here, so this format string should be fine |
| 166 | + (assoc request :message (apply format "The groups '%s' & '%s' have been verified." groups))))) |
| 167 | + |
| 168 | +(defn verify-vcs-groups |
| 169 | + "Verifies the net. and com. groups for a github/gitlab organization based on url." |
| 170 | + [db {:as request :keys [username url]}] |
| 171 | + (let [[provider org username-from-url] (parse-url url) |
| 172 | + candidate-groups (set (for [tld ["com" "net"]] (format "%s.%s.%s" tld provider org))) |
| 173 | + group-verifications (set (map |
| 174 | + (fn [group-name] |
| 175 | + (db/find-group-verification db group-name)) |
| 176 | + candidate-groups)) |
| 177 | + group-verification-names (set (map :group_name group-verifications)) |
| 178 | + groups-to-verify (set/difference candidate-groups group-verifications)] |
| 179 | + (cond |
| 180 | + (not provider) |
| 181 | + (err request "The format of the URL is invalid.") |
| 182 | + |
| 183 | + (= candidate-groups group-verification-names) |
| 184 | + (let [{:keys [created verified_by]} (first group-verifications)] |
| 185 | + (err request (format "Groups already verified by user '%s' on %s." |
| 186 | + verified_by |
| 187 | + (common/format-date created)))) |
| 188 | + |
| 189 | + (not (= username username-from-url)) |
| 190 | + (err request (format "The verification repo is for user '%s', not '%s' (you)." |
| 191 | + username-from-url username)) |
| 192 | + |
| 193 | + (not (repo-exists? url)) |
| 194 | + (err request "The verification repo does not exist.") |
| 195 | + |
| 196 | + :else |
| 197 | + (verify-groups db request username groups-to-verify)))) |
0 commit comments