FIX: correct diff truncation

FIX: correct diff truncation

Previously we mistakenly left part of the body in the diff section

Also shortens the link-back when we find a commit

Previously we mistakenly left part of the body in the diff section

Also shortens the link-back when we find a commit


diff --git a/lib/discourse_code_review/github_repo.rb b/lib/discourse_code_review/github_repo.rb
index 38119c8..20c6543 100644
--- a/lib/discourse_code_review/github_repo.rb
+++ b/lib/discourse_code_review/github_repo.rb
@@ -121,15 +121,25 @@ module DiscourseCodeReview
 
         hash = fields[0].strip
 
+        body = fields[4] || ''
+
         diff = git("show --format=%b #{hash}")
 
-        truncated = diff.length > MAX_DIFF_LENGTH
+        diff_lines = diff[0..MAX_DIFF_LENGTH + body.length]
+          .strip
+          .split("\n")
+
+        while !diff_lines[0].start_with?("diff --git")
+          diff_lines.delete_at(0)
+        end
+
+        truncated = diff.length > (MAX_DIFF_LENGTH + body.length)
         if truncated
-          diff = diff[0..MAX_DIFF_LENGTH]
-          diff.strip!
-          diff = diff.split("\n")[0..-2].join("\n")
+          diff_lines.delete_at(diff_lines.length - 1)
         end
 
+        diff = diff_lines.join("\n")
+
         github_data = lookup[hash] || {}
 
         {
diff --git a/lib/discourse_code_review/importer.rb b/lib/discourse_code_review/importer.rb
index 70e73d9..d979c24 100644
--- a/lib/discourse_code_review/importer.rb
+++ b/lib/discourse_code_review/importer.rb
@@ -121,7 +121,7 @@ module DiscourseCodeReview
         linked_topics.values.each do |topic|
           topic.add_moderator_post(
             user,
-            post.topic.url,
+            " #{post.topic.url}",
             bump: false,
             post_type: Post.types[:small_action],
             action_code: "followed_up"
diff --git a/spec/discourse_code_review/lib/github_repo_spec.rb b/spec/discourse_code_review/lib/github_repo_spec.rb
index 969e0da..7549e81 100644
--- a/spec/discourse_code_review/lib/github_repo_spec.rb
+++ b/spec/discourse_code_review/lib/github_repo_spec.rb
@@ -19,7 +19,7 @@ module DiscourseCodeReview
         `git add a`
         `git commit -am 'first commit'`
         File.write('a', 'hello2')
-        `git commit -am 'second commit'`
+        `git commit -am 'second commit\n\nline 2'`
 
         repo = GithubRepo.new('fake_repo/fake_repo', nil)
 
@@ -34,6 +34,7 @@ module DiscourseCodeReview
 
         # no point repeating the message
         expect(diff).not_to include("second commit")
+        expect(diff).not_to include("line 2")
       end
     end

GitHub

1 Like