Skip to content

Fallback to Clojars for finding artifact version #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [#251](https://github.com/clojure-emacs/refactor-nrepl/pull/251) `clean-ns` support extra message key `relative-path`, which will be used if `path` does not exist.
* [#256](https://github.com/clojure-emacs/refactor-nrepl/pull/256) ignore malformed artifact coordinates when fetching from Clojars.
* [#264](https://github.com/clojure-emacs/refactor-nrepl/pull/264) less bandwidth used to fetch artifacts from Clojars
* [#268](https://github.com/clojure-emacs/refactor-nrepl/pull/268) added support for fetching artifact versions from Clojars

## 2.4.0

Expand Down
18 changes: 15 additions & 3 deletions src/refactor_nrepl/artifacts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@

(defn- get-mvn-versions!
"Fetches all the versions of particular artifact from maven repository."
[for-artifact]
(let [[group-id artifact] (str/split for-artifact #"/")
[artifact]
(let [[group-id artifact] (str/split artifact #"/")
search-prefix "http://search.maven.org/solrsearch/select?q=g:%22"
{:keys [_ _ body _]} @(http/get (str search-prefix
group-id
Expand All @@ -101,6 +101,14 @@
(map #(vector (str group-id "/" %) nil))))
group-ids)))

(defn get-clojars-versions!
"Fetches all the versions of particular artifact from Clojars."
[artifact]
(let [{:keys [body status]} @(http/get (str "https://clojars.org/api/artifacts/"
artifact))]
(when (= 200 status)
(map :version (:recent_versions (json/parse-string body true))))))

(defn- get-artifacts-from-clojars!
[]
(reduce #(update %1 (str (first %2)) conj (second %2))
Expand All @@ -123,9 +131,13 @@
(->> @artifacts keys list*))

(defn artifact-versions
"Returns a sorted list of artifact version strings. The list can either come
from the artifacts cache, the maven search api or the clojars search api in
that order."
[{:keys [artifact]}]
(->> (or (get @artifacts artifact)
(get-mvn-versions! artifact))
(seq (get-mvn-versions! artifact))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good idea to explain the behaviour of this function in the docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

(get-clojars-versions! artifact))
distinct
versions/version-sort
reverse
Expand Down
19 changes: 13 additions & 6 deletions test/refactor_nrepl/artifacts_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
[clojure.java.io :as io]
[refactor-nrepl.artifacts :as artifacts]))

(def clojure-versions (-> (io/resource "resources/clojure-versions.edn")
slurp
edn/read-string))
(defn- resource [filename]
(edn/read-string (slurp (io/resource filename))))

(def clojure-versions (resource "clojure-versions.edn"))
(def aero-versions (resource "aero-versions.edn"))

(def sorted-clojure-versions
(vector "1.7.0-alpha1"
"1.6.0" "1.6.0-RC1" "1.6.0-beta1" "1.6.0-alpha1"
"1.5.1" "1.5.0"))
(def clojure-artifacts ["clojure"])
(def clojars-artifacts (-> (io/resource "resources/clojars-artifacts.edn")
slurp
edn/read-string))
(def clojars-artifacts (resource "clojars-artifacts.edn"))

(deftest creates-a-map-of-artifacts
(reset! artifacts/artifacts {})
(with-redefs
[artifacts/get-clojars-artifacts! (constantly clojars-artifacts)
artifacts/get-clojars-versions! (constantly aero-versions)
artifacts/get-mvn-artifacts! (constantly clojure-artifacts)
artifacts/get-mvn-versions! (constantly clojure-versions)]

Expand All @@ -38,6 +40,11 @@
(is (= (count (artifacts/artifact-versions {:artifact "org.clojure/clojure"}))
(count clojure-versions))))

(testing "Fetches version from Clojars when Maven has no results"
(with-redefs [artifacts/get-mvn-versions! (constantly (list))]
(is (= (set aero-versions)
(set (artifacts/artifact-versions {:artifact "aero"}))))))

(testing "Contains artifacts from clojars"
(is (contains? @artifacts/artifacts "alembic"))
(is (some #{"0.3.1"} (get-in @artifacts/artifacts ["alembic"]))))
Expand Down
25 changes: 25 additions & 0 deletions test/resources/aero-versions.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(
"1.1.3"
"1.1.2"
"1.1.1"
"1.1.0"
"1.0.3"
"1.0.2"
"1.0.1"
"1.0.0"
"1.0.0-beta5"
"1.0.0-beta4"
"1.0.0-beta3"
"1.0.0-beta2"
"1.0.0-beta1"
"0.2.3"
"0.2.2"
"0.2.1"
"0.2.0"
"0.1.5"
"0.1.4"
"0.1.3"
"0.1.2"
"0.1.1"
"0.1.0"
)