Edit github comment on balance update

This commit is contained in:
kagel 2016-09-12 19:44:29 +03:00
parent 397b3259b1
commit 8fb363218a
9 changed files with 93 additions and 23 deletions

View File

@ -39,6 +39,7 @@
[luminus-immutant "0.2.2"]
[overtone/at-at "1.2.0"]
[clj.qrgen "0.4.0"]
[digest "1.4.4"]
[tentacles "0.5.1"]]
:min-lein-version "2.0.0"

View File

@ -7,3 +7,5 @@ ALTER TABLE public.issues
DROP COLUMN confirm_hash;
ALTER TABLE public.issues
ADD address VARCHAR(256);
ALTER TABLE public.issues
DROP COLUMN comment_id;

View File

@ -4,6 +4,8 @@ ALTER TABLE public.issues
ADD contract_address VARCHAR(42) NULL;
ALTER TABLE public.issues
ADD confirm_hash VARCHAR(128) NULL;
ALTER TABLE public.issues
ADD comment_id INTEGER NULL;
-- noinspection SqlResolve
ALTER TABLE public.issues
DROP COLUMN address;

View File

@ -129,6 +129,12 @@ SET contract_address = :contract_address
FROM t
RETURNING t.issue_id, t.issue_number, t.title, t.transaction_hash, t.contract_address, t.login, t.repo, t.repo_id;
-- :name update-comment-id :<! :1
-- :doc updates comment-id for a given issue
UPDATE issues
SET comment_id = :comment_id
WHERE issue_id = :issue_id;
-- :name list-pending-deployments :? :*
-- :doc retrieves pending transaction ids
SELECT
@ -201,8 +207,8 @@ FROM issues i
ON r.repo_id = i.repo_id
WHERE r.user_id = :owner_id;
-- :name issues-list :? :*
-- :doc lists all issues
-- :name owner-issues-list :? :*
-- :doc lists all not yet fixed issues in a given owner's repository
SELECT
i.contract_address AS contract_address,
i.issue_id AS issue_id,
@ -215,7 +221,22 @@ FROM issues i
INNER JOIN repositories r
ON r.repo_id = i.repo_id
WHERE r.user_id = :owner_id
AND i.commit_id IS NULL;
AND i.commit_id IS NULL
AND NOT exists(SELECT 1
FROM pull_requests
WHERE issue_number = i.issue_number);
-- :name wallets-list :? :*
-- :doc lists all contract ids
SELECT
i.contract_address AS contract_address,
r.login AS login,
r.repo AS repo,
i.comment_id AS comment_id,
i.issue_number AS issue_number
FROM issues i
INNER JOIN repositories r ON r.repo_id = i.repo_id
WHERE contract_address IS NOT NULL;
-- :name get-bounty-address :? :1
SELECT

View File

@ -12,7 +12,7 @@
(defn list-not-fixed-issues
[owner-id]
(jdbc/with-db-connection [con-db *db*]
(db/issues-list con-db {:owner_id owner-id})))
(db/owner-issues-list con-db {:owner_id owner-id})))
(defn pending-bounties-list
[]
@ -28,3 +28,8 @@
[user repo issue-number]
(jdbc/with-db-connection [con-db *db*]
(db/get-bounty-address con-db {:login user :repo repo :issue_number issue-number})))
(defn list-wallets
[]
(jdbc/with-db-connection [con-db *db*]
(db/wallets-list con-db)))

View File

