Commit e2cce1d6 authored by Brett Walker's avatar Brett Walker Committed by Imre Farkas

Update Jira service deployment type

when test settings are run
parent a0efb830
...@@ -5,6 +5,7 @@ class JiraService < IssueTrackerService ...@@ -5,6 +5,7 @@ class JiraService < IssueTrackerService
include Gitlab::Routing include Gitlab::Routing
include ApplicationHelper include ApplicationHelper
include ActionView::Helpers::AssetUrlHelper include ActionView::Helpers::AssetUrlHelper
include Gitlab::Utils::StrongMemoize
PROJECTS_PER_PAGE = 50 PROJECTS_PER_PAGE = 50
...@@ -32,6 +33,7 @@ class JiraService < IssueTrackerService ...@@ -32,6 +33,7 @@ class JiraService < IssueTrackerService
data_field :username, :password, :url, :api_url, :jira_issue_transition_id, :project_key, :issues_enabled data_field :username, :password, :url, :api_url, :jira_issue_transition_id, :project_key, :issues_enabled
before_update :reset_password before_update :reset_password
after_commit :update_deployment_type, on: [:create, :update], if: :update_deployment_type?
enum comment_detail: { enum comment_detail: {
standard: 1, standard: 1,
...@@ -212,7 +214,7 @@ class JiraService < IssueTrackerService ...@@ -212,7 +214,7 @@ class JiraService < IssueTrackerService
end end
def test(_) def test(_)
result = test_settings result = server_info
success = result.present? success = result.present?
result = @error&.message unless success result = @error&.message unless success
...@@ -231,10 +233,10 @@ class JiraService < IssueTrackerService ...@@ -231,10 +233,10 @@ class JiraService < IssueTrackerService
private private
def test_settings def server_info
return unless client_url.present? strong_memoize(:server_info) do
client_url.present? ? jira_request { client.ServerInfo.all.attrs } : nil
jira_request { client.ServerInfo.all.attrs } end
end end
def can_cross_reference?(noteable) def can_cross_reference?(noteable)
...@@ -436,6 +438,25 @@ class JiraService < IssueTrackerService ...@@ -436,6 +438,25 @@ class JiraService < IssueTrackerService
url_changed? url_changed?
end end
def update_deployment_type?
api_url_changed? || url_changed? || username_changed? || password_changed?
end
def update_deployment_type
clear_memoization(:server_info) # ensure we run the request when we try to update deployment type
results = server_info
return data_fields.deployment_unknown! unless results.present?
case results['deploymentType']
when 'Server'
data_fields.deployment_server!
when 'Cloud'
data_fields.deployment_cloud!
else
data_fields.deployment_unknown!
end
end
def self.event_description(event) def self.event_description(event)
case event case event
when "merge_request", "merge_request_events" when "merge_request", "merge_request_events"
......
---
title: Store deployment_type of Jira server in jira_tracker_data table
merge_request: 37003
author:
type: changed
...@@ -23,9 +23,12 @@ RSpec.describe Admin::IntegrationsController do ...@@ -23,9 +23,12 @@ RSpec.describe Admin::IntegrationsController do
end end
describe '#update' do describe '#update' do
include JiraServiceHelper
let(:integration) { create(:jira_service, :instance) } let(:integration) { create(:jira_service, :instance) }
before do before do
stub_jira_service_test
allow(PropagateIntegrationWorker).to receive(:perform_async) allow(PropagateIntegrationWorker).to receive(:perform_async)
put :update, params: { id: integration.class.to_param, overwrite: true, service: { url: url } } put :update, params: { id: integration.class.to_param, overwrite: true, service: { url: url } }
......
...@@ -81,10 +81,13 @@ RSpec.describe Groups::Settings::IntegrationsController do ...@@ -81,10 +81,13 @@ RSpec.describe Groups::Settings::IntegrationsController do
end end
describe '#update' do describe '#update' do
include JiraServiceHelper
let(:integration) { create(:jira_service, project: nil, group_id: group.id) } let(:integration) { create(:jira_service, project: nil, group_id: group.id) }
before do before do
group.add_owner(user) group.add_owner(user)
stub_jira_service_test
put :update, params: { group_id: group, id: integration.class.to_param, service: { url: url } } put :update, params: { group_id: group, id: integration.class.to_param, service: { url: url } }
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Projects::ServicesController do RSpec.describe Projects::ServicesController do
include JiraServiceHelper
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:service) { create(:jira_service, project: project) } let(:service) { create(:jira_service, project: project) }
...@@ -54,8 +56,7 @@ RSpec.describe Projects::ServicesController do ...@@ -54,8 +56,7 @@ RSpec.describe Projects::ServicesController do
end end
it 'returns success' do it 'returns success' do
stub_request(:get, 'http://example.com/rest/api/2/serverInfo') stub_jira_service_test
.to_return(status: 200, body: '{}')
expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
...@@ -66,8 +67,7 @@ RSpec.describe Projects::ServicesController do ...@@ -66,8 +67,7 @@ RSpec.describe Projects::ServicesController do
end end
it 'returns success' do it 'returns success' do
stub_request(:get, 'http://example.com/rest/api/2/serverInfo') stub_jira_service_test
.to_return(status: 200, body: '{}')
expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
...@@ -200,6 +200,7 @@ RSpec.describe Projects::ServicesController do ...@@ -200,6 +200,7 @@ RSpec.describe Projects::ServicesController do
describe 'as JSON' do describe 'as JSON' do
before do before do
stub_jira_service_test
put :update, params: project_params(service: service_params, format: :json) put :update, params: project_params(service: service_params, format: :json)
end end
......
...@@ -61,7 +61,10 @@ RSpec.describe 'User activates Jira', :js do ...@@ -61,7 +61,10 @@ RSpec.describe 'User activates Jira', :js do
end end
describe 'user disables the Jira Service' do describe 'user disables the Jira Service' do
include JiraServiceHelper
before do before do
stub_jira_service_test
visit_project_integration('Jira') visit_project_integration('Jira')
fill_form(disable: true) fill_form(disable: true)
click_button('Save changes') click_button('Save changes')
......
...@@ -10,6 +10,11 @@ RSpec.describe JiraService do ...@@ -10,6 +10,11 @@ RSpec.describe JiraService do
let(:username) { 'jira-username' } let(:username) { 'jira-username' }
let(:password) { 'jira-password' } let(:password) { 'jira-password' }
let(:transition_id) { 'test27' } let(:transition_id) { 'test27' }
let(:server_info_results) { { 'deploymentType' => 'Cloud' } }
before do
WebMock.stub_request(:get, /serverInfo/).to_return(body: server_info_results.to_json )
end
describe '#options' do describe '#options' do
let(:options) do let(:options) do
...@@ -103,7 +108,7 @@ RSpec.describe JiraService do ...@@ -103,7 +108,7 @@ RSpec.describe JiraService do
expect(subject.properties).to be_nil expect(subject.properties).to be_nil
end end
it 'stores data in data_fields correcty' do it 'stores data in data_fields correctly' do
service = subject service = subject
expect(service.jira_tracker_data.url).to eq(url) expect(service.jira_tracker_data.url).to eq(url)
...@@ -111,6 +116,35 @@ RSpec.describe JiraService do ...@@ -111,6 +116,35 @@ RSpec.describe JiraService do
expect(service.jira_tracker_data.username).to eq(username) expect(service.jira_tracker_data.username).to eq(username)
expect(service.jira_tracker_data.password).to eq(password) expect(service.jira_tracker_data.password).to eq(password)
expect(service.jira_tracker_data.jira_issue_transition_id).to eq(transition_id) expect(service.jira_tracker_data.jira_issue_transition_id).to eq(transition_id)
expect(service.jira_tracker_data.deployment_cloud?).to be_truthy
end
context 'when loading serverInfo' do
let!(:jira_service) { subject }
context 'Cloud instance' do
let(:server_info_results) { { 'deploymentType' => 'Cloud' } }
it 'is detected' do
expect(jira_service.jira_tracker_data.deployment_cloud?).to be_truthy
end
end
context 'Server instance' do
let(:server_info_results) { { 'deploymentType' => 'Server' } }
it 'is detected' do
expect(jira_service.jira_tracker_data.deployment_server?).to be_truthy
end
end
context 'Unknown instance' do
let(:server_info_results) { { 'deploymentType' => 'FutureCloud' } }
it 'is detected' do
expect(jira_service.jira_tracker_data.deployment_unknown?).to be_truthy
end
end
end end
end end
...@@ -151,8 +185,8 @@ RSpec.describe JiraService do ...@@ -151,8 +185,8 @@ RSpec.describe JiraService do
describe '#update' do describe '#update' do
context 'basic update' do context 'basic update' do
let(:new_username) { 'new_username' } let_it_be(:new_username) { 'new_username' }
let(:new_url) { 'http://jira-new.example.com' } let_it_be(:new_url) { 'http://jira-new.example.com' }
before do before do
service.update(username: new_username, url: new_url) service.update(username: new_username, url: new_url)
...@@ -173,6 +207,53 @@ RSpec.describe JiraService do ...@@ -173,6 +207,53 @@ RSpec.describe JiraService do
end end
end end
context 'when updating the url, api_url, username, or password' do
it 'updates deployment type' do
service.update(url: 'http://first.url')
service.jira_tracker_data.update(deployment_type: 'server')
expect(service.jira_tracker_data.deployment_server?).to be_truthy
service.update(api_url: 'http://another.url')
service.jira_tracker_data.reload
expect(service.jira_tracker_data.deployment_cloud?).to be_truthy
expect(WebMock).to have_requested(:get, /serverInfo/).twice
end
it 'calls serverInfo for url' do
service.update(url: 'http://first.url')
expect(WebMock).to have_requested(:get, /serverInfo/)
end
it 'calls serverInfo for api_url' do
service.update(api_url: 'http://another.url')
expect(WebMock).to have_requested(:get, /serverInfo/)
end
it 'calls serverInfo for username' do
service.update(username: 'test-user')
expect(WebMock).to have_requested(:get, /serverInfo/)
end
it 'calls serverInfo for password' do
service.update(password: 'test-password')
expect(WebMock).to have_requested(:get, /serverInfo/)
end
end
context 'when not updating the url, api_url, username, or password' do
it 'does not update deployment type' do
service.update(jira_issue_transition_id: 'jira_issue_transition_id')
expect(WebMock).not_to have_requested(:get, /serverInfo/)
end
end
context 'stored password invalidation' do context 'stored password invalidation' do
context 'when a password was previously set' do context 'when a password was previously set' do
context 'when only web url present' do context 'when only web url present' do
...@@ -627,6 +708,7 @@ RSpec.describe JiraService do ...@@ -627,6 +708,7 @@ RSpec.describe JiraService do
end end
describe '#test' do describe '#test' do
let(:server_info_results) { { 'url' => 'http://url', 'deploymentType' => 'Cloud' } }
let(:jira_service) do let(:jira_service) do
described_class.new( described_class.new(
url: url, url: url,
...@@ -635,24 +717,21 @@ RSpec.describe JiraService do ...@@ -635,24 +717,21 @@ RSpec.describe JiraService do
) )
end end
def test_settings(url = 'jira.example.com') def server_info
test_url = "http://#{url}/rest/api/2/serverInfo"
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 'gets Jira project with URL when API URL not set' do it 'gets Jira project with URL when API URL not set' do
expect(test_settings).to eq(success: true, result: { 'url' => 'http://url' }) expect(server_info).to eq(success: true, result: server_info_results)
expect(WebMock).to have_requested(:get, /jira.example.com/)
end end
it 'gets 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')
expect(test_settings('jira.api.com')).to eq(success: true, result: { 'url' => 'http://url' }) expect(server_info).to eq(success: true, result: server_info_results)
expect(WebMock).to have_requested(:get, /jira.api.com/)
end end
end end
......
...@@ -4,6 +4,12 @@ require 'spec_helper' ...@@ -4,6 +4,12 @@ require 'spec_helper'
RSpec.describe Admin::PropagateIntegrationService do RSpec.describe Admin::PropagateIntegrationService do
describe '.propagate' do describe '.propagate' do
include JiraServiceHelper
before do
stub_jira_service_test
end
let(:excluded_attributes) { %w[id project_id inherit_from_id instance created_at updated_at default] } let(:excluded_attributes) { %w[id project_id inherit_from_id instance created_at updated_at default] }
let!(:project) { create(:project) } let!(:project) { create(:project) }
let!(:instance_integration) do let!(:instance_integration) do
......
...@@ -416,6 +416,7 @@ RSpec.describe Git::BranchPushService, services: true do ...@@ -416,6 +416,7 @@ RSpec.describe Git::BranchPushService, services: true do
before do before do
# project.create_jira_service doesn't seem to invalidate the cache here # project.create_jira_service doesn't seem to invalidate the cache here
project.has_external_issue_tracker = true project.has_external_issue_tracker = true
stub_jira_service_test
jira_service_settings jira_service_settings
stub_jira_urls("JIRA-1") stub_jira_urls("JIRA-1")
......
...@@ -152,6 +152,7 @@ RSpec.describe MergeRequests::MergeService do ...@@ -152,6 +152,7 @@ RSpec.describe MergeRequests::MergeService do
let(:commit) { double('commit', safe_message: "Fixes #{jira_issue.to_reference}") } let(:commit) { double('commit', safe_message: "Fixes #{jira_issue.to_reference}") }
before do before do
stub_jira_service_test
project.update!(has_external_issue_tracker: true) project.update!(has_external_issue_tracker: true)
jira_service_settings jira_service_settings
stub_jira_urls(jira_issue.id) stub_jira_urls(jira_issue.id)
......
...@@ -79,7 +79,11 @@ RSpec.describe Projects::PropagateServiceTemplate do ...@@ -79,7 +79,11 @@ RSpec.describe Projects::PropagateServiceTemplate do
end end
context 'service with data fields' do context 'service with data fields' do
include JiraServiceHelper
let(:service_template) do let(:service_template) do
stub_jira_service_test
JiraService.create!( JiraService.create!(
template: true, template: true,
active: true, active: true,
......
...@@ -347,6 +347,7 @@ RSpec.describe SystemNoteService do ...@@ -347,6 +347,7 @@ RSpec.describe SystemNoteService do
let(:success_message) { "SUCCESS: Successfully posted to http://jira.example.net." } let(:success_message) { "SUCCESS: Successfully posted to http://jira.example.net." }
before do before do
stub_jira_service_test
stub_jira_urls(jira_issue.id) stub_jira_urls(jira_issue.id)
jira_service_settings jira_service_settings
end end
......
...@@ -78,8 +78,7 @@ module JiraServiceHelper ...@@ -78,8 +78,7 @@ module JiraServiceHelper
end end
def stub_jira_service_test def stub_jira_service_test
WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/serverInfo') WebMock.stub_request(:get, /serverInfo/).to_return(body: { url: 'http://url' }.to_json)
.to_return(body: { url: 'http://url' }.to_json)
end end
def stub_jira_urls(issue_id) def stub_jira_urls(issue_id)
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
Service.available_services_names.each do |service| Service.available_services_names.each do |service|
RSpec.shared_context service do RSpec.shared_context service do
include JiraServiceHelper if service == 'jira'
let(:dashed_service) { service.dasherize } let(:dashed_service) { service.dasherize }
let(:service_method) { "#{service}_service".to_sym } let(:service_method) { "#{service}_service".to_sym }
let(:service_klass) { "#{service}_service".classify.constantize } let(:service_klass) { "#{service}_service".classify.constantize }
...@@ -39,6 +41,7 @@ Service.available_services_names.each do |service| ...@@ -39,6 +41,7 @@ Service.available_services_names.each do |service|
before do before do
enable_license_for_service(service) enable_license_for_service(service)
stub_jira_service_test if service == 'jira'
end end
def initialize_service(service) def initialize_service(service)
......
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