diff --git a/src/clj/commiteth/routes/webhooks.clj b/src/clj/commiteth/routes/webhooks.clj index e12ccd6..e8f2f03 100644 --- a/src/clj/commiteth/routes/webhooks.clj +++ b/src/clj/commiteth/routes/webhooks.clj @@ -82,28 +82,33 @@ (when (issues/is-bounty-issue? issue-id) (issues/update-open-status issue-id true))) -(def ^:const keywords - [#"(?i)close:?\s+#(\d+)" - #"(?i)closes:?\s+#(\d+)" - #"(?i)closed:?\s+#(\d+)" - #"(?i)fix:?\s+#(\d+)" - #"(?i)fixes:?\s+#(\d+)" - #"(?i)fixed:?\s+#(\d+)" - #"(?i)resolve:?\s?#(\d+)" - #"(?i)resolves:?\s+#(\d+)" - #"(?i)resolved:?\s+#(\d+)"]) +(defn pr-keywords [prefix] + (mapv + #(re-pattern (str "(?i)" %1 ":?\\s+" prefix "(\\d+)")) + ["close" + "closes" + "closed" + "fix" + "fixes" + "fixed" + "resolve" + "resolves" + "resolved"])) (defn extract-issue-number - [pr-body pr-title] + [owner repo pr-body pr-title] (let [cleaned-body (str/replace pr-body #"(?m)^\[comment.*$" "") + keywords (concat (pr-keywords "#") + (when-not (or (str/blank? owner) (str/blank? repo)) + (pr-keywords (str "https://github.com/" owner "/" repo "/")))) extract (fn [source] (mapcat #(keep - (fn [s] - (try (let [issue-number (Integer/parseInt (second s))] - (when (pos? issue-number) - issue-number)) - (catch NumberFormatException _))) - (re-seq % source)) keywords))] + (fn [s] + (try (let [issue-number (Integer/parseInt (second s))] + (when (pos? issue-number) + issue-number)) + (catch NumberFormatException _))) + (re-seq % source)) keywords))] (log/debug cleaned-body) (concat (extract cleaned-body) (extract pr-title)))) @@ -169,7 +174,7 @@ pr-body :body pr-title :title} :pull_request}] (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) + (if-let [issue (some->> (extract-issue-number owner repo pr-body pr-title) (first) (issues/get-issue repo-id))] (if-not (:commit_sha issue) ; no PR has been merged yet referencing this issue diff --git a/test/clj/commiteth/test/github.clj b/test/clj/commiteth/test/github.clj index 96e2416..6161dde 100644 --- a/test/clj/commiteth/test/github.clj +++ b/test/clj/commiteth/test/github.clj @@ -4,14 +4,20 @@ (deftest test-github-keywords (testing "Several keywords in mixed case" - (let [res (set (extract-issue-number "body" + (let [res (set (extract-issue-number "" "" "body" "Fixes #12 and cloSes #000028 and also resolved \n#32"))] (is (= #{12 28 32} res)))) (testing "Ignoring big numbers and zeroes" - (let [res (set (extract-issue-number "body" + (let [res (set (extract-issue-number "" "" "body" "Fixes #298374298229087345 and closes #0xFFEF"))] (is (= #{} res)))) (testing "Consider both body and title" - (let [res (set (extract-issue-number "Fixes #1" + (let [res (set (extract-issue-number "" "" + "Fixes #1" "Fixes #2"))] + (is (= #{1 2} res)))) + (testing "Use issue URL instead of number" + (let [res (set (extract-issue-number "status-im" "status-react" + "Fixes https://github.com/status-im/status-react/1" + "Fixes https://github.com/status-im/status-react/2"))] (is (= #{1 2} res))))) diff --git a/test/clj/commiteth/test/webhooks.clj b/test/clj/commiteth/test/webhooks.clj index 5395877..dfb0a56 100644 --- a/test/clj/commiteth/test/webhooks.clj +++ b/test/clj/commiteth/test/webhooks.clj @@ -7,15 +7,15 @@ (testing "Basic fixes case from PR body" (let [title "foo" body "fixes #123"] - (is (= '(123) (webhooks/extract-issue-number body title))))) + (is (= '(123) (webhooks/extract-issue-number "" "" body title))))) (testing "Basic fixes case from PR title" (let [title "My title (fixes: #123)" body "no use for a body"] - (is (= '(123) (webhooks/extract-issue-number body title))))) + (is (= '(123) (webhooks/extract-issue-number "" "" body title))))) (testing "Commented issue number ignored in PR body" (let [title "foo" body " fixes #123 [comment]: # (To auto-close issue on merge, please insert the related issue number after # i.e fixes #566) "] - (is (= '(123) (webhooks/extract-issue-number body title)))))) + (is (= '(123) (webhooks/extract-issue-number "" "" body title))))))