Commit 4277a831 authored by Aishwarya Subramanian's avatar Aishwarya Subramanian

Prevents MR merge when jira issue requirement is enforced

When jira issue requirement feature is available
and is enforced, prevent MR merge for internal API
and user API.
parent 8ccf6bb9
...@@ -9,6 +9,7 @@ module EE ...@@ -9,6 +9,7 @@ module EE
def error_check! def error_check!
check_size_limit check_size_limit
check_blocking_mrs check_blocking_mrs
check_jira_issue_enforcement
end end
override :hooks_validation_pass? override :hooks_validation_pass?
...@@ -61,6 +62,14 @@ module EE ...@@ -61,6 +62,14 @@ module EE
raise ::MergeRequests::MergeService::MergeError, _('Other merge requests block this MR') raise ::MergeRequests::MergeService::MergeError, _('Other merge requests block this MR')
end 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 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')
end
end end
end end
end end
...@@ -43,6 +43,78 @@ RSpec.describe MergeRequests::MergeService do ...@@ -43,6 +43,78 @@ RSpec.describe MergeRequests::MergeService do
expect(rule.approved_approvers).to contain_exactly(approver) expect(rule.approved_approvers).to contain_exactly(approver)
end end
end end
context 'jira issue enforcement' do
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
end
context 'when feature is available' 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
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_behaves_like 'merges the MR with no error'
end
end
end
end end
it_behaves_like 'merge validation hooks', persisted: true it_behaves_like 'merge validation hooks', persisted: true
......
...@@ -4632,6 +4632,9 @@ msgstr "" ...@@ -4632,6 +4632,9 @@ msgstr ""
msgid "Be careful. Renaming a project's repository can have unintended side effects." msgid "Be careful. Renaming a project's repository can have unintended side effects."
msgstr "" msgstr ""
msgid "Before this can be merged, a Jira issue must be linked in the title or description"
msgstr ""
msgid "Begin with the selected commit" msgid "Begin with the selected commit"
msgstr "" msgstr ""
......
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