Update repo name when processing issue/PR webhooks

This commit is contained in:
Vitaliy Vlasov 2018-02-27 17:38:27 +02:00 committed by Tetiana Churikova
parent 46f5f9f501
commit 13fb8dc4ec
4 changed files with 37 additions and 106 deletions

View File

@ -102,19 +102,16 @@ WHERE i.repo_id = :repo_id
AND i.confirm_hash is null
AND i.is_open = true;
-- :name update-repo-generic :! :n
/* :require [clojure.string :as string]
[hugsql.parameters :refer [identifier-param-quote]] */
-- :name update-repo-name :! :n
UPDATE repositories
SET
/*~
(string/join ","
(for [[field _] (:updates params)]
(str (identifier-param-quote (name field) options)
" = :v:updates." (name field))))
~*/
where repo_id = :repo_id;
SET repo = :repo_name
WHERE repo_id = :repo_id
AND repo != :repo_name
-- :name update-repo-state :! :n
UPDATE repositories
SET state = :repo_state
WHERE repo_id = :repo_id
-- Issues --------------------------------------------------------------------------

View File

@ -25,13 +25,17 @@
(jdbc/with-db-connection [con-db *db*]
(db/get-enabled-repositories con-db {:user_id user-id}))))
(defn update-repo
[repo-id updates]
(defn update-repo-name
[repo-id repo-name]
(jdbc/with-db-connection [con-db *db*]
(db/update-repo-generic con-db {:repo_id repo-id
:updates updates})))
(db/update-repo-name con-db {:repo_id repo-id
:repo_name repo-name})))
(defn update-repo-state
[repo-id repo-state]
(jdbc/with-db-connection [con-db *db*]
(db/update-repo-name con-db {:repo_id repo-id
:repo_state repo-state})))
(defn get-repo
"Get a repo from DB given it's full name (owner/repo-name)"
[full-name]

View File

@ -46,67 +46,6 @@
(log/debug "token" token "member?" member?)
member?))
(defn enable-repo [repo-id repo full-repo token]
(log/debug "enable-repo" repo-id repo)
(when (github/webhook-exists? full-repo token)
(github/remove-our-webhooks full-repo token))
(let [hook-secret (random/base64 32)]
(repositories/update-repo repo-id {:state 1
:hook_secret hook-secret})
(let [created-hook (github/add-webhook full-repo token hook-secret)]
(log/debug "Created webhook:" created-hook)
(repositories/update-repo repo-id {:hook_id (:id created-hook)})))
(github/create-label full-repo token)
(repositories/update-repo repo-id {:state 2})
(when (add-bounties-for-existing-issues?)
(bounties/add-bounties-for-existing-issues full-repo)))
(defn disable-repo [repo-id full-repo hook-id token]
(log/debug "disable-repo" repo-id full-repo)
(github/remove-webhook full-repo hook-id token)
(repositories/update-repo repo-id {:hook_secret ""
:state 0
:hook_id nil}))
;; NOTE(oskarth): This and above two functions about to be deprecated with Github App
(defn handle-toggle-repo [user params can-create?]
(log/info "XXX handle-toggle-repo" (pr-str user) (pr-str params))
(let [{user-id :id} user
{repo-id :id
full-repo :full_name
owner-avatar-url :owner-avatar-url
token :token
repo :name} params
[owner _] (str/split full-repo #"/")
db-user (users/get-user (:id user))]
(cond (not can-create?)
{:status 400
:body "Please join our Riot - chat.status.im/#/register and request
access in our #openbounty room to have your account whitelisted"}
(empty? (:address db-user))
{:status 400
:body "Please add your ethereum address to your profile first"}
:else
(try
(let [_ (println "CREATING")
db-item (repositories/create (merge params {:user_id user-id
:owner owner}))
is-enabled (= 2 (:state db-item))]
(if is-enabled
(disable-repo repo-id full-repo (:hook_id db-item) token)
(enable-repo repo-id repo full-repo token))
(ok (merge
{:enabled (not is-enabled)}
(select-keys params [:id :full_name]))))
(catch Exception e
(log/error "exception when enabling repo" e)
(repositories/update-repo repo-id {:state -1})
(internal-server-error))))))
(defn in? [coll elem]
(some #(= elem %) coll))
@ -286,9 +225,4 @@
:auth-rules authenticated?
:current-user user
(log/debug "/user/bounties")
(ok (user-bounties user)))
(POST "/repository/toggle" {:keys [params]}
;; NOTE: Don't allow anyone to create repos; manual add
:auth-rules authenticated?
:current-user user
(handle-toggle-repo user params (user-whitelisted? (:login user)))))))
(ok (user-bounties user))))))

