FIX: improve sha detection

FIX: improve sha detection

Long term maybe we should just throw in the towel and match on “NOT a letter” or something far more vague.

diff --git a/lib/discourse_code_review/importer.rb b/lib/discourse_code_review/importer.rb
index 70ce49a..9ce10c0 100644
--- a/lib/discourse_code_review/importer.rb
+++ b/lib/discourse_code_review/importer.rb
@@ -46,10 +46,14 @@ module DiscourseCodeReview
       [text, linked_commits]
     end
 
+    def detect_shas(text)
+      text.scan(/(?:\s|[\(\[\{"',;:.]|^)([a-f0-9]{8,})(?:\s|[\)\}\].,;:'"?!]|$)/).flatten
+    end
+
     def find_linked_commits(text)
       result = {}
 
-      shas = text.scan(/(?:\s|\(|^)([a-f0-9]{8,})(?:\s|\)|$)/).flatten
+      shas = detect_shas(text)
       if shas.length > 0
 
         like_clause = shas.map { |sha| "f.value LIKE '#{sha}%'" }.join(' OR ')
diff --git a/spec/discourse_code_review/lib/importer_spec.rb b/spec/discourse_code_review/lib/importer_spec.rb
index a96ba08..10d08cf 100644
--- a/spec/discourse_code_review/lib/importer_spec.rb
+++ b/spec/discourse_code_review/lib/importer_spec.rb
@@ -2,6 +2,28 @@ require 'rails_helper'
 
 module DiscourseCodeReview
   describe Importer do
+
+    it "has robust sha detection" do
+      text = (<<~STR).strip
+        hello abcdf672, a723c123444!
+        (abc2345662) {abcd87234} [1209823bc]
+        ,7862abcdf abcdefg722
+        abc7827421119a
+      STR
+
+      shas = Importer.new(nil).detect_shas(text)
+
+      expect(shas).to eq(%w{
+       abcdf672
+       a723c123444
+       abc2345662
+       abcd87234
+       1209823bc
+       7862abcdf
+       abc7827421119a
+      })
+    end
+
     it "can look up a category id consistently" do
 
       # lets muck stuff up first ... and create a dupe category

GitHub
sha: b73bb3c2

1 Like