Commit 43ec70a4 authored by Arturo Herrero's avatar Arturo Herrero

Merge branch '16119-fix-custom-transition-sequence' into 'master'

Add spec for failing Jira issue transitions

See merge request gitlab-org/gitlab!54566
parents 72227a5c d476e636
...@@ -472,7 +472,7 @@ RSpec.describe JiraService do ...@@ -472,7 +472,7 @@ RSpec.describe JiraService do
shared_examples 'close_issue' do shared_examples 'close_issue' do
let(:issue_key) { 'JIRA-123' } let(:issue_key) { 'JIRA-123' }
let(:issue_url) { "http://jira.example.com/rest/api/2/issue/#{issue_key}" } let(:issue_url) { "#{url}/rest/api/2/issue/#{issue_key}" }
let(:transitions_url) { "#{issue_url}/transitions" } let(:transitions_url) { "#{issue_url}/transitions" }
let(:comment_url) { "#{issue_url}/comment" } let(:comment_url) { "#{issue_url}/comment" }
let(:remote_link_url) { "#{issue_url}/remotelink" } let(:remote_link_url) { "#{issue_url}/remotelink" }
...@@ -487,24 +487,16 @@ RSpec.describe JiraService do ...@@ -487,24 +487,16 @@ RSpec.describe JiraService do
end end
subject(:close_issue) do subject(:close_issue) do
@jira_service.close_issue(resource, ExternalIssue.new(issue_key, project)) jira_service.close_issue(resource, ExternalIssue.new(issue_key, project))
end end
before do before do
@jira_service = described_class.new allow(jira_service).to receive_messages(jira_issue_transition_id: '999')
allow(@jira_service).to receive_messages(
project_id: project.id,
project: project,
url: 'http://jira.example.com',
username: 'gitlab_jira_username',
password: 'gitlab_jira_password',
jira_issue_transition_id: '999'
)
# These stubs are needed to test JiraService#close_issue. # 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. # 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. # Here is stubbed the API return with a closed and an opened issues.
open_issue = JIRA::Resource::Issue.new(@jira_service.client, attrs: issue_fields.deep_stringify_keys) open_issue = JIRA::Resource::Issue.new(jira_service.client, attrs: issue_fields.deep_stringify_keys)
closed_issue = open_issue.dup closed_issue = open_issue.dup
allow(open_issue).to receive(:resolution).and_return(false) allow(open_issue).to receive(:resolution).and_return(false)
allow(closed_issue).to receive(:resolution).and_return(true) allow(closed_issue).to receive(:resolution).and_return(true)
...@@ -513,18 +505,16 @@ RSpec.describe JiraService do ...@@ -513,18 +505,16 @@ RSpec.describe JiraService do
allow_any_instance_of(JIRA::Resource::Issue).to receive(:key).and_return(issue_key) allow_any_instance_of(JIRA::Resource::Issue).to receive(:key).and_return(issue_key)
allow(JIRA::Resource::Remotelink).to receive(:all).and_return([]) allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
@jira_service.save! WebMock.stub_request(:get, issue_url).with(basic_auth: %w(jira-username jira-password))
WebMock.stub_request(:post, transitions_url).with(basic_auth: %w(jira-username jira-password))
WebMock.stub_request(:get, issue_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)) WebMock.stub_request(:post, comment_url).with(basic_auth: %w(jira-username jira-password))
WebMock.stub_request(:post, transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)) WebMock.stub_request(:post, remote_link_url).with(basic_auth: %w(jira-username 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 end
let(:external_issue) { ExternalIssue.new('JIRA-123', project) } let(:external_issue) { ExternalIssue.new('JIRA-123', project) }
def close_issue def close_issue
@jira_service.close_issue(resource, external_issue, current_user) jira_service.close_issue(resource, external_issue, current_user)
end end
it 'calls Jira API' do it 'calls Jira API' do
...@@ -575,7 +565,7 @@ RSpec.describe JiraService do ...@@ -575,7 +565,7 @@ RSpec.describe JiraService do
context 'when "comment_on_event_enabled" is set to false' do context 'when "comment_on_event_enabled" is set to false' do
it 'creates Remote Link reference but does not create comment' do it 'creates Remote Link reference but does not create comment' do
allow(@jira_service).to receive_messages(comment_on_event_enabled: false) allow(jira_service).to receive_messages(comment_on_event_enabled: false)
close_issue close_issue
expect(WebMock).not_to have_requested(:post, comment_url) expect(WebMock).not_to have_requested(:post, comment_url)
...@@ -648,12 +638,12 @@ RSpec.describe JiraService do ...@@ -648,12 +638,12 @@ RSpec.describe JiraService do
end end
it 'logs exception when transition id is not valid' do it 'logs exception when transition id is not valid' do
allow(@jira_service).to receive(:log_error) 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") WebMock.stub_request(:post, transitions_url).with(basic_auth: %w(jira-username jira-password)).and_raise("Bad Request")
close_issue close_issue
expect(@jira_service).to have_received(:log_error).with( expect(jira_service).to have_received(:log_error).with(
"Issue transition failed", "Issue transition failed",
error: hash_including( error: hash_including(
exception_class: 'StandardError', exception_class: 'StandardError',
...@@ -682,7 +672,7 @@ RSpec.describe JiraService do ...@@ -682,7 +672,7 @@ RSpec.describe JiraService do
end end
before do before do
allow(@jira_service).to receive_messages(jira_issue_transition_id: '') allow(jira_service).to receive_messages(jira_issue_transition_id: '')
close_issue close_issue
end end
...@@ -715,9 +705,11 @@ RSpec.describe JiraService do ...@@ -715,9 +705,11 @@ RSpec.describe JiraService do
end end
context 'when using multiple transition ids' do context 'when using multiple transition ids' do
it 'calls the api with transition ids separated by comma' do before do
allow(@jira_service).to receive_messages(jira_issue_transition_id: '1,2,3') allow(jira_service).to receive_messages(jira_issue_transition_id: '1,2,3')
end
it 'calls the api with transition ids separated by comma' do
close_issue close_issue
1.upto(3) do |transition_id| 1.upto(3) do |transition_id|
...@@ -725,10 +717,12 @@ RSpec.describe JiraService do ...@@ -725,10 +717,12 @@ RSpec.describe JiraService do
body: /"id":"#{transition_id}"/ body: /"id":"#{transition_id}"/
).once ).once
end end
expect(WebMock).to have_requested(:post, comment_url)
end end
it 'calls the api with transition ids separated by semicolon' do it 'calls the api with transition ids separated by semicolon' do
allow(@jira_service).to receive_messages(jira_issue_transition_id: '1;2;3') allow(jira_service).to receive_messages(jira_issue_transition_id: '1;2;3')
close_issue close_issue
...@@ -737,6 +731,32 @@ RSpec.describe JiraService do ...@@ -737,6 +731,32 @@ RSpec.describe JiraService do
body: /"id":"#{transition_id}"/ body: /"id":"#{transition_id}"/
).once ).once
end end
expect(WebMock).to have_requested(:post, comment_url)
end
context 'when a transition fails' do
before do
WebMock.stub_request(:post, transitions_url).with(basic_auth: %w(jira-username jira-password)).to_return do |request|
{ status: request.body.include?('"id":"2"') ? 500 : 200 }
end
end
it 'stops the sequence' do
close_issue
1.upto(2) do |transition_id|
expect(WebMock).to have_requested(:post, transitions_url).with(
body: /"id":"#{transition_id}"/
)
end
expect(WebMock).not_to have_requested(:post, transitions_url).with(
body: /"id":"3"/
)
expect(WebMock).not_to have_requested(:post, comment_url)
end
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