summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authortiagonbotelho <tiagonbotelho@hotmail.com>2016-08-15 17:50:41 +0100
committertiagonbotelho <tiagonbotelho@hotmail.com>2016-08-30 18:37:21 +0100
commit7f0bcf04323ad69b64a90112896971ea8d1a5f99 (patch)
treec8347404516997787c053859a6097e91d9cfc9d9 /lib
parentb7d29ce659412e9a2acc411c841420eb13d115ba (diff)
downloadgitlab-ce-7f0bcf04323ad69b64a90112896971ea8d1a5f99.tar.gz
refactors update issue api request and some minor comments
Diffstat (limited to 'lib')
-rw-r--r--lib/api/helpers.rb8
-rw-r--r--lib/api/issues.rb26
2 files changed, 18 insertions, 16 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index da4b1bf9902..dbad86d8926 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -102,6 +102,14 @@ module API
label || not_found!('Label')
end
+ def get_label_ids(labels)
+ labels.split(",").map do |label_name|
+ user_project.labels.create_with(color: Label::DEFAULT_COLOR)
+ .find_or_create_by(title: label_name.strip)
+ .id
+ end
+ end
+
def find_project_issue(id)
issue = user_project.issues.find(id)
not_found! unless can?(current_user, :read_issue, issue)
diff --git a/lib/api/issues.rb b/lib/api/issues.rb
index 9a042e6e70d..39a46f69f16 100644
--- a/lib/api/issues.rb
+++ b/lib/api/issues.rb
@@ -154,14 +154,9 @@ module API
render_api_error!({ labels: errors }, 400)
end
- # Find or create labels
- if params[:labels].present?
- attrs[:label_ids] = params[:labels].split(",").map do |label_name|
- user_project.labels.create_with(color: Label::DEFAULT_COLOR)
- .find_or_create_by(title: label_name.strip)
- .id
- end
- end
+ # Find or create labels to attach to the issue. Labels are vaild
+ # because we already checked its name, so there can't be an error here
+ attrs[:label_ids] = get_label_ids(params[:labels]) if params[:labels].present?
issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute
@@ -203,17 +198,16 @@ module API
render_api_error!({ labels: errors }, 400)
end
+ # Find or create labels and attach to issue. Labels are valid because
+ # we already checked its name, so there can't be an error here
+ if params[:labels] && can?(current_user, :admin_issue, user_project)
+ issue.remove_labels
+ attrs[:label_ids] = get_label_ids(params[:labels])
+ end
+
issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue)
if issue.valid?
- # Find or create labels and attach to issue. Labels are valid because
- # we already checked its name, so there can't be an error here
- if params[:labels] && can?(current_user, :admin_issue, user_project)
- issue.remove_labels
- # Create and add labels to the new created issue
- issue.add_labels_by_names(params[:labels].split(','))
- end
-
present issue, with: Entities::Issue, current_user: current_user
else
render_validation_error!(issue)