2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-11-11 09:49:26 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe MergeRequests::RefreshService do
|
2017-09-29 04:04:50 -04:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2014-11-11 09:49:26 -05:00
|
|
|
let(:user) { create(:user) }
|
2017-07-25 13:09:00 -04:00
|
|
|
let(:service) { described_class }
|
2014-11-11 09:49:26 -05:00
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#execute' do
|
2014-11-11 09:49:26 -05:00
|
|
|
before do
|
|
|
|
@user = create(:user)
|
|
|
|
group = create(:group)
|
|
|
|
group.add_owner(@user)
|
|
|
|
|
2017-03-27 17:14:01 -04:00
|
|
|
@project = create(:project, :repository, namespace: group)
|
2017-09-29 04:04:50 -04:00
|
|
|
@fork_project = fork_project(@project, @user, repository: true)
|
|
|
|
|
2015-06-23 01:24:39 -04:00
|
|
|
@merge_request = create(:merge_request,
|
|
|
|
source_project: @project,
|
2014-11-11 09:49:26 -05:00
|
|
|
source_branch: 'master',
|
|
|
|
target_branch: 'feature',
|
2015-11-24 08:59:02 -05:00
|
|
|
target_project: @project,
|
2019-05-22 07:45:27 -04:00
|
|
|
auto_merge_enabled: true,
|
|
|
|
auto_merge_strategy: AutoMergeService::STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS,
|
2015-12-03 04:27:34 -05:00
|
|
|
merge_user: @user)
|
2014-11-11 09:49:26 -05:00
|
|
|
|
2017-07-25 07:56:09 -04:00
|
|
|
@another_merge_request = create(:merge_request,
|
|
|
|
source_project: @project,
|
|
|
|
source_branch: 'master',
|
|
|
|
target_branch: 'test',
|
|
|
|
target_project: @project,
|
2019-05-22 07:45:27 -04:00
|
|
|
auto_merge_enabled: true,
|
|
|
|
auto_merge_strategy: AutoMergeService::STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS,
|
2017-07-25 07:56:09 -04:00
|
|
|
merge_user: @user)
|
|
|
|
|
2015-06-23 01:24:39 -04:00
|
|
|
@fork_merge_request = create(:merge_request,
|
|
|
|
source_project: @fork_project,
|
2014-11-11 09:49:26 -05:00
|
|
|
source_branch: 'master',
|
|
|
|
target_branch: 'feature',
|
|
|
|
target_project: @project)
|
|
|
|
|
2016-03-08 13:22:50 -05:00
|
|
|
@build_failed_todo = create(:todo,
|
|
|
|
:build_failed,
|
|
|
|
user: @user,
|
|
|
|
project: @project,
|
|
|
|
target: @merge_request,
|
|
|
|
author: @user)
|
|
|
|
|
|
|
|
@fork_build_failed_todo = create(:todo,
|
|
|
|
:build_failed,
|
|
|
|
user: @user,
|
|
|
|
project: @project,
|
|
|
|
target: @merge_request,
|
|
|
|
author: @user)
|
|
|
|
|
2014-11-11 09:49:26 -05:00
|
|
|
@commits = @merge_request.commits
|
|
|
|
|
|
|
|
@oldrev = @commits.last.id
|
|
|
|
@newrev = @commits.first.id
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'push to origin repo source branch' do
|
2015-02-26 03:17:25 -05:00
|
|
|
let(:refresh_service) { service.new(@project, @user) }
|
2017-07-25 07:56:09 -04:00
|
|
|
let(:notification_service) { spy('notification_service') }
|
2017-04-03 05:11:30 -04:00
|
|
|
|
2014-11-11 09:49:26 -05:00
|
|
|
before do
|
2015-02-26 03:17:25 -05:00
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
2017-07-25 07:56:09 -04:00
|
|
|
allow(NotificationService).to receive(:new) { notification_service }
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
|
2017-04-03 05:11:30 -04:00
|
|
|
it 'executes hooks with update action' do
|
2018-01-29 10:56:19 -05:00
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(refresh_service).to have_received(:execute_hooks)
|
2017-09-15 13:08:27 -04:00
|
|
|
.with(@merge_request, 'update', old_rev: @oldrev)
|
2017-04-03 05:11:30 -04:00
|
|
|
|
2017-07-25 07:56:09 -04:00
|
|
|
expect(notification_service).to have_received(:push_to_merge_request)
|
|
|
|
.with(@merge_request, @user, new_commits: anything, existing_commits: anything)
|
|
|
|
expect(notification_service).to have_received(:push_to_merge_request)
|
|
|
|
.with(@another_merge_request, @user, new_commits: anything, existing_commits: anything)
|
|
|
|
|
2017-04-03 05:11:30 -04:00
|
|
|
expect(@merge_request.notes).not_to be_empty
|
|
|
|
expect(@merge_request).to be_open
|
2019-05-22 07:45:27 -04:00
|
|
|
expect(@merge_request.auto_merge_enabled).to be_falsey
|
2017-04-03 05:11:30 -04:00
|
|
|
expect(@merge_request.diff_head_sha).to eq(@newrev)
|
|
|
|
expect(@fork_merge_request).to be_open
|
|
|
|
expect(@fork_merge_request.notes).to be_empty
|
|
|
|
expect(@build_failed_todo).to be_done
|
|
|
|
expect(@fork_build_failed_todo).to be_done
|
|
|
|
end
|
2018-01-29 10:56:19 -05:00
|
|
|
|
2018-02-01 16:17:30 -05:00
|
|
|
it 'reloads source branch MRs memoization' do
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
|
|
|
|
expect { refresh_service.execute(@oldrev, @newrev, 'refs/heads/master') }.to change {
|
|
|
|
refresh_service.instance_variable_get("@source_merge_requests").first.merge_request_diff
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-15 13:35:34 -04:00
|
|
|
it 'outdates MR suggestions' do
|
|
|
|
expect_next_instance_of(Suggestions::OutdateService) do |service|
|
|
|
|
expect(service).to receive(:execute).with(@merge_request).and_call_original
|
|
|
|
expect(service).to receive(:execute).with(@another_merge_request).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
end
|
|
|
|
|
2018-01-29 10:56:19 -05:00
|
|
|
context 'when source branch ref does not exists' do
|
|
|
|
before do
|
|
|
|
DeleteBranchService.new(@project, @user).execute(@merge_request.source_branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'closes MRs without source branch ref' do
|
|
|
|
expect { refresh_service.execute(@oldrev, @newrev, 'refs/heads/master') }
|
|
|
|
.to change { @merge_request.reload.state }
|
|
|
|
.from('opened')
|
|
|
|
.to('closed')
|
|
|
|
|
|
|
|
expect(@fork_merge_request.reload).to be_open
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not change the merge request diff' do
|
|
|
|
expect { refresh_service.execute(@oldrev, @newrev, 'refs/heads/master') }
|
|
|
|
.not_to change { @merge_request.reload.merge_request_diff }
|
|
|
|
end
|
|
|
|
end
|
2017-09-07 03:58:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline exists for the source branch' do
|
|
|
|
let!(:pipeline) { create(:ci_empty_pipeline, ref: @merge_request.source_branch, project: @project, sha: @commits.first.sha)}
|
|
|
|
|
|
|
|
subject { service.new(@project, @user).execute(@oldrev, @newrev, 'refs/heads/master') }
|
|
|
|
|
|
|
|
it 'updates the head_pipeline_id for @merge_request' do
|
|
|
|
expect { subject }.to change { @merge_request.reload.head_pipeline_id }.from(nil).to(pipeline.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the head_pipeline_id for @fork_merge_request' do
|
|
|
|
expect { subject }.not_to change { @fork_merge_request.reload.head_pipeline_id }
|
|
|
|
end
|
2017-04-03 05:11:30 -04:00
|
|
|
end
|
|
|
|
|
2019-03-21 09:08:32 -04:00
|
|
|
describe 'Pipelines for merge requests' do
|
2018-12-05 01:57:00 -05:00
|
|
|
before do
|
|
|
|
stub_ci_pipeline_yaml_file(YAML.dump(config))
|
|
|
|
end
|
|
|
|
|
2019-04-03 07:33:52 -04:00
|
|
|
subject { service.new(project, @user).execute(@oldrev, @newrev, ref) }
|
|
|
|
|
|
|
|
let(:ref) { 'refs/heads/master' }
|
|
|
|
let(:project) { @project }
|
2018-12-05 01:57:00 -05:00
|
|
|
|
|
|
|
context "when .gitlab-ci.yml has merge_requests keywords" do
|
|
|
|
let(:config) do
|
|
|
|
{
|
|
|
|
test: {
|
|
|
|
stage: 'test',
|
|
|
|
script: 'echo',
|
|
|
|
only: ['merge_requests']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-21 09:08:32 -04:00
|
|
|
it 'create detached merge request pipeline with commits' do
|
2018-12-05 01:57:00 -05:00
|
|
|
expect { subject }
|
2019-03-27 04:46:29 -04:00
|
|
|
.to change { @merge_request.pipelines_for_merge_request.count }.by(1)
|
|
|
|
.and change { @another_merge_request.pipelines_for_merge_request.count }.by(0)
|
2019-01-19 21:41:11 -05:00
|
|
|
|
2019-01-28 00:54:33 -05:00
|
|
|
expect(@merge_request.has_commits?).to be_truthy
|
|
|
|
expect(@another_merge_request.has_commits?).to be_falsy
|
2018-12-05 01:57:00 -05:00
|
|
|
end
|
|
|
|
|
2019-04-03 07:33:52 -04:00
|
|
|
it 'does not create detached merge request pipeline for forked project' do
|
|
|
|
expect { subject }
|
2019-03-27 04:46:29 -04:00
|
|
|
.not_to change { @fork_merge_request.pipelines_for_merge_request.count }
|
2019-04-03 07:33:52 -04:00
|
|
|
end
|
|
|
|
|
2019-03-21 09:08:32 -04:00
|
|
|
it 'create detached merge request pipeline for non-fork merge request' do
|
|
|
|
subject
|
|
|
|
|
2019-03-27 04:46:29 -04:00
|
|
|
expect(@merge_request.pipelines_for_merge_request.first)
|
2019-03-21 09:08:32 -04:00
|
|
|
.to be_detached_merge_request_pipeline
|
|
|
|
end
|
|
|
|
|
2019-04-03 07:33:52 -04:00
|
|
|
context 'when service is hooked by target branch' do
|
|
|
|
let(:ref) { 'refs/heads/feature' }
|
2019-03-21 09:08:32 -04:00
|
|
|
|
2019-04-03 07:33:52 -04:00
|
|
|
it 'does not create detached merge request pipeline' do
|
|
|
|
expect { subject }
|
2019-03-27 04:46:29 -04:00
|
|
|
.not_to change { @merge_request.pipelines_for_merge_request.count }
|
2019-04-03 07:33:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when service runs on forked project' do
|
|
|
|
let(:project) { @fork_project }
|
|
|
|
|
|
|
|
it 'creates legacy detached merge request pipeline for fork merge request' do
|
|
|
|
expect { subject }
|
2019-03-27 04:46:29 -04:00
|
|
|
.to change { @fork_merge_request.pipelines_for_merge_request.count }.by(1)
|
2019-04-03 07:33:52 -04:00
|
|
|
|
2019-03-27 04:46:29 -04:00
|
|
|
expect(@fork_merge_request.pipelines_for_merge_request.first)
|
2019-04-03 07:33:52 -04:00
|
|
|
.to be_legacy_detached_merge_request_pipeline
|
|
|
|
end
|
2019-03-21 09:08:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ci_use_merge_request_ref feature flag is false' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_use_merge_request_ref: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create legacy detached merge request pipeline for non-fork merge request' do
|
|
|
|
subject
|
|
|
|
|
2019-03-27 04:46:29 -04:00
|
|
|
expect(@merge_request.pipelines_for_merge_request.first)
|
2019-03-21 09:08:32 -04:00
|
|
|
.to be_legacy_detached_merge_request_pipeline
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when branch pipeline was created before a detaced merge request pipeline has been created" do
|
2018-12-05 01:57:00 -05:00
|
|
|
before do
|
|
|
|
create(:ci_pipeline, project: @merge_request.source_project,
|
|
|
|
sha: @merge_request.diff_head_sha,
|
|
|
|
ref: @merge_request.source_branch,
|
|
|
|
tag: false)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2019-03-21 09:08:32 -04:00
|
|
|
it 'sets the latest detached merge request pipeline as a head pipeline' do
|
2018-12-05 01:57:00 -05:00
|
|
|
@merge_request.reload
|
2019-02-25 06:05:26 -05:00
|
|
|
expect(@merge_request.actual_head_pipeline).to be_merge_request_event
|
2018-12-05 01:57:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns pipelines in correct order' do
|
|
|
|
@merge_request.reload
|
2019-02-25 06:05:26 -05:00
|
|
|
expect(@merge_request.all_pipelines.first).to be_merge_request_event
|
2018-12-05 01:57:00 -05:00
|
|
|
expect(@merge_request.all_pipelines.second).to be_push
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when MergeRequestUpdateWorker is retried by an exception" do
|
2019-03-21 09:08:32 -04:00
|
|
|
it 'does not re-create a duplicate detached merge request pipeline' do
|
2018-12-05 01:57:00 -05:00
|
|
|
expect do
|
|
|
|
service.new(@project, @user).execute(@oldrev, @newrev, 'refs/heads/master')
|
2019-03-27 04:46:29 -04:00
|
|
|
end.to change { @merge_request.pipelines_for_merge_request.count }.by(1)
|
2018-12-05 01:57:00 -05:00
|
|
|
|
|
|
|
expect do
|
|
|
|
service.new(@project, @user).execute(@oldrev, @newrev, 'refs/heads/master')
|
2019-03-27 04:46:29 -04:00
|
|
|
end.not_to change { @merge_request.pipelines_for_merge_request.count }
|
2018-12-05 01:57:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when .gitlab-ci.yml does not have merge_requests keywords" do
|
|
|
|
let(:config) do
|
|
|
|
{
|
|
|
|
test: {
|
|
|
|
stage: 'test',
|
|
|
|
script: 'echo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-21 09:08:32 -04:00
|
|
|
it 'does not create a detached merge request pipeline' do
|
2018-12-05 01:57:00 -05:00
|
|
|
expect { subject }
|
2019-03-27 04:46:29 -04:00
|
|
|
.not_to change { @merge_request.pipelines_for_merge_request.count }
|
2018-12-05 01:57:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-04 03:20:43 -04:00
|
|
|
context 'push to origin repo source branch' do
|
2017-04-03 05:11:30 -04:00
|
|
|
let(:refresh_service) { service.new(@project, @user) }
|
2017-07-25 07:56:09 -04:00
|
|
|
let(:notification_service) { spy('notification_service') }
|
2017-04-03 05:11:30 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
2017-07-25 07:56:09 -04:00
|
|
|
allow(NotificationService).to receive(:new) { notification_service }
|
2017-04-03 05:11:30 -04:00
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'executes hooks with update action' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(refresh_service).to have_received(:execute_hooks)
|
2017-09-15 13:08:27 -04:00
|
|
|
.with(@merge_request, 'update', old_rev: @oldrev)
|
2017-07-25 07:56:09 -04:00
|
|
|
expect(notification_service).to have_received(:push_to_merge_request)
|
|
|
|
.with(@merge_request, @user, new_commits: anything, existing_commits: anything)
|
|
|
|
expect(notification_service).to have_received(:push_to_merge_request)
|
|
|
|
.with(@another_merge_request, @user, new_commits: anything, existing_commits: anything)
|
2015-02-26 03:17:25 -05:00
|
|
|
|
2017-03-08 03:39:56 -05:00
|
|
|
expect(@merge_request.notes).not_to be_empty
|
|
|
|
expect(@merge_request).to be_open
|
2019-05-22 07:45:27 -04:00
|
|
|
expect(@merge_request.auto_merge_enabled).to be_falsey
|
2017-03-08 03:39:56 -05:00
|
|
|
expect(@merge_request.diff_head_sha).to eq(@newrev)
|
|
|
|
expect(@fork_merge_request).to be_open
|
|
|
|
expect(@fork_merge_request.notes).to be_empty
|
|
|
|
expect(@build_failed_todo).to be_done
|
|
|
|
expect(@fork_build_failed_todo).to be_done
|
|
|
|
end
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'push to origin repo target branch' do
|
2017-07-07 10:09:29 -04:00
|
|
|
context 'when all MRs to the target branch had diffs' do
|
|
|
|
before do
|
|
|
|
service.new(@project, @user).execute(@oldrev, @newrev, 'refs/heads/feature')
|
|
|
|
reload_mrs
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the merge state' do
|
|
|
|
expect(@merge_request.notes.last.note).to include('merged')
|
|
|
|
expect(@merge_request).to be_merged
|
|
|
|
expect(@fork_merge_request).to be_merged
|
|
|
|
expect(@fork_merge_request.notes.last.note).to include('merged')
|
|
|
|
expect(@build_failed_todo).to be_done
|
|
|
|
expect(@fork_build_failed_todo).to be_done
|
|
|
|
end
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
|
2017-07-07 10:09:29 -04:00
|
|
|
context 'when an MR to be closed was empty already' do
|
|
|
|
let!(:empty_fork_merge_request) do
|
|
|
|
create(:merge_request,
|
|
|
|
source_project: @fork_project,
|
|
|
|
source_branch: 'master',
|
|
|
|
target_branch: 'master',
|
|
|
|
target_project: @project)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
# This spec already has a fake push, so pretend that we were targeting
|
|
|
|
# feature all along.
|
|
|
|
empty_fork_merge_request.update_columns(target_branch: 'feature')
|
|
|
|
|
|
|
|
service.new(@project, @user).execute(@oldrev, @newrev, 'refs/heads/feature')
|
|
|
|
reload_mrs
|
|
|
|
empty_fork_merge_request.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only updates the non-empty MRs' do
|
|
|
|
expect(@merge_request).to be_merged
|
|
|
|
expect(@merge_request.notes.last.note).to include('merged')
|
|
|
|
|
|
|
|
expect(@fork_merge_request).to be_merged
|
|
|
|
expect(@fork_merge_request.notes.last.note).to include('merged')
|
|
|
|
|
|
|
|
expect(empty_fork_merge_request).to be_open
|
|
|
|
expect(empty_fork_merge_request.merge_request_diff.state).to eq('empty')
|
|
|
|
expect(empty_fork_merge_request.notes).to be_empty
|
|
|
|
end
|
2017-03-08 03:39:56 -05:00
|
|
|
end
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
|
2015-11-06 11:04:02 -05:00
|
|
|
context 'manual merge of source branch' do
|
|
|
|
before do
|
|
|
|
# Merge master -> feature branch
|
2017-09-08 08:00:53 -04:00
|
|
|
@project.repository.merge(@user, @merge_request.diff_head_sha, @merge_request, 'Test message')
|
2015-11-06 11:04:02 -05:00
|
|
|
commit = @project.repository.commit('feature')
|
|
|
|
service.new(@project, @user).execute(@oldrev, commit.id, 'refs/heads/feature')
|
|
|
|
reload_mrs
|
|
|
|
end
|
|
|
|
|
2017-03-08 03:39:56 -05:00
|
|
|
it 'updates the merge state' do
|
|
|
|
expect(@merge_request.notes.last.note).to include('merged')
|
|
|
|
expect(@merge_request).to be_merged
|
|
|
|
expect(@merge_request.diffs.size).to be > 0
|
|
|
|
expect(@fork_merge_request).to be_merged
|
|
|
|
expect(@fork_merge_request.notes.last.note).to include('merged')
|
|
|
|
expect(@build_failed_todo).to be_done
|
|
|
|
expect(@fork_build_failed_todo).to be_done
|
|
|
|
end
|
2015-11-06 11:04:02 -05:00
|
|
|
end
|
|
|
|
|
2014-11-11 09:49:26 -05:00
|
|
|
context 'push to fork repo source branch' do
|
2015-02-26 03:17:25 -05:00
|
|
|
let(:refresh_service) { service.new(@fork_project, @user) }
|
2014-11-11 09:49:26 -05:00
|
|
|
|
2019-03-15 13:35:34 -04:00
|
|
|
def refresh
|
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end
|
2017-01-13 08:35:33 -05:00
|
|
|
|
2019-03-15 13:35:34 -04:00
|
|
|
context 'open fork merge request' do
|
2017-01-13 08:35:33 -05:00
|
|
|
it 'executes hooks with update action' do
|
2019-03-15 13:35:34 -04:00
|
|
|
refresh
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(refresh_service).to have_received(:execute_hooks)
|
2017-09-15 13:08:27 -04:00
|
|
|
.with(@fork_merge_request, 'update', old_rev: @oldrev)
|
2017-01-13 08:35:33 -05:00
|
|
|
|
2017-03-08 03:39:56 -05:00
|
|
|
expect(@merge_request.notes).to be_empty
|
|
|
|
expect(@merge_request).to be_open
|
|
|
|
expect(@fork_merge_request.notes.last.note).to include('added 28 commits')
|
|
|
|
expect(@fork_merge_request).to be_open
|
|
|
|
expect(@build_failed_todo).to be_pending
|
|
|
|
expect(@fork_build_failed_todo).to be_pending
|
|
|
|
end
|
2019-03-15 13:35:34 -04:00
|
|
|
|
|
|
|
it 'outdates opened forked MR suggestions' do
|
|
|
|
expect_next_instance_of(Suggestions::OutdateService) do |service|
|
|
|
|
expect(service).to receive(:execute).with(@fork_merge_request).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
refresh
|
|
|
|
end
|
2015-02-26 03:17:25 -05:00
|
|
|
end
|
|
|
|
|
2017-01-13 08:35:33 -05:00
|
|
|
context 'closed fork merge request' do
|
|
|
|
before do
|
|
|
|
@fork_merge_request.close!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'do not execute hooks with update action' do
|
2019-03-15 13:35:34 -04:00
|
|
|
refresh
|
|
|
|
|
2017-01-13 08:35:33 -05:00
|
|
|
expect(refresh_service).not_to have_received(:execute_hooks)
|
|
|
|
end
|
|
|
|
|
2017-03-08 03:39:56 -05:00
|
|
|
it 'updates merge request to closed state' do
|
2019-03-15 13:35:34 -04:00
|
|
|
refresh
|
|
|
|
|
2017-03-08 03:39:56 -05:00
|
|
|
expect(@merge_request.notes).to be_empty
|
|
|
|
expect(@merge_request).to be_open
|
|
|
|
expect(@fork_merge_request.notes).to be_empty
|
|
|
|
expect(@fork_merge_request).to be_closed
|
|
|
|
expect(@build_failed_todo).to be_pending
|
|
|
|
expect(@fork_build_failed_todo).to be_pending
|
|
|
|
end
|
2017-01-13 08:35:33 -05:00
|
|
|
end
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'push to fork repo target branch' do
|
2016-12-09 05:12:38 -05:00
|
|
|
describe 'changes to merge requests' do
|
|
|
|
before do
|
|
|
|
service.new(@fork_project, @user).execute(@oldrev, @newrev, 'refs/heads/feature')
|
|
|
|
reload_mrs
|
|
|
|
end
|
|
|
|
|
2017-03-08 03:39:56 -05:00
|
|
|
it 'updates the merge request state' do
|
|
|
|
expect(@merge_request.notes).to be_empty
|
|
|
|
expect(@merge_request).to be_open
|
|
|
|
expect(@fork_merge_request.notes).to be_empty
|
|
|
|
expect(@fork_merge_request).to be_open
|
|
|
|
expect(@build_failed_todo).to be_pending
|
|
|
|
expect(@fork_build_failed_todo).to be_pending
|
|
|
|
end
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
|
2016-12-09 05:12:38 -05:00
|
|
|
describe 'merge request diff' do
|
|
|
|
it 'does not reload the diff of the merge request made from fork' do
|
|
|
|
expect do
|
|
|
|
service.new(@fork_project, @user).execute(@oldrev, @newrev, 'refs/heads/feature')
|
|
|
|
end.not_to change { @fork_merge_request.reload.merge_request_diff }
|
|
|
|
end
|
|
|
|
end
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
|
2018-10-25 16:04:03 -04:00
|
|
|
context 'forked projects with the same source branch name as target branch' do
|
|
|
|
let!(:first_commit) do
|
|
|
|
@fork_project.repository.create_file(@user, 'test1.txt', 'Test data',
|
|
|
|
message: 'Test commit',
|
|
|
|
branch_name: 'master')
|
|
|
|
end
|
|
|
|
let!(:second_commit) do
|
|
|
|
@fork_project.repository.create_file(@user, 'test2.txt', 'More test data',
|
|
|
|
message: 'Second test commit',
|
|
|
|
branch_name: 'master')
|
|
|
|
end
|
|
|
|
let!(:forked_master_mr) do
|
|
|
|
create(:merge_request,
|
|
|
|
source_project: @fork_project,
|
|
|
|
source_branch: 'master',
|
|
|
|
target_branch: 'master',
|
|
|
|
target_project: @project)
|
|
|
|
end
|
|
|
|
let(:force_push_commit) { @project.commit('feature').id }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'reloads a new diff for a push to the forked project' do
|
2018-10-25 16:04:03 -04:00
|
|
|
expect do
|
|
|
|
service.new(@fork_project, @user).execute(@oldrev, first_commit, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end.to change { forked_master_mr.merge_request_diffs.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'reloads a new diff for a force push to the source branch' do
|
2018-10-25 16:04:03 -04:00
|
|
|
expect do
|
|
|
|
service.new(@fork_project, @user).execute(@oldrev, force_push_commit, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end.to change { forked_master_mr.merge_request_diffs.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'reloads a new diff for a force push to the target branch' do
|
2018-10-25 16:04:03 -04:00
|
|
|
expect do
|
|
|
|
service.new(@project, @user).execute(@oldrev, force_push_commit, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end.to change { forked_master_mr.merge_request_diffs.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'reloads a new diff for a push to the target project that contains a commit in the MR' do
|
2018-10-25 16:04:03 -04:00
|
|
|
expect do
|
|
|
|
service.new(@project, @user).execute(@oldrev, first_commit, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end.to change { forked_master_mr.merge_request_diffs.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'does not increase the diff count for a new push to target branch' do
|
2018-10-25 16:04:03 -04:00
|
|
|
new_commit = @project.repository.create_file(@user, 'new-file.txt', 'A new file',
|
|
|
|
message: 'This is a test',
|
|
|
|
branch_name: 'master')
|
|
|
|
|
|
|
|
expect do
|
|
|
|
service.new(@project, @user).execute(@newrev, new_commit, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end.not_to change { forked_master_mr.merge_request_diffs.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-11 09:49:26 -05:00
|
|
|
context 'push to origin repo target branch after fork project was removed' do
|
|
|
|
before do
|
|
|
|
@fork_project.destroy
|
|
|
|
service.new(@project, @user).execute(@oldrev, @newrev, 'refs/heads/feature')
|
|
|
|
reload_mrs
|
|
|
|
end
|
|
|
|
|
2017-03-08 03:39:56 -05:00
|
|
|
it 'updates the merge request state' do
|
|
|
|
expect(@merge_request.notes.last.note).to include('merged')
|
|
|
|
expect(@merge_request).to be_merged
|
|
|
|
expect(@fork_merge_request).to be_open
|
|
|
|
expect(@fork_merge_request.notes).to be_empty
|
|
|
|
expect(@build_failed_todo).to be_done
|
|
|
|
expect(@fork_build_failed_todo).to be_done
|
|
|
|
end
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
|
2015-10-15 04:41:46 -04:00
|
|
|
context 'push new branch that exists in a merge request' do
|
|
|
|
let(:refresh_service) { service.new(@fork_project, @user) }
|
2015-10-16 03:25:19 -04:00
|
|
|
|
|
|
|
it 'refreshes the merge request' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(refresh_service).to receive(:execute_hooks)
|
2017-09-15 13:08:27 -04:00
|
|
|
.with(@fork_merge_request, 'update', old_rev: Gitlab::Git::BLANK_SHA)
|
2015-10-16 03:25:19 -04:00
|
|
|
allow_any_instance_of(Repository).to receive(:merge_base).and_return(@oldrev)
|
|
|
|
|
2015-10-15 04:41:46 -04:00
|
|
|
refresh_service.execute(Gitlab::Git::BLANK_SHA, @newrev, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
|
2015-10-16 03:25:19 -04:00
|
|
|
expect(@merge_request.notes).to be_empty
|
|
|
|
expect(@merge_request).to be_open
|
2015-10-15 04:41:46 -04:00
|
|
|
|
2015-10-16 03:25:19 -04:00
|
|
|
notes = @fork_merge_request.notes.reorder(:created_at).map(&:note)
|
2016-11-23 01:55:23 -05:00
|
|
|
expect(notes[0]).to include('restored source branch `master`')
|
|
|
|
expect(notes[1]).to include('added 28 commits')
|
2015-10-16 03:25:19 -04:00
|
|
|
expect(@fork_merge_request).to be_open
|
|
|
|
end
|
2015-10-15 04:41:46 -04:00
|
|
|
end
|
|
|
|
|
2016-09-20 16:45:02 -04:00
|
|
|
context 'merge request metrics' do
|
2016-09-19 03:26:25 -04:00
|
|
|
let(:issue) { create :issue, project: @project }
|
|
|
|
let(:commit_author) { create :user }
|
|
|
|
let(:commit) { project.commit }
|
|
|
|
|
|
|
|
before do
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_developer(commit_author)
|
|
|
|
project.add_developer(user)
|
2016-09-19 03:26:25 -04:00
|
|
|
|
|
|
|
allow(commit).to receive_messages(
|
|
|
|
safe_message: "Closes #{issue.to_reference}",
|
|
|
|
references: [issue],
|
|
|
|
author_name: commit_author.name,
|
|
|
|
author_email: commit_author.email,
|
|
|
|
committed_date: Time.now
|
|
|
|
)
|
|
|
|
|
2019-01-03 05:59:14 -05:00
|
|
|
allow_any_instance_of(MergeRequest).to receive(:commits).and_return(CommitCollection.new(@project, [commit], 'feature'))
|
2016-09-19 03:26:25 -04:00
|
|
|
end
|
|
|
|
|
2016-09-20 16:45:02 -04:00
|
|
|
context 'when the merge request is sourced from the same project' do
|
|
|
|
it 'creates a `MergeRequestsClosingIssues` record for each issue closed by a commit' do
|
|
|
|
merge_request = create(:merge_request, target_branch: 'master', source_branch: 'feature', source_project: @project)
|
|
|
|
refresh_service = service.new(@project, @user)
|
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/feature')
|
|
|
|
|
|
|
|
issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
|
|
|
|
expect(issue_ids).to eq([issue.id])
|
|
|
|
end
|
|
|
|
end
|
2016-09-19 03:26:25 -04:00
|
|
|
|
2016-09-20 16:45:02 -04:00
|
|
|
context 'when the merge request is sourced from a different project' do
|
|
|
|
it 'creates a `MergeRequestsClosingIssues` record for each issue closed by a commit' do
|
2017-09-29 04:04:50 -04:00
|
|
|
forked_project = fork_project(@project, @user, repository: true)
|
2016-09-20 16:45:02 -04:00
|
|
|
|
|
|
|
merge_request = create(:merge_request,
|
|
|
|
target_branch: 'master',
|
|
|
|
source_branch: 'feature',
|
|
|
|
target_project: @project,
|
|
|
|
source_project: forked_project)
|
|
|
|
refresh_service = service.new(@project, @user)
|
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/feature')
|
|
|
|
|
|
|
|
issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
|
|
|
|
expect(issue_ids).to eq([issue.id])
|
|
|
|
end
|
2016-09-19 03:26:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-15 15:48:26 -05:00
|
|
|
context 'marking the merge request as work in progress' do
|
|
|
|
let(:refresh_service) { service.new(@project, @user) }
|
|
|
|
before do
|
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the merge request as work in progress from fixup commits' do
|
|
|
|
fixup_merge_request = create(:merge_request,
|
|
|
|
source_project: @project,
|
|
|
|
source_branch: 'wip',
|
|
|
|
target_branch: 'master',
|
|
|
|
target_project: @project)
|
|
|
|
commits = fixup_merge_request.commits
|
|
|
|
oldrev = commits.last.id
|
|
|
|
newrev = commits.first.id
|
|
|
|
|
|
|
|
refresh_service.execute(oldrev, newrev, 'refs/heads/wip')
|
|
|
|
fixup_merge_request.reload
|
|
|
|
|
|
|
|
expect(fixup_merge_request.work_in_progress?).to eq(true)
|
|
|
|
expect(fixup_merge_request.notes.last.note).to match(
|
|
|
|
/marked as a \*\*Work In Progress\*\* from #{Commit.reference_pattern}/
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'references the commit that caused the Work in Progress status' do
|
2018-02-02 09:59:15 -05:00
|
|
|
wip_merge_request = create(:merge_request,
|
|
|
|
source_project: @project,
|
|
|
|
source_branch: 'wip',
|
|
|
|
target_branch: 'master',
|
|
|
|
target_project: @project)
|
|
|
|
|
|
|
|
commits = wip_merge_request.commits
|
|
|
|
oldrev = commits.last.id
|
|
|
|
newrev = commits.first.id
|
|
|
|
wip_commit = wip_merge_request.commits.find(&:work_in_progress?)
|
|
|
|
|
|
|
|
refresh_service.execute(oldrev, newrev, 'refs/heads/wip')
|
|
|
|
|
|
|
|
expect(wip_merge_request.reload.notes.last.note).to eq(
|
|
|
|
"marked as a **Work In Progress** from #{wip_commit.id}"
|
2016-12-15 15:48:26 -05:00
|
|
|
)
|
|
|
|
end
|
2017-02-09 05:34:02 -05:00
|
|
|
|
|
|
|
it 'does not mark as WIP based on commits that do not belong to an MR' do
|
|
|
|
allow(refresh_service).to receive(:find_new_commits)
|
|
|
|
refresh_service.instance_variable_set("@commits", [
|
|
|
|
double(
|
|
|
|
id: 'aaaaaaa',
|
|
|
|
sha: 'aaaaaaa',
|
|
|
|
short_id: 'aaaaaaa',
|
|
|
|
title: 'Fix issue',
|
|
|
|
work_in_progress?: false
|
|
|
|
),
|
|
|
|
double(
|
|
|
|
id: 'bbbbbbb',
|
|
|
|
sha: 'bbbbbbbb',
|
|
|
|
short_id: 'bbbbbbb',
|
|
|
|
title: 'fixup! Fix issue',
|
|
|
|
work_in_progress?: true,
|
|
|
|
to_reference: 'bbbbbbb'
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
|
|
|
|
expect(@merge_request.work_in_progress?).to be_falsey
|
|
|
|
end
|
2016-12-15 15:48:26 -05:00
|
|
|
end
|
|
|
|
|
2014-11-11 09:49:26 -05:00
|
|
|
def reload_mrs
|
|
|
|
@merge_request.reload
|
|
|
|
@fork_merge_request.reload
|
2016-03-08 13:22:50 -05:00
|
|
|
@build_failed_todo.reload
|
|
|
|
@fork_build_failed_todo.reload
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|
|
|
|
end
|
2018-11-03 05:36:19 -04:00
|
|
|
|
|
|
|
describe 'updating merge_commit' do
|
|
|
|
let(:service) { described_class.new(project, user) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
|
|
|
|
let(:oldrev) { TestEnv::BRANCH_SHA['merge-commit-analyze-before'] }
|
|
|
|
let(:newrev) { TestEnv::BRANCH_SHA['merge-commit-analyze-after'] } # Pretend branch is now updated
|
|
|
|
|
|
|
|
let!(:merge_request) do
|
|
|
|
create(
|
|
|
|
:merge_request,
|
|
|
|
source_project: project,
|
|
|
|
source_branch: 'merge-commit-analyze-after',
|
|
|
|
target_branch: 'merge-commit-analyze-before',
|
|
|
|
target_project: project,
|
|
|
|
merge_user: user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:merge_request_side_branch) do
|
|
|
|
create(
|
|
|
|
:merge_request,
|
|
|
|
source_project: project,
|
|
|
|
source_branch: 'merge-commit-analyze-side-branch',
|
|
|
|
target_branch: 'merge-commit-analyze-before',
|
|
|
|
target_project: project,
|
|
|
|
merge_user: user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { service.execute(oldrev, newrev, 'refs/heads/merge-commit-analyze-before') }
|
|
|
|
|
2018-12-07 04:58:39 -05:00
|
|
|
context 'feature enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(branch_push_merge_commit_analyze: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates merge requests' merge_commits" do
|
|
|
|
expect(Gitlab::BranchPushMergeCommitAnalyzer).to receive(:new).and_wrap_original do |original_method, commits|
|
|
|
|
expect(commits.map(&:id)).to eq(%w{646ece5cfed840eca0a4feb21bcd6a81bb19bda3 29284d9bcc350bcae005872d0be6edd016e2efb5 5f82584f0a907f3b30cfce5bb8df371454a90051 8a994512e8c8f0dfcf22bb16df6e876be7a61036 689600b91aabec706e657e38ea706ece1ee8268f db46a1c5a5e474aa169b6cdb7a522d891bc4c5f9})
|
|
|
|
|
|
|
|
original_method.call(commits)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
merge_request.reload
|
|
|
|
merge_request_side_branch.reload
|
2018-11-03 05:36:19 -04:00
|
|
|
|
2018-12-07 04:58:39 -05:00
|
|
|
expect(merge_request.merge_commit.id).to eq('646ece5cfed840eca0a4feb21bcd6a81bb19bda3')
|
|
|
|
expect(merge_request_side_branch.merge_commit.id).to eq('29284d9bcc350bcae005872d0be6edd016e2efb5')
|
2018-11-03 05:36:19 -04:00
|
|
|
end
|
2018-12-07 04:58:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when feature is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(branch_push_merge_commit_analyze: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not trigger analysis" do
|
|
|
|
expect(Gitlab::BranchPushMergeCommitAnalyzer).not_to receive(:new)
|
2018-11-03 05:36:19 -04:00
|
|
|
|
2018-12-07 04:58:39 -05:00
|
|
|
subject
|
2018-11-03 05:36:19 -04:00
|
|
|
|
2018-12-07 04:58:39 -05:00
|
|
|
merge_request.reload
|
|
|
|
merge_request_side_branch.reload
|
2018-11-03 05:36:19 -04:00
|
|
|
|
2018-12-07 04:58:39 -05:00
|
|
|
expect(merge_request.merge_commit).to eq(nil)
|
|
|
|
expect(merge_request_side_branch.merge_commit).to eq(nil)
|
|
|
|
end
|
2018-11-03 05:36:19 -04:00
|
|
|
end
|
|
|
|
end
|
2014-11-11 09:49:26 -05:00
|
|
|
end
|