Permalink
Browse files

save http uploads into database

  • Loading branch information...
1 parent 00fb03b commit f6f401c16a906c4834528a7af85062ce46cc7453 @xeqi xeqi committed May 6, 2012
View
@@ -4,16 +4,7 @@
[org.apache.maven/maven-model "3.0.4"
:exclusions
[org.codehaus.plexus/plexus-utils]]
-
- ;; TODO remove exclusions when updated
- ;; Fix pomegranate http deploy
- [com.cemerick/pomegranate "0.0.11"
- :exclusions
- [[org.apache.maven.wagon/wagon-http]
- [org.apache.maven.wagon/wagon-provider-api]]]
- [org.apache.maven.wagon/wagon-http "2.2"]
- [org.apache.maven.wagon/wagon-provider-api "2.2"]
-
+ [com.cemerick/pomegranate "0.0.12"]
[compojure "1.0.1"]
[ring/ring-jetty-adapter "1.0.2"]
[hiccup "0.3.8"]
View
@@ -0,0 +1,16 @@
+(ns clojars.auth
+ (:require [cemerick.friend :as friend]
+ [clojars.db :refer [group-membernames]]))
+
+(defmacro with-account [body]
+ `(friend/authenticated (try-account ~body)))
+
+(defmacro try-account [body]
+ `(let [~'account (:username (friend/current-authentication))]
+ ~body))
+
+(defmacro authorized! [group & body]
+ `(if (or (some #{~'account} (group-membernames ~group))
+ (empty? (group-membernames ~group)))
+ (do ~@body)
+ (friend/throw-unauthorized friend/*identity*)))
View
@@ -189,18 +189,32 @@
(throw (Exception. (str "You don't have access to the "
groupname " group.")))))))
-(defn- add-jar-helper [account jarmap]
- (check-and-add-group account (:group jarmap))
+(defn- add-jar-helper [account {:keys [group name version
+ description homepage authors]}]
+ (check-and-add-group account group)
(insert jars
- (values {:group_name (:group jarmap)
- :jar_name (:name jarmap)
- :version (:version jarmap)
+ (values {:group_name group
+ :jar_name name
+ :version version
:user account
:created (get-time)
- :description (:description jarmap)
- :homepage (:homepage jarmap)
+ :description description
+ :homepage homepage
:authors (str/join ", " (map #(.replace % "," "")
- (:authors jarmap)))})))
+ authors))})))
+
+(defn update-jar [account {:keys [group name version
+ description homepage authors]}]
+ (update jars
+ (set-fields {:user account
+ :created (get-time)
+ :description description
+ :homepage homepage
+ :authors (str/join ", " (map #(.replace % "," "")
+ authors))})
+ (where {:group_name group
+ :jar_name name
+ :version version})))
(defn add-jar [account jarmap & [check-only]]
(when-not (re-matches #"^[a-z0-9-_.]+$" (:name jarmap))
View
@@ -20,3 +20,6 @@
[file]
(with-open [reader (io/reader file)]
(.read (MavenXpp3Reader.) reader)))
+
+
+(def pom-to-map (comp model-to-map read-pom))
View
@@ -0,0 +1,55 @@
+(ns clojars.repo
+ (:require [clojars.auth :refer [with-account authorized!]]
+ [clojars.db :refer [find-jar add-jar update-jar]]
+ [clojars.config :refer [config]]
+ [clojars.maven :as maven]
+ [compojure.core :refer [defroutes PUT ANY]]
+ [clojure.java.io :as io]
+ [clojure.string :as string])
+ (:import java.io.StringReader))
+
+(defn save-to-file [sent-file body]
+ (-> sent-file
+ .getParentFile
+ .mkdirs)
+ (with-open [wrtr (io/writer sent-file)]
+ (.write wrtr body)))
+
+(defroutes routes
+ (PUT ["/:group/:artifact/:file"
+ :group #".+" :artifact #"[^/]+" :file #"maven-metadata\.xml[^/]*"]
+ {body :body {:keys [group artifact file]} :params}
+ (with-account
+ (authorized!
+ (string/replace group "/" ".")
+ (save-to-file (io/file (config :repo) group artifact file)
+ (slurp body))
+ {:status 201 :headers {} :body nil})))
+ (PUT ["/:group/:artifact/:version/:file"
+ :group #".+" :artifact #"[^/]+" :version #"[^/]+" :file #"[^/]+"]
+ {body :body {:keys [group artifact version file]} :params}
+ (let [groupname (string/replace group "/" ".")]
+ (with-account
+ (authorized!
+ (string/replace group "/" ".")
+ (let [contents (slurp body)]
+ (if (.endsWith file ".pom")
+ (if (find-jar groupname artifact version)
+ (update-jar account
+ (merge
+ (maven/pom-to-map (StringReader. contents))
+ {:group groupname
+ :name artifact
+ :version version}))
+ (add-jar account (merge
+ (maven/pom-to-map (StringReader. contents))
+ {:group groupname
+ :name artifact
+ :version version})))
+ (when-not (find-jar groupname artifact version)
+ (add-jar account {:group groupname
+ :name artifact
+ :version version})))
+ (save-to-file (io/file (config :repo) group artifact version file)
+ contents))
+ {:status 201 :headers {} :body nil})))))
View
@@ -76,11 +76,6 @@
(defmacro printerr [& strs]
`(.println (.err ~'ctx) (str ~@(interleave strs (repeat " ")))))
-(defn read-metadata [f default-group]
- (let [model (maven/read-pom (:file f))
- jarmap (maven/model-to-map model)]
- [[model jarmap]]))
-
(defn jar-names
"Construct a few possible name variations a jar might have."
[jarmap]
@@ -99,8 +94,8 @@
(doseq [metafile metadata
:when (not= (:name metafile) "maven-metadata.xml")
- [model jarmap] (read-metadata metafile act-group)
- :let [names (jar-names jarmap)]]
+ :let [jarmap (maven/pom-to-map (:file metafile))
+ names (jar-names jarmap)]]
(if-let [jarfile (some jarfiles names)]
(do
(.println (.err ctx) (str "\nDeploying " (:group jarmap) "/"
View
@@ -3,6 +3,8 @@
find-jar recent-versions count-versions
find-user-by-user-or-email]]
[clojars.config :refer [config]]
+ [clojars.auth :refer [with-account try-account]]
+ [clojars.repo :as repo]
[clojars.web.dashboard :refer [dashboard index-page]]
[clojars.web.search :refer [search]]
[clojars.web.user :refer [profile-form update-profile show-user
@@ -29,42 +31,8 @@
(html [:h1 "Page not found"]
[:p "Thundering typhoons! I think we lost it. Sorry!"]))
-(defmacro with-account [body]
- `(friend/authenticated (try-account ~body)))
-
-(defmacro try-account [body]
- `(let [~'account (:username (friend/current-authentication))]
- ~body))
-
-(defn save-to-file [sent-file body]
- (-> sent-file
- .getParentFile
- .mkdirs)
- (with-open [wrtr (io/writer sent-file)]
- (.write wrtr (slurp body))))
-
-(defroutes repo-routes
- (PUT ["/:group/:artifact/:file"
- :group #".+" :artifact #"[^/]+" :file #"maven-metadata\.xml[^/]*"]
- {body :body {:keys [group artifact file]} :params}
- (with-account
- (if (some #{account}
- (group-membernames (string/replace group "/" ".")))
- (do (save-to-file (io/file (config :repo) group artifact file) body)
- {:status 201 :headers {} :body nil})
- (friend/throw-unauthorized friend/*identity*))))
- (PUT ["/:group/:artifact/:version/:file"
- :group #".+" :artifact #"[^/]+" :version #"[^/]+" :file #"[^/]+"]
- {body :body {:keys [group artifact version file]} :params}
- (with-account
- (if (some #{account}
- (group-membernames (string/replace group "/" ".")))
- (do (save-to-file (io/file (config :repo) group artifact version file) body)
- {:status 201 :headers {} :body nil})
- (friend/throw-unauthorized friend/*identity*)))))
-
(defroutes main-routes
- (context "/repo" request repo-routes)
+ (context "/repo" request repo/routes)
(GET "/search" {session :session params :params}
(try-account
(search account params)))
@@ -8,6 +8,7 @@
<packaging>jar</packaging>
<name>asdf</name>
+ <url>https://example.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -47,7 +47,7 @@
(deftest user-can-register-and-deploy
(-> (session clojars-app)
- (register-as "dantheman" "test@example.org" "password" valid-ssh-key))
+ (register-as "dantheman" "test@example.org" "password" ""))
(help/delete-file-recursively help/local-repo)
(help/delete-file-recursively help/local-repo2)
(aether/deploy
@@ -69,37 +69,37 @@
(has (status? 200))
(within [:article [:ul enlive/last-of-type] [:li enlive/last-child] :a]
(has (text? "dantheman")))
- (doto prn)
- (follow "test")))
+ (follow "org.clojars.dantheman/test")))
-;; (deftest user-can-deploy-to-new-group
-;; (-> (session clojars-app)
-;; (register-as "dantheman" "test@example.org" "password" valid-ssh-key))
-;; (help/delete-file-recursively help/local-repo)
-;; (help/delete-file-recursively help/local-repo2)
-;; (aether/deploy
-;; :coordinates '[fake/test "1.0.0"]
-;; :jar-file (io/file (io/resource "test.jar"))
-;; :pom-file (io/file (io/resource "test-0.0.1/test.pom"))
-;; :repository {"test" {:url (str "http://localhost:" test-port "/repo")
-;; :username "dantheman"
-;; :password "password"}}
-;; :local-repo help/local-repo)
-;; (is (= '{[fake/test "1.0.0"] nil}
-;; (aether/resolve-dependencies
-;; :coordinates '[[fake/test "1.0.0"]]
-;; :repositories {"test" {:url
-;; (str "http://localhost:" test-port "/repo")}}
-;; :local-repo help/local-repo2)))
-;; (-> (session clojars-app)
-;; (visit "/groups/fake")
-;; (has (status? 200))
-;; (within [:article [:ul enlive/last-of-type] [:li enlive/last-child] :a]
-;; (has (text? "dantheman")))
-;; (doto prn)
-;; (follow "test")))
+(deftest user-can-deploy-to-new-group
+ (-> (session clojars-app)
+ (register-as "dantheman" "test@example.org" "password" ""))
+ (help/delete-file-recursively help/local-repo)
+ (help/delete-file-recursively help/local-repo2)
+ (aether/deploy
+ :coordinates '[fake/test "1.0.0"]
+ :jar-file (io/file (io/resource "test.jar"))
+ :pom-file (io/file (io/resource "test-0.0.1/test.pom"))
+ :repository {"test" {:url (str "http://localhost:" test-port "/repo")
+ :username "dantheman"
+ :password "password"}}
+ :local-repo help/local-repo)
+ (is (= '{[fake/test "1.0.0"] nil}
+ (aether/resolve-dependencies
+ :coordinates '[[fake/test "1.0.0"]]
+ :repositories {"test" {:url
+ (str "http://localhost:" test-port "/repo")}}
+ :local-repo help/local-repo2)))
+ (-> (session clojars-app)
+ (visit "/groups/fake")
+ (has (status? 200))
+ (within [:article [:ul enlive/last-of-type] [:li enlive/last-child] :a]
+ (has (text? "dantheman")))
+ (follow "fake/test")
+ (within [:article [:p (enlive/nth-of-type 2)]]
+ (has (text? "https://example.org")))))
-(deftest user-cannot-deploy-to-other-users-groups
+(deftest user-cannot-deploy-to-groups-without-permission
(-> (session clojars-app)
(register-as "dantheman" "test@example.org" "password" valid-ssh-key))
(-> (session clojars-app)
@@ -124,8 +124,7 @@
:local-repo help/local-repo))))
(deftest bad-login-cannot-deploy
- (is (thrown-with-msg? org.sonatype.aether.deployment.DeploymentException
- #"Unauthorized"
+ (is (thrown? org.sonatype.aether.deployment.DeploymentException
(aether/deploy
:coordinates '[fake/test "1.0.0"]
:jar-file (io/file (io/resource "test.jar"))

0 comments on commit f6f401c

Please sign in to comment.