Module: API::Helpers::LabelHelpers

Extended by:
Grape::API::Helpers
Defined in:
lib/api/helpers/label_helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_label(parent, entity) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/api/helpers/label_helpers.rb', line 54

def create_label(parent, entity)
  authorize! :admin_label, parent

  label = available_labels_for(parent).find_by_title(params[:name])
  conflict!('Label already exists') if label

  priority = params.delete(:priority)
  label_params = declared_params(include_missing: false)

  label = ::Labels::CreateService.new(label_params).execute(create_service_params(parent))

  if label.persisted?
    if parent.is_a?(Project)
      label.prioritize!(parent, priority) if priority
    end

    present label, with: entity, current_user: current_user, parent: parent
  else
    render_validation_error!(label)
  end
end

#create_service_params(parent) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/api/helpers/label_helpers.rb', line 144

def create_service_params(parent)
  case parent
  when Project
    { project: parent }
  when Group
    { group: parent }
  else
    raise TypeError, 'Parent type is not supported'
  end
end

#delete_label(parent) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/api/helpers/label_helpers.rb', line 108

def delete_label(parent)
  label = find_label(parent, params_id_or_title, include_ancestor_groups: false)

  authorize! :admin_label, label

  return if destroy_conditionally!(label)

  render_api_error!('Label is locked and was not removed', 400)
end

#find_label(parent, id_or_title, params = { include_ancestor_groups: true }) ⇒ Object



33
34
35
36
37
38
# File 'lib/api/helpers/label_helpers.rb', line 33

def find_label(parent, id_or_title, params = { include_ancestor_groups: true })
  labels = available_labels_for(parent, params)
  label = labels.find_by_id(id_or_title) || labels.find_by_title(id_or_title)

  label || not_found!('Label')
end

#get_label(parent, entity, params = {}) ⇒ Object



48
49
50
51
52
# File 'lib/api/helpers/label_helpers.rb', line 48

def get_label(parent, entity, params = {})
  label = find_label(parent, params_id_or_title, params)

  present label, with: entity, current_user: current_user, parent: parent
end

#get_labels(parent, entity, params = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/api/helpers/label_helpers.rb', line 40

def get_labels(parent, entity, params = {})
  present paginate(available_labels_for(parent, params)),
    with: entity,
    current_user: current_user,
    parent: parent,
    with_counts: params[:with_counts]
end

#params_id_or_titleObject



140
141
142
# File 'lib/api/helpers/label_helpers.rb', line 140

def params_id_or_title
  @params_id_or_title ||= params[:label_id] || params[:name]
end

#promote_label(parent) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/api/helpers/label_helpers.rb', line 118

def promote_label(parent)
  unless parent.group
    render_api_error!('Failed to promote project label to group label', 400)
  end

  authorize! :admin_label, parent.group

  label = find_label(parent, params[:name], include_ancestor_groups: false)

  begin
    group_label = ::Labels::PromoteService.new(parent, current_user).execute(label)

    if group_label
      present group_label, with: Entities::GroupLabel, current_user: current_user, parent: parent.group
    else
      render_api_error!('Failed to promote project label to group label', 400)
    end
  rescue StandardError => error
    render_api_error!(error.to_s, 400)
  end
end

#update_label(parent, entity) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/api/helpers/label_helpers.rb', line 76

def update_label(parent, entity)
  authorize! :admin_label, parent

  label = find_label(parent, params_id_or_title, include_ancestor_groups: false)
  update_priority = params.key?(:priority)
  priority = params.delete(:priority)

  # params is used to update the label so we need to remove this field here
  params.delete(:label_id)
  params.delete(:name)
  params.delete(:archived) unless Feature.enabled?(:labels_archive, :instance)

  update_params = declared_params(include_missing: false)

  if update_params.present?
    authorize! :admin_label, label

    label = ::Labels::UpdateService.new(update_params).execute(label)
    render_validation_error!(label) unless label.valid?
  end

  if parent.is_a?(Project) && update_priority
    if priority.nil?
      label.unprioritize!(parent)
    else
      label.prioritize!(parent, priority)
    end
  end

  present label, with: entity, current_user: current_user, parent: parent
end