Skip to content

Commit 71e057c

Browse files
yannvanhalewynbbatsov
authored andcommitted
Fallback to Clojars for finding artifact version (#268)
1 parent bb4d730 commit 71e057c

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [#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.
66
* [#256](https://github.com/clojure-emacs/refactor-nrepl/pull/256) ignore malformed artifact coordinates when fetching from Clojars.
77
* [#264](https://github.com/clojure-emacs/refactor-nrepl/pull/264) less bandwidth used to fetch artifacts from Clojars
8+
* [#268](https://github.com/clojure-emacs/refactor-nrepl/pull/268) added support for fetching artifact versions from Clojars
89

910
## 2.4.0
1011

src/refactor_nrepl/artifacts.clj

+15-3
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979

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

104+
(defn get-clojars-versions!
105+
"Fetches all the versions of particular artifact from Clojars."
106+
[artifact]
107+
(let [{:keys [body status]} @(http/get (str "https://clojars.org/api/artifacts/"
108+
artifact))]
109+
(when (= 200 status)
110+
(map :version (:recent_versions (json/parse-string body true))))))
111+
104112
(defn- get-artifacts-from-clojars!
105113
[]
106114
(reduce #(update %1 (str (first %2)) conj (second %2))
@@ -123,9 +131,13 @@
123131
(->> @artifacts keys list*))
124132

125133
(defn artifact-versions
134+
"Returns a sorted list of artifact version strings. The list can either come
135+
from the artifacts cache, the maven search api or the clojars search api in
136+
that order."
126137
[{:keys [artifact]}]
127138
(->> (or (get @artifacts artifact)
128-
(get-mvn-versions! artifact))
139+
(seq (get-mvn-versions! artifact))
140+
(get-clojars-versions! artifact))
129141
distinct
130142
versions/version-sort
131143
reverse

test/refactor_nrepl/artifacts_test.clj

+13-6
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
[clojure.java.io :as io]
66
[refactor-nrepl.artifacts :as artifacts]))
77

8-
(def clojure-versions (-> (io/resource "resources/clojure-versions.edn")
9-
slurp
10-
edn/read-string))
8+
(defn- resource [filename]
9+
(edn/read-string (slurp (io/resource filename))))
10+
11+
(def clojure-versions (resource "clojure-versions.edn"))
12+
(def aero-versions (resource "aero-versions.edn"))
13+
1114
(def sorted-clojure-versions
1215
(vector "1.7.0-alpha1"
1316
"1.6.0" "1.6.0-RC1" "1.6.0-beta1" "1.6.0-alpha1"
1417
"1.5.1" "1.5.0"))
1518
(def clojure-artifacts ["clojure"])
16-
(def clojars-artifacts (-> (io/resource "resources/clojars-artifacts.edn")
17-
slurp
18-
edn/read-string))
19+
(def clojars-artifacts (resource "clojars-artifacts.edn"))
1920

2021
(deftest creates-a-map-of-artifacts
2122
(reset! artifacts/artifacts {})
2223
(with-redefs
2324
[artifacts/get-clojars-artifacts! (constantly clojars-artifacts)
25+
artifacts/get-clojars-versions! (constantly aero-versions)
2426
artifacts/get-mvn-artifacts! (constantly clojure-artifacts)
2527
artifacts/get-mvn-versions! (constantly clojure-versions)]
2628

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

43+
(testing "Fetches version from Clojars when Maven has no results"
44+
(with-redefs [artifacts/get-mvn-versions! (constantly (list))]
45+
(is (= (set aero-versions)
46+
(set (artifacts/artifact-versions {:artifact "aero"}))))))
47+
4148
(testing "Contains artifacts from clojars"
4249
(is (contains? @artifacts/artifacts "alembic"))
4350
(is (some #{"0.3.1"} (get-in @artifacts/artifacts ["alembic"]))))

test/resources/aero-versions.edn

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(
2+
"1.1.3"
3+
"1.1.2"
4+
"1.1.1"
5+
"1.1.0"
6+
"1.0.3"
7+
"1.0.2"
8+
"1.0.1"
9+
"1.0.0"
10+
"1.0.0-beta5"
11+
"1.0.0-beta4"
12+
"1.0.0-beta3"
13+
"1.0.0-beta2"
14+
"1.0.0-beta1"
15+
"0.2.3"
16+
"0.2.2"
17+
"0.2.1"
18+
"0.2.0"
19+
"0.1.5"
20+
"0.1.4"
21+
"0.1.3"
22+
"0.1.2"
23+
"0.1.1"
24+
"0.1.0"
25+
)

0 commit comments

Comments
 (0)