debian-mirror-gitlab/spec/services/merge_requests/refresh_service_spec.rb

238 lines
9.4 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
2015-12-23 02:04:40 +05:30
describe MergeRequests::RefreshService, services: true do
2015-04-26 12:48:37 +05:30
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:service) { MergeRequests::RefreshService }
2016-08-24 12:49:21 +05:30
describe '#execute' do
2015-04-26 12:48:37 +05:30
before do
@user = create(:user)
group = create(:group)
group.add_owner(@user)
@project = create(:project, namespace: group)
@fork_project = Projects::ForkService.new(@project, @user).execute
2015-09-11 14:41:01 +05:30
@merge_request = create(:merge_request,
source_project: @project,
2015-04-26 12:48:37 +05:30
source_branch: 'master',
target_branch: 'feature',
2015-12-23 02:04:40 +05:30
target_project: @project,
merge_when_build_succeeds: true,
merge_user: @user)
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
@fork_merge_request = create(:merge_request,
source_project: @fork_project,
2015-04-26 12:48:37 +05:30
source_branch: 'master',
target_branch: 'feature',
target_project: @project)
2016-06-02 11:05:42 +05:30
@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)
2015-04-26 12:48:37 +05:30
@commits = @merge_request.commits
@oldrev = @commits.last.id
@newrev = @commits.first.id
end
context 'push to origin repo source branch' do
2015-09-11 14:41:01 +05:30
let(:refresh_service) { service.new(@project, @user) }
2015-04-26 12:48:37 +05:30
before do
2015-09-11 14:41:01 +05:30
allow(refresh_service).to receive(:execute_hooks)
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
2015-04-26 12:48:37 +05:30
reload_mrs
end
2016-09-13 17:45:13 +05:30
it 'executes hooks with update action' do
2015-09-11 14:41:01 +05:30
expect(refresh_service).to have_received(:execute_hooks).
2016-09-13 17:45:13 +05:30
with(@merge_request, 'update', @oldrev)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
it { expect(@merge_request.notes).not_to be_empty }
it { expect(@merge_request).to be_open }
2016-11-03 12:29:30 +05:30
it { expect(@merge_request.merge_when_build_succeeds).to be_falsey }
it { expect(@merge_request.diff_head_sha).to eq(@newrev) }
2015-04-26 12:48:37 +05:30
it { expect(@fork_merge_request).to be_open }
it { expect(@fork_merge_request.notes).to be_empty }
2016-06-02 11:05:42 +05:30
it { expect(@build_failed_todo).to be_done }
it { expect(@fork_build_failed_todo).to be_done }
2015-04-26 12:48:37 +05:30
end
context 'push to origin repo target branch' do
before do
service.new(@project, @user).execute(@oldrev, @newrev, 'refs/heads/feature')
reload_mrs
end
it { expect(@merge_request.notes.last.note).to include('changed to merged') }
it { expect(@merge_request).to be_merged }
it { expect(@fork_merge_request).to be_merged }
it { expect(@fork_merge_request.notes.last.note).to include('changed to merged') }
2016-11-03 12:29:30 +05:30
it { expect(@build_failed_todo).to be_done }
it { expect(@fork_build_failed_todo).to be_done }
2015-04-26 12:48:37 +05:30
end
2015-11-26 14:37:03 +05:30
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 }
2016-08-24 12:49:21 +05:30
@project.repository.merge(@user, @merge_request, commit_options)
2015-11-26 14:37:03 +05:30
commit = @project.repository.commit('feature')
service.new(@project, @user).execute(@oldrev, commit.id, 'refs/heads/feature')
reload_mrs
end
it { expect(@merge_request.notes.last.note).to include('changed to merged') }
it { expect(@merge_request).to be_merged }
2016-06-02 11:05:42 +05:30
it { expect(@merge_request.diffs.size).to be > 0 }
2015-11-26 14:37:03 +05:30
it { expect(@fork_merge_request).to be_merged }
it { expect(@fork_merge_request.notes.last.note).to include('changed to merged') }
2016-11-03 12:29:30 +05:30
it { expect(@build_failed_todo).to be_done }
it { expect(@fork_build_failed_todo).to be_done }
2015-11-26 14:37:03 +05:30
end
2015-04-26 12:48:37 +05:30
context 'push to fork repo source branch' do
2015-09-11 14:41:01 +05:30
let(:refresh_service) { service.new(@fork_project, @user) }
2015-04-26 12:48:37 +05:30
before do
2015-09-11 14:41:01 +05:30
allow(refresh_service).to receive(:execute_hooks)
refresh_service.execute(@oldrev, @newrev, 'refs/heads/master')
2015-04-26 12:48:37 +05:30
reload_mrs
end
2016-09-13 17:45:13 +05:30
it 'executes hooks with update action' do
2015-09-11 14:41:01 +05:30
expect(refresh_service).to have_received(:execute_hooks).
2016-09-13 17:45:13 +05:30
with(@fork_merge_request, 'update', @oldrev)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
it { expect(@merge_request.notes).to be_empty }
it { expect(@merge_request).to be_open }
2016-11-03 12:29:30 +05:30
it { expect(@fork_merge_request.notes.last.note).to include('Added 28 commits') }
2015-04-26 12:48:37 +05:30
it { expect(@fork_merge_request).to be_open }
2016-06-02 11:05:42 +05:30
it { expect(@build_failed_todo).to be_pending }
it { expect(@fork_build_failed_todo).to be_pending }
2015-04-26 12:48:37 +05:30
end
context 'push to fork repo target branch' do
before do
service.new(@fork_project, @user).execute(@oldrev, @newrev, 'refs/heads/feature')
reload_mrs
end
it { expect(@merge_request.notes).to be_empty }
it { expect(@merge_request).to be_open }
it { expect(@fork_merge_request.notes).to be_empty }
it { expect(@fork_merge_request).to be_open }
2016-06-02 11:05:42 +05:30
it { expect(@build_failed_todo).to be_pending }
it { expect(@fork_build_failed_todo).to be_pending }
2015-04-26 12:48:37 +05:30
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
it { expect(@merge_request.notes.last.note).to include('changed to merged') }
it { expect(@merge_request).to be_merged }
it { expect(@fork_merge_request).to be_open }
it { expect(@fork_merge_request.notes).to be_empty }
2016-11-03 12:29:30 +05:30
it { expect(@build_failed_todo).to be_done }
it { expect(@fork_build_failed_todo).to be_done }
2015-04-26 12:48:37 +05:30
end
2015-10-24 18:46:33 +05:30
context 'push new branch that exists in a merge request' do
let(:refresh_service) { service.new(@fork_project, @user) }
it 'refreshes the merge request' do
expect(refresh_service).to receive(:execute_hooks).
2016-09-13 17:45:13 +05:30
with(@fork_merge_request, 'update', Gitlab::Git::BLANK_SHA)
2015-10-24 18:46:33 +05:30
allow_any_instance_of(Repository).to receive(:merge_base).and_return(@oldrev)
refresh_service.execute(Gitlab::Git::BLANK_SHA, @newrev, 'refs/heads/master')
reload_mrs
expect(@merge_request.notes).to be_empty
expect(@merge_request).to be_open
notes = @fork_merge_request.notes.reorder(:created_at).map(&:note)
expect(notes[0]).to include('Restored source branch `master`')
2016-11-03 12:29:30 +05:30
expect(notes[1]).to include('Added 28 commits')
2015-10-24 18:46:33 +05:30
expect(@fork_merge_request).to be_open
end
end
2016-09-29 09:46:39 +05:30
context 'merge request metrics' do
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
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
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
forked_project = create(:project)
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
end
end
2015-04-26 12:48:37 +05:30
def reload_mrs
@merge_request.reload
@fork_merge_request.reload
2016-06-02 11:05:42 +05:30
@build_failed_todo.reload
@fork_build_failed_todo.reload
2015-04-26 12:48:37 +05:30
end
end
end