blob: cf4f7606c9442b6702ba41d75886d07deb209cbc (
plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
module Labels
class FindOrCreateService
def initialize(current_user, project, params = {})
@current_user = current_user
@project = project
@params = params.dup
end
def execute(skip_authorization: false)
@skip_authorization = skip_authorization
find_or_create_label
end
private
attr_reader :current_user, :project, :params, :skip_authorization
def available_labels
@available_labels ||= LabelsFinder.new(
current_user,
project_id: project.id
).execute(skip_authorization: skip_authorization)
end
# Only creates the label if current_user can do so, if the label does not exist
# and the user can not create the label, nil is returned
def find_or_create_label
new_label = available_labels.find_by(title: title)
if new_label.nil? && (skip_authorization || Ability.allowed?(current_user, :admin_label, project))
new_label = project.labels.create(params)
end
new_label
end
def title
params[:title] || params[:name]
end
end
end
|