Commit cb2cb85d authored by Aishwarya Subramanian's avatar Aishwarya Subramanian

Addressed review suggestions

Abstracted jira check functionality to project
instance method.
Tabulated specs
parent 4277a831
......@@ -770,6 +770,12 @@ module EE
end
end
def prevent_merge_without_jira_issue?
return false unless jira_issue_association_required_to_merge_enabled?
prevent_merge_without_jira_issue
end
private
def group_hooks
......
......@@ -64,8 +64,7 @@ module EE
end
def check_jira_issue_enforcement
return unless merge_request.project.jira_issue_association_required_to_merge_enabled?
return unless merge_request.project.prevent_merge_without_jira_issue
return unless merge_request.project.prevent_merge_without_jira_issue?
return if Atlassian::JiraIssueKeyExtractor.has_keys?(merge_request.title, merge_request.description)
raise ::MergeRequests::MergeService::MergeError, _('Before this can be merged, a Jira issue must be linked in the title or description')
......
......@@ -2733,6 +2733,28 @@ RSpec.describe Project do
end
end
describe '#prevent_merge_without_jira_issue?' do
using RSpec::Parameterized::TableSyntax
subject { project.prevent_merge_without_jira_issue? }
where(:feature_available, :prevent_merge, :result) do
true | true | true
true | false | false
false | true | false
false | false | false
end
with_them do
before do
allow(project).to receive(:jira_issue_association_required_to_merge_enabled?).and_return(feature_available)
project.create_project_setting(prevent_merge_without_jira_issue: prevent_merge)
end
it { is_expected.to be result }
end
end
context 'indexing updates in Elasticsearch', :elastic do
before do
stub_ee_application_setting(elasticsearch_indexing: true)
......
......@@ -44,74 +44,35 @@ RSpec.describe MergeRequests::MergeService do
end
end
context 'jira issue enforcement' do
context 'with jira issue enforcement' do
using RSpec::Parameterized::TableSyntax
subject do
perform_enqueued_jobs do
service.execute(merge_request)
end
end
shared_examples 'merges the MR with no error' do
it do
subject
expect(merge_request.reload.merged?).to eq(true)
end
where(:prevent_merge, :issue_specified, :merged) do
true | true | true
true | false | false
false | true | true
false | false | true
end
context 'when feature is available' do
with_them do
before do
stub_licensed_features(jira_issue_association_enforcement: true)
stub_feature_flags(jira_issue_association_on_merge_request: true)
end
context 'when jira issue is required for merge' do
before do
project.create_project_setting(prevent_merge_without_jira_issue: true)
end
context 'when issue key is NOT specified in MR title / description' do
it 'returns appropriate merge error' do
subject
expect(merge_request.merge_error).to include('Before this can be merged, a Jira issue must be linked in the title or description')
end
end
context 'when issue key is specified in MR title / description' do
before do
merge_request.update!(title: "Fixes login issue SECURITY-1234")
end
it_behaves_like 'merges the MR with no error'
end
allow(project).to receive(:prevent_merge_without_jira_issue?).and_return(prevent_merge)
allow(Atlassian::JiraIssueKeyExtractor).to receive(:has_keys?)
.with(merge_request.title, merge_request.description)
.and_return(issue_specified)
end
context 'when jira issue is NOT required for merge' do
before do
project.create_project_setting(prevent_merge_without_jira_issue: false)
end
it_behaves_like 'merges the MR with no error'
end
end
context 'when feature is NOT available' do
using RSpec::Parameterized::TableSyntax
where(:licensed, :feature_flag) do
false | true
true | false
false | false
end
with_them do
before do
stub_licensed_features(jira_issue_association_enforcement: licensed)
stub_feature_flags(jira_issue_association_on_merge_request: feature_flag)
end
it 'sets the correct merged state and raises an error when applicable', :aggregate_failures do
subject
it_behaves_like 'merges the MR with no error'
expect(merge_request.reload.merged?).to eq(merged)
expect(merge_request.merge_error).to include('Before this can be merged, a Jira issue must be linked in the title or description') unless merged
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