Reuse LabelsFinder on Issueable#add_labels_by_names

This commit is contained in:
Douglas Barbosa Alexandre 2016-10-03 18:41:46 -03:00
parent 3db2261005
commit 7e11ca86fd
5 changed files with 11 additions and 16 deletions

View File

@ -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

View File

@ -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
# 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

View File

@ -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'])

View File

@ -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"])

View File

@ -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
@ -31,9 +32,9 @@ describe Gitlab::GoogleCodeImport::Importer, lib: true do
subject.execute subject.execute
%w( %w(
Type-Defect Type-Enhancement Type-Task Type-Review Type-Other Milestone-0.12 Priority-Critical Type-Defect Type-Enhancement Type-Task Type-Review Type-Other Milestone-0.12 Priority-Critical
Priority-High Priority-Medium Priority-Low OpSys-All OpSys-Windows OpSys-Linux OpSys-OSX Security Priority-High Priority-Medium Priority-Low OpSys-All OpSys-Windows OpSys-Linux OpSys-OSX Security
Performance Usability Maintainability Component-Panel Component-Taskbar Component-Battery Performance Usability Maintainability Component-Panel Component-Taskbar Component-Battery
Component-Systray Component-Clock Component-Launcher Component-Tint2conf Component-Docs Component-New Component-Systray Component-Clock Component-Launcher Component-Tint2conf Component-Docs Component-New
).each do |label| ).each do |label|
label.sub!("-", ": ") label.sub!("-", ": ")