Parse issue URLs during issue number extraction

This commit is contained in:
Vitaliy Vlasov 2018-03-26 14:08:31 +03:00 committed by Tetiana Churikova
parent 400f848e08
commit df93a502df
3 changed files with 35 additions and 24 deletions

View File

@ -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

View File

@ -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)))))

View File

@ -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))))))