allow entering of auth token so we do not hit rate limits so hard

allow entering of auth token so we do not hit rate limits so hard

From 55d57a7079110107ff5b3baad40e2ce5eb4175dd Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Tue, 20 Nov 2018 17:09:54 +1100
Subject: [PATCH] allow entering of auth token so we do not hit rate limits so
 hard


diff --git a/config/settings.yml b/config/settings.yml
index 43a20e8..9acf4d7 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -1,6 +1,7 @@
 plugins:
   code_review_enabled: false
   code_review_github_repo: ""
+  code_review_api_username: ""
   code_review_pending_category_id:
     hidden: true
     default: -1
diff --git a/plugin.rb b/plugin.rb
index f42d8c4..68f46ae 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -100,10 +100,24 @@ after_initialize do
 
     MAX_DIFF_LENGTH = 8000
 
+    def self.octokit_client
+      client = Octokit::Client.new
+
+      if username = SiteSetting.code_review_api_username.presence
+        username = username.downcase
+        id = User.where(username_lower: username).pluck(:id).first
+        if id && (token = UserCustomField.where(user_id: id, name: DiscourseCodeReview::UserToken).pluck(:value).first)
+          client = Octokit::Client.new(access_token: token)
+        end
+      end
+
+      client
+    end
+
     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|
+      octokit_client.list_commit_comments(SiteSetting.code_review_github_repo, page: page).map do |hash|
 
         line_content = nil
 
@@ -144,7 +158,7 @@ after_initialize do
       commits = git("log #{hash}.. --pretty=%H").split("\n").map { |x| x.strip }
 
       commits.each_slice(30).each do |x|
-        commits = Octokit.commits(SiteSetting.code_review_github_repo, sha: x.first)
+        commits = octokit_client.commits(SiteSetting.code_review_github_repo, sha: x.first)
         github_info.concat(commits)
       end

GitHub

1 Like