Skip to content
Browse files

Added github listener

  • Loading branch information...
1 parent 439f33b commit 5aedabe4a03e4bda19fbf21328f940c4e0e15c71 @skuro committed
Showing with 95 additions and 2 deletions.
  1. +4 −1 project.clj
  2. +6 −1 src/leiningen/misaki.clj
  3. +85 −0 src/leiningen/misaki/github.clj
View
5 project.clj
@@ -5,5 +5,8 @@
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[misaki "0.2.3-beta"]
[gh-file-reader "0.0.2"]
- [clj-text-decoration "0.0.1"]]
+ [clj-text-decoration "0.0.1"]
+ [compojure "1.1.3"]
+ [org.clojure/data.json "0.2.0"]
+ [ring/ring-jetty-adapter "1.1.0"]]
:eval-in-leiningen true)
View
7 src/leiningen/misaki.clj
@@ -1,7 +1,8 @@
(ns leiningen.misaki
(:use [misaki server config])
(:require [gh-file-reader.core :as gh]
- [text-decoration.core :as td]))
+ [text-decoration.core :as td]
+ [leiningen.misaki.github :as github]))
(defn get-current-directory []
(-> "."
@@ -23,9 +24,13 @@
project-name)
(println (td/red (str "Template '" template-name "' is not found"))))))
+(defn misaki-github-listener [destdir branch]
+ (github/listen destdir branch))
+
(defn ^:no-project-needed misaki
"Compiles your Misaki sources and starts a local server"
[project & args]
(case (first args)
"new" (apply misaki-new (rest args))
+ "listen" (apply misaki-github-listener (rest args))
(apply -main (get-current-directory) args)))
View
85 src/leiningen/misaki/github.clj
@@ -0,0 +1,85 @@
+(ns leiningen.misaki.github
+ (:use compojure.core
+ ring.adapter.jetty
+ clojure.java.io)
+ (:require [compojure.handler :as handler]
+ [clojure.data.json :as json]
+ [clojure.java.shell :as sh]
+ [misaki.server :as misaki]
+ [misaki.config :as config]))
+
+(defonce update-queue (java.util.concurrent.LinkedBlockingDeque.))
+
+(def config (atom {}))
+
+(defn create-temp-folder []
+ (let [tmpdir (System/getProperty "java.io.tmpdir")]
+ (-> (sh/sh "mktemp" "-d" (str tmpdir "misaki.XXXX"))
+ :out
+ (.replaceAll "\n" ""))))
+
+(defn compile-from [repodir]
+ ; cut-n-paste from misaki.server/-main
+ (binding [misaki.config/*base-dir* repodir]
+ (config/with-config
+ ; override the destination dir, if needed
+ (if-let [todir (:destdir @config)]
+ (binding [misaki.config/*config* (assoc misaki.config/*config* :public-dir todir)]
+ (misaki/do-all-compile))
+ (misaki/do-all-compile)))))
+
+(defn prepare-sources [tmpdir repo branch all-done]
+ (if-let [branch (:branch @config)]
+ (sh/sh "git" "clone" repo "--branch" branch tmpdir)
+ (sh/sh "git" "clone" repo tmpdir))
+ (compile-from (str tmpdir "/"))
+ (deliver all-done true))
+
+(defn checkout-sources [repo]
+ (let [tmpdir (create-temp-folder)
+ all-done (promise)]
+ (try
+ (println "Working dir: " tmpdir)
+ (prepare-sources tmpdir repo (:branch @config) all-done)
+ (finally
+ (if (.exists (file tmpdir))
+ (do
+ (deref all-done 10000 false)
+ (sh/sh "rm" "-Rf" tmpdir)))))))
+
+(defn poll-updates []
+ (while true
+ (let [url (.take update-queue)]
+ (println "Refreshing from repository: " url))))
+
+(defonce worker-tread (doto
+ (Thread. poll-updates)
+ (.setDaemon true)
+ (.start)))
+
+(defn refresh-site [payload]
+ (let [url (get-in payload [:repository :url])]
+ (if (.offer update-queue url)
+ (str "A new update has been scheduled: " url "\n")
+ "Can't schedule a new update, please try again later")))
+
+(defn process-payload [payload]
+ (if (nil? payload)
+ "Payload is null, aborting."
+ (let [mpayload (json/read-str payload :key-fn keyword)]
+ (refresh-site mpayload))))
+
+(defroutes github-listeners
+ (POST "/post-commit" [payload] (process-payload payload))
+ (GET "/manual-update" [] (refresh-site {:manual true})))
+
+(def github-listeners-app
+ (-> github-listeners
+ handler/site))
+
+(defn listen
+ ([] (listen nil nil))
+ ([destdir branch]
+ (reset! config {:destdir destdir :branch branch})
+ (run-jetty github-listeners-app {:port 9090})))
+

0 comments on commit 5aedabe

Please sign in to comment.
Something went wrong with that request. Please try again.