BigW Consortium Gitlab

labels.rb 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
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
14
        present user_project.labels, with: Entities::Label, current_user: current_user
15 16 17 18 19
      end

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

31
        attrs = attributes_for_keys [:name, :color, :description]
32 33
        label = user_project.find_label(attrs[:name])

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

        label = user_project.labels.create(attrs)

        if label.valid?
39
          present label, with: Entities::Label, current_user: current_user
40
        else
41
          render_validation_error!(label)
42 43 44 45 46 47 48 49 50 51 52 53
        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
54
        authorize! :admin_label, user_project
55 56 57
        required_attributes! [:name]

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

        label.destroy
      end
Robert Schilling committed
62 63 64 65

      # Updates an existing label. At least one optional parameter is required.
      #
      # Parameters:
66 67 68 69 70 71
      #   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)
      #   description (optional) - The description of label to be created
Robert Schilling committed
72 73 74
      # Example Request:
      #   PUT /projects/:id/labels
      put ':id/labels' do
75
        authorize! :admin_label, user_project
Robert Schilling committed
76 77 78
        required_attributes! [:name]

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

81
        attrs = attributes_for_keys [:new_name, :color, :description]
Robert Schilling committed
82 83

        if attrs.empty?
84 85 86
          render_api_error!('Required parameters "new_name" or "color" ' \
                            'missing',
                            400)
Robert Schilling committed
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)
93
          present label, with: Entities::Label, current_user: current_user
Robert Schilling committed
94
        else
95
          render_validation_error!(label)
Robert Schilling committed
96 97
        end
      end
98 99 100
    end
  end
end