Reuse LabelsFinder on Issueable#add_labels_by_names

parent 3db22610
...@@ -234,19 +234,13 @@ module Issuable ...@@ -234,19 +234,13 @@ module Issuable
labels.delete_all labels.delete_all
end end
def add_labels_by_names(label_names) def add_labels_by_names(label_names, current_user)
label_ids = [] available_labels = LabelsFinder.new(current_user, project_id: project.id).execute
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})")
label_names.each do |label_name| label_names.each do |label_name|
title = label_name.strip title = label_name.strip
label = available_labels.find_by(title: title) 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 self.labels << label
end end
......
...@@ -91,7 +91,7 @@ module API ...@@ -91,7 +91,7 @@ module API
if merge_request.valid? if merge_request.valid?
# Find or create labels and attach to issue # Find or create labels and attach to issue
if params[:labels].present? 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 end
present merge_request, with: Entities::MergeRequest, current_user: current_user present merge_request, with: Entities::MergeRequest, current_user: current_user
...@@ -201,7 +201,7 @@ module API ...@@ -201,7 +201,7 @@ module API
# Find or create labels and attach to issue # Find or create labels and attach to issue
unless params[:labels].nil? unless params[:labels].nil?
merge_request.remove_labels 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 end
present merge_request, with: Entities::MergeRequest, current_user: current_user present merge_request, with: Entities::MergeRequest, current_user: current_user
......
...@@ -129,7 +129,7 @@ module Gitlab ...@@ -129,7 +129,7 @@ module Gitlab
assignee_id: assignee_id, assignee_id: assignee_id,
state: bug['fOpen'] == 'true' ? 'opened' : 'closed' 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'] if issue.iid != bug['ixBug']
issue.update_attribute(:iid, bug['ixBug']) issue.update_attribute(:iid, bug['ixBug'])
......
...@@ -100,7 +100,7 @@ module Gitlab ...@@ -100,7 +100,7 @@ module Gitlab
state: raw_issue["state"] == "closed" ? "closed" : "opened" 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"] if issue.iid != raw_issue["id"]
issue.update_attribute(:iid, raw_issue["id"]) issue.update_attribute(:iid, raw_issue["id"])
......
...@@ -15,6 +15,7 @@ describe Gitlab::GoogleCodeImport::Importer, lib: true do ...@@ -15,6 +15,7 @@ describe Gitlab::GoogleCodeImport::Importer, lib: true do
subject { described_class.new(project) } subject { described_class.new(project) }
before do before do
project.team << [project.creator, :master]
project.create_import_data(data: import_data) project.create_import_data(data: import_data)
end 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