Created
October 7, 2011 21:02
-
-
Save devth/1271345 to your computer and use it in GitHub Desktop.
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
; synchronous api call helper | |
(defmacro with-client [uri verb-fn & body] | |
`(with-open [~'client (client/create-client)] | |
(let [~'response (~verb-fn ~'client ~uri :auth auth)] | |
~@body))) | |
; helpers | |
(def job-names | |
(memoize | |
(fn [] | |
(with-client (str base-uri "api/json") #'client/GET | |
(client/await response) | |
(let [json (json/read-json (client/string response))] | |
(map (fn [item] (:name item)) (:jobs json))))))) | |
; API Calls | |
(defn last-build [job-name] | |
(println (str "Running last-build with " job-name)) | |
(with-open [client (client/create-client)] | |
(let [uri (str base-uri "job/" job-name "/lastBuild/api/json") | |
response (client/GET client uri :auth auth)] | |
(println (str "trying to fetch " uri)) | |
(client/await response) | |
(let [json (json/read-json (client/string response))] | |
(println json) | |
(if-let [building (str (:building json))] ; convert to string so a `false` doesn't give a false-negative | |
(if-let [result (str (:result json))] | |
(cf/send-message (str "Currently building: " building ". Last build result: " result)))))))) | |
(defn build [job-name] | |
(println (str "Build: " job-name)) | |
(with-open [client (client/create-client)] | |
(let [uri (str base-uri "job/" job-name "/build") | |
response (client/GET client uri :auth auth)] | |
(client/await response) | |
(cf/send-message (str "Sent a build off for " job-name))))) | |
(defn chat-job-names [job-names] | |
(cf/send-message (s/join "\n" job-names))) | |
(defn list-jobs | |
([] (list-jobs 20)) ; show 20 by default | |
([n] (if (nil? n) | |
(list-jobs) | |
(do | |
(println (str "List " n " jobs")) | |
(chat-job-names (take n (job-names))))))) | |
(defn list-jobs-matching [match] | |
(println (str "list jobs matching " match)) | |
(chat-job-names | |
(s/grep (re-pattern match) (job-names)))) | |
; parse jenkins sub-commands if cmd matches "jen" | |
(defn jenkins-hook [callback cmd args] | |
(if (re-find #"jen" (s/lower-case cmd)) | |
(let [jen-args (rest args) | |
jen-args-str (s/join " " jen-args)] | |
(condp = (first args) | |
"status" (last-build jen-args-str) | |
"build" (build jen-args-str) | |
"list" (let [arg (first jen-args)] | |
(if (empty? arg) | |
(list-jobs) | |
(let [arg (read-string arg)] | |
(if (number? arg) | |
(list-jobs arg) | |
(list-jobs-matching (name arg)))))) | |
(yetibot.core/unknown-command cmd (first args))) | |
; otherwise continue | |
(callback cmd args)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment