Skip to content

Commit 1b636fe

Browse files
committed
Support private repo for CommitAuditor for differential updates
1 parent 7405d78 commit 1b636fe

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

app/workers/commit_auditor.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class CommitAuditor
2323
include Sidekiq::Worker
2424
sidekiq_options queue: :audit_commits
2525

26-
def perform(project_id, commit, rules, github_token)
26+
def perform(project_id, project_username, project_access_token, commit, rules, github_token)
2727
commit = JSON.parse(commit, symbolize_names: true)
2828
rules = JSON.parse(rules, symbolize_names: true)
29-
diff = get_commit_diff(commit, rules, github_token)
29+
diff = get_commit_diff(commit, project_username, project_access_token, rules, github_token)
3030

3131
builder = AuditResultsBuilder.new
3232
results = builder.build(project_id, commit, diff, rules)
@@ -41,12 +41,12 @@ def perform(project_id, commit, rules, github_token)
4141

4242
private
4343

44-
def get_commit_diff(commit, rules, github_token)
44+
def get_commit_diff(commit, project_username, project_access_token, rules, github_token)
4545
rule_type_ids = rules.collect { |e| e[:rule_type_id] }
4646
diff_rule_type_ids = RuleTypes.select { |k, v| v[:requires_diff] }.keys
4747
return if (rule_type_ids & diff_rule_type_ids).empty?
4848

49-
gh = GitHubAPI.new(github_token)
49+
gh = GitHubAPI.new(github_token, project_username, project_access_token)
5050
diff_raw = gh.get_diff(commit[:url])
5151
diff_raw.empty? ? nil : GitDiffParser.parse(diff_raw)
5252
end

app/workers/commit_collector.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class CommitCollector
2323

2424
COMMITS_URL = 'https://api.github.com/repos/%s/commits'.freeze
2525

26-
def perform(project_id, project_name, last_commit_time, rules, github_token)
26+
def perform(project_id, project_name, project_username, project_access_token, last_commit_time, rules, github_token)
2727
last_commit_time = Time.parse(last_commit_time)
2828
Rails.logger.debug "Collecting commits for #{project_name} since #{last_commit_time}"
2929

30-
commits = collect_commits(project_name, last_commit_time, github_token)
30+
commits = collect_commits(project_name, project_username, project_access_token, last_commit_time, github_token)
3131
Rails.logger.debug "Collected #{commits.size} commits from #{project_name}"
3232
return if commits.size == 0
3333

@@ -39,8 +39,8 @@ def perform(project_id, project_name, last_commit_time, rules, github_token)
3939
Projects[id: project_id].update(last_commit_time: last_commit_time)
4040
end
4141

42-
def collect_commits(project_name, last_commit_time, github_token)
43-
gh = GitHubAPI.new(github_token)
42+
def collect_commits(project_name, project_username, project_access_token, last_commit_time, github_token)
43+
gh = GitHubAPI.new(github_token, project_username, project_access_token)
4444
gh.get_commits(project_name, last_commit_time)
4545
end
4646
end

lib/github_api.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class GitHubAPI
2323
COMMITS_URL = 'https://api.github.com/repos/%s/commits'.freeze
2424
REPOS_URL = 'https://api.github.com/users/%s/repos'.freeze
2525

26-
def initialize(token)
26+
def initialize(token, project_username, project_access_token)
27+
@project_username = project_username
28+
@project_access_token = project_access_token
2729
@token = token
2830
end
2931

@@ -86,6 +88,7 @@ def request_raw(uri, params = nil, headers = {})
8688
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
8789
request = Net::HTTP::Get.new(uri)
8890
request['Authorization'] = "token #{@token}"
91+
request.basic_auth @project_username, @project_access_token unless (@project_username.empty? or @project_access_token.empty?)
8992
headers.each { |k,v| request[k] = v }
9093
response = http.request(request)
9194
end

0 commit comments

Comments
 (0)