BigW Consortium Gitlab

labels.rb 3.6 KB
Newer Older
1 2
module API
  class Labels < Grape::API
3 4
    include PaginationParams
    
5 6
    before { authenticate! }

7 8 9
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
10
    resource :projects do
11 12 13
      desc 'Get all labels of the project' do
        success Entities::Label
      end
14 15 16
      params do
        use :pagination
      end
17
      get ':id/labels' do
18
        present paginate(available_labels), with: Entities::Label, current_user: current_user, project: user_project
19 20
      end

21 22 23 24 25 26 27
      desc 'Create a new label' do
        success Entities::Label
      end
      params do
        requires :name, type: String, desc: 'The name of the label to be created'
        requires :color, type: String, desc: "The color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)"
        optional :description, type: String, desc: 'The description of label to be created'
28
        optional :priority, type: Integer, desc: 'The priority of the label', allow_blank: true
29
      end
30
      post ':id/labels' do
31
        authorize! :admin_label, user_project
32

33
        label = available_labels.find_by(title: params[:name])
34
        conflict!('Label already exists') if label
35

36
        priority = params.delete(:priority)
37
        label = user_project.labels.create(declared_params(include_missing: false))
38 39

        if label.valid?
40 41
          label.prioritize!(user_project, priority) if priority
          present label, with: Entities::Label, current_user: current_user, project: user_project
42
        else
43
          render_validation_error!(label)
44 45 46
        end
      end

47 48 49 50 51 52
      desc 'Delete an existing label' do
        success Entities::Label
      end
      params do
        requires :name, type: String, desc: 'The name of the label to be deleted'
      end
53
      delete ':id/labels' do
54
        authorize! :admin_label, user_project
55

56
        label = user_project.labels.find_by(title: params[:name])
57
        not_found!('Label') unless label
58

59
        present label.destroy, with: Entities::Label, current_user: current_user, project: user_project
60
      end
Robert Schilling committed
61

62 63 64 65 66 67 68 69
      desc 'Update an existing label. At least one optional parameter is required.' do
        success Entities::Label
      end
      params do
        requires :name,  type: String, desc: 'The name of the label to be updated'
        optional :new_name, type: String, desc: 'The new name of the label'
        optional :color, type: String, desc: "The new color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)"
        optional :description, type: String, desc: 'The new description of label'
70 71
        optional :priority, type: Integer, desc: 'The priority of the label', allow_blank: true
        at_least_one_of :new_name, :color, :description, :priority
72
      end
Robert Schilling committed
73
      put ':id/labels' do
74
        authorize! :admin_label, user_project
Robert Schilling committed
75

76
        label = user_project.labels.find_by(title: params[:name])
77
        not_found!('Label not found') unless label
Robert Schilling committed
78

79 80
        update_priority = params.key?(:priority)
        priority = params.delete(:priority)
81
        label_params = declared_params(include_missing: false)
Robert Schilling committed
82
        # Rename new name to the actual label attribute name
83
        label_params[:name] = label_params.delete(:new_name) if label_params.key?(:new_name)
Robert Schilling committed
84

85 86 87 88 89 90 91 92
        render_validation_error!(label) unless label.update(label_params)

        if update_priority
          if priority.nil?
            label.unprioritize!(user_project)
          else
            label.prioritize!(user_project, priority)
          end
Robert Schilling committed
93
        end
94 95

        present label, with: Entities::Label, current_user: current_user, project: user_project
Robert Schilling committed
96
      end
97 98 99
    end
  end
end