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

View File

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

View File

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