-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from nenadalm/circleci
Add circleci support
- Loading branch information
Showing
10 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
(ns antq.dep.circle-ci | ||
(:require | ||
[clojure.java.io :as io] | ||
[clojure.string :as str] | ||
[clj-yaml.core :as yaml] | ||
[antq.util.dep :as u.dep] | ||
[antq.record :as r])) | ||
|
||
(defn extract-deps [file-path content-str] | ||
(let [parsed (yaml/parse-string content-str)] | ||
(->> parsed | ||
:orbs | ||
vals | ||
(mapv (fn [orb-s] | ||
(let [[orb-name version] (str/split orb-s #"@" 2)] | ||
(r/map->Dependency {:name orb-name | ||
:version version | ||
:type :circle-ci-orb | ||
:project :circle-ci | ||
:file file-path}))))))) | ||
|
||
(defn load-deps | ||
{:malli/schema [:function | ||
[:=> :cat [:maybe r/?dependencies]] | ||
[:=> [:cat 'string?] [:maybe r/?dependencies]]]} | ||
([] (load-deps ".")) | ||
([dir] | ||
(let [config-file (io/file dir ".circleci/config.yml")] | ||
(when (.exists config-file) | ||
(extract-deps (u.dep/relative-path config-file) | ||
(slurp config-file)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
(ns antq.upgrade.circle-ci | ||
(:require | ||
[clojure.string :as str] | ||
[antq.upgrade :as upgrade] | ||
[rewrite-indented.zip :as ri.zip])) | ||
|
||
(defn- update-value | ||
[new-value] | ||
(fn [line] | ||
(str/replace line #"([^@]+\s*@\s*['\"]?)[^\s'\"]+(['\"]?)" | ||
(str "$1" new-value "$2")))) | ||
|
||
(defn upgrade-dep [loc version-checked-dep] | ||
(loop [loc loc] | ||
(if-let [loc (ri.zip/find-next-string loc #(re-seq (re-pattern (str "[^:]+\\s*:\\s*" (:name version-checked-dep) "@")) %))] | ||
(recur (-> (ri.zip/update loc (update-value (:latest-version version-checked-dep))) | ||
ri.zip/next)) | ||
(ri.zip/move-to-root loc)))) | ||
|
||
(defmethod upgrade/upgrader :circle-ci | ||
[version-checked-dep] | ||
(some-> (:file version-checked-dep) | ||
(ri.zip/of-file) | ||
(upgrade-dep version-checked-dep) | ||
(ri.zip/root-string))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(ns antq.ver.circle-ci-orb | ||
(:require | ||
[clojure.java.io :as io] | ||
[clojure.string :as str] | ||
[clojure.data.json :as json] | ||
[antq.log :as log] | ||
[antq.ver :as ver])) | ||
|
||
(defn- orb-id [orb-ns orb-name] | ||
(try | ||
(-> (io/as-url (str "https://internal.circleci.com/api/v2/orbs?ns=" orb-ns "&name=" orb-name)) | ||
slurp | ||
(json/read-str :key-fn keyword) | ||
:items | ||
first | ||
:id) | ||
(catch Exception ex | ||
(log/error (str "Failed to fetch orb id from circleci: " | ||
(.getMessage ex)))))) | ||
|
||
(defn- orb-versions [id] | ||
(try | ||
(-> (io/as-url (str "https://internal.circleci.com/api/v2/orbs/" id)) | ||
slurp | ||
(json/read-str :key-fn keyword) | ||
:versions) | ||
(catch Exception ex | ||
(log/error (str "Failed to fetch orb versions from circleci: " | ||
(.getMessage ex)))))) | ||
|
||
(defmethod ver/get-sorted-versions :circle-ci-orb | ||
[dep _options] | ||
(let [[orb-ns orb-name] (str/split (:name dep) #"/" 2) | ||
id (orb-id orb-ns orb-name)] | ||
(orb-versions id))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(ns antq.dep.circle-ci-test | ||
(:require | ||
[antq.dep.circle-ci :as sut] | ||
[antq.record :as r] | ||
[clojure.test :as t] | ||
[clojure.java.io :as io])) | ||
|
||
(defn- circle-ci-orb-dependency | ||
[m] | ||
(r/map->Dependency (merge {:project :circle-ci | ||
:type :circle-ci-orb | ||
:file "dep/test_circle_ci.yml"} m))) | ||
|
||
(t/deftest extract-deps-test | ||
(let [deps (sut/extract-deps "dep/test_circle_ci.yml" | ||
(slurp (io/resource "dep/test_circle_ci.yml")))] | ||
(t/is (sequential? deps)) | ||
(t/is (every? #(instance? antq.record.Dependency %) deps)) | ||
(t/is (= #{(circle-ci-orb-dependency {:name "circleci/node" :version "6.3.0"}) | ||
(circle-ci-orb-dependency {:name "circleci/docker" :version "2.8.0"})} | ||
(set deps))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
(ns antq.upgrade.circle-ci-test | ||
(:require | ||
[antq.upgrade :as upgrade] | ||
[antq.upgrade.circle-ci] | ||
[antq.record :as r] | ||
[antq.dep.circle-ci :as dep.circle-ci] | ||
[antq.test-helper :as h] | ||
[clojure.java.io :as io] | ||
[clojure.test :as t])) | ||
|
||
(def ^:private node-dep | ||
(r/map->Dependency {:project :circle-ci | ||
:type :circle-ci-orb | ||
:name "circleci/node" | ||
:version "6.3.0" | ||
:latest-version "7.0.0" | ||
:file (io/resource "dep/test_circle_ci.yml")})) | ||
|
||
(t/deftest upgrade-dep-test | ||
(t/testing "supported" | ||
(let [from-deps (->> (:file node-dep) | ||
(slurp) | ||
(dep.circle-ci/extract-deps "")) | ||
temp-content (->> node-dep | ||
(upgrade/upgrader)) | ||
to-deps (h/with-temp-file | ||
[temp-file temp-content] | ||
(->> (assoc node-dep | ||
:version "7.0.0" | ||
:file temp-file) | ||
(upgrade/upgrader) | ||
(dep.circle-ci/extract-deps "")))] | ||
(t/is (= #{{:name "circleci/node" :version {:- "6.3.0" :+ "7.0.0"}}} | ||
(h/diff-deps from-deps to-deps)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(ns antq.ver.circle-ci-orb-test | ||
(:require | ||
[clojure.test :as t] | ||
[antq.record :as r] | ||
[antq.ver :as ver] | ||
[antq.ver.circle-ci-orb :as sut])) | ||
|
||
(defn- dep | ||
[m] | ||
(r/map->Dependency (merge {:type :circle-ci-orb} m))) | ||
|
||
(defn- orb-id [orb-ns orb-name] | ||
(get-in {"circleci" {"node" "circleci-node-id"}} [orb-ns orb-name])) | ||
|
||
(defn- orb-versions [id] | ||
(get {"circleci-node-id" ["3.0.0" "2.0.0" "1.0.0"]} id)) | ||
|
||
(t/deftest get-sorted-versions-test | ||
(with-redefs [sut/orb-id orb-id | ||
sut/orb-versions orb-versions] | ||
(t/is (= ["3.0.0" "2.0.0" "1.0.0"] | ||
(ver/get-sorted-versions (dep {:name "circleci/node" | ||
:version "1.0.0"}) | ||
{}))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
version: 2.1 | ||
|
||
orbs: | ||
node: circleci/[email protected] | ||
docker: circleci/[email protected] |