From fdfaafc2c1c51703cede5614528c716c97868191 Mon Sep 17 00:00:00 2001 From: kagel Date: Wed, 31 Aug 2016 00:45:25 +0300 Subject: [PATCH] Get rid of parents and merge_commit_sha --- resources/migrations/20160830232606-.down.sql | 10 ++++ resources/migrations/20160830232606-.up.sql | 9 ++++ resources/sql/queries.sql | 7 ++- src/clj/commiteth/db/issues.clj | 2 +- src/clj/commiteth/routes/webhooks.clj | 52 +++++++++---------- 5 files changed, 47 insertions(+), 33 deletions(-) create mode 100644 resources/migrations/20160830232606-.down.sql create mode 100644 resources/migrations/20160830232606-.up.sql diff --git a/resources/migrations/20160830232606-.down.sql b/resources/migrations/20160830232606-.down.sql new file mode 100644 index 0000000..bff0a6a --- /dev/null +++ b/resources/migrations/20160830232606-.down.sql @@ -0,0 +1,10 @@ +ALTER TABLE pull_requests + ADD COLUMN parents VARCHAR(4099); + +-- noinspection SqlResolve +ALTER TABLE pull_requests + DROP COLUMN commit_id; + +-- noinspection SqlResolve +ALTER TABLE pull_requests + DROP COLUMN issue_number; diff --git a/resources/migrations/20160830232606-.up.sql b/resources/migrations/20160830232606-.up.sql new file mode 100644 index 0000000..25bb8a8 --- /dev/null +++ b/resources/migrations/20160830232606-.up.sql @@ -0,0 +1,9 @@ +-- noinspection SqlResolve +ALTER TABLE public.pull_requests + DROP parents; + +ALTER TABLE public.pull_requests + ADD COLUMN commit_id VARCHAR(40); + +ALTER TABLE public.pull_requests + ADD COLUMN issue_number INTEGER; diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 40178d3..2f3897c 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -101,13 +101,12 @@ RETURNING repo_id, issue_id, issue_number, title, address, commit_id; -- :name create-pull-request! :! :n -- :doc creates pull request -INSERT INTO pull_requests (repo_id, pr_id, pr_number, user_id, parents) +INSERT INTO pull_requests (repo_id, pr_id, pr_number, user_id) SELECT :repo_id, :pr_id, :pr_number, - :user_id, - :parents + :user_id WHERE NOT exists(SELECT 1 FROM pull_requests WHERE repo_id = :repo_id AND pr_id = :pr_id); @@ -132,7 +131,7 @@ SELECT r.repo AS repo_name FROM issues i INNER JOIN pull_requests p - ON p.parents LIKE '%' || i.commit_id || '%' + ON (p.commit_id = i.commit_id OR coalesce(p.issue_number, -1) = i.issue_number) AND p.repo_id = i.repo_id INNER JOIN users u ON u.id = p.user_id diff --git a/src/clj/commiteth/db/issues.clj b/src/clj/commiteth/db/issues.clj index 516e36f..f1c0951 100644 --- a/src/clj/commiteth/db/issues.clj +++ b/src/clj/commiteth/db/issues.clj @@ -15,6 +15,6 @@ (defn close "Updates issue with commit_id" - [issue-id commit-id] + [commit-id issue-id] (jdbc/with-db-connection [con-db *db*] (db/close-issue! con-db {:issue_id issue-id :commit_id commit-id}))) diff --git a/src/clj/commiteth/routes/webhooks.clj b/src/clj/commiteth/routes/webhooks.clj index b8d9e43..bd730f6 100644 --- a/src/clj/commiteth/routes/webhooks.clj +++ b/src/clj/commiteth/routes/webhooks.clj @@ -10,45 +10,41 @@ (def label-name "bounty") -(defn find-issue-closed-event - [events] - (first (filter #(= "closed" (:event %)) events))) +(defn find-issue-event + [events type] + (first (filter #(= type (:event %)) events))) + +(defn find-commit-id + [user repo issue-number event-type] + (-> + (github/get-issue-events user repo issue-number) + (find-issue-event event-type) + (:commit_id))) (defn handle-issue-closed [{{{user :login} :owner repo :name} :repository {issue-id :id issue-number :number} :issue}] (future - (->> - (github/get-issue-events user repo issue-number) - (find-issue-closed-event) - (:commit_id) - (issues/close issue-id)))) - -(defn get-commit-parents - [commit] - (->> commit :parents (map :sha) (join ","))) + (when-let [commit-id (find-commit-id user repo issue-number "closed")] + (issues/close commit-id issue-id)))) (defn handle-pull-request-closed [{{{owner :login} :owner - repo-name :name - repo-id :id} :repository + repo :name + repo-id :id} :repository {{user-id :id login :login - name :name} :user - id :id - pr-number :number - merge-commit-sha :merge_commit_sha} :pull_request}] + name :name} :user + id :id + pr-number :number} :pull_request}] (future - (->> - (github/get-commit owner repo-name merge-commit-sha) - (get-commit-parents) - (hash-map :parents) - (merge {:repo_id repo-id - :pr_id id - :pr_number pr-number - :user_id user-id}) - (pull-requests/create)) - (users/create-user user-id login name nil nil))) + (when-let [commit-id (find-commit-id owner repo pr-number "merged")] + (pull-requests/create {:repo_id repo-id + :pr_id id + :pr_number pr-number + :user_id user-id + :commit_id commit-id}) + (users/create-user user-id login name nil nil)))) (defn labeled-as-bounty? [action issue]