BigW Consortium Gitlab

labels.rb 2.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
module API
  # Labels API
  class Labels < Grape::API
    before { authenticate! }

    resource :projects do
      # Get all labels of the project
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   GET /projects/:id/labels
      get ':id/labels' do
        present user_project.labels, with: Entities::Label
      end

      # Creates a new label
      #
      # Parameters:
      #   id    (required) - The ID of a project
      #   name  (required) - The name of the label to be deleted
      #   color (required) - Color of the label given in 6-digit hex
      #                      notation with leading '#' sign (e.g. #FFAABB)
      # Example Request:
      #   POST /projects/:id/labels
      post ':id/labels' do
27
        authorize! :admin_label, user_project
28 29 30 31 32
        required_attributes! [:name, :color]

        attrs = attributes_for_keys [:name, :color]
        label = user_project.find_label(attrs[:name])

33
        conflict!('Label already exists') if label
34 35 36 37 38 39

        label = user_project.labels.create(attrs)

        if label.valid?
          present label, with: Entities::Label
        else
40
          render_validation_error!(label)
41 42 43 44 45 46 47 48 49 50 51 52
        end
      end

      # Deletes an existing label
      #
      # Parameters:
      #   id    (required) - The ID of a project
      #   name  (required) - The name of the label to be deleted
      #
      # Example Request:
      #   DELETE /projects/:id/labels
      delete ':id/labels' do
53
        authorize! :admin_label, user_project
54 55 56
        required_attributes! [:name]

        label = user_project.find_label(params[:name])
57
        not_found!('Label') unless label
58 59 60

        label.destroy
      end
Robert Schilling committed
61 62 63 64

      # Updates an existing label. At least one optional parameter is required.
      #
      # Parameters:
65 66 67 68 69
      #   id        (required) - The ID of a project
      #   name      (required) - The name of the label to be deleted
      #   new_name  (optional) - The new name of the label
      #   color     (optional) - Color of the label given in 6-digit hex
      #                          notation with leading '#' sign (e.g. #FFAABB)
Robert Schilling committed
70 71 72
      # Example Request:
      #   PUT /projects/:id/labels
      put ':id/labels' do
73
        authorize! :admin_label, user_project
Robert Schilling committed
74 75 76
        required_attributes! [:name]

        label = user_project.find_label(params[:name])
77
        not_found!('Label not found') unless label
Robert Schilling committed
78 79 80 81

        attrs = attributes_for_keys [:new_name, :color]

        if attrs.empty?
82 83 84
          render_api_error!('Required parameters "new_name" or "color" ' \
                            'missing',
                            400)
Robert Schilling committed
85 86 87 88 89 90 91 92
        end

        # Rename new name to the actual label attribute name
        attrs[:name] = attrs.delete(:new_name) if attrs.key?(:new_name)

        if label.update(attrs)
          present label, with: Entities::Label
        else
93
          render_validation_error!(label)
Robert Schilling committed
94 95
        end
      end
96 97 98
    end
  end
end