FEATURE: reduce number of catch up commit

FEATURE: reduce number of catch up commit

This commit adds the setting code_review_catch_up_commits which allows
you to define how many topics to create by default when you encounter
a new repo via a webhook. It also reduces the default from 30 to 10.

From e8fd212b337f3faf007f6a4e73703b4044408b7b Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Thu, 29 Nov 2018 10:47:06 +1100
Subject: [PATCH] FEATURE: reduce number of catch up commit

This commit adds the setting `code_review_catch_up_commits` which allows
you to define how many topics to create by default when you encounter
a new repo via a webhook. It also reduces the default from 30 to 10.

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 10cc65b..fdab627 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1,6 +1,7 @@
 en:
   site_settings:
     code_review_enabled: 'enable Discourse code review'
+    code_review_catch_up_commits: 'When a new repo is added create this number of topics for old commits'
     code_review_api_username: 'OAuth key of this username will be used for API calls, this means we get 5000 an hour vs 60'
     code_review_pending_tag: 'Tag to apply to pending commits'
     code_review_followup_tag: 'Tag to apply to follow up commits'
diff --git a/config/settings.yml b/config/settings.yml
index 9f548fa..9696116 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -1,5 +1,6 @@
 plugins:
   code_review_enabled: false
+  code_review_catch_up_commits: 10
   code_review_api_username: ""
   code_review_pending_tag: "pending"
   code_review_approved_tag: "approved"
diff --git a/lib/discourse_code_review/github_repo.rb b/lib/discourse_code_review/github_repo.rb
index 5bdcd7b..5b74f17 100644
--- a/lib/discourse_code_review/github_repo.rb
+++ b/lib/discourse_code_review/github_repo.rb
@@ -33,7 +33,10 @@ module DiscourseCodeReview
 
     def last_commit
       PluginStore.get(DiscourseCodeReview::PluginName, LastCommit + @name) ||
-        (self.last_commit = git('rev-parse HEAD~30', backup_command: 'rev-list --max-parents=0 HEAD'))
+        begin
+          commits = [SiteSetting.code_review_catch_up_commits, 1].max - 1
+          (self.last_commit = git("rev-parse HEAD~#{commits}", backup_command: 'rev-list --max-parents=0 HEAD'))
+        end
     end
 
     def last_commit=(v)
@@ -142,6 +145,11 @@ module DiscourseCodeReview
       @path ||= (Rails.root + "tmp/code-review-repo/#{clean_name}").to_s
     end
 
+    # for testing
+    def path=(v)
+      @path = v
+    end
+
     def git(command, backup_command: nil, raise_error: true)
       FileUtils.mkdir_p(Rails.root + "tmp/code-review-repo")
 
diff --git a/spec/discourse_code_review/lib/github_repo_spec.rb b/spec/discourse_code_review/lib/github_repo_spec.rb
new file mode 100644
index 0000000..5dd1f63
--- /dev/null
+++ b/spec/discourse_code_review/lib/github_repo_spec.rb
@@ -0,0 +1,37 @@
+require 'rails_helper'
+
+module DiscourseCodeReview
+  describe GithubRepo do
+
+    before do
+      @git_path = "#{Pathname.new(Dir.tmpdir).realpath}/#{SecureRandom.hex}"
+      FileUtils.mkdir @git_path
+    end
+
+    after do
+      FileUtils.rm_rf(@git_path)
+    end
+
+    it "can respect catchup commits" do
+      Dir.chdir(@git_path) do
+        `git init .`
+        File.write('a', 'hello')
+        `git add a`
+        `git commit -am 'first commit'`
+        File.write('a', 'hello2')
+        `git commit -am 'second commit'`
+        File.write('a', 'hello3')
+        `git commit -am 'third commit'`
+
+        repo = GithubRepo.new('fake_repo/fake_repo', nil)
+        repo.path = @git_path
+
+        SiteSetting.code_review_catch_up_commits = 1
+
+        sha = `git rev-parse HEAD~0`.strip
+
+        expect(repo.last_commit).to eq(sha)
+      end
+    end
+  end
+end

GitHub

1 Like

FYI @ZogStriP default is now 10 commits which is a compromise I guess, lets see how we cope with the reduced flood

1 Like