2014-11-11 09:49:26 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 05:55:49 -05:00
|
|
|
describe MergeRequests::RefreshService, services: true do
|
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) }
|
|
|
|
let(:service) { MergeRequests::RefreshService }
|
|
|
|
|
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)
|
2014-11-11 09:49:26 -05:00
|
|
|
@fork_project = Projects::ForkService.new(@project, @user).execute
|
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,
|
2017-02-17 08:56:13 -05:00
|
|
|
merge_when_pipeline_succeeds: true,
|
2015-12-03 04:27:34 -05:00
|
|
|
merge_user: @user)
|
2014-11-11 09:49:26 -05:00
|
|
|
|
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-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)
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
2014-11-11 09:49:26 -05:00
|
|
|
reload_mrs
|
|
|
|
end
|
|
|
|
|
2017-04-03 05:11:30 -04:00
|
|
|
it 'executes hooks with update action' do
|
|
|
|
expect(refresh_service).to have_received(:execute_hooks).
|
|
|
|
with(@merge_request, 'update', @oldrev)
|
|
|
|
|
|
|
|
expect(@merge_request.notes).not_to be_empty
|
|
|
|
expect(@merge_request).to be_open
|
|
|
|
expect(@merge_request.merge_when_pipeline_succeeds).to be_falsey
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'push to origin repo source branch when an MR was reopened' do
|
|
|
|
let(:refresh_service) { service.new(@project, @user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
@merge_request.update(state: :reopened)
|
|
|
|
|
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
|
|
|
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
|
2015-02-26 03:17:25 -05:00
|
|
|
expect(refresh_service).to have_received(:execute_hooks).
|
2016-07-27 15:39:45 -04:00
|
|
|
with(@merge_request, 'update', @oldrev)
|
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
|
|
|
|
expect(@merge_request.merge_when_pipeline_succeeds).to be_falsey
|
|
|
|
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
|
|
|
|
before do
|
|
|
|
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 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
|
|
|
|
|
2015-11-06 11:04:02 -05:00
|
|
|
context 'manual merge of source branch' do
|
|
|
|
before do
|
|
|
|
# Merge master -> feature branch
|
|
|
|
author = { email: 'test@gitlab.com', time: Time.now, name: "Me" }
|
|
|
|
commit_options = { message: 'Test message', committer: author, author: author }
|
2017-02-01 09:28:04 -05:00
|
|
|
@project.repository.merge(@user, @merge_request.diff_head_sha, @merge_request, commit_options)
|
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
|
|
|
|
2017-01-13 08:35:33 -05:00
|
|
|
context 'open fork merge request' do
|
|
|
|
before do
|
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes hooks with update action' do
|
|
|
|
expect(refresh_service).to have_received(:execute_hooks).
|
|
|
|
with(@fork_merge_request, 'update', @oldrev)
|
|
|
|
|
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
|
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!
|
|
|
|
allow(refresh_service).to receive(:execute_hooks)
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
reload_mrs
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'do not execute hooks with update action' do
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
expect(refresh_service).to receive(:execute_hooks).
|
2016-07-27 15:39:45 -04:00
|
|
|
with(@fork_merge_request, 'update', 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
|
|
|
|
project.team << [commit_author, :developer]
|
|
|
|
project.team << [user, :developer]
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
allow_any_instance_of(MergeRequest).to receive(:commits).and_return([commit])
|
|
|
|
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-03-27 17:14:01 -04:00
|
|
|
forked_project = create(:project, :repository)
|
2016-09-20 16:45:02 -04:00
|
|
|
create(:forked_project_link, forked_to_project: forked_project, forked_from_project: @project)
|
|
|
|
|
|
|
|
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
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
|
|
|
|
allow(refresh_service).to receive(:find_new_commits)
|
|
|
|
refresh_service.instance_variable_set("@commits", [
|
2017-02-09 05:34:02 -05:00
|
|
|
double(
|
2016-12-15 15:48:26 -05:00
|
|
|
id: 'aaaaaaa',
|
2017-02-09 05:34:02 -05:00
|
|
|
sha: '38008cb17ce1466d8fec2dfa6f6ab8dcfe5cf49e',
|
2016-12-15 15:48:26 -05:00
|
|
|
short_id: 'aaaaaaa',
|
|
|
|
title: 'Fix issue',
|
|
|
|
work_in_progress?: false
|
|
|
|
),
|
2017-02-09 05:34:02 -05:00
|
|
|
double(
|
2016-12-15 15:48:26 -05:00
|
|
|
id: 'bbbbbbb',
|
2017-02-09 05:34:02 -05:00
|
|
|
sha: '498214de67004b1da3d820901307bed2a68a8ef6',
|
2016-12-15 15:48:26 -05:00
|
|
|
short_id: 'bbbbbbb',
|
|
|
|
title: 'fixup! Fix issue',
|
|
|
|
work_in_progress?: true,
|
|
|
|
to_reference: 'bbbbbbb'
|
|
|
|
),
|
2017-02-09 05:34:02 -05:00
|
|
|
double(
|
2016-12-15 15:48:26 -05:00
|
|
|
id: 'ccccccc',
|
2017-02-09 05:34:02 -05:00
|
|
|
sha: '1b12f15a11fc6e62177bef08f47bc7b5ce50b141',
|
2016-12-15 15:48:26 -05:00
|
|
|
short_id: 'ccccccc',
|
|
|
|
title: 'fixup! Fix issue',
|
|
|
|
work_in_progress?: true,
|
|
|
|
to_reference: 'ccccccc'
|
|
|
|
),
|
|
|
|
])
|
|
|
|
refresh_service.execute(@oldrev, @newrev, 'refs/heads/wip')
|
|
|
|
reload_mrs
|
|
|
|
expect(@merge_request.notes.last.note).to eq(
|
|
|
|
"marked as a **Work In Progress** from bbbbbbb"
|
|
|
|
)
|
|
|
|
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
|
|
|
|
end
|