diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-07-02 14:43:23 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-07-02 14:43:23 +0300 |
commit | 5a88873e5cac26edf65dcfae8d449beb4ed865bb (patch) | |
tree | 6c46297d86797ea1b94658124758aa65a21a79f5 | |
parent | 81d41c0f927f69d75937d3c8f757740e936ed6e9 (diff) | |
download | gitlab-ce-5a88873e5cac26edf65dcfae8d449beb4ed865bb.tar.gz |
Move API contributors logic to repository model
-rw-r--r-- | app/models/repository.rb | 26 | ||||
-rw-r--r-- | lib/api/repositories.rb | 24 | ||||
-rw-r--r-- | lib/gitlab/contributors.rb | 9 | ||||
-rw-r--r-- | spec/requests/api/repositories_spec.rb | 3 |
4 files changed, 38 insertions, 24 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index 22c16abe480..a2285bb5dab 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -242,4 +242,30 @@ class Repository branches end end + + def contributors + contributors = {} + log = graph_log.group_by { |i| i[:author_email] } + + log.each do |email, contributions| + contributors[email] = { + email: email, + commits: 0, + additions: 0, + deletions: 0, + } + + contributions.each do |contribution| + if contributors[email][:name].blank? + contributors[email][:name] = contribution[:author_name] + end + + contributors[email][:commits] += 1 + contributors[email][:additions] += contribution[:additions] || 0 + contributors[email][:deletions] += contribution[:deletions] || 0 + end + end + + contributors.values + end end diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb index ace3283616a..241036729b5 100644 --- a/lib/api/repositories.rb +++ b/lib/api/repositories.rb @@ -160,29 +160,7 @@ module API get ':id/repository/contributors' do authorize! :download_code, user_project - contributors = {} - contributors - - log = user_project.repository.graph_log - log.each do |entry| - email = entry[:author_email].to_s - - if contributors.has_key?(email) - contributors[email][:commits] += 1 - contributors[email][:additions] += entry[:additions] || 0 - contributors[email][:deletions] += entry[:deletions] || 0 - else - contributors[email] = { - email: email, - name: entry[:author_name], - commits: 1, - additions: entry[:additions] || 0, - deletions: entry[:deletions] || 0, - } - end - end - - contributors + user_project.repository.contributors end end end diff --git a/lib/gitlab/contributors.rb b/lib/gitlab/contributors.rb new file mode 100644 index 00000000000..834532470aa --- /dev/null +++ b/lib/gitlab/contributors.rb @@ -0,0 +1,9 @@ +module Gitlab + class Contributor + attr_accessor :email, :name, :commits, :additions, :deletions + + def initialize + + end + end +end diff --git a/spec/requests/api/repositories_spec.rb b/spec/requests/api/repositories_spec.rb index daf534d7317..a02231a5bba 100644 --- a/spec/requests/api/repositories_spec.rb +++ b/spec/requests/api/repositories_spec.rb @@ -171,7 +171,8 @@ describe API::API, api: true do it 'should return valid data' do get api("/projects/#{project.id}/repository/contributors", user) response.status.should == 200 - contributor = json_response.values.first + json_response.should be_an Array + contributor = json_response.first contributor['email'].should == 'dmitriy.zaporozhets@gmail.com' contributor['name'].should == 'Dmitriy Zaporozhets' contributor['commits'].should == 185 |