Reuse LabelsFinder on Issueable#add_labels_by_names

parent 3db22610
......@@ -234,19 +234,13 @@ module Issuable
labels.delete_all
end
def add_labels_by_names(label_names)
label_ids = []
label_ids << project.group.labels.select(:id) if project.group.present?
label_ids << project.labels.select(:id)
union = Gitlab::SQL::Union.new(label_ids)
available_labels = Label.where("labels.id IN (#{union.to_sql})")
def add_labels_by_names(label_names, current_user)
available_labels = LabelsFinder.new(current_user, project_id: project.id).execute
label_names.each do |label_name|
title = label_name.strip
label = available_labels.find_by(title: title)
label = project.labels.build(title: title, color: Label::DEFAULT_COLOR) if label.nil?
label ||= project.labels.build(title: title, color: Label::DEFAULT_COLOR)
self.labels << label
end
......
......@@ -91,7 +91,7 @@ module API
if merge_request.valid?
# Find or create labels and attach to issue
if params[:labels].present?
merge_request.add_labels_by_names(params[:labels].split(","))
merge_request.add_labels_by_names(params[:labels].split(","), current_user)
end
present merge_request, with: Entities::MergeRequest, current_user: current_user
......@@ -201,7 +201,7 @@ module API
# Find or create labels and attach to issue
unless params[:labels].nil?
merge_request.remove_labels
merge_request.add_labels_by_names(params[:labels].split(","))
merge_request.add_labels_by_names(params[:labels].split(","), current_user)
end
present merge_request, with: Entities::MergeRequest, current_user: current_user
......
......@@ -129,7 +129,7 @@ module Gitlab
assignee_id: assignee_id,
state: bug['fOpen'] == 'true' ? 'opened' : 'closed'
)
issue.add_labels_by_names(labels)
issue.add_labels_by_names(labels, project.creator)
if issue.iid != bug['ixBug']
issue.update_attribute(:iid, bug['ixBug'])
......
......@@ -100,7 +100,7 @@ module Gitlab
state: raw_issue["state"] == "closed" ? "closed" : "opened"
)
issue.add_labels_by_names(labels)
issue.add_labels_by_names(labels, project.creator)
if issue.iid != raw_issue["id"]
issue.update_attribute(:iid, raw_issue["id"])
......
......@@ -15,6 +15,7 @@ describe Gitlab::GoogleCodeImport::Importer, lib: true do
subject { described_class.new(project) }
before do
project.team << [project.creator, :master]
project.create_import_data(data: import_data)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment