Commit 7f0bcf04 authored by tiagonbotelho's avatar tiagonbotelho

refactors update issue api request and some minor comments

parent b7d29ce6
...@@ -162,7 +162,12 @@ class IssuableBaseService < BaseService ...@@ -162,7 +162,12 @@ class IssuableBaseService < BaseService
if params.present? && update_issuable(issuable, params) if params.present? && update_issuable(issuable, params)
issuable.reset_events_cache issuable.reset_events_cache
# We do not touch as it will affect a update on updated_at field
ActiveRecord::Base.no_touching do
handle_common_system_notes(issuable, old_labels: old_labels) handle_common_system_notes(issuable, old_labels: old_labels)
end
handle_changes(issuable, old_labels: old_labels, old_mentioned_users: old_mentioned_users) handle_changes(issuable, old_labels: old_labels, old_mentioned_users: old_mentioned_users)
issuable.create_new_cross_references!(current_user) issuable.create_new_cross_references!(current_user)
execute_hooks(issuable, 'update') execute_hooks(issuable, 'update')
......
...@@ -102,6 +102,14 @@ module API ...@@ -102,6 +102,14 @@ module API
label || not_found!('Label') label || not_found!('Label')
end 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) def find_project_issue(id)
issue = user_project.issues.find(id) issue = user_project.issues.find(id)
not_found! unless can?(current_user, :read_issue, issue) not_found! unless can?(current_user, :read_issue, issue)
......
...@@ -154,14 +154,9 @@ module API ...@@ -154,14 +154,9 @@ module API
render_api_error!({ labels: errors }, 400) render_api_error!({ labels: errors }, 400)
end end
# Find or create labels # Find or create labels to attach to the issue. Labels are vaild
if params[:labels].present? # because we already checked its name, so there can't be an error here
attrs[:label_ids] = params[:labels].split(",").map do |label_name| attrs[:label_ids] = get_label_ids(params[:labels]) if params[:labels].present?
user_project.labels.create_with(color: Label::DEFAULT_COLOR)
.find_or_create_by(title: label_name.strip)
.id
end
end
issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute
...@@ -203,17 +198,16 @@ module API ...@@ -203,17 +198,16 @@ module API
render_api_error!({ labels: errors }, 400) render_api_error!({ labels: errors }, 400)
end 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 # 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 # we already checked its name, so there can't be an error here
if params[:labels] && can?(current_user, :admin_issue, user_project) if params[:labels] && can?(current_user, :admin_issue, user_project)
issue.remove_labels issue.remove_labels
# Create and add labels to the new created issue attrs[:label_ids] = get_label_ids(params[:labels])
issue.add_labels_by_names(params[:labels].split(','))
end end
issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue)
if issue.valid?
present issue, with: Entities::Issue, current_user: current_user present issue, with: Entities::Issue, current_user: current_user
else else
render_validation_error!(issue) render_validation_error!(issue)
......
...@@ -479,16 +479,16 @@ describe API::API, api: true do ...@@ -479,16 +479,16 @@ describe API::API, api: true do
expect(json_response['labels']).to eq(['label', 'label2']) expect(json_response['labels']).to eq(['label', 'label2'])
end end
it "emails label subscribers" do it "sends notifications for subscribers of newly added labels" do
clear_enqueued_jobs
label = project.labels.first label = project.labels.first
label.toggle_subscription(user2) label.toggle_subscription(user2)
expect do perform_enqueued_jobs do
post api("/projects/#{project.id}/issues", user), post api("/projects/#{project.id}/issues", user),
title: 'new issue', labels: label.title title: 'new issue', labels: label.title
end.to change{enqueued_jobs.size}.by(1) end
expect(response.status).to eq(201)
should_email(user2)
end end
it "returns a 400 bad request if title not given" do it "returns a 400 bad request if title not given" do
...@@ -646,6 +646,18 @@ describe API::API, api: true do ...@@ -646,6 +646,18 @@ describe API::API, api: true do
expect(json_response['labels']).to eq([label.title]) expect(json_response['labels']).to eq([label.title])
end end
it "sends notifications for subscribers of newly added labels when issue is updated" do
label = project.labels.first
label.toggle_subscription(user2)
perform_enqueued_jobs do
put api("/projects/#{project.id}/issues/#{issue.id}", user),
title: 'updated title', labels: label.title
end
should_email(user2)
end
it 'removes all labels' do it 'removes all labels' do
put api("/projects/#{project.id}/issues/#{issue.id}", user), put api("/projects/#{project.id}/issues/#{issue.id}", user),
labels: '' labels: ''
......
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