Commit 67de82cf authored by Jacob Vosmaer's avatar Jacob Vosmaer

Add option to use CommitLanguages RPC

parent e9956456
......@@ -43,23 +43,7 @@ class Projects::GraphsController < Projects::ApplicationController
end
def get_languages
@languages = Linguist::Repository.new(@repository.rugged, @repository.rugged.head.target_id).languages
total = @languages.map(&:last).sum
@languages = @languages.map do |language|
name, share = language
color = Linguist::Language[name].color || "##{Digest::SHA256.hexdigest(name)[0...6]}"
{
value: (share.to_f * 100 / total).round(2),
label: name,
color: color,
highlight: color
}
end
@languages.sort! do |x, y|
y[:value] <=> x[:value]
end
@languages = @project.repository.languages
end
def fetch_graph
......
......@@ -636,6 +636,33 @@ module Gitlab
@attributes.attributes(path)
end
def languages(ref = nil)
Gitlab::GitalyClient.migrate(:commit_languages) do |is_enabled|
if is_enabled
gitaly_commit_client.languages(ref)
else
ref ||= rugged.head.target_id
languages = Linguist::Repository.new(rugged, ref).languages
total = languages.map(&:last).sum
languages = languages.map do |language|
name, share = language
color = Linguist::Language[name].color || "##{Digest::SHA256.hexdigest(name)[0...6]}"
{
value: (share.to_f * 100 / total).round(2),
label: name,
color: color,
highlight: color
}
end
languages.sort do |x, y|
y[:value] <=> x[:value]
end
end
end
end
def gitaly_repository
Gitlab::GitalyClient::Util.repository(@storage, @relative_path)
end
......
......@@ -118,6 +118,13 @@ module Gitlab
consume_commits_response(response)
end
def languages(ref = nil)
request = Gitaly::CommitLanguagesRequest.new(repository: @gitaly_repo, revision: ref || '')
response = GitalyClient.call(@repository.storage, :commit_service, :commit_languages, request)
response.languages.map { |l| { value: l.share.round(2), label: l.name, color: l.color, highlight: l.color } }
end
private
def commit_diff_request_params(commit, options = {})
......
......@@ -24,37 +24,4 @@ describe Projects::GraphsController do
expect(response).to redirect_to action: :charts
end
end
describe 'GET charts' do
let(:linguist_repository) do
double(languages: {
'Ruby' => 1000,
'CoffeeScript' => 350,
'NSIS' => 15
})
end
let(:expected_values) do
nsis_color = "##{Digest::SHA256.hexdigest('NSIS')[0...6]}"
[
# colors from Linguist:
{ label: "Ruby", color: "#701516", highlight: "#701516" },
{ label: "CoffeeScript", color: "#244776", highlight: "#244776" },
# colors from SHA256 fallback:
{ label: "NSIS", color: nsis_color, highlight: nsis_color }
]
end
before do
allow(Linguist::Repository).to receive(:new).and_return(linguist_repository)
end
it 'sets the correct colour according to language' do
get(:charts, namespace_id: project.namespace, project_id: project, id: 'master')
expected_values.each do |val|
expect(assigns(:languages)).to include(a_hash_including(val))
end
end
end
end
......@@ -1127,6 +1127,45 @@ describe Gitlab::Git::Repository, seed_helper: true do
end
end
describe '#languages' do
shared_examples 'languages' do
it 'returns exactly the expected results' do
languages = repository.languages('4b4918a572fa86f9771e5ba40fbd48e1eb03e2c6')
expected_languages = [
{ value: 66.63, label: "Ruby", color: "#701516", highlight: "#701516" },
{ value: 22.96, label: "JavaScript", color: "#f1e05a", highlight: "#f1e05a" },
{ value: 7.9, label: "HTML", color: "#e44b23", highlight: "#e44b23" },
{ value: 2.51, label: "CoffeeScript", color: "#244776", highlight: "#244776" }
]
expect(languages.size).to eq(expected_languages.size)
expected_languages.size.times do |i|
a = expected_languages[i]
b = languages[i]
expect(a.keys.sort).to eq(b.keys.sort)
expect(a[:value]).to be_within(0.1).of(b[:value])
non_float_keys = a.keys - [:value]
expect(a.values_at(*non_float_keys)).to eq(b.values_at(*non_float_keys))
end
end
it "uses the repository's HEAD when no ref is passed" do
lang = repository.languages.first
expect(lang[:label]).to eq('Ruby')
end
end
it_behaves_like 'languages'
context 'with rugged', skip_gitaly_mock: true do
it_behaves_like 'languages'
end
end
def create_remote_branch(repository, remote_name, branch_name, source_branch_name)
source_branch = repository.branches.find { |branch| branch.name == source_branch_name }
rugged = repository.rugged
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment