2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-27 06:08:27 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe JiraService do
|
2018-05-22 16:37:35 +00:00
|
|
|
include AssetsHelpers
|
2016-11-03 14:05:31 +00:00
|
|
|
|
2019-09-16 15:06:26 +00:00
|
|
|
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' }
|
|
|
|
|
2018-01-27 00:06:01 +00:00
|
|
|
describe '#options' do
|
2019-10-16 21:07:22 +00:00
|
|
|
let(:options) do
|
|
|
|
{
|
2019-09-16 15:06:26 +00:00
|
|
|
project: create(:project),
|
2018-01-27 00:06:01 +00:00
|
|
|
active: true,
|
2019-10-16 21:07:22 +00:00
|
|
|
username: 'username',
|
2018-01-27 00:06:01 +00:00
|
|
|
password: 'test',
|
|
|
|
jira_issue_transition_id: 24,
|
|
|
|
url: 'http://jira.test.com/path/'
|
2019-10-16 21:07:22 +00:00
|
|
|
}
|
2018-01-27 00:06:01 +00:00
|
|
|
end
|
|
|
|
|
2019-10-16 21:07:22 +00:00
|
|
|
let(:service) { described_class.create(options) }
|
|
|
|
|
2018-01-27 00:06:01 +00:00
|
|
|
it 'sets the URL properly' do
|
2019-10-16 21:07:22 +00:00
|
|
|
# jira-ruby gem parses the URI and handles trailing slashes fine:
|
|
|
|
# https://github.com/sumoheavy/jira-ruby/blob/v1.7.0/lib/jira/http_client.rb#L62
|
2018-01-27 00:06:01 +00:00
|
|
|
expect(service.options[:site]).to eq('http://jira.test.com/')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves out trailing slashes in context' do
|
|
|
|
expect(service.options[:context_path]).to eq('/path')
|
|
|
|
end
|
2019-10-11 12:06:29 +00:00
|
|
|
|
2019-10-16 21:07:22 +00:00
|
|
|
context 'username with trailing whitespaces' do
|
|
|
|
before do
|
|
|
|
options.merge!(username: 'username ')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves out trailing whitespaces in username' do
|
|
|
|
expect(service.options[:username]).to eq('username')
|
|
|
|
end
|
2019-10-11 12:06:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'provides additional cookies to allow basic auth with oracle webgate' do
|
|
|
|
expect(service.options[:use_cookies]).to eq(true)
|
|
|
|
expect(service.options[:additional_cookies]).to eq(['OBBasicAuth=fromDialog'])
|
|
|
|
end
|
2019-10-16 21:07:22 +00:00
|
|
|
|
|
|
|
context 'using api URL' do
|
|
|
|
before do
|
|
|
|
options.merge!(api_url: 'http://jira.test.com/api_path/')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves out trailing slashes in context' do
|
|
|
|
expect(service.options[:context_path]).to eq('/api_path')
|
|
|
|
end
|
|
|
|
end
|
2018-01-27 00:06:01 +00:00
|
|
|
end
|
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
describe 'Associations' do
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to belong_to :project }
|
|
|
|
it { is_expected.to have_one :service_hook }
|
2016-04-21 15:13:14 +00:00
|
|
|
end
|
|
|
|
|
2017-06-30 12:47:53 +00:00
|
|
|
describe '.reference_pattern' do
|
2020-04-21 15:21:10 +00:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:key, :result) do
|
|
|
|
'#123' | ''
|
|
|
|
'1#23#12' | ''
|
|
|
|
'JIRA-1234A' | 'JIRA-1234'
|
|
|
|
'JIRA-1234-some_tag' | 'JIRA-1234'
|
|
|
|
'JIRA-1234_some_tag' | 'JIRA-1234'
|
|
|
|
'EXT_EXT-1234' | 'EXT_EXT-1234'
|
|
|
|
'EXT3_EXT-1234' | 'EXT3_EXT-1234'
|
|
|
|
'3EXT_EXT-1234' | ''
|
|
|
|
end
|
2016-10-06 22:05:27 +00:00
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
with_them do
|
|
|
|
specify do
|
|
|
|
expect(described_class.reference_pattern.match(key).to_s).to eq(result)
|
|
|
|
end
|
2016-10-06 22:05:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-26 14:03:57 +00:00
|
|
|
describe '#create' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
2019-09-16 15:06:26 +00:00
|
|
|
project: create(:project),
|
|
|
|
url: url, api_url: api_url,
|
|
|
|
username: username, password: password,
|
|
|
|
jira_issue_transition_id: transition_id
|
2019-06-26 14:03:57 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.create(params) }
|
|
|
|
|
2019-09-16 15:06:26 +00:00
|
|
|
it 'does not store data into properties' do
|
|
|
|
expect(subject.properties).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores data in data_fields correcty' do
|
|
|
|
service = subject
|
|
|
|
|
|
|
|
expect(service.jira_tracker_data.url).to eq(url)
|
|
|
|
expect(service.jira_tracker_data.api_url).to eq(api_url)
|
|
|
|
expect(service.jira_tracker_data.username).to eq(username)
|
|
|
|
expect(service.jira_tracker_data.password).to eq(password)
|
|
|
|
expect(service.jira_tracker_data.jira_issue_transition_id).to eq(transition_id)
|
|
|
|
end
|
2019-06-26 14:03:57 +00:00
|
|
|
end
|
|
|
|
|
2019-09-16 15:06:26 +00:00
|
|
|
# we need to make sure we are able to read both from properties and jira_tracker_data table
|
2019-10-11 12:06:29 +00:00
|
|
|
# TODO: change this as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
|
2019-06-26 14:03:57 +00:00
|
|
|
context 'overriding properties' do
|
|
|
|
let(:access_params) do
|
2019-09-16 15:06:26 +00:00
|
|
|
{ url: url, api_url: api_url, username: username, password: password,
|
|
|
|
jira_issue_transition_id: transition_id }
|
|
|
|
end
|
|
|
|
let(:data_params) do
|
|
|
|
{
|
|
|
|
url: url, api_url: api_url,
|
|
|
|
username: username, password: password,
|
|
|
|
jira_issue_transition_id: transition_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'handles jira fields' do
|
|
|
|
let(:data_params) do
|
|
|
|
{
|
|
|
|
url: url, api_url: api_url,
|
|
|
|
username: username, password: password,
|
|
|
|
jira_issue_transition_id: transition_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'reading data' do
|
|
|
|
it 'reads data correctly' do
|
|
|
|
expect(service.url).to eq(url)
|
|
|
|
expect(service.api_url).to eq(api_url)
|
|
|
|
expect(service.username).to eq(username)
|
|
|
|
expect(service.password).to eq(password)
|
|
|
|
expect(service.jira_issue_transition_id).to eq(transition_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 21:08:48 +00:00
|
|
|
describe '#update' do
|
2019-09-16 15:06:26 +00:00
|
|
|
context 'basic update' do
|
|
|
|
let(:new_username) { 'new_username' }
|
|
|
|
let(:new_url) { 'http://jira-new.example.com' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
service.update(username: new_username, url: new_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves properties field emtpy' do
|
|
|
|
# expect(service.reload.properties).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores updated data in jira_tracker_data table' do
|
|
|
|
data = service.jira_tracker_data.reload
|
|
|
|
|
|
|
|
expect(data.url).to eq(new_url)
|
|
|
|
expect(data.api_url).to eq(api_url)
|
|
|
|
expect(data.username).to eq(new_username)
|
|
|
|
expect(data.password).to eq(password)
|
|
|
|
expect(data.jira_issue_transition_id).to eq(transition_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'stored password invalidation' do
|
|
|
|
context 'when a password was previously set' do
|
|
|
|
context 'when only web url present' do
|
|
|
|
let(:data_params) do
|
|
|
|
{
|
|
|
|
url: url, api_url: nil,
|
|
|
|
username: username, password: password,
|
|
|
|
jira_issue_transition_id: transition_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets password if url changed' do
|
|
|
|
service
|
|
|
|
service.url = 'http://jira_edited.example.com'
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.reload.url).to eq('http://jira_edited.example.com')
|
|
|
|
expect(service.password).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not reset password if url "changed" to the same url as before' do
|
|
|
|
service.url = 'http://jira.example.com'
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.reload.url).to eq('http://jira.example.com')
|
|
|
|
expect(service.password).not_to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets password if url not changed but api url added' do
|
|
|
|
service.api_url = 'http://jira_edited.example.com/rest/api/2'
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.reload.api_url).to eq('http://jira_edited.example.com/rest/api/2')
|
|
|
|
expect(service.password).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not reset password if new url is set together with password, even if it\'s the same password' do
|
|
|
|
service.url = 'http://jira_edited.example.com'
|
|
|
|
service.password = password
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.password).to eq(password)
|
|
|
|
expect(service.url).to eq('http://jira_edited.example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets password if url changed, even if setter called multiple times' do
|
|
|
|
service.url = 'http://jira1.example.com/rest/api/2'
|
|
|
|
service.url = 'http://jira1.example.com/rest/api/2'
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.password).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not reset password if username changed' do
|
|
|
|
service.username = 'some_name'
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.reload.password).to eq(password)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not reset password if password changed' do
|
|
|
|
service.url = 'http://jira_edited.example.com'
|
|
|
|
service.password = 'new_password'
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.reload.password).to eq('new_password')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not reset password if the password is touched and same as before' do
|
|
|
|
service.url = 'http://jira_edited.example.com'
|
|
|
|
service.password = password
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.reload.password).to eq(password)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when both web and api url present' do
|
|
|
|
let(:data_params) do
|
|
|
|
{
|
|
|
|
url: url, api_url: 'http://jira.example.com/rest/api/2',
|
|
|
|
username: username, password: password,
|
|
|
|
jira_issue_transition_id: transition_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets password if api url changed' do
|
|
|
|
service.api_url = 'http://jira_edited.example.com/rest/api/2'
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.password).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not reset password if url changed' do
|
|
|
|
service.url = 'http://jira_edited.example.com'
|
|
|
|
service.save
|
|
|
|
|
|
|
|
expect(service.password).to eq(password)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets password if api url set to empty' do
|
|
|
|
service.update(api_url: '')
|
|
|
|
|
|
|
|
expect(service.reload.password).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no password was previously set' do
|
|
|
|
let(:data_params) do
|
|
|
|
{
|
|
|
|
url: url, username: username
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves password if new url is set together with password' do
|
|
|
|
service.url = 'http://jira_edited.example.com/rest/api/2'
|
|
|
|
service.password = 'password'
|
|
|
|
service.save
|
|
|
|
expect(service.reload.password).to eq('password')
|
|
|
|
expect(service.reload.url).to eq('http://jira_edited.example.com/rest/api/2')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-06-26 14:03:57 +00:00
|
|
|
end
|
|
|
|
|
2019-10-07 15:05:59 +00:00
|
|
|
# this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
|
2019-06-26 14:03:57 +00:00
|
|
|
context 'when data are stored in properties' do
|
2020-06-22 12:08:47 +00:00
|
|
|
let(:properties) { data_params }
|
2019-09-16 15:06:26 +00:00
|
|
|
let!(:service) do
|
2019-10-01 12:05:59 +00:00
|
|
|
create(:jira_service, :without_properties_callback, properties: properties.merge(additional: 'something'))
|
2019-07-04 17:09:58 +00:00
|
|
|
end
|
2019-06-26 14:03:57 +00:00
|
|
|
|
2019-09-16 15:06:26 +00:00
|
|
|
it_behaves_like 'handles jira fields'
|
2019-06-26 14:03:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when data are stored in separated fields' do
|
|
|
|
let(:service) do
|
2020-06-22 12:08:47 +00:00
|
|
|
create(:jira_service, data_params.merge(properties: {}))
|
2019-06-26 14:03:57 +00:00
|
|
|
end
|
|
|
|
|
2019-09-16 15:06:26 +00:00
|
|
|
it_behaves_like 'handles jira fields'
|
2019-06-26 14:03:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when data are stored in both properties and separated fields' do
|
2020-06-22 12:08:47 +00:00
|
|
|
let(:properties) { data_params }
|
2019-06-26 14:03:57 +00:00
|
|
|
let(:service) do
|
2019-09-16 15:06:26 +00:00
|
|
|
create(:jira_service, :without_properties_callback, active: false, properties: properties).tap do |service|
|
|
|
|
create(:jira_tracker_data, data_params.merge(service: service))
|
|
|
|
end
|
2019-06-26 14:03:57 +00:00
|
|
|
end
|
|
|
|
|
2019-09-16 15:06:26 +00:00
|
|
|
it_behaves_like 'handles jira fields'
|
2019-06-26 14:03:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-01 08:45:06 +00:00
|
|
|
describe '#close_issue' do
|
2016-11-05 13:59:08 +00:00
|
|
|
let(:custom_base_url) { 'http://custom_url' }
|
2015-12-17 22:08:14 +00:00
|
|
|
let(:user) { create(:user) }
|
2018-08-21 08:22:11 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-09-29 21:11:32 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
shared_examples 'close_issue' do
|
|
|
|
before do
|
|
|
|
@jira_service = described_class.new
|
|
|
|
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',
|
|
|
|
jira_issue_transition_id: '999'
|
|
|
|
)
|
2015-12-17 22:08:14 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
# These stubs are needed to test JiraService#close_issue.
|
|
|
|
# We close the issue then do another request to API to check if it got closed.
|
|
|
|
# Here is stubbed the API return with a closed and an opened issues.
|
|
|
|
open_issue = JIRA::Resource::Issue.new(@jira_service.client, attrs: { 'id' => 'JIRA-123' })
|
|
|
|
closed_issue = open_issue.dup
|
|
|
|
allow(open_issue).to receive(:resolution).and_return(false)
|
|
|
|
allow(closed_issue).to receive(:resolution).and_return(true)
|
|
|
|
allow(JIRA::Resource::Issue).to receive(:find).and_return(open_issue, closed_issue)
|
2016-09-29 21:11:32 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
allow_any_instance_of(JIRA::Resource::Issue).to receive(:key).and_return('JIRA-123')
|
|
|
|
allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
|
2015-12-17 22:08:14 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.save
|
2016-11-09 21:55:21 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
project_issues_url = 'http://jira.example.com/rest/api/2/issue/JIRA-123'
|
|
|
|
@transitions_url = 'http://jira.example.com/rest/api/2/issue/JIRA-123/transitions'
|
|
|
|
@comment_url = 'http://jira.example.com/rest/api/2/issue/JIRA-123/comment'
|
|
|
|
@remote_link_url = 'http://jira.example.com/rest/api/2/issue/JIRA-123/remotelink'
|
2016-11-09 21:55:21 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
WebMock.stub_request(:get, project_issues_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password))
|
|
|
|
WebMock.stub_request(:post, @transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password))
|
|
|
|
WebMock.stub_request(:post, @comment_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password))
|
|
|
|
WebMock.stub_request(:post, @remote_link_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password))
|
|
|
|
end
|
2016-11-09 21:55:21 +00:00
|
|
|
|
2019-06-28 13:25:56 +00:00
|
|
|
it 'calls Jira API' do
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
2016-11-09 21:55:21 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
expect(WebMock).to have_requested(:post, @comment_url).with(
|
|
|
|
body: /Issue solved with/
|
|
|
|
).once
|
|
|
|
end
|
2017-08-01 19:19:54 +00:00
|
|
|
|
2019-05-10 13:33:44 +00:00
|
|
|
it 'does not fail if remote_link.all on issue returns nil' do
|
|
|
|
allow(JIRA::Resource::Remotelink).to receive(:all).and_return(nil)
|
|
|
|
|
|
|
|
expect { @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project)) }
|
2019-07-18 16:44:24 +00:00
|
|
|
.not_to raise_error
|
2019-05-10 13:33:44 +00:00
|
|
|
end
|
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
# Check https://developer.atlassian.com/jiradev/jira-platform/guides/other/guide-jira-remote-issue-links/fields-in-remote-issue-links
|
|
|
|
# for more information
|
2019-06-28 13:25:56 +00:00
|
|
|
it 'creates Remote Link reference in Jira for comment' do
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
|
|
|
|
|
|
|
favicon_path = "http://localhost/assets/#{find_asset('favicon.png').digest_path}"
|
|
|
|
|
|
|
|
# Creates comment
|
|
|
|
expect(WebMock).to have_requested(:post, @comment_url)
|
2019-06-28 13:25:56 +00:00
|
|
|
# Creates Remote Link in Jira issue fields
|
2018-08-21 08:22:11 +00:00
|
|
|
expect(WebMock).to have_requested(:post, @remote_link_url).with(
|
|
|
|
body: hash_including(
|
|
|
|
GlobalID: 'GitLab',
|
2019-02-28 09:46:09 +00:00
|
|
|
relationship: 'mentioned on',
|
2018-08-21 08:22:11 +00:00
|
|
|
object: {
|
2020-02-05 21:09:02 +00:00
|
|
|
url: "#{Gitlab.config.gitlab.url}/#{project.full_path}/-/commit/#{commit_id}",
|
2019-02-28 09:46:09 +00:00
|
|
|
title: "Solved by commit #{commit_id}.",
|
2018-08-21 08:22:11 +00:00
|
|
|
icon: { title: 'GitLab', url16x16: favicon_path },
|
|
|
|
status: { resolved: true }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).once
|
|
|
|
end
|
2017-08-01 19:19:54 +00:00
|
|
|
|
2019-11-26 12:06:18 +00:00
|
|
|
context 'when "comment_on_event_enabled" is set to false' do
|
|
|
|
it 'creates Remote Link reference but does not create comment' do
|
|
|
|
allow(@jira_service).to receive_messages(comment_on_event_enabled: false)
|
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
|
|
|
|
|
|
|
expect(WebMock).not_to have_requested(:post, @comment_url)
|
|
|
|
expect(WebMock).to have_requested(:post, @remote_link_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 00:09:34 +00:00
|
|
|
context 'when Remote Link already exists' do
|
|
|
|
let(:remote_link) do
|
|
|
|
double(
|
|
|
|
'remote link',
|
|
|
|
object: {
|
|
|
|
url: "#{Gitlab.config.gitlab.url}/#{project.full_path}/-/commit/#{commit_id}"
|
|
|
|
}.with_indifferent_access
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create comment' do
|
|
|
|
allow(JIRA::Resource::Remotelink).to receive(:all).and_return([remote_link])
|
|
|
|
|
|
|
|
expect(remote_link).to receive(:save!)
|
|
|
|
|
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
|
|
|
|
|
|
|
expect(WebMock).not_to have_requested(:post, @comment_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
it 'does not send comment or remote links to issues already closed' do
|
|
|
|
allow_any_instance_of(JIRA::Resource::Issue).to receive(:resolution).and_return(true)
|
2017-08-01 19:19:54 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
2016-09-01 05:50:27 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
expect(WebMock).not_to have_requested(:post, @comment_url)
|
|
|
|
expect(WebMock).not_to have_requested(:post, @remote_link_url)
|
|
|
|
end
|
2016-11-12 17:22:39 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
it 'does not send comment or remote links to issues with unknown resolution' do
|
|
|
|
allow_any_instance_of(JIRA::Resource::Issue).to receive(:respond_to?).with(:resolution).and_return(false)
|
2016-09-01 05:50:27 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
2016-09-01 05:50:27 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
expect(WebMock).not_to have_requested(:post, @comment_url)
|
|
|
|
expect(WebMock).not_to have_requested(:post, @remote_link_url)
|
2016-11-03 15:02:19 +00:00
|
|
|
end
|
2016-09-01 05:50:27 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
it 'references the GitLab commit' do
|
|
|
|
stub_config_setting(base_url: custom_base_url)
|
2016-09-01 05:50:27 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, @comment_url).with(
|
2020-02-05 21:09:02 +00:00
|
|
|
body: %r{#{custom_base_url}/#{project.full_path}/-/commit/#{commit_id}}
|
2018-08-21 08:22:11 +00:00
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'references the GitLab commit' do
|
|
|
|
stub_config_setting(relative_url_root: '/gitlab')
|
|
|
|
stub_config_setting(url: Settings.send(:build_gitlab_url))
|
|
|
|
|
|
|
|
allow(described_class).to receive(:default_url_options) do
|
|
|
|
{ script_name: '/gitlab' }
|
|
|
|
end
|
|
|
|
|
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
2016-09-01 05:50:27 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
expect(WebMock).to have_requested(:post, @comment_url).with(
|
2020-02-05 21:09:02 +00:00
|
|
|
body: %r{#{Gitlab.config.gitlab.url}/#{project.full_path}/-/commit/#{commit_id}}
|
2018-08-21 08:22:11 +00:00
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs exception when transition id is not valid' do
|
2018-08-20 18:34:07 +00:00
|
|
|
allow(@jira_service).to receive(:log_error)
|
|
|
|
WebMock.stub_request(:post, @transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).and_raise("Bad Request")
|
2016-09-29 21:11:32 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
2018-07-31 19:17:22 +00:00
|
|
|
|
2020-02-18 15:08:51 +00:00
|
|
|
expect(@jira_service).to have_received(:log_error).with(
|
|
|
|
"Issue transition failed",
|
|
|
|
error: hash_including(
|
|
|
|
exception_class: 'StandardError',
|
|
|
|
exception_message: "Bad Request"
|
|
|
|
),
|
|
|
|
client_url: "http://jira.example.com"
|
|
|
|
)
|
2018-07-31 19:17:22 +00:00
|
|
|
end
|
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
it 'calls the api with jira_issue_transition_id' do
|
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
2018-07-31 19:17:22 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, @transitions_url).with(
|
|
|
|
body: /999/
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
context 'when have multiple transition ids' do
|
|
|
|
it 'calls the api with transition ids separated by comma' do
|
|
|
|
allow(@jira_service).to receive_messages(jira_issue_transition_id: '1,2,3')
|
2018-07-31 19:17:22 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
2018-07-31 19:17:22 +00:00
|
|
|
|
|
|
|
1.upto(3) do |transition_id|
|
|
|
|
expect(WebMock).to have_requested(:post, @transitions_url).with(
|
|
|
|
body: /#{transition_id}/
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
it 'calls the api with transition ids separated by semicolon' do
|
|
|
|
allow(@jira_service).to receive_messages(jira_issue_transition_id: '1;2;3')
|
2018-07-31 19:17:22 +00:00
|
|
|
|
2018-08-21 08:22:11 +00:00
|
|
|
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
|
2018-07-31 19:17:22 +00:00
|
|
|
|
|
|
|
1.upto(3) do |transition_id|
|
|
|
|
expect(WebMock).to have_requested(:post, @transitions_url).with(
|
|
|
|
body: /#{transition_id}/
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-17 22:08:14 +00:00
|
|
|
end
|
2018-08-21 08:22:11 +00:00
|
|
|
|
|
|
|
context 'when resource is a merge request' do
|
|
|
|
let(:resource) { create(:merge_request) }
|
|
|
|
let(:commit_id) { resource.diff_head_sha }
|
|
|
|
|
|
|
|
it_behaves_like 'close_issue'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when resource is a commit' do
|
|
|
|
let(:resource) { project.commit('master') }
|
|
|
|
let(:commit_id) { resource.id }
|
|
|
|
|
|
|
|
it_behaves_like 'close_issue'
|
|
|
|
end
|
2016-12-01 08:45:06 +00:00
|
|
|
end
|
2016-10-27 21:20:47 +00:00
|
|
|
|
2020-05-06 21:10:00 +00:00
|
|
|
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(:jira_issue) { ExternalIssue.new('JIRA-123', project) }
|
|
|
|
|
|
|
|
subject { jira_service.create_cross_reference_note(jira_issue, resource, user) }
|
|
|
|
|
|
|
|
shared_examples 'creates a comment on Jira' do
|
|
|
|
let(:issue_url) { "#{url}/rest/api/2/issue/JIRA-123" }
|
|
|
|
let(:comment_url) { "#{issue_url}/comment" }
|
|
|
|
let(:remote_link_url) { "#{issue_url}/remotelink" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
|
|
|
|
stub_request(:get, issue_url).with(basic_auth: [username, password])
|
|
|
|
stub_request(:post, comment_url).with(basic_auth: [username, password])
|
|
|
|
stub_request(:post, remote_link_url).with(basic_auth: [username, password])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a comment on Jira' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, comment_url).with(
|
|
|
|
body: /mentioned this issue in/
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when resource is a commit' do
|
|
|
|
let(:resource) { project.commit('master') }
|
|
|
|
|
|
|
|
context 'when disabled' do
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(JiraService) do |instance|
|
|
|
|
allow(instance).to receive(:commit_events) { false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('Events for commits are disabled.') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when enabled' do
|
|
|
|
it_behaves_like 'creates a comment on Jira'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when resource is a merge request' do
|
|
|
|
let(:resource) { build_stubbed(:merge_request, source_project: project) }
|
|
|
|
|
|
|
|
context 'when disabled' do
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(JiraService) do |instance|
|
|
|
|
allow(instance).to receive(:merge_requests_events) { false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('Events for merge requests are disabled.') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when enabled' do
|
|
|
|
it_behaves_like 'creates a comment on Jira'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-27 18:09:04 +00:00
|
|
|
describe '#test' do
|
2016-12-01 08:45:06 +00:00
|
|
|
let(:jira_service) do
|
|
|
|
described_class.new(
|
2020-01-27 18:09:04 +00:00
|
|
|
url: url,
|
|
|
|
username: username,
|
|
|
|
password: password
|
2016-12-01 08:45:06 +00:00
|
|
|
)
|
|
|
|
end
|
2016-10-27 21:20:47 +00:00
|
|
|
|
2020-01-27 18:09:04 +00:00
|
|
|
def test_settings(url = 'jira.example.com')
|
|
|
|
test_url = "http://#{url}/rest/api/2/serverInfo"
|
2017-05-23 09:14:43 +00:00
|
|
|
|
2020-01-27 18:09:04 +00:00
|
|
|
WebMock.stub_request(:get, test_url).with(basic_auth: [username, password])
|
|
|
|
.to_return(body: { url: 'http://url' }.to_json )
|
2016-12-01 08:45:06 +00:00
|
|
|
|
2017-07-26 07:57:56 +00:00
|
|
|
jira_service.test(nil)
|
2017-05-23 09:14:43 +00:00
|
|
|
end
|
|
|
|
|
2017-07-26 07:57:56 +00:00
|
|
|
context 'when the test succeeds' do
|
2020-01-27 18:09:04 +00:00
|
|
|
it 'gets Jira project with URL when API URL not set' do
|
|
|
|
expect(test_settings).to eq(success: true, result: { 'url' => 'http://url' })
|
2017-07-26 07:57:56 +00:00
|
|
|
end
|
|
|
|
|
2020-01-27 18:09:04 +00:00
|
|
|
it 'gets Jira project with API URL if set' do
|
2017-07-26 07:57:56 +00:00
|
|
|
jira_service.update(api_url: 'http://jira.api.com')
|
2020-01-27 18:09:04 +00:00
|
|
|
|
|
|
|
expect(test_settings('jira.api.com')).to eq(success: true, result: { 'url' => 'http://url' })
|
2017-07-26 07:57:56 +00:00
|
|
|
end
|
2017-05-23 09:14:43 +00:00
|
|
|
end
|
2016-12-01 08:45:06 +00:00
|
|
|
|
2017-07-26 07:57:56 +00:00
|
|
|
context 'when the test fails' do
|
|
|
|
it 'returns result with the error' do
|
|
|
|
test_url = 'http://jira.example.com/rest/api/2/serverInfo'
|
2020-05-25 09:08:30 +00:00
|
|
|
error_message = 'Some specific failure.'
|
2020-01-27 18:09:04 +00:00
|
|
|
|
|
|
|
WebMock.stub_request(:get, test_url).with(basic_auth: [username, password])
|
2020-05-25 09:08:30 +00:00
|
|
|
.to_raise(JIRA::HTTPError.new(double(message: error_message)))
|
2017-07-26 07:57:56 +00:00
|
|
|
|
2020-01-27 18:09:04 +00:00
|
|
|
expect(jira_service).to receive(:log_error).with(
|
2020-05-25 09:08:30 +00:00
|
|
|
'Error sending message',
|
|
|
|
client_url: 'http://jira.example.com',
|
|
|
|
error: error_message
|
2020-01-27 18:09:04 +00:00
|
|
|
)
|
|
|
|
|
2020-05-25 09:08:30 +00:00
|
|
|
expect(jira_service.test(nil)).to eq(success: false, result: error_message)
|
2017-07-26 07:57:56 +00:00
|
|
|
end
|
2016-10-27 21:20:47 +00:00
|
|
|
end
|
2015-12-17 22:08:14 +00:00
|
|
|
end
|
|
|
|
|
2015-01-27 06:08:27 +00:00
|
|
|
describe 'project and issue urls' do
|
|
|
|
context 'when gitlab.yml was initialized' do
|
2019-06-26 14:03:57 +00:00
|
|
|
it 'is prepopulated with the settings' do
|
2015-12-17 22:08:14 +00:00
|
|
|
settings = {
|
2017-05-23 09:14:43 +00:00
|
|
|
'jira' => {
|
|
|
|
'url' => 'http://jira.sample/projects/project_a',
|
|
|
|
'api_url' => 'http://jira.sample/api'
|
2015-01-27 06:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-12 18:17:35 +00:00
|
|
|
allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
|
2015-01-27 06:08:27 +00:00
|
|
|
|
2019-06-26 14:03:57 +00:00
|
|
|
project = create(:project)
|
|
|
|
service = project.create_jira_service(active: true)
|
2015-01-27 06:08:27 +00:00
|
|
|
|
2019-09-16 15:06:26 +00:00
|
|
|
expect(service.url).to eq('http://jira.sample/projects/project_a')
|
|
|
|
expect(service.api_url).to eq('http://jira.sample/api')
|
2015-01-27 06:08:27 +00:00
|
|
|
end
|
|
|
|
end
|
2019-07-18 16:44:24 +00:00
|
|
|
|
|
|
|
it 'removes trailing slashes from url' do
|
|
|
|
service = described_class.new(url: 'http://jira.test.com/path/')
|
|
|
|
|
|
|
|
expect(service.url).to eq('http://jira.test.com/path')
|
|
|
|
end
|
2015-01-27 06:08:27 +00:00
|
|
|
end
|
2018-04-13 20:30:08 +00:00
|
|
|
|
2019-09-18 14:02:45 +00:00
|
|
|
describe 'favicon urls' do
|
2018-04-13 20:30:08 +00:00
|
|
|
it 'includes the standard favicon' do
|
|
|
|
props = described_class.new.send(:build_remote_link_props, url: 'http://example.com', title: 'title')
|
|
|
|
expect(props[:object][:icon][:url16x16]).to match %r{^http://localhost/assets/favicon(?:-\h+).png$}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes returns the custom favicon' do
|
2018-06-11 11:54:25 +00:00
|
|
|
create :appearance, favicon: fixture_file_upload('spec/fixtures/dk.png')
|
2018-04-13 20:30:08 +00:00
|
|
|
|
|
|
|
props = described_class.new.send(:build_remote_link_props, url: 'http://example.com', title: 'title')
|
2018-06-15 10:28:23 +00:00
|
|
|
expect(props[:object][:icon][:url16x16]).to match %r{^http://localhost/uploads/-/system/appearance/favicon/\d+/dk.png$}
|
2018-04-13 20:30:08 +00:00
|
|
|
end
|
|
|
|
end
|
2019-07-18 16:44:24 +00:00
|
|
|
|
|
|
|
context 'generating external URLs' do
|
|
|
|
let(:service) { described_class.new(url: 'http://jira.test.com/path/') }
|
|
|
|
|
|
|
|
describe '#issues_url' do
|
|
|
|
it 'handles trailing slashes' do
|
|
|
|
expect(service.issues_url).to eq('http://jira.test.com/path/browse/:id')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#new_issue_url' do
|
|
|
|
it 'handles trailing slashes' do
|
2020-07-14 06:09:17 +00:00
|
|
|
expect(service.new_issue_url).to eq('http://jira.test.com/path/secure/CreateIssue!default.jspa')
|
2019-07-18 16:44:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-01-27 06:08:27 +00:00
|
|
|
end
|