Commit 2fe45183 authored by Jarka Košanová's avatar Jarka Košanová

Extract methods to issue tracker services

- add methods to service_tracker_service
- check based on these methods instead of checking if Jira is active
parent ac2e2c6a
...@@ -1190,6 +1190,10 @@ class Project < ApplicationRecord ...@@ -1190,6 +1190,10 @@ class Project < ApplicationRecord
update_column(:has_external_issue_tracker, services.external_issue_trackers.any?) if Gitlab::Database.read_write? update_column(:has_external_issue_tracker, services.external_issue_trackers.any?) if Gitlab::Database.read_write?
end end
def external_references_supported?
external_issue_tracker&.support_cross_reference?
end
def has_wiki? def has_wiki?
wiki_enabled? || has_external_wiki? wiki_enabled? || has_external_wiki?
end end
......
...@@ -151,6 +151,14 @@ class IssueTrackerService < Service ...@@ -151,6 +151,14 @@ class IssueTrackerService < Service
result result
end end
def support_close_issue?
false
end
def support_cross_reference?
false
end
private private
def enabled_in_gitlab_config def enabled_in_gitlab_config
......
# frozen_string_literal: true # frozen_string_literal: true
class JiraService < IssueTrackerService class JiraService < IssueTrackerService
extend ::Gitlab::Utils::Override
include Gitlab::Routing include Gitlab::Routing
include ApplicationHelper include ApplicationHelper
include ActionView::Helpers::AssetUrlHelper include ActionView::Helpers::AssetUrlHelper
...@@ -205,6 +206,16 @@ class JiraService < IssueTrackerService ...@@ -205,6 +206,16 @@ class JiraService < IssueTrackerService
nil nil
end end
override :support_close_issue?
def support_close_issue?
true
end
override :support_cross_reference?
def support_cross_reference?
true
end
private private
def test_settings def test_settings
......
...@@ -18,9 +18,9 @@ module Issues ...@@ -18,9 +18,9 @@ module Issues
# The code calling this method is responsible for ensuring that a user is # The code calling this method is responsible for ensuring that a user is
# allowed to close the given issue. # allowed to close the given issue.
def close_issue(issue, closed_via: nil, notifications: true, system_note: true) def close_issue(issue, closed_via: nil, notifications: true, system_note: true)
if project.jira_tracker_active? && issue.is_a?(ExternalIssue) if issue.is_a?(ExternalIssue)
project.jira_service.close_issue(closed_via, issue) close_external_issue(issue, closed_via)
todo_service.close_issue(issue, current_user)
return issue return issue
end end
...@@ -47,6 +47,13 @@ module Issues ...@@ -47,6 +47,13 @@ module Issues
private private
def close_external_issue(issue, closed_via)
return unless project.external_issue_tracker&.support_close_issue?
project.external_issue_tracker.close_issue(closed_via, issue)
todo_service.close_issue(issue, current_user)
end
def create_note(issue, current_commit) def create_note(issue, current_commit)
SystemNoteService.change_status(issue, issue.project, current_user, issue.state, current_commit) SystemNoteService.change_status(issue, issue.project, current_user, issue.state, current_commit)
end end
......
...@@ -144,7 +144,7 @@ module SystemNotes ...@@ -144,7 +144,7 @@ module SystemNotes
# #
# Returns Boolean # Returns Boolean
def cross_reference_disallowed?(mentioner) def cross_reference_disallowed?(mentioner)
return true if noteable.is_a?(ExternalIssue) && !noteable.project.jira_tracker_active? return true if noteable.is_a?(ExternalIssue) && !noteable.project&.external_references_supported?
return false unless mentioner.is_a?(MergeRequest) return false unless mentioner.is_a?(MergeRequest)
return false unless noteable.is_a?(Commit) return false unless noteable.is_a?(Commit)
......
...@@ -44,7 +44,7 @@ module Gitlab ...@@ -44,7 +44,7 @@ module Gitlab
end end
def issues def issues
if project && project.jira_tracker? if project&.external_references_supported?
if project.issues_enabled? if project.issues_enabled?
@references[:all_issues] ||= references(:external_issue) + references(:issue) @references[:all_issues] ||= references(:external_issue) + references(:issue)
else else
......
...@@ -225,6 +225,24 @@ describe Gitlab::ReferenceExtractor do ...@@ -225,6 +225,24 @@ describe Gitlab::ReferenceExtractor do
end end
end end
context 'with an inactive external issue tracker' do
let(:project) { create(:project) }
let!(:jira_service) { create(:jira_service, project: project, active: false) }
let(:issue) { create(:issue, project: project) }
context 'when GitLab issues are enabled' do
it 'returns only internal issue' do
subject.analyze("JIRA-123 and FOOBAR-4567 and #{issue.to_reference}")
expect(subject.issues).to eq([issue])
end
it 'does not return any issue if the internal one does not exists' do
subject.analyze("JIRA-123 and FOOBAR-4567 and #999")
expect(subject.issues).to be_empty
end
end
end
context 'with a project with an underscore' do context 'with a project with an underscore' do
let(:other_project) { create(:project, path: 'test_project') } let(:other_project) { create(:project, path: 'test_project') }
let(:issue) { create(:issue, project: other_project) } let(:issue) { create(:issue, project: other_project) }
......
...@@ -70,6 +70,38 @@ describe Issues::CloseService do ...@@ -70,6 +70,38 @@ describe Issues::CloseService do
end end
describe '#close_issue' do describe '#close_issue' do
context 'with external issue' do
context 'with an active external issue tracker supporting close_issue' do
let!(:external_issue_tracker) { create(:jira_service, project: project) }
it 'closes the issue on the external issue tracker' do
expect(project.external_issue_tracker).to receive(:close_issue)
described_class.new(project, user).close_issue(external_issue)
end
end
context 'with innactive external issue tracker supporting close_issue' do
let!(:external_issue_tracker) { create(:jira_service, project: project, active: false) }
it 'does not close the issue on the external issue tracker' do
expect(project.external_issue_tracker).not_to receive(:close_issue)
described_class.new(project, user).close_issue(external_issue)
end
end
context 'with an active external issue tracker not supporting close_issue' do
let!(:external_issue_tracker) { create(:bugzilla_service, project: project) }
it 'does not close the issue on the external issue tracker' do
expect(project.external_issue_tracker).not_to receive(:close_issue)
described_class.new(project, user).close_issue(external_issue)
end
end
end
context "closed by a merge request", :sidekiq_might_not_need_inline do context "closed by a merge request", :sidekiq_might_not_need_inline do
it 'mentions closure via a merge request' do it 'mentions closure via a merge request' do
perform_enqueued_jobs do perform_enqueued_jobs do
......
...@@ -158,7 +158,7 @@ describe MergeRequests::MergeService do ...@@ -158,7 +158,7 @@ describe MergeRequests::MergeService do
end end
it 'does not close issue' do it 'does not close issue' do
allow(jira_tracker).to receive_messages(jira_issue_transition_id: nil) jira_tracker.update(jira_issue_transition_id: nil)
expect_any_instance_of(JiraService).not_to receive(:transition_issue) expect_any_instance_of(JiraService).not_to receive(:transition_issue)
......
...@@ -598,8 +598,8 @@ describe ::SystemNotes::IssuablesService do ...@@ -598,8 +598,8 @@ describe ::SystemNotes::IssuablesService do
context 'when mentioner is not a MergeRequest' do context 'when mentioner is not a MergeRequest' do
it 'is falsey' do it 'is falsey' do
mentioner = noteable.dup mentioner = noteable.dup
expect(service.cross_reference_disallowed?(mentioner))
.to be_falsey expect(service.cross_reference_disallowed?(mentioner)).to be_falsey
end end
end end
...@@ -609,24 +609,35 @@ describe ::SystemNotes::IssuablesService do ...@@ -609,24 +609,35 @@ describe ::SystemNotes::IssuablesService do
it 'is truthy when noteable is in commits' do it 'is truthy when noteable is in commits' do
expect(mentioner).to receive(:commits).and_return([noteable]) expect(mentioner).to receive(:commits).and_return([noteable])
expect(service.cross_reference_disallowed?(mentioner))
.to be_truthy expect(service.cross_reference_disallowed?(mentioner)).to be_truthy
end end
it 'is falsey when noteable is not in commits' do it 'is falsey when noteable is not in commits' do
expect(mentioner).to receive(:commits).and_return([]) expect(mentioner).to receive(:commits).and_return([])
expect(service.cross_reference_disallowed?(mentioner))
.to be_falsey expect(service.cross_reference_disallowed?(mentioner)).to be_falsey
end end
end end
context 'when notable is an ExternalIssue' do context 'when notable is an ExternalIssue' do
let(:project) { create(:project) }
let(:noteable) { ExternalIssue.new('EXT-1234', project) } let(:noteable) { ExternalIssue.new('EXT-1234', project) }
it 'is truthy' do it 'is false with issue tracker supporting referencing' do
mentioner = noteable.dup create(:jira_service, project: project)
expect(service.cross_reference_disallowed?(mentioner))
.to be_truthy expect(service.cross_reference_disallowed?(noteable)).to be_falsey
end
it 'is true with issue tracker not supporting referencing' do
create(:bugzilla_service, project: project)
expect(service.cross_reference_disallowed?(noteable)).to be_truthy
end
it 'is true without issue tracker' do
expect(service.cross_reference_disallowed?(noteable)).to be_truthy
end end
end 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