diff options
author | Rémy Coutable <remy@rymai.me> | 2017-12-13 20:05:46 +0000 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-12-13 20:05:46 +0000 |
commit | 43b98944fb34d1a3ca37ae598327e4575d2ec315 (patch) | |
tree | 325b180d9e182154183274b71baca9a495361202 /app | |
parent | 066f1b9392f848887280fe17c5fcdfc0272de61c (diff) | |
parent | 55f322085d0507640366b7a774fe7819771ff54b (diff) | |
download | gitlab-ce-43b98944fb34d1a3ca37ae598327e4575d2ec315.tar.gz |
Merge branch '13695-order-contributors-in-api' into 'master'
Adds ordering to projects contributors in API
Closes #13695
See merge request gitlab-org/gitlab-ce!15469
Diffstat (limited to 'app')
-rw-r--r-- | app/models/commit.rb | 14 | ||||
-rw-r--r-- | app/models/repository.rb | 9 |
2 files changed, 21 insertions, 2 deletions
diff --git a/app/models/commit.rb b/app/models/commit.rb index 307e4fcedfe..13c31111134 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -52,6 +52,20 @@ class Commit diffs.reduce(0) { |sum, d| sum + Gitlab::Git::Util.count_lines(d.diff) } end + def order_by(collection:, order_by:, sort:) + return collection unless %w[email name commits].include?(order_by) + return collection unless %w[asc desc].include?(sort) + + collection.sort do |a, b| + operands = [a, b].tap { |o| o.reverse! if sort == 'desc' } + + attr1, attr2 = operands.first.public_send(order_by), operands.second.public_send(order_by) # rubocop:disable PublicSend + + # use case insensitive comparison for string values + order_by.in?(%w[email name]) ? attr1.casecmp(attr2) : attr1 <=> attr2 + end + end + # Truncate sha to 8 characters def truncate_sha(sha) sha[0..MIN_SHA_LENGTH] diff --git a/app/models/repository.rb b/app/models/repository.rb index c0e31eca8da..28f5fc28b8c 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -697,10 +697,14 @@ class Repository end end - def contributors + # Params: + # + # order_by: name|email|commits + # sort: asc|desc default: 'asc' + def contributors(order_by: nil, sort: 'asc') commits = self.commits(nil, limit: 2000, offset: 0, skip_merges: true) - commits.group_by(&:author_email).map do |email, commits| + commits = commits.group_by(&:author_email).map do |email, commits| contributor = Gitlab::Contributor.new contributor.email = email @@ -714,6 +718,7 @@ class Repository contributor end + Commit.order_by(collection: commits, order_by: order_by, sort: sort) end def refs_contains_sha(ref_type, sha) |