View File

@ -199,9 +199,15 @@
new-title (:title gh-issue)]
(issues/update-issue-title issue-id new-title)))
(defn update-repo-name [webhook-payload]
"Update repo name in DB if changed"
(let [{repo-id :id
repo-name :name} (:repository webhook-payload)]
(repositories/update-repo-name repo-id repo-name)))
(defn handle-issue
[webhook-payload]
(update-repo-name webhook-payload)
(when-let [action (:action webhook-payload)]
(log/debug "handle-issue" action)
(when (labeled-as-bounty? action webhook-payload)
@ -216,17 +222,17 @@
(handle-issue-reopened webhook-payload)))
(ok))
(defn enable-repo-2 [repo-id full-repo]
(log/debug "enable-repo-2" repo-id full-repo)
(defn enable-repo [repo-id full-repo]
(log/debug "enable-repo" repo-id full-repo)
;; TODO(oskarth): Add granular permissions to enable creation of label
#_(github/create-label full-repo)
(repositories/update-repo repo-id {:state 2})
(repositories/update-repo-state repo-id 2)
(when (add-bounties-for-existing-issues?)
(bounties/add-bounties-for-existing-issues full-repo)))
(defn disable-repo-2 [repo-id full-repo]
(log/debug "disable-repo-2" repo-id full-repo)
(repositories/update-repo repo-id {:state 0}))
(defn disable-repo [repo-id full-repo]
(log/debug "disable-repo" repo-id full-repo)
(repositories/update-repo-state repo-id 0))
(defn full-repo->owner [full-repo]
(try
@ -236,8 +242,6 @@
(log/error "exception when parsing repo" e)
nil)))
;; NOTE(oskarth): Together with {enable,disable}-repo-2 above, this replaces
;; handle-toggle-repo for Github App.
(defn handle-add-repo [user-id username owner-avatar-url repo can-create?]
(let [repo-id (:id repo)
repo-name (:name repo)
@ -277,14 +281,14 @@
_ (log/info "handle-add-repo db-item" db-item)
is-enabled (= 2 (:state db-item))]
(if is-enabled
(disable-repo-2 repo-id full-repo)
(enable-repo-2 repo-id full-repo))
(disable-repo repo-id full-repo)
(enable-repo repo-id full-repo))
(ok {:enabled (not is-enabled)
:id repo-id
:full_name full-repo}))
(catch Exception e
(log/error "exception when enabling repo" e)
(repositories/update-repo repo-id {:state -1})
(repositories/update-repo-state repo-id -1)
(internal-server-error))))))
(defn handle-installation [{:keys [action installation repositories sender]}]
@ -327,12 +331,13 @@
(ok))
(defn handle-pull-request
[pull-request]
(let [action (keyword (:action pull-request))]
[webhook-payload]
(update-repo-name webhook-payload)
(let [action (keyword (:action webhook-payload))]
(when (contains? #{:opened
:edited
:closed} action)
(handle-pull-request-event action pull-request))
(handle-pull-request-event action webhook-payload))
(ok)))
@ -385,14 +390,5 @@
"pull_request" (handle-pull-request payload)
"installation" (handle-installation payload)
"installation_repositories" (handle-installation-repositories payload)
;; NOTE(oskarth): These two webhooks are / will be deprecated on
;; November 22, 2017 but they keep being called. According to
;; documentation they should contain same format.
;; https://developer.github.com/webhooks/
"integration_installation" (handle-installation payload)
"integration_installation_repositories" (handle-installation-repositories payload)
(ok)))
(forbidden)))))