summaryrefslogtreecommitdiff
path: root/app/models/commit.rb
diff options
context:
space:
mode:
authorJacopo <beschi.jacopo@gmail.com>2017-11-18 15:06:55 +0100
committerJacopo <beschi.jacopo@gmail.com>2017-12-13 18:02:20 +0100
commit55f322085d0507640366b7a774fe7819771ff54b (patch)
tree30fcb5e3f952fa007445342cbd67802a7f0958e3 /app/models/commit.rb
parent6930fa3102f0ba197e969f9996e86bf11346470c (diff)
downloadgitlab-ce-55f322085d0507640366b7a774fe7819771ff54b.tar.gz
Adds ordering to projects contributors in API
Allows ordering in GET api/v4/projects/:project_id/repository/contributors through `order_by` and `sort` params. The available `order_by` options are: name|email|commits. The available `sort` options are: asc|desc.
Diffstat (limited to 'app/models/commit.rb')
-rw-r--r--app/models/commit.rb14
1 files changed, 14 insertions, 0 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]