Skip to content

Commit 1b4faa0

Browse files
committed
Generalize generic-package download from chart build
Signed-off-by: Greg Haskins <[email protected]>
1 parent 4c423f9 commit 1b4faa0

File tree

5 files changed

+148
-3
lines changed

5 files changed

+148
-3
lines changed

dev-resources/user.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
;; Copyright © Manetu, Inc. All rights reserved
2+
3+
(ns user
4+
(:require [clojure.tools.namespace.repl :refer [refresh]]))

project.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject io.github.manetu/gitlab-api "1.0.1-SNAPSHOT"
1+
(defproject io.github.manetu/gitlab-api "1.1.0-SNAPSHOT"
22
:description "A babashka compatible library for accessing the gitlab API "
33
:url "https://github.com/manetu/gitlab-api"
44
:license {:name "MIT"
@@ -8,4 +8,8 @@
88
[environ "1.2.0"]
99
[medley "1.4.0"]
1010
[cheshire "5.11.0"]
11-
[com.taoensso/timbre "5.2.1"]])
11+
[com.taoensso/timbre "5.2.1"]]
12+
:repl-options {:init-ns user}
13+
:profiles {:dev {:dependencies [[org.clojure/tools.namespace "1.4.4"]
14+
[criterium "0.4.6"]
15+
[eftest "0.6.0"]]}})

src/manetu/gitlab_api.clj renamed to src/manetu/gitlab/api.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
(ns manetu.gitlab-api
1+
;; Copyright © Manetu, Inc. All rights reserved
2+
3+
(ns manetu.gitlab.api
24
(:require [clojure.string :as string]
35
[babashka.curl :as curl]
46
[environ.core :refer [env]]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
;; Copyright © Manetu, Inc. All rights reserved
2+
3+
(ns manetu.gitlab.download.generic-package
4+
(:require [clojure.string :as string]
5+
[clojure.spec.alpha :as s]
6+
[babashka.curl :as curl]
7+
[taoensso.timbre :as log]
8+
[manetu.gitlab.api :as gitlab]
9+
[manetu.gitlab.semver :as semver]))
10+
11+
(def not-blank? (complement string/blank?))
12+
13+
(s/def ::version semver/valid-format?)
14+
(s/def ::package-name not-blank?)
15+
(s/def ::project-id not-blank?)
16+
(s/def ::options (s/keys :req-un [::version ::package-name ::project-id]))
17+
18+
(defn valid-options?
19+
[data]
20+
(or (s/valid? ::options data)
21+
(throw (ex-info "invalid argument" (s/explain-data ::options data)))))
22+
23+
(defn- compute-api-prefix
24+
[{:keys [project-id]}]
25+
(str "/projects/" project-id "/packages"))
26+
27+
(defn- get-packages
28+
[{:keys [package-name] :as options}]
29+
(gitlab/invoke-allpages {} "GET" (str (compute-api-prefix options) "?package_name=" package-name)))
30+
31+
(defn- has-prefix?
32+
[prefix s]
33+
(log/debug "prefix:" prefix "s:" s)
34+
(when s
35+
(some? (re-find (re-pattern (str "^" prefix)) s))))
36+
37+
(defn- open-download
38+
[{:keys [package-name] :as options} version]
39+
(let [url (str gitlab/gitlab-api-base (compute-api-prefix options) "/generic/" package-name "/" version "/" package-name ".tgz")]
40+
(log/debug "url:" url)
41+
(:body
42+
(curl/get url {:headers {"PRIVATE-TOKEN" gitlab/gitlab-token}
43+
:as :bytes}))))
44+
45+
(defn download!
46+
[{:keys [version] :as options}]
47+
{:pre [(valid-options? options)]}
48+
(let [packages (get-packages options)
49+
newest-version (->> packages (map :version) (filter (partial has-prefix? version)) semver/newest)]
50+
(log/info "downloading version:" newest-version)
51+
(open-download options newest-version)))

src/manetu/gitlab/semver.clj

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
;; Copyright © Manetu, Inc. All rights reserved
2+
;;
3+
;; adapted from https://github.com/grimradical/clj-semver
4+
5+
(ns manetu.gitlab.semver
6+
(:require [clojure.string :as string]))
7+
8+
(def pattern #"^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$")
9+
10+
(defn valid-format?
11+
"Checks the string `s` for semantic versioning formatting"
12+
[s]
13+
(if (nil? (re-matches pattern s)) false true))
14+
15+
(defn- try-parse-int
16+
"Attempt to parse `o` to an int, returning `o` if that fails or the
17+
parsed version of `o` if successful."
18+
[o]
19+
(try
20+
(Integer/parseInt o)
21+
(catch NumberFormatException e o)))
22+
23+
(defn- valid?
24+
"Checks if the supplied version map is valid or not"
25+
[v]
26+
(and (map? v)
27+
(number? (:major v))
28+
(number? (:minor v))
29+
(number? (:patch v))
30+
(>= (:major v) 0)
31+
(>= (:minor v) 0)
32+
(>= (:patch v) 0)
33+
(or (nil? (:pre-release v))
34+
(string? (:pre-release v)))
35+
(or (nil? (:build v))
36+
(string? (:build v)))))
37+
38+
(defn- parse
39+
"Parses string `s` into a version map"
40+
[s]
41+
{:pre [(string? s)
42+
(valid-format? s)]
43+
:post [(valid? %)]}
44+
(let [[[_ major minor patch pre-release build]] (re-seq pattern s)]
45+
{:major (try-parse-int major)
46+
:minor (try-parse-int minor)
47+
:patch (try-parse-int patch)
48+
:pre-release pre-release
49+
:build build}))
50+
51+
(defn- version
52+
"If `o` is a valid version map, does nothing. Otherwise, we'll
53+
attempt to parse `o` and return a version map."
54+
[o]
55+
{:post [(valid? %)]}
56+
(if (and (map? o) (valid? o))
57+
o
58+
(parse o)))
59+
60+
(defn- cmp
61+
"Compares versions a and b, returning -1 if a is older than b, 0 if
62+
they're the same version, and 1 if a is newer than b"
63+
[a b]
64+
{:post [(number? %)]}
65+
(let [try-parse-int #(try
66+
(Integer/parseInt %)
67+
(catch NumberFormatException e %))
68+
key-for-ident #(when %
69+
(into [] (map try-parse-int (string/split % #"\."))))
70+
key (juxt :major
71+
:minor
72+
:patch
73+
;; Because non-existent pre-release tags take
74+
;; precedence over existing ones
75+
#(nil? (% :pre-release))
76+
#(key-for-ident (:pre-release %))
77+
#(key-for-ident (:build %)))]
78+
(compare (key (version a))
79+
(key (version b)))))
80+
81+
(defn newest
82+
"Returns the newest version within 'versions'"
83+
[versions]
84+
(first (sort #(cmp (version %2) (version %1)) versions)))

0 commit comments

Comments
 (0)