Commit 5d0450d8 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Save relations whenever possible (dropping transaction)

parent c97f060c
......@@ -10,25 +10,19 @@ module IssueLinks
end
create_issue_link
success_message
rescue => exception
error(exception.message, 401)
success
end
private
def create_issue_link
IssueLink.transaction do
referenced_issues.each do |referenced_issue|
relate_issues(referenced_issue)
create_notes(referenced_issue)
end
referenced_issues.each do |referenced_issue|
create_notes(referenced_issue) if relate_issues(referenced_issue)
end
end
def relate_issues(referenced_issue)
IssueLink.create!(source: @issue, target: referenced_issue)
IssueLink.create(source: @issue, target: referenced_issue)
end
def create_notes(referenced_issue)
......@@ -49,15 +43,5 @@ module IssueLinks
end
end
end
def success_message
verb = referenced_issues.size > 1 ? 'were' : 'was'
success(message: "#{issues_sentence(referenced_issues)} #{verb} successfully related")
end
def issues_sentence(issues)
issues.map { |issue| issue.to_reference(@issue.project) }.sort.to_sentence
end
end
end
......@@ -38,6 +38,7 @@ describe Projects::IssueLinksController do
describe 'POST /*namespace_id/:project_id/issues/:issue_id/links' do
let(:issue_b) { create :issue, project: project }
let(:issue_references) { [issue_b.to_reference] }
let(:user_role) { :developer }
before do
......@@ -49,7 +50,7 @@ describe Projects::IssueLinksController do
post namespace_project_issue_links_path(namespace_id: issue.project.namespace,
project_id: issue.project,
issue_id: issue,
issue_references: [issue_b.to_reference],
issue_references: issue_references,
format: :json)
end
......@@ -60,7 +61,7 @@ describe Projects::IssueLinksController do
list_service_response = IssueLinks::ListService.new(issue, user).execute
expect(response).to have_http_status(200)
expect(json_response['result']).to eq('message' => "#{issue_b.to_reference} was successfully related", 'status' => 'success')
expect(json_response['result']).to eq('status' => 'success')
expect(json_response['issues']).to eq(list_service_response.as_json)
end
end
......@@ -77,7 +78,7 @@ describe Projects::IssueLinksController do
end
context 'when failing service result' do
let(:issue_b) { issue }
let(:issue_references) { ['#999'] }
it 'returns failure JSON' do
subject
......@@ -85,7 +86,7 @@ describe Projects::IssueLinksController do
list_service_response = IssueLinks::ListService.new(issue, user).execute
expect(response).to have_http_status(401)
expect(json_response['result']).to eq('message' => "Validation failed: Source issue cannot be related to itself",
expect(json_response['result']).to eq('message' => 'No Issue found for given reference',
'status' => 'error',
'http_status' => 401)
expect(json_response['issues']).to eq(list_service_response.as_json)
......
......@@ -85,7 +85,7 @@ describe IssueLinks::CreateService, service: true do
end
it 'returns success message with Issue reference' do
is_expected.to eq(message: "#{issue_a_ref} and #{another_project_issue_ref} were successfully related", status: :success)
is_expected.to eq(status: :success)
end
it 'creates notes' do
......@@ -105,40 +105,7 @@ describe IssueLinks::CreateService, service: true do
end
end
context 'success message' do
let(:issue_a) { create :issue, project: project }
let(:another_project) { create :empty_project, namespace: project.namespace }
let(:another_project_issue) { create :issue, project: another_project }
let(:issue_a_ref) { issue_a.to_reference }
let(:another_project_issue_ref) { another_project_issue.to_reference(project) }
before do
another_project.team << [user, :developer]
end
context 'multiple Issues relation' do
let(:params) do
{ issue_references: [issue_a_ref, another_project_issue_ref] }
end
it 'returns success message with Issue reference' do
is_expected.to eq(message: "#{issue_a_ref} and #{another_project_issue_ref} were successfully related", status: :success)
end
end
context 'single Issue relation' do
let(:params) do
{ issue_references: [issue_a_ref] }
end
it 'returns success message with Issue reference' do
is_expected.to eq(message: "#{issue_a_ref} was successfully related", status: :success)
end
end
end
context 'when relation already exists' do
context 'when reference of any already related issue is present' do
let(:issue_a) { create :issue, project: project }
let(:issue_b) { create :issue, project: project }
......@@ -150,12 +117,14 @@ describe IssueLinks::CreateService, service: true do
{ issue_references: [issue_b.to_reference, issue_a.to_reference] }
end
it 'returns error' do
is_expected.to eq(message: 'Validation failed: Source issue is already related', status: :error, http_status: 401)
it 'returns success' do
is_expected.to eq(status: :success)
end
it 'no relation is created' do
expect { subject }.not_to change(IssueLink, :count)
it 'valid relations are created' do
expect { subject }.to change(IssueLink, :count).from(1).to(2)
expect(IssueLink.find_by!(target: issue_b)).to have_attributes(source: issue)
end
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