Skip to content

Commit

Permalink
Merge pull request #265 from nenadalm/circleci
Browse files Browse the repository at this point in the history
Add circleci support
  • Loading branch information
liquidz authored Nov 6, 2024
2 parents 11e3565 + 356562c commit ec0903c
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ If you have a trouble, please see link:./doc/non-supported-clojure-version.adoc[
| https://maven.apache.org[Maven]
|

| .circleci/config.yml
| https://circleci.com/[CircleCI]
|

| .github/workflows/*.yml
| https://github.com/features/actions[GitHub Actions]
|
Expand Down
5 changes: 5 additions & 0 deletions src/antq/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[antq.changelog :as changelog]
[antq.dep.babashka :as dep.bb]
[antq.dep.boot :as dep.boot]
[antq.dep.circle-ci :as dep.circle-ci]
[antq.dep.clojure :as dep.clj]
[antq.dep.clojure.tool :as dep.clj.tool]
[antq.dep.github-action :as dep.gh-action]
Expand All @@ -32,6 +33,7 @@
[antq.report.table]
[antq.upgrade :as upgrade]
[antq.upgrade.boot]
[antq.upgrade.circle-ci]
[antq.upgrade.clojure]
[antq.upgrade.clojure.tool]
[antq.upgrade.github-action]
Expand All @@ -43,6 +45,7 @@
[antq.util.maven :as u.maven]
[antq.util.ver :as u.ver]
[antq.ver :as ver]
[antq.ver.circle-ci-orb]
[antq.ver.git-sha]
[antq.ver.git-tag-and-sha]
[antq.ver.github-tag]
Expand All @@ -65,6 +68,7 @@

(def ^:private skippable
#{"boot"
"circle-ci"
"clojure-cli"
"github-action"
"gradle"
Expand Down Expand Up @@ -275,6 +279,7 @@
(let [skip (set (:skip options))]
(mapcat #(concat
(when-not (skip "boot") (dep.boot/load-deps %))
(when-not (skip "circle-ci") (dep.circle-ci/load-deps %))
(when-not (skip "clojure-cli") (dep.clj/load-deps %))
(when-not (skip "github-action") (dep.gh-action/load-deps %))
(when-not (skip "pom") (dep.pom/load-deps %))
Expand Down
31 changes: 31 additions & 0 deletions src/antq/dep/circle_ci.clj
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))))))
4 changes: 2 additions & 2 deletions src/antq/record.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
[:map [:url 'string?]])

(def ?type
[:enum :git-sha :git-tag-and-sha :github-tag :java])
[:enum :git-sha :git-tag-and-sha :github-tag :java :circle-ci-orb])

(def ?project
[:enum :boot :clojure :clojure-tool :github-action :gradle :leiningen :pom :shadow-cljs])
[:enum :boot :clojure :clojure-tool :github-action :gradle :leiningen :pom :shadow-cljs :circle-ci])

(def ?dependency
[:map
Expand Down
25 changes: 25 additions & 0 deletions src/antq/upgrade/circle_ci.clj
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)))
35 changes: 35 additions & 0 deletions src/antq/ver/circle_ci_orb.clj
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)))
21 changes: 21 additions & 0 deletions test/antq/dep/circle_ci_test.clj
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)))))
34 changes: 34 additions & 0 deletions test/antq/upgrade/circle_ci_test.clj
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))))))
24 changes: 24 additions & 0 deletions test/antq/ver/circle_ci_orb_test.clj
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"})
{})))))
5 changes: 5 additions & 0 deletions test/resources/dep/test_circle_ci.yml
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]

0 comments on commit ec0903c

Please sign in to comment.