Commit 35261b8b authored by Kerri Miller's avatar Kerri Miller

Merge branch 'jira-mr-merge-rule' into 'master'

Adds MR <> Jira association attributes to MR widget serializer

See merge request gitlab-org/gitlab!55553
parents 2e08918a a4e4042b
...@@ -211,6 +211,8 @@ module EE ...@@ -211,6 +211,8 @@ module EE
delegate :pipeline_configuration_full_path, to: :compliance_management_framework, allow_nil: true delegate :pipeline_configuration_full_path, to: :compliance_management_framework, allow_nil: true
alias_attribute :compliance_pipeline_configuration_full_path, :pipeline_configuration_full_path alias_attribute :compliance_pipeline_configuration_full_path, :pipeline_configuration_full_path
delegate :prevent_merge_without_jira_issue, to: :project_setting
validates :repository_size_limit, validates :repository_size_limit,
numericality: { only_integer: true, greater_than_or_equal_to: 0, allow_nil: true } numericality: { only_integer: true, greater_than_or_equal_to: 0, allow_nil: true }
validates :max_pages_size, validates :max_pages_size,
......
...@@ -56,6 +56,10 @@ module EE ...@@ -56,6 +56,10 @@ module EE
project_security_discover_path(project) if show_discover_project_security?(project) project_security_discover_path(project) if show_discover_project_security?(project)
end end
def issue_keys
Atlassian::JiraIssueKeyExtractor.new(merge_request.title, merge_request.description).issue_keys
end
private private
def expose_mr_approval_path? def expose_mr_approval_path?
......
...@@ -16,6 +16,16 @@ module EE ...@@ -16,6 +16,16 @@ module EE
expose :missing_security_scan_types do |merge_request| expose :missing_security_scan_types do |merge_request|
presenter(merge_request).missing_security_scan_types presenter(merge_request).missing_security_scan_types
end end
expose :jira_associations, if: -> (mr) { mr.project.jira_issue_association_required_to_merge_enabled? } do
expose :enforced do |merge_request|
presenter(merge_request).project.prevent_merge_without_jira_issue
end
expose :issue_keys do |merge_request|
presenter(merge_request).issue_keys
end
end
end end
end end
end end
...@@ -21,6 +21,8 @@ RSpec.describe Project do ...@@ -21,6 +21,8 @@ RSpec.describe Project do
it { is_expected.to delegate_method(:pipeline_configuration_full_path).to(:compliance_management_framework) } it { is_expected.to delegate_method(:pipeline_configuration_full_path).to(:compliance_management_framework) }
it { is_expected.to delegate_method(:prevent_merge_without_jira_issue).to(:project_setting) }
it { is_expected.to belong_to(:deleting_user) } it { is_expected.to belong_to(:deleting_user) }
it { is_expected.to have_one(:import_state).class_name('ProjectImportState') } it { is_expected.to have_one(:import_state).class_name('ProjectImportState') }
......
...@@ -156,4 +156,28 @@ RSpec.describe MergeRequestPresenter do ...@@ -156,4 +156,28 @@ RSpec.describe MergeRequestPresenter do
end end
end end
end end
describe '#issue_keys' do
let(:presenter) { described_class.new(merge_request, current_user: user) }
subject { presenter.issue_keys }
context 'when Jira issue is provided in MR title / description' do
let(:issue_key) { 'SIGNUP-1234' }
before do
merge_request.update!(title: "Fixes sign up issue #{issue_key}", description: "Related to #{issue_key}")
end
it { is_expected.to contain_exactly(issue_key) }
end
context 'when Jira issue is NOT provided in MR title / description' do
before do
merge_request.update!(title: "Fixes sign up issue", description: "Prevent spam sign ups by adding a rate limiter")
end
it { is_expected.to be_empty }
end
end
end end
...@@ -4,7 +4,7 @@ require 'spec_helper' ...@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe MergeRequestPollCachedWidgetEntity do RSpec.describe MergeRequestPollCachedWidgetEntity do
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:resource) { create(:merge_request, source_project: project, target_project: project) } let_it_be_with_reload(:resource) { create(:merge_request, source_project: project, target_project: project) }
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:request) { double('request', current_user: user, project: project) } let(:request) { double('request', current_user: user, project: project) }
...@@ -22,4 +22,86 @@ RSpec.describe MergeRequestPollCachedWidgetEntity do ...@@ -22,4 +22,86 @@ RSpec.describe MergeRequestPollCachedWidgetEntity do
it 'includes missing security scan types' do it 'includes missing security scan types' do
is_expected.to include(:missing_security_scan_types) is_expected.to include(:missing_security_scan_types)
end end
context 'jira_associations' do
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
it { is_expected.to include(:jira_associations) }
shared_examples 'contains the issue key specified in MR title / description' do
context 'when Jira issue is provided in MR title' do
let(:issue_key) { 'SIGNUP-1234' }
before do
resource.update!(title: "Fixes sign up issue #{issue_key}")
end
it { expect(subject[:jira_associations][:issue_keys]).to contain_exactly(issue_key) }
end
context 'when Jira issue is provided in MR description' do
let(:issue_key) { 'SECURITY-1234' }
before do
resource.update!(description: "Related to #{issue_key}")
end
it { expect(subject[:jira_associations][:issue_keys]).to contain_exactly(issue_key) }
end
end
shared_examples 'when issue key is NOT specified in MR title / description' do
before do
resource.update!(title: "Fixes sign up issue", description: "Prevent spam sign ups by adding a rate limiter")
end
it { expect(subject[:jira_associations][:issue_keys]).to be_empty }
end
context 'when jira issue is required for merge' do
before do
project.create_project_setting(prevent_merge_without_jira_issue: true)
end
it { expect(subject[:jira_associations][:enforced]).to be_truthy }
it_behaves_like 'contains the issue key specified in MR title / description'
it_behaves_like 'when issue key is NOT specified in MR title / description'
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 { expect(subject[:jira_associations][:enforced]).to be_falsey }
it_behaves_like 'contains the issue key specified in MR title / description'
it_behaves_like 'when issue key is NOT specified in MR title / description'
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 { is_expected.not_to include(:jira_associations) }
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