diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 1421ced..0673ca6 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -412,6 +412,14 @@ SELECT exists(SELECT 1 FROM issues WHERE issue_id = :issue_id); +-- :name get-issue :? :1 +-- :doc get issue from DB by repo-id and issue-number +SELECT issue_id, issue_number, is_open, winner_login, commit_sha +FROM issues +WHERE repo_id = :repo_id +AND issue_number = :issue_number; + + -- :name open-bounties :? :* -- :doc all open bounty issues diff --git a/src/clj/commiteth/db/issues.clj b/src/clj/commiteth/db/issues.clj index 834684f..a32634a 100644 --- a/src/clj/commiteth/db/issues.clj +++ b/src/clj/commiteth/db/issues.clj @@ -101,3 +101,9 @@ first :exists boolean))) + +(defn get-issue + [repo-id issue-number] + (jdbc/with-db-connection [con-db *db*] + (db/get-issue con-db {:repo_id repo-id + :issue_number issue-number}))) diff --git a/src/clj/commiteth/github/core.clj b/src/clj/commiteth/github/core.clj index 93a4f56..e51ce95 100644 --- a/src/clj/commiteth/github/core.clj +++ b/src/clj/commiteth/github/core.clj @@ -219,7 +219,7 @@ (str "Tokens: " (str/join " " (map (fn [[tla balance]] (format "%s: %.2f" (subs (str tla) 1) - (float balance))) + (double balance))) token-balances)) "\n"))) diff --git a/src/clj/commiteth/routes/webhooks.clj b/src/clj/commiteth/routes/webhooks.clj index 32997e1..8ab27bc 100644 --- a/src/clj/commiteth/routes/webhooks.clj +++ b/src/clj/commiteth/routes/webhooks.clj @@ -109,26 +109,17 @@ (extract pr-title)))) -(defn ensure-bounty-issue - "Checks if an issue has a bounty label attached and returns its number" - [user repo issue-number] - (when-let [issue (github/get-issue user repo issue-number)] - (when (bounties/has-bounty-label? issue) - issue-number))) - - (defn handle-claim - [user-id login name avatar_url owner repo repo-id bounty-issue-number pr-id pr-number head-sha merged? event-type] + [issue user-id login name avatar_url owner repo repo-id pr-id pr-number head-sha merged? event-type] (users/create-user user-id login name nil avatar_url) - (let [issue (github/get-issue owner repo bounty-issue-number) - open-or-edit? (contains? #{:opened :edited} event-type) + (let [open-or-edit? (contains? #{:opened :edited} event-type) close? (= :closed event-type) pr-data {:repo_id repo-id :pr_id pr-id :pr_number pr-number :user_id user-id - :issue_number bounty-issue-number - :issue_id (:id issue) + :issue_number (:issue_number issue) + :issue_id (:issue_id issue) :state event-type}] ;; TODO: in the opened case if the submitting user has no @@ -138,18 +129,18 @@ (cond open-or-edit? (do (log/info "PR with reference to bounty issue" - bounty-issue-number "opened") + (:issue_number issue) "opened") (pull-requests/save (merge pr-data {:state :opened :commit_sha head-sha}))) close? (if merged? (do (log/info "PR with reference to bounty issue" - bounty-issue-number "merged") + (:issue_number issue) "merged") (pull-requests/save (merge pr-data {:state :merged :commit_sha head-sha})) - (issues/update-commit-sha (:id issue) head-sha)) + (issues/update-commit-sha (:issue_id issue) head-sha)) (do (log/info "PR with reference to bounty issue" - bounty-issue-number "closed with no merge") + (:issue_number issue) "closed with no merge") (pull-requests/save (merge pr-data {:state :closed :commit_sha head-sha}))))))) @@ -177,26 +168,27 @@ pr-number :number pr-body :body pr-title :title} :pull_request}] - (log/debug "handle-pull-request-event" event-type owner repo repo-id login pr-body pr-title) - (log/debug (extract-issue-number pr-body pr-title)) - (if-let [bounty-issue-number (->> - (extract-issue-number pr-body pr-title) - (first) - (ensure-bounty-issue owner repo))] - (do - (log/debug "Referenced bounty issue found" owner repo bounty-issue-number) - (handle-claim user-id - login name - avatar_url - owner repo - repo-id - bounty-issue-number - pr-id - pr-number - head-sha - merged? - event-type)) + (log/info "handle-pull-request-event" event-type owner repo repo-id login pr-body pr-title) + (if-let [issue (some->> (extract-issue-number pr-body pr-title) + (first) + (issues/get-issue repo-id))] + (if-not (:commit_sha issue) ; no PR has been merged yet referencing this issue + (do + (log/info "Referenced bounty issue found" owner repo (:issue_number issue)) + (handle-claim issue + user-id + login name + avatar_url + owner repo + repo-id + pr-id + pr-number + head-sha + merged? + event-type)) + (log/info "PR for issue already merged")) (when (= :edited event-type) + ; Remove PR if it does not reference any issue (pull-requests/remove pr-id)))) diff --git a/src/clj/commiteth/scheduler.clj b/src/clj/commiteth/scheduler.clj index f9c25dd..90fc9cd 100644 --- a/src/clj/commiteth/scheduler.clj +++ b/src/clj/commiteth/scheduler.clj @@ -226,12 +226,6 @@ (neg? n) (- n) :else n)) -(defn float= - ([x y] (float= x y 0.0000001)) - ([x y epsilon] - (log/debug x y epsilon) - (let [scale (if (or (zero? x) (zero? y)) 1 (abs x))] - (<= (abs (- x y)) (* scale epsilon))))) (defn update-bounty-token-balances "Helper function for updating internal ERC20 token balances to token multisig contract. Will be called periodically for all open bounty contracts." @@ -291,6 +285,17 @@ (db-bounties/open-bounty-contracts)] (update-issue-usd-value bounty-addr))) +(defn float= + ([x y] (float= x y 0.0000001)) + ([x y epsilon] + (log/debug x y epsilon) + (let [scale (if (or (zero? x) (zero? y)) 1 (abs x))] + (<= (abs (- x y)) (* scale epsilon))))) + +(defn map-float= [m1 m2] + (and (= (set (keys m1)) (set (keys m2))) + (every? #(float= (get m1 %1) (get m2 %1)) (keys m1)))) + (defn update-balances [] (log/info "In update-balances") @@ -312,13 +317,13 @@ (when (or (not (float= db-balance-eth balance-eth)) - (not= db-tokens token-balances)) - (log/debug "balances differ") - (log/debug "ETH (db):" db-balance-eth (type db-balance-eth) ) - (log/debug "ETH (chain):" balance-eth (type balance-eth) ) - (log/debug "ETH cmp:" (float= db-balance-eth balance-eth)) - (log/debug "tokens (db):" db-tokens (type db-tokens) (type (:SNT db-tokens))) - (log/debug "tokens (chain):" token-balances (type token-balances) (type (:SNT token-balances))) + (not (map-float= db-tokens token-balances))) + (log/info "balances differ") + (log/info "ETH (db):" db-balance-eth (type db-balance-eth) ) + (log/info "ETH (chain):" balance-eth (type balance-eth) ) + (log/info "ETH cmp:" (float= db-balance-eth balance-eth)) + (log/info "tokens (db):" db-tokens (type db-tokens) (type (:SNT db-tokens))) + (log/info "tokens (chain):" token-balances (type token-balances) (type (:SNT token-balances))) (log/debug "tokens cmp:" (= db-tokens token-balances)) (issues/update-eth-balance contract-address balance-eth)