Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4ee8cb0502
Fetching contributors…

Cannot retrieve contributors at this time

114 lines (97 sloc) 4.143 kb
(ns pull-panel.views.welcome
(:require [pull-panel.views.common :as common])
(:use [noir.core :only [defpage]]
[hiccup.core :only [html]]
[noir.options :only [dev-mode?]])
(:require [noir.response :as resp]
[clj-http.client :as client]
[noir.session :as session]
[clj-json.core :as json]
[clojure.set :as set]
[hiccup.form-helpers :as form]
[ :as page-helpers]))
(defpage "/repos/delete" {:keys [user repo]}
(session/put! :repos (remove #{[user repo]} (session/get :repos)))
(session/flash-put! (str "Deleted repo " user "/" repo) )
(resp/redirect "/"))
(defn github-auth-callback-host []
"When server is started in development mode it returns URL that points to"
(if (dev-mode?) "" ""))
(defn pull-list [pulls]
(page-helpers/ordered-list (map #(page-helpers/link-to (% "html_url") (% "body")) pulls)))
(defn format-pull-structure [pulls]
(if (map? pulls)
(if (empty? pulls)
" No open pulls"
(pull-list pulls))))
(defn linked-repo [org repo pulls token]
[:a {:href (str "" org "/" repo)} org "/" repo " "]
[:a {:href (str "/repos/delete?user=" org "&repo=" repo "&token=" token) } "(Unwatch)"]
[:span.pulls (format-pull-structure pulls) ]
(defn pulls-for [user repo token]
(let [pull-url (str "" user "/" repo "/pulls?access_token=" token)]
[user repo (json/parse-string ((client/get pull-url {:accept :json :throw-exceptions false}) :body))]))
(defn all-pulls [repos token]
(map #(pulls-for (first %) (last %) token) repos))
(defn new-repo-form []
"Form for adding a new repo to polled collection"
(form/form-to [:post "/repos"]
(form/label "user" "User / Organization")
(form/text-field "user")]
(form/label "repo" "Repository")
(form/text-field "repo")
(form/submit-button "Submit")))
(defn repo-html-list [repos token]
(if (empty? repos)
[:p "Add some repos and we'll show you Pull Requests."]
(map #(linked-repo (% 0) (% 1) (% 2) (session/get :token))
(session/get :repos)
(session/get :token))))))
(defpage "/" []
[:h1 "Pull Panel"]
(if (session/get :token)
(repo-html-list (session/get :repos) (session/get :token))
[:h2 "Add a new repo"]
[:p "You must log in to use Pull Panel."])))
(defpage "/auth/github" []
(str ""
"client_id=" (get (System/getenv) "GITHUB_CLIENT_ID") "&"
"redirect_uri=" (github-auth-callback-host) "/auth/github-callback&"
(defn delete-repo-form [user repo]
"Form to delete a repo"
(form/form-to [:delete "/repos"]
(form/hidden-field "user" user)
(form/hidden-field "repo" repo)
(form/submit-button "Unwatch")))
(defpage [:post "/repos"] {:keys [user repo]}
(session/put! :repos (set/union (session/get :repos) #{[user repo]}))
(session/flash-put! (str "Added repo " user "/" repo) )
(resp/redirect "/"))
(defpage "/logout" []
(session/flash-put! "You have been logged out")
(resp/redirect "/"))
(defpage "/auth/github-callback" {:keys [code]}
(let [res (client/post ""
{:form-params {:client_id (get (System/getenv) "GITHUB_CLIENT_ID")
:client_secret (get (System/getenv) "GITHUB_SECRET")
:code code }
:accept :json})]
((session/put! :token ((json/parse-string (res :body)) "access_token"))
(session/flash-put! (str "Github user session created" ))
(resp/redirect "/"))))
Jump to Line
Something went wrong with that request. Please try again.