From ec5f07a578505b7195c1bd7c52c85ad2552b8caf Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Fri, 24 Feb 2017 11:05:13 +0200 Subject: [PATCH] DB query refactoring, save PRs also when opened * store pull-requests to DB also when opened instead of only when merged to allow for showing bounty claims * refactor SQL queries * look for PR keywords also in title * improve PR keyword regexes --- ...170222143232-bounty-claim-changes.down.sql | 0 ...20170222143232-bounty-claim-changes.up.sql | 21 ++ resources/sql/queries.sql | 180 +++++++++--------- src/clj/commiteth/db/bounties.clj | 4 +- src/clj/commiteth/db/pull_requests.clj | 17 +- src/clj/commiteth/routes/webhooks.clj | 121 +++++++----- src/clj/commiteth/scheduler.clj | 2 +- 7 files changed, 206 insertions(+), 139 deletions(-) create mode 100644 resources/migrations/20170222143232-bounty-claim-changes.down.sql create mode 100644 resources/migrations/20170222143232-bounty-claim-changes.up.sql diff --git a/resources/migrations/20170222143232-bounty-claim-changes.down.sql b/resources/migrations/20170222143232-bounty-claim-changes.down.sql new file mode 100644 index 0000000..e69de29 diff --git a/resources/migrations/20170222143232-bounty-claim-changes.up.sql b/resources/migrations/20170222143232-bounty-claim-changes.up.sql new file mode 100644 index 0000000..bbf98a4 --- /dev/null +++ b/resources/migrations/20170222143232-bounty-claim-changes.up.sql @@ -0,0 +1,21 @@ + +ALTER TABLE "public"."issue_comment" +ADD COLUMN "updated" timestamp DEFAULT timezone('utc'::text, now()); + + +ALTER TABLE "public"."pull_requests" ADD COLUMN "state" integer DEFAULT '0'; + +CREATE TABLE "public"."pull_request_state" ( + "id" integer, + "description" character varying(32), + PRIMARY KEY ("id") +); + +INSERT INTO pull_request_state (id, description) +VALUES(0, 'opened'); + +INSERT INTO pull_request_state (id, description) +VALUES(1, 'merged'); + +INSERT INTO pull_request_state (id, description) +VALUES(2, 'closed'); diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 0c4f87e..d6cbbd9 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -21,7 +21,11 @@ RETURNING id, login, name, email, avatar_url, token, address, created; -- :name update-user! :! :n -- :doc updates an existing user record UPDATE users -SET login = :login, name = :name, email = :email, token = :token, address = :address +SET login = :login, +name = :name, +email = :email, +token = :token, +address = :address WHERE id = :id; -- :name update-user-token! :> - (extract-issue-number pr-body) - (first) - (validate-issue-number owner repo)) - m {:commit_id commit-id :issue_number issue-number}] - (log/debug "handle-pull-request-closed" commit-id issue-number) - (when (or commit-id issue-number) - (log/debug (format "Pull request %s/%s/%s closed with reference to %s" - login repo pr-number - (if commit-id (str "commit-id " commit-id) - (str "issue-number " issue-number)))) - (pull-requests/create (merge m {:repo_id repo-id - :pr_id id - :pr_number pr-number - :user_id user-id})) - (users/create-user user-id login name nil nil))))) + 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)) + (when-let [bounty-issue-number (->> + (extract-issue-number pr-body pr-title) + (first) + (ensure-bounty-issue owner repo))] + (log/debug "Referenced bounty issue found" bounty-issue-number) + (users/create-user user-id login name nil avatar_url nil) + (let [pr-data {:repo_id repo-id + :pr_id id + :pr_number pr-number + :user_id user-id + :issue_number bounty-issue-number + :state event-type}] + (case event-type + :opened (do + (log/info "PR with reference to bounty issue" + bounty-issue-number "opened") + (pull-requests/save (merge pr-data {:state :opened + :commit_id nil}))) + :closed (if-let [commit-id (find-commit-id owner + repo + pr-number + ["merged"])] + (do (log/info "PR with reference to bounty issue" + bounty-issue-number "merged") + (pull-requests/save + (merge pr-data {:state :merged + :commit_id commit-id}))) + (do (log/info "PR with reference to bounty issue" + bounty-issue-number "closed with no merge") + (pull-requests/save + (merge pr-data {:state :closed + :commit_id nil})))))))) (defn handle-issue @@ -134,8 +163,10 @@ (defn handle-pull-request [pull-request] - (when (= "closed" (:action pull-request)) - (handle-pull-request-closed pull-request)) + (case (:action pull-request) + "opened" (handle-pull-request-event :opened pull-request) + "closed" (handle-pull-request-event :closed pull-request) + nil) (ok)) diff --git a/src/clj/commiteth/scheduler.clj b/src/clj/commiteth/scheduler.clj index 79101ce..a08d8a2 100644 --- a/src/clj/commiteth/scheduler.clj +++ b/src/clj/commiteth/scheduler.clj @@ -78,7 +78,7 @@ comment-id :comment_id issue-id :issue_id old-balance :balance - issue-number :issue_number} (db-bounties/list-wallets)] + issue-number :issue_number} (db-bounties/open-bounty-contracts)] (when comment-id (let [current-balance-hex (eth/get-balance-hex contract-address) current-balance-eth (eth/hex->eth current-balance-hex 8)