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 ...@@ -770,6 +770,12 @@ module EE
end end
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 private
def group_hooks def group_hooks
......
...@@ -64,8 +64,7 @@ module EE ...@@ -64,8 +64,7 @@ module EE
end end
def check_jira_issue_enforcement 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) 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') 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 ...@@ -2733,6 +2733,28 @@ RSpec.describe Project do
end end
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 context 'indexing updates in Elasticsearch', :elastic do
before do before do
stub_ee_application_setting(elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_indexing: true)
......
...@@ -44,74 +44,35 @@ RSpec.describe MergeRequests::MergeService do ...@@ -44,74 +44,35 @@ RSpec.describe MergeRequests::MergeService do
end end
end end
context 'jira issue enforcement' do context 'with jira issue enforcement' do
using RSpec::Parameterized::TableSyntax
subject do subject do
perform_enqueued_jobs do perform_enqueued_jobs do
service.execute(merge_request) service.execute(merge_request)
end end
end end
shared_examples 'merges the MR with no error' do where(:prevent_merge, :issue_specified, :merged) do
it do true | true | true
subject true | false | false
false | true | true
expect(merge_request.reload.merged?).to eq(true) false | false | true
end
end end
context 'when feature is available' do with_them do
before do before do
stub_licensed_features(jira_issue_association_enforcement: true) allow(project).to receive(:prevent_merge_without_jira_issue?).and_return(prevent_merge)
stub_feature_flags(jira_issue_association_on_merge_request: true) allow(Atlassian::JiraIssueKeyExtractor).to receive(:has_keys?)
end .with(merge_request.title, merge_request.description)
.and_return(issue_specified)
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
end end
context 'when jira issue is NOT required for merge' do it 'sets the correct merged state and raises an error when applicable', :aggregate_failures do
before do subject
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_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 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