diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-10-24 19:01:44 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-11-07 18:27:55 +0300 |
commit | 6fbdc5ed5224154b89cf351e11a8f9db48e6d7f0 (patch) | |
tree | dd2ccecd4b1100d4b83f91292b96ec988eecde6d /spec/services | |
parent | 6d8810a64f944ff96af54e5c759f866bb68a7453 (diff) |
Apply patches when creating MR via email
This allows users to add patches as attachments to merge request
created via email.
When an email to create a merge request is sent, all the attachments
ending in `.patch` will be applied to the branch specified in the
subject of the email. If the branch did not exist, it will be created
from the HEAD of the repository.
When the patches could not be applied, the error message will be
replied to the user.
The patches can have a maximum combined size of 2MB for now.
Diffstat (limited to 'spec/services')
-rw-r--r-- | spec/services/commits/commit_patch_service_spec.rb | 92 | ||||
-rw-r--r-- | spec/services/merge_requests/build_service_spec.rb | 8 |
2 files changed, 100 insertions, 0 deletions
diff --git a/spec/services/commits/commit_patch_service_spec.rb b/spec/services/commits/commit_patch_service_spec.rb new file mode 100644 index 00000000000..f4fcec2fbc2 --- /dev/null +++ b/spec/services/commits/commit_patch_service_spec.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true +require 'spec_helper' + +describe Commits::CommitPatchService do + describe '#execute' do + let(:patches) do + patches_folder = Rails.root.join('spec/fixtures/patchfiles') + content_1 = File.read(File.join(patches_folder, "0001-This-does-not-apply-to-the-feature-branch.patch")) + content_2 = File.read(File.join(patches_folder, "0001-A-commit-from-a-patch.patch")) + + [content_1, content_2] + end + let(:user) { project.creator } + let(:branch_name) { 'branch-with-patches' } + let(:project) { create(:project, :repository) } + let(:start_branch) { nil } + let(:params) { { branch_name: branch_name, patches: patches, start_branch: start_branch } } + + subject(:service) do + described_class.new(project, user, params) + end + + it 'returns a successful result' do + result = service.execute + + branch = project.repository.find_branch(branch_name) + + expect(result[:status]).to eq(:success) + expect(result[:result]).to eq(branch.target) + end + + it 'is based off HEAD when no start ref is passed' do + service.execute + + merge_base = project.repository.merge_base(project.repository.root_ref, branch_name) + + expect(merge_base).to eq(project.repository.commit('HEAD').sha) + end + + context 'when specifying a different start branch' do + let(:start_branch) { 'with-codeowners' } + + it 'is based of the correct branch' do + service.execute + + merge_base = project.repository.merge_base(start_branch, branch_name) + + expect(merge_base).to eq(project.repository.commit(start_branch).sha) + end + end + + shared_examples 'an error response' do |expected_message| + it 'returns the correct error' do + result = service.execute + + expect(result[:status]).to eq(:error) + expect(result[:message]).to match(expected_message) + end + end + + context 'when the user does not have access' do + let(:user) { create(:user) } + + it_behaves_like 'an error response', + 'You are not allowed to push into this branch' + end + + context 'when the patches are not valid' do + let(:patches) { "a" * 2.1.megabytes } + + it_behaves_like 'an error response', 'Patches are too big' + end + + context 'when the new branch name is invalid' do + let(:branch_name) { 'HEAD' } + + it_behaves_like 'an error response', 'Branch name is invalid' + end + + context 'when the patches do not apply' do + let(:branch_name) { 'feature' } + + it_behaves_like 'an error response', 'Patch failed at' + end + + context 'when specifying a non existent start branch' do + let(:start_branch) { 'does-not-exist' } + + it_behaves_like 'an error response', 'Invalid reference name' + end + end +end diff --git a/spec/services/merge_requests/build_service_spec.rb b/spec/services/merge_requests/build_service_spec.rb index 9f1da7d9419..c9a668994eb 100644 --- a/spec/services/merge_requests/build_service_spec.rb +++ b/spec/services/merge_requests/build_service_spec.rb @@ -392,5 +392,13 @@ describe MergeRequests::BuildService do expect(merge_request.source_project).to eq(project) end end + + context 'when specifying target branch in the description' do + let(:description) { "A merge request targeting another branch\n\n/target_branch with-codeowners" } + + it 'sets the attribute from the quick actions' do + expect(merge_request.target_branch).to eq('with-codeowners') + end + end end end |