@ -32,6 +32,13 @@
(db/update-contract-address con-db {:issue_id issue-id
:contract_address contract-address})))
(defn update-comment-id
"Updates issue with comment id"
[issue-id comment-id]
(jdbc/with-db-connection [con-db *db*]
(db/update-comment-id con-db {:issue_id issue-id
:comment_id comment-id})))
(defn list-pending-deployments
"Retrieves pending transaction ids"
[]

View File

@ -6,6 +6,7 @@
[ring.util.codec :as codec]
[clj-http.client :as http]
[commiteth.config :refer [env]]
[digest :refer [sha-256]]
[clojure.tools.logging :as log])
(:import [java.util UUID]))
@ -93,9 +94,14 @@
(println "removing webhook")
(repos/delete-hook user repo hook-id (auth-params token)))
(defn github-comment-hash
[user repo issue-number]
(digest/sha-256 (str "SALT_Yoh2looghie9jishah7aiphahphoo6udiju" user repo issue-number)))
(defn- get-qr-url
[user repo issue-number]
(str (server-address) (format "/qr/%s/%s/bounty/%s/qr.png" user repo issue-number)))
(let [hash (github-comment-hash user repo issue-number)]
(str (server-address) (format "/qr/%s/%s/bounty/%s/%s/qr.png" user repo issue-number hash))))
(defn- md-url
([text url]
@ -107,15 +113,25 @@
[alt src]
(str "!" (md-url alt src)))
(defn generate-comment
[user repo issue-number balance]
(let [image-url (md-image "QR Code" (get-qr-url user repo issue-number))
balance (str balance " ETH")
site-url (md-url (server-address) (server-address))]
(format "Current balance: %s\n%s\n%s" balance image-url site-url)))
(defn post-comment
[user repo issue-number balance]
(let [balance (str balance " ETH")
image-url (md-image "QR Code" (get-qr-url user repo issue-number))
site-url (md-url (server-address) (server-address))
comment (format "Current balance: %s\n%s\n%s" balance image-url site-url)]
(let [comment (generate-comment user repo issue-number balance)]
(log/info "Comment to" (str user "/" repo) ":" comment)
(issues/create-comment user repo issue-number comment (self-auth-params))))
(defn update-comment
[user repo comment-id issue-number balance]
(let [comment (generate-comment user repo issue-number balance)]
(log/info (str "Updating " user "/" repo " comment #" comment-id " with contents: " comment))
(issues/edit-comment user repo comment-id comment (self-auth-params))))
(defn get-issue
[user repo issue-number]
(issues/specific-issue user repo issue-number (self-auth-params)))

View File

@ -5,7 +5,8 @@
[commiteth.layout :as layout]
[commiteth.util.images :refer :all]
[clj.qrgen :as qr]
[commiteth.eth.core :as eth])
[commiteth.eth.core :as eth]
[commiteth.github.core :as github])
(:import [javax.imageio ImageIO]
[java.io InputStream]))
@ -28,13 +29,15 @@
(defapi qr-routes
(context "/qr" []
(GET "/:user/:repo/bounty/:issue{[0-9]{1,9}}/qr.png" [user repo issue]
(let [{address :contract_address
login :login
repo :repo
issue-number :issue_number} (bounties/get-bounty-address user repo (Integer/parseInt issue))
balance (eth/get-balance-eth address 8)
issue-url (str login "/" repo "/issues/" issue-number)]
(if address
(ok (generate-image address balance issue-url 768 256))
(bad-request))))))
(GET "/:user/:repo/bounty/:issue{[0-9]{1,9}}/:hash/qr.png" [user repo issue hash]
(if (= hash (github/github-comment-hash user repo issue))
(let [{address :contract_address
login :login
repo :repo
issue-number :issue_number} (bounties/get-bounty-address user repo (Integer/parseInt issue))]
(if address
(let [balance (eth/get-balance-eth address 8)
issue-url (str login "/" repo "/issues/" issue-number)]
(ok (generate-image address balance issue-url 768 256)))
(bad-request)))
(bad-request)))))

View File

@ -26,8 +26,9 @@
repo-id :repo_id
issue-number :issue_number} issue
balance (eth/get-balance-eth contract-address 4)
repo-owner-address (:address (users/get-repo-owner repo-id))]
(github/post-comment user repo issue-number balance)
repo-owner-address (:address (users/get-repo-owner repo-id))
{comment-id :id} (github/post-comment user repo issue-number balance)]
(issues/update-comment-id issue-id comment-id)
(wallet/add-owner contract-address (eth/eth-account))
(wallet/add-owner contract-address repo-owner-address))))))
@ -42,7 +43,19 @@
(wallet/execute contract-address payout-address value)
(bounties/update-confirm-hash issue-id)))))
(defn update-balance
[]
(for [{contract-address :contract_address
login :login
repo :repo
comment-id :comment_id
issue-number :issue_number} (bounties/list-wallets)]
(when comment-id
(let [balance (eth/get-balance-eth contract-address 8)]
(github/update-comment login repo comment-id issue-number balance)))))
(mount/defstate scheduler :start
(do
(every (* 5 60 1000) update-issue-contract-address pool)
(every (* 60 1000) self-sign-bounty pool)))
(every (* 60 1000) self-sign-bounty pool)
(every (* 5 60 1000) update-balance pool)))