summaryrefslogtreecommitdiff
path: root/lib/api/entities.rb
diff options
context:
space:
mode:
authorFilipa Lacerda <filipa@gitlab.com>2017-08-30 19:51:51 +0100
committerFilipa Lacerda <filipa@gitlab.com>2017-08-30 19:51:51 +0100
commitb6502103d4fd08a4046907b4b076b3611f017cb1 (patch)
tree784bb8e46e66a7e9a6b646fbbbef19b824cf03e7 /lib/api/entities.rb
parent9d25efd5c0c37982a16c3636e27cd3e5da0d7fdb (diff)
parent81f08d30e641dc1a6666022ab1f5d36dbcdced7e (diff)
downloadgitlab-ce-36917-branch-tooltip.tar.gz
Merge branch 'master' into 36917-branch-tooltip36917-branch-tooltip
* master: (26 commits) Fix invalid attribute used for time-ago-tooltip component Update latest artifacts doc Update share project with groups docs Remove tooltips from new sidebar Use `git update-ref --stdin -z` to delete refs Don't use public_send in destroy_conditionally! helper Fix MySQL failure for emoji autocomplete Replace 'project/user_lookup.feature' spinach test with an rspec analog Add changelog Add time stats to issue and merge request API end points Resolve new N+1 by adding preloads and metadata to issues end points Add missing N+1 test to issues spec Add time stats to API schema for issue and merge request end points Add time stats documentation to issue and merge request end points Improve migrations using triggers Increase z-index of dropdowns Just use a block Fix tests Try to make reserved ref names more obvious Resolve feedback on the MR: ...
Diffstat (limited to 'lib/api/entities.rb')
-rw-r--r--lib/api/entities.rb44
1 files changed, 41 insertions, 3 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index e8dd61e493f..803b48dd88a 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -320,7 +320,10 @@ module API
end
class IssueBasic < ProjectEntity
- expose :label_names, as: :labels
+ expose :labels do |issue, options|
+ # Avoids an N+1 query since labels are preloaded
+ issue.labels.map(&:title).sort
+ end
expose :milestone, using: Entities::Milestone
expose :assignees, :author, using: Entities::UserBasic
@@ -329,13 +332,32 @@ module API
end
expose :user_notes_count
- expose :upvotes, :downvotes
+ expose :upvotes do |issue, options|
+ if options[:issuable_metadata]
+ # Avoids an N+1 query when metadata is included
+ options[:issuable_metadata][issue.id].upvotes
+ else
+ issue.upvotes
+ end
+ end
+ expose :downvotes do |issue, options|
+ if options[:issuable_metadata]
+ # Avoids an N+1 query when metadata is included
+ options[:issuable_metadata][issue.id].downvotes
+ else
+ issue.downvotes
+ end
+ end
expose :due_date
expose :confidential
expose :web_url do |issue, options|
Gitlab::UrlBuilder.build(issue)
end
+
+ expose :time_stats, using: 'API::Entities::IssuableTimeStats' do |issue|
+ issue
+ end
end
class Issue < IssueBasic
@@ -365,10 +387,22 @@ module API
end
class IssuableTimeStats < Grape::Entity
+ format_with(:time_tracking_formatter) do |time_spent|
+ Gitlab::TimeTrackingFormatter.output(time_spent)
+ end
+
expose :time_estimate
expose :total_time_spent
expose :human_time_estimate
- expose :human_total_time_spent
+
+ with_options(format_with: :time_tracking_formatter) do
+ expose :total_time_spent, as: :human_total_time_spent
+ end
+
+ def total_time_spent
+ # Avoids an N+1 query since timelogs are preloaded
+ object.timelogs.map(&:time_spent).sum
+ end
end
class ExternalIssue < Grape::Entity
@@ -418,6 +452,10 @@ module API
expose :web_url do |merge_request, options|
Gitlab::UrlBuilder.build(merge_request)
end
+
+ expose :time_stats, using: 'API::Entities::IssuableTimeStats' do |merge_request|
+ merge_request
+ end
end
class MergeRequest < MergeRequestBasic