Commit b0e56289 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Create RelatedIssues module namespace

parent c438cc4c
......@@ -11,7 +11,7 @@ module Projects
def create
opts = { issue_references: params[:issue_references] }
result = CreateRelatedIssueService.new(issue, current_user, opts).execute
result = RelatedIssues::CreateService.new(issue, current_user, opts).execute
render json: result, status: result['http_status']
end
......
module RelatedIssues
class CreateService < BaseService
def initialize(issue, user, params)
@issue, @current_user, @params = issue, user, params.dup
end
def execute
if referenced_issues.blank?
return error('No Issue found for given reference', 401)
end
begin
create_related_issues!
rescue => exception
return error(exception.message, 401)
end
success(message: "#{issues_sentence(referenced_issues)} were successfully related")
end
private
def create_related_issues!
RelatedIssue.transaction do
referenced_issues.each do |referenced_issue|
RelatedIssue.create!(issue: @issue, related_issue: referenced_issue)
end
end
end
def referenced_issues
@referenced_issues ||= begin
issue_references = params[:issue_references]
text = issue_references.join(' ')
extractor = Gitlab::ReferenceExtractor.new(@issue.project, @current_user)
extractor.analyze(text)
extractor.issues
end
end
def issues_sentence(issues)
issues.map { |issue| issue.to_reference(@issue.project) }.sort.to_sentence
end
end
end
......@@ -69,7 +69,7 @@ describe Projects::RelatedIssuesController, type: :controller do
end
describe 'POST #create' do
let(:service) { double(CreateRelatedIssueService, execute: service_response) }
let(:service) { double(RelatedIssues::CreateService, execute: service_response) }
let(:service_response) { { 'message' => 'yay' } }
let(:issue_references) { double }
let(:user_role) { :developer }
......@@ -77,7 +77,7 @@ describe Projects::RelatedIssuesController, type: :controller do
before do
project.team << [user, user_role]
allow(CreateRelatedIssueService).to receive(:new)
allow(RelatedIssues::CreateService).to receive(:new)
.with(issue, user, { issue_references: issue_references })
.and_return(service)
end
......
require 'spec_helper'
describe CreateRelatedIssueService, service: true do
describe RelatedIssues::CreateService, service: true do
describe '#execute' do
let(:namespace) { create :namespace }
let(:project) { create :empty_project, namespace: namespace }
......
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