add a bit more context to comments (work in progress)

add a bit more context to comments (work in progress)

From cec5afe99bdfde52af2227ff9479679af5e21b4e Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Mon, 19 Nov 2018 18:41:08 +1100
Subject: [PATCH] add a bit more context to comments (work in progress)


diff --git a/jobs/import_commits.rb b/jobs/import_commits.rb
index eda0232..8825b6b 100644
--- a/jobs/import_commits.rb
+++ b/jobs/import_commits.rb
@@ -127,9 +127,19 @@ module Jobs
         login = comment[:login] || "unknown"
         user = ensure_user(email: "#{login}@fake.github.com", name: login, github_login: login)
 
+        context = ""
+        if comment[:line_content]
+          context = <<~MD
+            > #{comment[:path]}
+            >
+            > `#{comment[:line_content]}`
+
+          MD
+        end
+
         PostCreator.create!(
           user,
-          raw: comment[:body],
+          raw: context + comment[:body],
           skip_validations: true,
           created_at: comment[:created_at],
           topic_id: topic_id,
diff --git a/plugin.rb b/plugin.rb
index 1f19189..71aabb7 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -100,9 +100,20 @@ after_initialize do
 
     MAX_DIFF_LENGTH = 8000
 
-    def self.commit_comments(page)
+    def self.commit_comments(page = nil)
+      page ||= DiscourseCodeReview.current_comment_page
 
       Octokit.list_commit_comments(SiteSetting.code_review_github_repo, page: page).map do |hash|
+
+        line_content = nil
+
+        if hash[:path].present?
+          file = git("show #{hash[:commit_id]}:#{hash[:path]}")
+          if file.present? && hash[:line].present?
+            line_content = file.split("\n")[hash[:line] - 1]
+          end
+        end
+
         login = hash[:user][:login] if hash[:user]
         {
           url: hash[:html_url],
@@ -110,10 +121,12 @@ after_initialize do
           login: login,
           position: hash[:position],
           line: hash[:line],
+          path: hash[:path],
           commit_hash: hash[:commit_id],
           created_at: hash[:created_at],
           updated_at: hash[:updated_at],
-          body: hash[:body]
+          body: hash[:body],
+          line_content: line_content
         }
       end
 
@@ -261,7 +274,6 @@ after_initialize do
         if token = post.user.custom_fields[DiscourseCodeReview::UserToken]
           client = Octokit::Client.new(access_token: token)
           comment = client.create_commit_comment(SiteSetting.code_review_github_repo, hash, post.raw)
-          p comment
           post.custom_fields[DiscourseCodeReview::GithubId] = comment.id
           post.save_custom_fields
         end

GitHub

1 Like