FEATURE: allow cloning of private repos if option is enabled

FEATURE: allow cloning of private repos if option is enabled
From 90e5b96bec9c53dfc1b8c2238e0b85d292db15cb Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Mon, 10 Dec 2018 09:02:49 +1100
Subject: [PATCH] FEATURE: allow cloning of private repos if option is enabled

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index fdab627..97eadf8 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3,6 +3,7 @@ en:
     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_allow_private_clone: 'REQUIRES: code_review_api_username, will clone the repos as the entered user name, this safeguard is in place to disable cloning of private repos on public sites.'
     code_review_pending_tag: 'Tag to apply to pending commits'
     code_review_followup_tag: 'Tag to apply to follow up commits'
     code_review_approved_tag: 'Tag to apply to approved commits'
diff --git a/config/settings.yml b/config/settings.yml
index ebe0669..c338e1c 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -2,6 +2,7 @@ plugins:
   code_review_enabled: false
   code_review_catch_up_commits: 10
   code_review_api_username: ""
+  code_review_allow_private_clone: false
   code_review_pending_tag: "pending"
     client: true
diff --git a/lib/discourse_code_review/github_repo.rb b/lib/discourse_code_review/github_repo.rb
index 5b74f17..2ad3f47 100644
--- a/lib/discourse_code_review/github_repo.rb
+++ b/lib/discourse_code_review/github_repo.rb
@@ -150,11 +150,21 @@ module DiscourseCodeReview
       @path = v
+    def clone(path)
+      url =
+        if (SiteSetting.code_review_allow_private_clone && SiteSetting.code_review_api_username.present?)
+          "https://#{octokit_client.access_token}@github.com/#{@name}.git"
+        else
+          "https://github.com/#{@name}.git"
+        end
+      `git clone #{url} '#{path}'`
+    end
     def git(command, backup_command: nil, raise_error: true)
       FileUtils.mkdir_p(Rails.root + "tmp/code-review-repo")
       if !File.exist?(path)
-        `git clone https://github.com/#{@name}.git '#{path}'`
+        clone(path)
         if $?.exitstatus != 0
           raise StandardError, "Failed to clone repo #{@name} in tmp/code-review-repo"


1 Like