resource_label_events.rb 2.07 KB
Newer Older
1 2 3 4 5 6 7 8 9
# frozen_string_literal: true

module API
  class ResourceLabelEvents < Grape::API
    include PaginationParams
    helpers ::API::Helpers::NotesHelpers

    before { authenticate! }

10
    Helpers::ResourceLabelEventsHelpers.eventable_types.each do |eventable_type|
11 12 13 14 15 16
      parent_type = eventable_type.parent_class.to_s.underscore
      eventables_str = eventable_type.to_s.underscore.pluralize

      params do
        requires :id, type: String, desc: "The ID of a #{parent_type}"
      end
17
      resource parent_type.pluralize.to_sym, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
18 19 20 21 22 23 24 25
        desc "Get a list of #{eventable_type.to_s.downcase} resource label events" do
          success Entities::ResourceLabelEvent
          detail 'This feature was introduced in 11.3'
        end
        params do
          requires :eventable_id, types: [Integer, String], desc: 'The ID of the eventable'
          use :pagination
        end
26 27

        # rubocop: disable CodeReuse/ActiveRecord
28 29 30 31 32 33
        get ":id/#{eventables_str}/:eventable_id/resource_label_events" do
          eventable = find_noteable(parent_type, eventables_str, params[:eventable_id])
          events = eventable.resource_label_events.includes(:label, :user)

          present paginate(events), with: Entities::ResourceLabelEvent
        end
34
        # rubocop: enable CodeReuse/ActiveRecord
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

        desc "Get a single #{eventable_type.to_s.downcase} resource label event" do
          success Entities::ResourceLabelEvent
          detail 'This feature was introduced in 11.3'
        end
        params do
          requires :event_id, type: String, desc: 'The ID of a resource label event'
          requires :eventable_id, types: [Integer, String], desc: 'The ID of the eventable'
        end
        get ":id/#{eventables_str}/:eventable_id/resource_label_events/:event_id" do
          eventable = find_noteable(parent_type, eventables_str, params[:eventable_id])
          event = eventable.resource_label_events.find(params[:event_id])

          present event, with: Entities::ResourceLabelEvent
        end
      end
    end
  end
end