Commit 24f16072 authored by James Lopez's avatar James Lopez

Merge branch '198411-more-verbose-jira-error-logs' into 'master'

More verbose JiraService error logs

Closes #198411

See merge request gitlab-org/gitlab!23688
parents 02ccaf48 8944a2f8
...@@ -194,7 +194,7 @@ class JiraService < IssueTrackerService ...@@ -194,7 +194,7 @@ class JiraService < IssueTrackerService
def test(_) def test(_)
result = test_settings result = test_settings
success = result.present? success = result.present?
result = @error if @error && !success result = @error&.message unless success
{ success: success, result: result } { success: success, result: result }
end end
...@@ -205,6 +205,8 @@ class JiraService < IssueTrackerService ...@@ -205,6 +205,8 @@ class JiraService < IssueTrackerService
nil nil
end end
private
def test_settings def test_settings
return unless client_url.present? return unless client_url.present?
...@@ -212,8 +214,6 @@ class JiraService < IssueTrackerService ...@@ -212,8 +214,6 @@ class JiraService < IssueTrackerService
jira_request { client.ServerInfo.all.attrs } jira_request { client.ServerInfo.all.attrs }
end end
private
def can_cross_reference?(noteable) def can_cross_reference?(noteable)
case noteable case noteable
when Commit then commit_events when Commit then commit_events
...@@ -346,9 +346,17 @@ class JiraService < IssueTrackerService ...@@ -346,9 +346,17 @@ class JiraService < IssueTrackerService
# Handle errors when doing Jira API calls # Handle errors when doing Jira API calls
def jira_request def jira_request
yield yield
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => e rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => error
@error = e.message @error = error
log_error("Error sending message", client_url: client_url, error: @error) log_error(
"Error sending message",
client_url: client_url,
error: {
exception_class: error.class.name,
exception_message: error.message,
exception_backtrace: error.backtrace.join("\n")
}
)
nil nil
end end
......
---
title: More verbose JiraService error logs
merge_request: 23688
author:
type: other
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
require 'spec_helper' require 'spec_helper'
describe JiraService do describe JiraService do
include Gitlab::Routing
include AssetsHelpers include AssetsHelpers
let(:title) { 'custom title' } let(:title) { 'custom title' }
...@@ -543,47 +542,55 @@ describe JiraService do ...@@ -543,47 +542,55 @@ describe JiraService do
end end
end end
describe '#test_settings' do describe '#test' do
let(:jira_service) do let(:jira_service) do
described_class.new( described_class.new(
project: create(:project), url: url,
url: 'http://jira.example.com', username: username,
username: 'jira_username', password: password
password: 'jira_password'
) )
end end
def test_settings(api_url = nil) def test_settings(url = 'jira.example.com')
api_url ||= 'jira.example.com' test_url = "http://#{url}/rest/api/2/serverInfo"
test_url = "http://#{api_url}/rest/api/2/serverInfo"
WebMock.stub_request(:get, test_url).with(basic_auth: %w(jira_username jira_password)).to_return(body: { url: 'http://url' }.to_json ) WebMock.stub_request(:get, test_url).with(basic_auth: [username, password])
.to_return(body: { url: 'http://url' }.to_json )
jira_service.test(nil) jira_service.test(nil)
end end
context 'when the test succeeds' do context 'when the test succeeds' do
it 'tries to get Jira project with URL when API URL not set' do it 'gets Jira project with URL when API URL not set' do
test_settings('jira.example.com') expect(test_settings).to eq(success: true, result: { 'url' => 'http://url' })
end
it 'returns correct result' do
expect(test_settings).to eq( { success: true, result: { 'url' => 'http://url' } })
end end
it 'tries to get Jira project with API URL if set' do it 'gets Jira project with API URL if set' do
jira_service.update(api_url: 'http://jira.api.com') jira_service.update(api_url: 'http://jira.api.com')
test_settings('jira.api.com')
expect(test_settings('jira.api.com')).to eq(success: true, result: { 'url' => 'http://url' })
end end
end end
context 'when the test fails' do context 'when the test fails' do
it 'returns result with the error' do it 'returns result with the error' do
test_url = 'http://jira.example.com/rest/api/2/serverInfo' test_url = 'http://jira.example.com/rest/api/2/serverInfo'
WebMock.stub_request(:get, test_url).with(basic_auth: %w(jira_username jira_password))
WebMock.stub_request(:get, test_url).with(basic_auth: [username, password])
.to_raise(JIRA::HTTPError.new(double(message: 'Some specific failure.'))) .to_raise(JIRA::HTTPError.new(double(message: 'Some specific failure.')))
expect(jira_service.test(nil)).to eq( { success: false, result: 'Some specific failure.' }) expect(jira_service).to receive(:log_error).with(
"Error sending message",
hash_including(
client_url: url,
error: hash_including(
exception_class: 'JIRA::HTTPError',
exception_message: 'Some specific failure.'
)
)
)
expect(jira_service.test(nil)).to eq(success: false, result: 'Some specific failure.')
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