FEATURE: helper for importing a single commit

FEATURE: helper for importing a single commit

Handy for backfilling particular commits, only usable from console for now

diff --git a/lib/discourse_code_review/github_repo.rb b/lib/discourse_code_review/github_repo.rb
index ff122bd..7cf4602 100644
--- a/lib/discourse_code_review/github_repo.rb
+++ b/lib/discourse_code_review/github_repo.rb
@@ -95,7 +95,17 @@ module DiscourseCodeReview
 
     end
 
-    def commits_since(hash = nil, merge_github_info: true, pull: true)
+    def commit(hash)
+      git("pull")
+      begin
+        git("log -1 #{hash}", warn: false)
+        commits_since(hash, single: true, pull: false)
+      rescue StandardError
+        nil
+      end
+    end
+
+    def commits_since(hash = nil, merge_github_info: true, pull: true, single: false)
       if pull
         git("pull")
       end
@@ -104,7 +114,9 @@ module DiscourseCodeReview
 
       github_info = []
 
-      commits = git("log #{hash}.. --pretty=%H").split("\n").map { |x| x.strip }
+      range = single ? "-1 #{hash}" : "#{hash}.."
+
+      commits = git("log #{range} --pretty=%H").split("\n").map { |x| x.strip }
 
       if merge_github_info
         commits.each_slice(30).each do |x|
@@ -126,7 +138,7 @@ module DiscourseCodeReview
       # hash name email subject body
       format = %w{%H %aN %aE %s %B %at}.join(FEILD_END) << LINE_END
 
-      data = git("log #{hash}.. --pretty='#{format}'")
+      data = git("log #{range} --pretty='#{format}'")
 
       data.split(LINE_END).map do |line|
         fields = line.split(FEILD_END).map { |f| f.strip if f }
@@ -190,7 +202,7 @@ module DiscourseCodeReview
       `git clone #{url} '#{path}'`
     end
 
-    def git(command, backup_command: nil, raise_error: true)
+    def git(command, backup_command: nil, raise_error: true, warn: true)
       FileUtils.mkdir_p(Rails.root + "tmp/code-review-repo")
 
       if !File.exist?(path)
@@ -208,7 +220,9 @@ module DiscourseCodeReview
           end
 
           if $?.exitstatus != 0
-            Rails.logger.warn("Discourse Code Review: Failed to run `#{command}` in #{path} error code: #{$?}")
+            if warn
+              Rails.logger.warn("Discourse Code Review: Failed to run `#{command}` in #{path} error code: #{$?}")
+            end
 
             if raise_error
               raise StandardError, "Failed to run git command #{command} on #{@name} in tmp/code-review-repo"
diff --git a/lib/discourse_code_review/importer.rb b/lib/discourse_code_review/importer.rb
index 794a588..b9c2ae1 100644
--- a/lib/discourse_code_review/importer.rb
+++ b/lib/discourse_code_review/importer.rb
@@ -8,6 +8,21 @@ module DiscourseCodeReview
       @github_repo = github_repo
     end
 
+    def self.import_commit(sha)
+      client = DiscourseCodeReview.octokit_client
+      CategoryCustomField.where(name: GithubRepoName).pluck(:value).each do |repo_name|
+        repo = GithubRepo.new(repo_name, client)
+        importer = Importer.new(repo)
+
+        if commit = repo.commit(sha)
+          importer.import_commit(commit)
+          return repo_name
+        end
+      end
+
+      nil
+    end
+
     def category_id
       @category_id ||=
         begin

GitHub sha: 35d235a7

1 Like

minor correction to #35d235a7