Commit 2f21b551 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'jira-mr-merge-api-check' into 'master'

Prevents MR merge when jira issue requirement is enforced

See merge request gitlab-org/gitlab!55710
parents f91bfe8b 8691f859
......@@ -782,6 +782,10 @@ module EE
end
end
def prevent_merge_without_jira_issue?
jira_issue_association_required_to_merge_enabled? && prevent_merge_without_jira_issue
end
private
def group_hooks
......
......@@ -9,6 +9,7 @@ module EE
def error_check!
check_size_limit
check_blocking_mrs
check_jira_issue_enforcement
end
override :hooks_validation_pass?
......@@ -61,6 +62,13 @@ module EE
raise ::MergeRequests::MergeService::MergeError, _('Other merge requests block this MR')
end
def check_jira_issue_enforcement
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
......@@ -2775,6 +2775,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)
......
......@@ -43,6 +43,39 @@ RSpec.describe MergeRequests::MergeService do
expect(rule.approved_approvers).to contain_exactly(approver)
end
end
context 'with jira issue enforcement' do
using RSpec::Parameterized::TableSyntax
subject do
perform_enqueued_jobs do
service.execute(merge_request)
end
end
where(:prevent_merge, :issue_specified, :merged) do
true | true | true
true | false | false
false | true | true
false | false | true
end
with_them do
before do
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
it 'sets the correct merged state and raises an error when applicable', :aggregate_failures do
subject
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
it_behaves_like 'merge validation hooks', persisted: true
......
......@@ -4666,6 +4666,9 @@ msgstr ""
msgid "Be careful. Renaming a project's repository can have unintended side effects."
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"
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