Commit d8a00988 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'id-master-head-diff' into 'master'

Compare diff against HEAD if diff_head is passed

See merge request gitlab-org/gitlab!23157
parents afde97a1 8b84a926
......@@ -111,6 +111,11 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic
end
end
if Gitlab::Utils.to_boolean(params[:diff_head]) && @merge_request.diffable_merge_ref?
return CompareService.new(@project, @merge_request.merge_ref_head.sha)
.execute(@project, @merge_request.target_branch)
end
if @start_sha
@merge_request_diff.compare_with(@start_sha)
else
......
......@@ -840,6 +840,10 @@ class MergeRequest < ApplicationRecord
end
# rubocop: enable CodeReuse/ServiceClass
def diffable_merge_ref?
Feature.enabled?(:diff_compare_with_head, target_project) && can_be_merged? && merge_ref_head.present?
end
# Returns boolean indicating the merge_status should be rechecked in order to
# switch to either can_be_merged or cannot_be_merged.
def recheck_merge_status?
......
......@@ -34,6 +34,14 @@ class MergeRequestDiffEntity < Grape::Entity
merge_request_version_path(project, merge_request, merge_request_diff)
end
expose :head_version_path do |merge_request_diff|
project = merge_request.target_project
next unless project && merge_request.diffable_merge_ref?
diffs_project_merge_request_path(project, merge_request, diff_head: true)
end
expose :version_path do |merge_request_diff|
start_sha = options[:start_sha]
project = merge_request.target_project
......
......@@ -203,6 +203,41 @@ describe Projects::MergeRequests::DiffsController do
end
end
context 'with diff_head param passed' do
before do
allow(merge_request).to receive(:diffable_merge_ref?)
.and_return(diffable_merge_ref)
end
context 'the merge request can be compared with head' do
let(:diffable_merge_ref) { true }
it 'compares diffs with the head' do
MergeRequests::MergeToRefService.new(project, merge_request.author).execute(merge_request)
expect(CompareService).to receive(:new).with(
project, merge_request.merge_ref_head.sha
).and_call_original
go(diff_head: true)
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'the merge request cannot be compared with head' do
let(:diffable_merge_ref) { false }
it 'compares diffs with the base' do
expect(CompareService).not_to receive(:new)
go(diff_head: true)
expect(response).to have_gitlab_http_status(:ok)
end
end
end
context 'with MR regular diff params' do
it 'returns success' do
go
......
......@@ -3576,4 +3576,44 @@ describe MergeRequest do
expect(merge_request.recent_visible_deployments.count).to eq(10)
end
end
describe '#diffable_merge_ref?' do
context 'diff_compare_with_head enabled' do
context 'merge request can be merged' do
context 'merge_to_ref is not calculated' do
it 'returns true' do
expect(subject.diffable_merge_ref?).to eq(false)
end
end
context 'merge_to_ref is calculated' do
before do
MergeRequests::MergeToRefService.new(subject.project, subject.author).execute(subject)
end
it 'returns true' do
expect(subject.diffable_merge_ref?).to eq(true)
end
end
end
context 'merge request cannot be merged' do
it 'returns false' do
subject.mark_as_unchecked!
expect(subject.diffable_merge_ref?).to eq(false)
end
end
end
context 'diff_compare_with_head disabled' do
before do
stub_feature_flags(diff_compare_with_head: { enabled: false, thing: subject.target_project })
end
it 'returns false' do
expect(subject.diffable_merge_ref?).to eq(false)
end
end
end
end
......@@ -3,7 +3,7 @@
require 'spec_helper'
describe MergeRequestDiffEntity do
let(:project) { create(:project, :repository) }
let_it_be(:project) { create(:project, :repository) }
let(:request) { EntityRequest.new(project: project) }
let(:merge_request) { create(:merge_request_with_diffs, target_project: project, source_project: project) }
let(:merge_request_diffs) { merge_request.merge_request_diffs }
......@@ -36,4 +36,29 @@ describe MergeRequestDiffEntity do
expect(subject[:short_commit_sha]).to eq(nil)
end
end
describe '#head_version_path' do
before do
allow(merge_request).to receive(:diffable_merge_ref?)
.and_return(diffable_merge_ref)
end
context 'merge request can be merged' do
let(:diffable_merge_ref) { true }
it 'returns diff path with diff_head param set' do
expect(subject[:head_version_path]).to eq(
"/#{project.full_path}/-/merge_requests/#{merge_request.iid}/diffs?diff_head=true"
)
end
end
context 'merge request cannot be merged' do
let(:diffable_merge_ref) { false }
it 'returns diff path with diff_head param set' do
expect(subject[:head_version_path]).to be_nil
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