Add support for using org repos

Now repositories are shown with their full name ('<org/user>/repo'
instead of 'repo') and it is possible to add bounties for both
user repositories and organization repositories. Using org repos
requires the user to have admin rights to the repo.
This commit is contained in:
Teemu Patja 2017-01-19 11:08:43 +02:00
parent e68891a936
commit 60cc76919e
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
4 changed files with 53 additions and 42 deletions

View File

@ -9,7 +9,8 @@
[commiteth.config :refer [env]] [commiteth.config :refer [env]]
[digest :refer [sha-256]] [digest :refer [sha-256]]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[cheshire.core :as json]) [cheshire.core :as json]
[clojure.string :as str])
(:import [java.util UUID])) (:import [java.util UUID]))
(def ^:dynamic url "https://api.github.com/") (def ^:dynamic url "https://api.github.com/")
@ -25,7 +26,7 @@
(defn authorize-url [] (defn authorize-url []
(let [params (codec/form-encode {:client_id (client-id) (let [params (codec/form-encode {:client_id (client-id)
:redirect_uri (redirect-uri) :redirect_uri (redirect-uri)
:scope "admin:repo_hook repo" :scope "admin:repo_hook repo admin:org_hook"
:allow_signup true :allow_signup true
:state (str (UUID/randomUUID))})] :state (str (UUID/randomUUID))})]
(str "https://github.com/login/oauth/authorize" "?" params))) (str "https://github.com/login/oauth/authorize" "?" params)))
@ -33,12 +34,12 @@
(defn post-for-token (defn post-for-token
[code state] [code state]
(http/post "https://github.com/login/oauth/access_token" (http/post "https://github.com/login/oauth/access_token"
{:content-type :json {:content-type :json
:form-params {:client_id (client-id) :form-params {:client_id (client-id)
:client_secret (client-secret) :client_secret (client-secret)
:code code :code code
:redirect_uri (redirect-uri) :redirect_uri (redirect-uri)
:state state}})) :state state}}))
(defn- auth-params (defn- auth-params
[token] [token]
@ -69,32 +70,34 @@
"List all repos managed by the given user." "List all repos managed by the given user."
[token] [token]
(->> (->>
(map #(merge (map #(merge
{:login (get-in % login-field)} {:login (get-in % login-field)}
(select-keys % repo-fields)) (select-keys % repo-fields))
(repos/repos (merge (auth-params token) {:type "all" (repos/repos (merge (auth-params token) {:type "all"
:all-pages true}))) :all-pages true})))
(filter #(not (:fork %))) (filter #(not (:fork %)))
(filter #(-> % :permissions :admin)))) (filter #(-> % :permissions :admin))))
(defn get-user (defn get-user
[token] [token]
(users/me (auth-params token))) (users/me (auth-params token)))
(defn add-webhook (defn add-webhook
[user repo token] [full-repo token]
(log/debug "adding webhook" (str user "/" repo) token) (log/debug "adding webhook" full-repo token)
(repos/create-hook user repo "web" (let [[user repo] (str/split full-repo #"/")]
{:url (str (server-address) "/webhook") (repos/create-hook user repo "web"
:content_type "json"} {:url (str (server-address) "/webhook")
(merge (auth-params token) :content_type "json"}
{:events ["issues", "issue_comment", "pull_request"] (merge (auth-params token)
:active true}))) {:events ["issues", "issue_comment", "pull_request"]
:active true}))))
(defn remove-webhook (defn remove-webhook
[user repo hook-id token] [full-repo hook-id token]
(log/debug "removing webhook" (str user "/" repo) hook-id token) (let [[user repo] (str/split full-repo #"/")]
(repos/delete-hook user repo hook-id (auth-params token))) (log/debug "removing webhook" (str user "/" repo) hook-id token)
(repos/delete-hook user repo hook-id (auth-params token))))
(defn github-comment-hash (defn github-comment-hash
[user repo issue-number] [user repo issue-number]
@ -132,13 +135,18 @@
(let [{:keys [auth oauth-token] (let [{:keys [auth oauth-token]
:as query} query :as query} query
req (merge-with merge req (merge-with merge
{:url (tentacles/format-url end-point positional) {:url (tentacles/format-url end-point positional)
:basic-auth auth :basic-auth auth
:method :patch} :method :patch}
(when oauth-token (when oauth-token
{:headers {"Authorization" (str "token " oauth-token)}})) {:headers {"Authorization" (str "token " oauth-token)}}))
raw-query (:raw query) raw-query (:raw query)
proper-query (tentacles/query-map (dissoc query :auth :oauth-token :all-pages :accept :user-agent :otp))] proper-query (tentacles/query-map (dissoc query :auth
:oauth-token
:all-pages
:accept
:user-agent
:otp))]
(assoc req :body (json/generate-string (or raw-query proper-query))))) (assoc req :body (json/generate-string (or raw-query proper-query)))))
(defn update-comment (defn update-comment
@ -146,7 +154,8 @@
(let [comment (generate-comment user repo issue-number balance)] (let [comment (generate-comment user repo issue-number balance)]
(log/debug (str "Updating " user "/" repo "/" issue-number " comment #" comment-id " with contents: " comment)) (log/debug (str "Updating " user "/" repo "/" issue-number " comment #" comment-id " with contents: " comment))
(let [req (make-patch-request "repos/%s/%s/issues/comments/%s" (let [req (make-patch-request "repos/%s/%s/issues/comments/%s"
[user repo comment-id] (assoc (self-auth-params) :body comment))] [user repo comment-id]
(assoc (self-auth-params) :body comment))]
(tentacles/safe-parse (http/request req))))) (tentacles/safe-parse (http/request req)))))
(defn get-issue (defn get-issue
@ -158,6 +167,7 @@
(issues/issue-events user repo issue-id (self-auth-params))) (issues/issue-events user repo issue-id (self-auth-params)))
(defn create-label (defn create-label
[user repo token] [full-repo token]
(log/debug "creating bounty label" (str user "/" repo) token) (let [[user repo] (str/split full-repo #"/")]
(issues/create-label user repo "bounty" "00ff00" (auth-params token))) (log/debug "creating bounty label" (str user "/" repo) token)
(issues/create-label user repo "bounty" "00ff00" (auth-params token))))

View File

@ -77,7 +77,7 @@
:auth-rules authenticated? :auth-rules authenticated?
:current-user user :current-user user
(ok (let [{repo-id :id (ok (let [{repo-id :id
repo :name} params repo :full_name} params
{token :token {token :token
login :login login :login
user-id :id} user user-id :id} user
@ -86,9 +86,9 @@
(repositories/toggle repo-id))] (repositories/toggle repo-id))]
(if (:enabled result) (if (:enabled result)
;; @todo: do we really want to make this call at this moment? ;; @todo: do we really want to make this call at this moment?
(let [created-hook (github/add-webhook login repo token)] (let [created-hook (github/add-webhook repo token)]
(log/debug "Created webhook:" created-hook) (log/debug "Created webhook:" created-hook)
(github/create-label login repo token) (github/create-label repo token)
(repositories/update-hook-id repo-id (:id created-hook))) (repositories/update-hook-id repo-id (:id created-hook)))
(github/remove-webhook login repo (:hook_id result) token)) (github/remove-webhook repo (:hook_id result) token))
result))))) result)))))

View File

@ -137,11 +137,12 @@
(reg-event-fx (reg-event-fx
:toggle-repo :toggle-repo
(fn [{:keys [db]} [_ repo]] (fn [{:keys [db]} [_ repo]]
(println "toggle-repo" repo)
{:db db {:db db
:http {:method POST :http {:method POST
:url "/api/repository/toggle" :url "/api/repository/toggle"
:on-success #(println %) :on-success #(println %)
:params (select-keys repo [:id :login :name])}})) :params (select-keys repo [:id :login :full_name :name])}}))
(reg-event-fx (reg-event-fx
:save-user-address :save-user-address

View File

@ -8,7 +8,7 @@
(defn repository-row [repo] (defn repository-row [repo]
(let [{repo-id :id (let [{repo-id :id
url :html_url url :html_url
name :name name :full_name
description :description} repo] description :description} repo]
^{:key repo-id} ^{:key repo-id}
[:div.d-table.width-full [:div.d-table.width-full