2017-05-09 00:15:34 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-12-07 16:56:59 -05:00
|
|
|
describe MergeRequestWidgetEntity do
|
2018-08-31 13:16:34 -04:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
2017-10-10 10:56:04 -04:00
|
|
|
let(:project) { create :project, :repository }
|
2017-05-09 00:15:34 -04:00
|
|
|
let(:resource) { create(:merge_request, source_project: project, target_project: project) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2017-12-01 08:26:51 -05:00
|
|
|
let(:request) { double('request', current_user: user, project: project) }
|
2017-05-09 00:15:34 -04:00
|
|
|
|
|
|
|
subject do
|
|
|
|
described_class.new(resource, request: request).as_json
|
|
|
|
end
|
|
|
|
|
2019-04-02 03:07:11 -04:00
|
|
|
it 'has the latest sha of the target branch' do
|
|
|
|
is_expected.to include(:target_branch_sha)
|
|
|
|
end
|
|
|
|
|
2018-07-10 05:35:16 -04:00
|
|
|
describe 'source_project_full_path' do
|
|
|
|
it 'includes the full path of the source project' do
|
|
|
|
expect(subject[:source_project_full_path]).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the source project is missing' do
|
|
|
|
it 'returns `nil` for the source project' do
|
|
|
|
resource.allow_broken = true
|
|
|
|
resource.update!(source_project: nil)
|
|
|
|
|
|
|
|
expect(subject[:source_project_full_path]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-01 08:26:51 -05:00
|
|
|
describe 'pipeline' do
|
|
|
|
let(:pipeline) { create(:ci_empty_pipeline, project: project, ref: resource.source_branch, sha: resource.source_branch_sha, head_pipeline_of: resource) }
|
2017-05-09 00:15:34 -04:00
|
|
|
|
2019-01-28 07:12:30 -05:00
|
|
|
before do
|
|
|
|
allow_any_instance_of(MergeRequestPresenter).to receive(:can?).and_call_original
|
|
|
|
allow_any_instance_of(MergeRequestPresenter).to receive(:can?).with(user, :read_pipeline, anything).and_return(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has access to pipelines' do
|
|
|
|
let(:result) { true }
|
|
|
|
|
|
|
|
context 'when is up to date' do
|
|
|
|
let(:req) { double('request', current_user: user, project: project) }
|
|
|
|
|
|
|
|
it 'returns pipeline' do
|
|
|
|
pipeline_payload = PipelineDetailsEntity
|
|
|
|
.represent(pipeline, request: req)
|
|
|
|
.as_json
|
|
|
|
|
|
|
|
expect(subject[:pipeline]).to eq(pipeline_payload)
|
|
|
|
end
|
|
|
|
end
|
2017-05-09 00:15:34 -04:00
|
|
|
|
2019-01-28 07:12:30 -05:00
|
|
|
context 'when is not up to date' do
|
|
|
|
it 'returns nil' do
|
|
|
|
pipeline.update(sha: "not up to date")
|
2017-12-01 08:26:51 -05:00
|
|
|
|
2019-01-28 07:12:30 -05:00
|
|
|
expect(subject[:pipeline]).to eq(nil)
|
|
|
|
end
|
2017-12-01 08:26:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-28 07:12:30 -05:00
|
|
|
context 'when user does not have access to pipelines' do
|
|
|
|
let(:result) { false }
|
2017-12-01 08:26:51 -05:00
|
|
|
|
2019-01-28 07:12:30 -05:00
|
|
|
it 'does not have pipeline' do
|
|
|
|
expect(subject[:pipeline]).to eq(nil)
|
2017-12-01 08:26:51 -05:00
|
|
|
end
|
|
|
|
end
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
2018-10-29 07:34:41 -04:00
|
|
|
describe 'merge_pipeline' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject[:merge_pipeline]).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when is merged' do
|
|
|
|
let(:resource) { create(:merged_merge_request, source_project: project, merge_commit_sha: project.commit.id) }
|
|
|
|
let(:pipeline) { create(:ci_empty_pipeline, project: project, ref: resource.target_branch, sha: resource.merge_commit_sha) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns merge_pipeline' do
|
|
|
|
pipeline.reload
|
|
|
|
pipeline_payload = PipelineDetailsEntity
|
|
|
|
.represent(pipeline, request: request)
|
|
|
|
.as_json
|
|
|
|
|
|
|
|
expect(subject[:merge_pipeline]).to eq(pipeline_payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user cannot read pipelines on target project' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject[:merge_pipeline]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-07 12:41:30 -05:00
|
|
|
describe 'metrics' do
|
|
|
|
context 'when metrics record exists with merged data' do
|
|
|
|
before do
|
|
|
|
resource.mark_as_merged!
|
|
|
|
resource.metrics.update!(merged_by: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches merge request metrics schema' do
|
|
|
|
expect(subject[:metrics].with_indifferent_access)
|
|
|
|
.to match_schema('entities/merge_request_metrics')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns values from metrics record' do
|
|
|
|
expect(subject.dig(:metrics, :merged_by, :id))
|
|
|
|
.to eq(resource.metrics.merged_by_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metrics record exists with closed data' do
|
|
|
|
before do
|
|
|
|
resource.close!
|
|
|
|
resource.metrics.update!(latest_closed_by: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches merge request metrics schema' do
|
|
|
|
expect(subject[:metrics].with_indifferent_access)
|
|
|
|
.to match_schema('entities/merge_request_metrics')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns values from metrics record' do
|
|
|
|
expect(subject.dig(:metrics, :closed_by, :id))
|
|
|
|
.to eq(resource.metrics.latest_closed_by_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metrics does not exists' do
|
|
|
|
before do
|
2018-01-02 16:27:00 -05:00
|
|
|
resource.mark_as_merged!
|
2017-12-07 12:41:30 -05:00
|
|
|
resource.metrics.destroy!
|
|
|
|
resource.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when events exists' do
|
|
|
|
let!(:closed_event) { create(:event, :closed, project: project, target: resource) }
|
|
|
|
let!(:merge_event) { create(:event, :merged, project: project, target: resource) }
|
|
|
|
|
|
|
|
it 'matches merge request metrics schema' do
|
|
|
|
expect(subject[:metrics].with_indifferent_access)
|
|
|
|
.to match_schema('entities/merge_request_metrics')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns values from events record' do
|
|
|
|
expect(subject.dig(:metrics, :merged_by, :id))
|
|
|
|
.to eq(merge_event.author_id)
|
|
|
|
|
|
|
|
expect(subject.dig(:metrics, :closed_by, :id))
|
|
|
|
.to eq(closed_event.author_id)
|
|
|
|
|
|
|
|
expect(subject.dig(:metrics, :merged_at).to_s)
|
|
|
|
.to eq(merge_event.updated_at.to_s)
|
|
|
|
|
|
|
|
expect(subject.dig(:metrics, :closed_at).to_s)
|
|
|
|
.to eq(closed_event.updated_at.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when events does not exists' do
|
|
|
|
it 'matches merge request metrics schema' do
|
|
|
|
expect(subject[:metrics].with_indifferent_access)
|
|
|
|
.to match_schema('entities/merge_request_metrics')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
it 'has email_patches_path' do
|
|
|
|
expect(subject[:email_patches_path])
|
|
|
|
.to eq("/#{resource.project.full_path}/merge_requests/#{resource.iid}.patch")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has plain_diff_path' do
|
|
|
|
expect(subject[:plain_diff_path])
|
|
|
|
.to eq("/#{resource.project.full_path}/merge_requests/#{resource.iid}.diff")
|
|
|
|
end
|
|
|
|
|
2019-02-06 07:33:11 -05:00
|
|
|
it 'has default_merge_commit_message_with_description' do
|
|
|
|
expect(subject[:default_merge_commit_message_with_description])
|
|
|
|
.to eq(resource.default_merge_commit_message(include_description: true))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has default_squash_commit_message' do
|
|
|
|
expect(subject[:default_squash_commit_message])
|
|
|
|
.to eq(resource.default_squash_commit_message)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
2017-05-10 19:29:00 -04:00
|
|
|
describe 'new_blob_path' do
|
|
|
|
context 'when user can push to project' do
|
|
|
|
it 'returns path' do
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
expect(subject[:new_blob_path])
|
|
|
|
.to eq("/#{resource.project.full_path}/new/#{resource.source_branch}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user cannot push to project' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject[:new_blob_path]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
describe 'diff_head_sha' do
|
|
|
|
before do
|
|
|
|
allow(resource).to receive(:diff_head_sha) { 'sha' }
|
|
|
|
end
|
|
|
|
|
2018-03-06 20:34:46 -05:00
|
|
|
context 'when diff head commit is empty' do
|
2017-05-09 00:15:34 -04:00
|
|
|
it 'returns nil' do
|
2018-03-06 20:34:46 -05:00
|
|
|
allow(resource).to receive(:diff_head_sha) { '' }
|
2017-05-09 00:15:34 -04:00
|
|
|
|
|
|
|
expect(subject[:diff_head_sha]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when diff head commit present' do
|
|
|
|
it 'returns diff head commit short id' do
|
|
|
|
expect(subject[:diff_head_sha]).to eq('sha')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'diverged_commits_count' do
|
|
|
|
context 'when MR open and its diverging' do
|
|
|
|
it 'returns diverged commits count' do
|
|
|
|
allow(resource).to receive_messages(open?: true, diverged_from_target_branch?: true,
|
|
|
|
diverged_commits_count: 10)
|
|
|
|
|
|
|
|
expect(subject[:diverged_commits_count]).to eq(10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when MR is not open' do
|
|
|
|
it 'returns 0' do
|
|
|
|
allow(resource).to receive_messages(open?: false)
|
|
|
|
|
|
|
|
expect(subject[:diverged_commits_count]).to be_zero
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when MR is not diverging' do
|
|
|
|
it 'returns 0' do
|
|
|
|
allow(resource).to receive_messages(open?: true, diverged_from_target_branch?: false)
|
|
|
|
|
|
|
|
expect(subject[:diverged_commits_count]).to be_zero
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-20 04:01:21 -05:00
|
|
|
|
|
|
|
describe 'when source project is deleted' do
|
|
|
|
let(:project) { create(:project, :repository) }
|
2018-08-31 13:16:34 -04:00
|
|
|
let(:forked_project) { fork_project(project) }
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: forked_project, target_project: project) }
|
2017-12-20 04:01:21 -05:00
|
|
|
|
|
|
|
it 'returns a blank rebase_path' do
|
|
|
|
allow(merge_request).to receive(:should_be_rebased?).and_return(true)
|
2018-08-31 13:16:34 -04:00
|
|
|
forked_project.destroy
|
2017-12-20 04:01:21 -05:00
|
|
|
merge_request.reload
|
|
|
|
|
|
|
|
entity = described_class.new(merge_request, request: request).as_json
|
|
|
|
|
|
|
|
expect(entity[:rebase_path]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2019-02-06 07:33:11 -05:00
|
|
|
|
|
|
|
describe 'commits_without_merge_commits' do
|
2019-03-14 00:20:40 -04:00
|
|
|
def find_matching_commit(short_id)
|
|
|
|
resource.commits.find { |c| c.short_id == short_id }
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'does not include merge commits' do
|
2019-03-14 00:20:40 -04:00
|
|
|
commits_in_widget = subject[:commits_without_merge_commits]
|
2019-02-06 07:33:11 -05:00
|
|
|
|
2019-03-14 00:20:40 -04:00
|
|
|
expect(commits_in_widget.length).to be < resource.commits.length
|
|
|
|
expect(commits_in_widget.length).to eq(resource.commits.without_merge_commits.length)
|
|
|
|
commits_in_widget.each do |c|
|
|
|
|
expect(find_matching_commit(c[:short_id]).merge_commit?).to eq(false)
|
|
|
|
end
|
2019-02-06 07:33:11 -05:00
|
|
|
end
|
|
|
|
end
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|