some more shuffling redimentary support for comment import

some more shuffling redimentary support for comment import

From 4ea5c0375ef4a764f4066968bd084df81e080e62 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Wed, 7 Nov 2018 16:52:56 +1100
Subject: [PATCH] some more shuffling redimentary support for comment import


diff --git a/jobs/import_commits.rb b/jobs/import_commits.rb
index d688e63..669a9ea 100644
--- a/jobs/import_commits.rb
+++ b/jobs/import_commits.rb
@@ -10,20 +10,7 @@ module Jobs
         title = commit[:subject]
         raw = commit[:body] + "\n\n```diff\n#{commit[:diff]}\n```"
 
-        user = User.find_by_email(commit[:email])
-        if !user
-          name = commit[:name]
-          username = UserNameSuggester.sanitize_username(name)
-          email = commit[:email]
-          begin
-            user = User.create!(
-              email: email,
-              username: UserNameSuggester.suggest(username.presence || email),
-              name: name.presence || User.suggest_name(email),
-              staged: true
-            )
-          end
-        end
+        user = ensure_user(email: commit[:email], name: commit[:name])
 
         if !TopicCustomField.exists?(name: DiscourseCodeReview::CommitHash, value: commit[:hash])
 
@@ -42,11 +29,65 @@ module Jobs
           )
 
           DiscourseCodeReview.last_commit = commit[:hash]
+        end
+      end
+
+    end
 
+    def ensure_user(email:, name:)
+      user = User.find_by_email(email)
+      if !user
+        username = UserNameSuggester.sanitize_username(name)
+        begin
+          user = User.create!(
+            email: email,
+            username: UserNameSuggester.suggest(username.presence || email),
+            name: name.presence || User.suggest_name(email),
+            staged: true
+          )
         end
+      end
+      user
+    end
+
+    def import_comments
+      # 140
+      page = 140 # DiscourseCodeReview.current_comment_page
+
+      while true
+        comments = DiscourseCodeReview.commit_comments(page)
 
+        break if comments.blank?
+
+        comments.each do |comment|
+          import_comment(comment)
+        end
+
+        DiscourseCodeReview.current_comment_page = page
+        page += 1
       end
+    end
+
+    def import_comment(comment)
 
+      # skip if we already have the comment
+      return if PostCustomField.exists?(name: DiscourseCodeReview::GithubId, value: comment[:id])
+
+      # do we have the commit?
+      if topic_id = TopicCustomField.where(name: DiscourseCodeReview::CommitHash, value: comment[:commit_hash]).pluck(:topic_id).first
+        login = comment[:login] || "unknown"
+        user = ensure_user(email: "#{login}@fake.github.com", name: login)
+
+        post = PostCreator.create!(
+          user,
+          raw: comment[:body],
+          skip_validations: true,
+          created_at: comment[:created_at],
+          topic_id: topic_id
+        )
+
+        PostCustomField.create!(post_id: post.id, name: DiscourseCodeReview::GithubId, value: comment[:id])
+      end
     end
 
   end
diff --git a/plugin.rb b/plugin.rb
index 60130f6..fb3dc76 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -23,6 +23,8 @@ after_initialize do
 
     LastCommit = 'last commit'
     CommitHash = 'commit hash'
+    GithubId = 'github id'
+    CommentPage = 'comment page'
 
     def self.last_commit
       PluginStore.get(DiscourseCodeReview::PluginName, LastCommit) ||
@@ -34,11 +36,39 @@ after_initialize do
       v
     end
 
+    def self.current_comment_page
+      (PluginStore.get(DiscourseCodeReview::PluginName, CommentPage) || 1).to_i
+    end
+
+    def self.current_comment_page=(v)
+      PluginStore.set(DiscourseCodeReview::PluginName, CommentPage, v)
+      v
+    end
+
     LINE_END = "52fc72dfa9cafa9da5e6266810b884ae"
     FEILD_END = "52fc72dfa9cafa9da5e6266810b884ff"
 
     MAX_DIFF_LENGTH = 8000
 
+    def self.commit_comments(page)
+
+      Octokit.list_commit_comments(SiteSetting.code_review_github_repo, page: page).map do |hash|
+        login = hash[:user][:login] if hash[:user]
+        {
+          url: hash[:html_url],
+          id: hash[:id],
+          login: login,
+          position: hash[:position],
+          line: hash[:line],
+          commit_hash: hash[:commit_id],
+          created_at: hash[:created_at],
+          updated_at: hash[:updated_at],
+          body: hash[:body]
+        }
+      end
+
+    end
+
     def self.commits_since(hash = nil)
 
       git("pull")

GitHub

1 Like