Commit ced2b10e authored by Markus Koller's avatar Markus Koller

Merge branch '294155-extract-jira-service-find-issue' into 'master'

Extract JiraService#find_issue method

See merge request gitlab-org/gitlab!51637
parents aa377cbd 757493e2
......@@ -157,8 +157,12 @@ class JiraService < IssueTrackerService
# support any events.
end
def find_issue(issue_key)
jira_request { client.Issue.find(issue_key) }
end
def close_issue(entity, external_issue)
issue = jira_request { client.Issue.find(external_issue.iid) }
issue = find_issue(external_issue.iid)
return if issue.nil? || has_resolution?(issue) || !jira_issue_transition_id.present?
......@@ -172,7 +176,7 @@ class JiraService < IssueTrackerService
# Depending on the Jira project's workflow, a comment during transition
# may or may not be allowed. Refresh the issue after transition and check
# if it is closed, so we don't have one comment for every commit.
issue = jira_request { client.Issue.find(issue.key) } if transition_issue(issue)
issue = find_issue(issue.key) if transition_issue(issue)
add_issue_solved_comment(issue, commit_id, commit_url) if has_resolution?(issue)
end
......@@ -181,7 +185,7 @@ class JiraService < IssueTrackerService
return s_("JiraService|Events for %{noteable_model_name} are disabled.") % { noteable_model_name: noteable.model_name.plural.humanize(capitalize: false) }
end
jira_issue = jira_request { client.Issue.find(mentioned.id) }
jira_issue = find_issue(mentioned.id)
return unless jira_issue.present?
......
......@@ -5,12 +5,21 @@ require 'spec_helper'
RSpec.describe JiraService do
include AssetsHelpers
let_it_be(:project) { create(:project, :repository) }
let(:url) { 'http://jira.example.com' }
let(:api_url) { 'http://api-jira.example.com' }
let(:username) { 'jira-username' }
let(:password) { 'jira-password' }
let(:transition_id) { 'test27' }
let(:server_info_results) { { 'deploymentType' => 'Cloud' } }
let(:jira_service) do
described_class.new(
project: project,
url: url,
username: username,
password: password
)
end
before do
WebMock.stub_request(:get, /serverInfo/).to_return(body: server_info_results.to_json )
......@@ -19,7 +28,7 @@ RSpec.describe JiraService do
describe '#options' do
let(:options) do
{
project: create(:project),
project: project,
active: true,
username: 'username',
password: 'test',
......@@ -108,7 +117,7 @@ RSpec.describe JiraService do
describe '#create' do
let(:params) do
{
project: create(:project),
project: project,
url: url, api_url: api_url,
username: username, password: password,
jira_issue_transition_id: transition_id
......@@ -434,10 +443,23 @@ RSpec.describe JiraService do
end
end
describe '#find_issue' do
let(:issue_key) { 'JIRA-123' }
let(:issue_url) { "#{url}/rest/api/2/issue/#{issue_key}" }
before do
stub_request(:get, issue_url).with(basic_auth: [username, password])
end
it 'call the Jira API to get the issue' do
jira_service.find_issue(issue_key)
expect(WebMock).to have_requested(:get, issue_url)
end
end
describe '#close_issue' do
let(:custom_base_url) { 'http://custom_url' }
let(:user) { create(:user) }
let(:project) { create(:project, :repository) }
shared_examples 'close_issue' do
before do
......@@ -445,7 +467,6 @@ RSpec.describe JiraService do
allow(@jira_service).to receive_messages(
project_id: project.id,
project: project,
service_hook: true,
url: 'http://jira.example.com',
username: 'gitlab_jira_username',
password: 'gitlab_jira_password',
......@@ -657,17 +678,7 @@ RSpec.describe JiraService do
end
describe '#create_cross_reference_note' do
let_it_be(:user) { build_stubbed(:user) }
let_it_be(:project) { create(:project, :repository) }
let(:jira_service) do
described_class.new(
project: project,
url: url,
username: username,
password: password
)
end
let_it_be(:user) { build_stubbed(:user) }
let(:jira_issue) { ExternalIssue.new('JIRA-123', project) }
subject { jira_service.create_cross_reference_note(jira_issue, resource, user) }
......@@ -732,15 +743,6 @@ RSpec.describe JiraService do
describe '#test' do
let(:server_info_results) { { 'url' => 'http://url', 'deploymentType' => 'Cloud' } }
let_it_be(:project) { create(:project, :repository) }
let(:jira_service) do
described_class.new(
url: url,
project: project,
username: username,
password: password
)
end
def server_info
jira_service.test(nil)
......@@ -790,7 +792,6 @@ RSpec.describe JiraService do
}
allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
project = create(:project)
service = project.create_jira_service(active: true)
expect(service.url).to eq('http://jira.sample/projects/project_a')
......
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