Skip to content

Commit

Permalink
show build status in badges; fix #243
Browse files Browse the repository at this point in the history
  • Loading branch information
martinklepsch committed Mar 26, 2019
1 parent 970c543 commit 52c7352
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
26 changes: 24 additions & 2 deletions src/cljdoc/server/build_log.clj
Expand Up @@ -20,7 +20,8 @@
(completed! [_ build-id])
(get-build [_ build-id])
(recent-builds [_ days])
(running-build [_ group-id artifact-id version]))
(running-build [_ group-id artifact-id version])
(last-build [_ group-id artifact-id version]))

(defrecord SQLBuildTracker [db-spec]
IBuildTracker
Expand Down Expand Up @@ -87,7 +88,28 @@
"order by id desc "
"limit 1")
group-id artifact-id version
(str (.minus (Instant/now) (Duration/ofMinutes 10)))]))))
(str (.minus (Instant/now) (Duration/ofMinutes 10)))])))
(last-build [_ group-id artifact-id version]
(first
(sql/query db-spec [(str "select * from builds where "
"(group_id is ? "
"and artifact_id = ? "
"and version = ?) "
"and (import_completed_ts is not null "
"or error is not null) "
"order by id desc "
"limit 1")
group-id artifact-id version]))))


(defn api-import-successful? [build]
(and (:api_imported_ts build)
(nil? (:error build))))

(defn git-import-successful? [build]
(and (:scm_url build)
(:git_imported_ts build)
(nil? (:git_problem build))))

(comment
(require 'ragtime.repl 'ragtime.jdbc)
Expand Down
46 changes: 30 additions & 16 deletions src/cljdoc/server/pedestal.clj
Expand Up @@ -226,28 +226,42 @@
(cljdoc.render.build-log/builds-page)
(pu/ok-html ctx)))})

(defn badge-url [status color]
(format "https://badgen.now.sh/badge/cljdoc/%s/%s"
(string/replace status #"/" "%2F")
(name color)))

(defn badge-interceptor []
(defn return-badge [ctx status color]
(let [url (format "https://badgen.now.sh/badge/cljdoc/%s/%s"
(string/replace status #"/" "%2F")
(name color))]
(->> {:status 200
:headers {"Content-Type" "image/svg+xml;charset=utf-8"
"Cache-Control" (format "public; max-age=%s" (* 30 60))}
:body (:body (clj-http.lite.client/get url {:headers {"User-Agent" "clj-http-lite"}}))}
(assoc ctx :response))))

(defn badge-interceptor [build-tracker]
{:name ::badge
:enter (fn badge [ctx]
(log/info "Badge req headers" (-> ctx :request :headers))
(let [project (-> ctx :request :path-params :project)
release (try (repos/latest-release-version project)
(catch Exception e
(log/warnf "Could not find release for %s" project)))
url (if release
(badge-url release :blue)
(badge-url (str "no%20release%20found%20for%20" project) :red))
badge (clj-http.lite.client/get url {:headers {"User-Agent" "clj-http-lite"}})]
(->> {:status 200
:headers {"Content-Type" "image/svg+xml;charset=utf-8"
"Cache-Control" (format "public; max-age=%s" (* 30 60))}
:body (:body badge)}
(assoc ctx :response))))})
last-build (when release
(build-log/last-build build-tracker
(util/group-id project)
(util/artifact-id project)
release))
[status color] (cond
(nil? release)
[(str "no%20release%20found%20for%20" project) :red]

(and release last-build (not (build-log/api-import-successful? last-build)))
["API import failed" :red]

(and release last-build (not (build-log/git-import-successful? last-build)))
["Git import failed" :red]

:else
[release :blue])]
(return-badge ctx status color)))})

(defn jump-interceptor []
{:name ::jump
Expand Down Expand Up @@ -372,7 +386,7 @@
:artifact/current-via-short-id [(jump-interceptor)]
:artifact/current [(jump-interceptor)]
:jump-to-project [(jump-interceptor)]
:badge-for-project [(badge-interceptor)])
:badge-for-project [(badge-interceptor build-tracker)])
(assoc route :interceptors)))

(defmethod ig/init-key :cljdoc/pedestal [_ opts]
Expand Down

0 comments on commit 52c7352

Please sign in to comment.