Commit 96080fce authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '58583-confidential-mr-branch-backend' into 'master'

Support creating an MR/branch on a fork from an issue

See merge request gitlab-org/gitlab-ce!29831
parents 0e8b76e1 6b68acbf
...@@ -68,8 +68,9 @@ class Projects::BranchesController < Projects::ApplicationController ...@@ -68,8 +68,9 @@ class Projects::BranchesController < Projects::ApplicationController
success = (result[:status] == :success) success = (result[:status] == :success)
if params[:issue_iid] && success if params[:issue_iid] && success
issue = IssuesFinder.new(current_user, project_id: @project.id).find_by(iid: params[:issue_iid]) target_project = confidential_issue_project || @project
SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue issue = IssuesFinder.new(current_user, project_id: target_project.id).find_by(iid: params[:issue_iid])
SystemNoteService.new_issue_branch(issue, target_project, current_user, branch_name, branch_project: @project) if issue
end end
respond_to do |format| respond_to do |format|
...@@ -166,4 +167,15 @@ class Projects::BranchesController < Projects::ApplicationController ...@@ -166,4 +167,15 @@ class Projects::BranchesController < Projects::ApplicationController
@branches = Kaminari.paginate_array(@branches).page(params[:page]) @branches = Kaminari.paginate_array(@branches).page(params[:page])
end end
end end
def confidential_issue_project
return unless Feature.enabled?(:create_confidential_merge_request, @project)
return if params[:confidential_issue_project_id].blank?
confidential_issue_project = Project.find(params[:confidential_issue_project_id])
return unless can?(current_user, :update_issue, confidential_issue_project)
confidential_issue_project
end
end end
...@@ -172,6 +172,7 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -172,6 +172,7 @@ class Projects::IssuesController < Projects::ApplicationController
def create_merge_request def create_merge_request
create_params = params.slice(:branch_name, :ref).merge(issue_iid: issue.iid) create_params = params.slice(:branch_name, :ref).merge(issue_iid: issue.iid)
create_params[:target_project_id] = params[:target_project_id] if Feature.enabled?(:create_confidential_merge_request, @project)
result = ::MergeRequests::CreateFromIssueService.new(project, current_user, create_params).execute result = ::MergeRequests::CreateFromIssueService.new(project, current_user, create_params).execute
if result[:status] == :success if result[:status] == :success
......
...@@ -6,17 +6,20 @@ module MergeRequests ...@@ -6,17 +6,20 @@ module MergeRequests
# branch - the name of new branch # branch - the name of new branch
# ref - the source of new branch. # ref - the source of new branch.
@branch_name = params[:branch_name] @branch_name = params[:branch_name]
@issue_iid = params[:issue_iid] @issue_iid = params[:issue_iid]
@ref = params[:ref] @ref = params[:ref]
@target_project_id = params[:target_project_id]
super(project, user) super(project, user)
end end
def execute def execute
return error('Project not found') if target_project.blank?
return error('Not allowed to create merge request') unless can_create_merge_request?
return error('Invalid issue iid') unless @issue_iid.present? && issue.present? return error('Invalid issue iid') unless @issue_iid.present? && issue.present?
result = CreateBranchService.new(project, current_user).execute(branch_name, ref) result = CreateBranchService.new(target_project, current_user).execute(branch_name, ref)
return result if result[:status] == :error return result if result[:status] == :error
new_merge_request = create(merge_request) new_merge_request = create(merge_request)
...@@ -26,7 +29,7 @@ module MergeRequests ...@@ -26,7 +29,7 @@ module MergeRequests
success(new_merge_request) success(new_merge_request)
else else
SystemNoteService.new_issue_branch(issue, project, current_user, branch_name) SystemNoteService.new_issue_branch(issue, project, current_user, branch_name, branch_project: target_project)
error(new_merge_request.errors) error(new_merge_request.errors)
end end
...@@ -34,6 +37,10 @@ module MergeRequests ...@@ -34,6 +37,10 @@ module MergeRequests
private private
def can_create_merge_request?
can?(current_user, :create_merge_request_from, target_project)
end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def issue def issue
@issue ||= IssuesFinder.new(current_user, project_id: project.id).find_by(iid: @issue_iid) @issue ||= IssuesFinder.new(current_user, project_id: project.id).find_by(iid: @issue_iid)
...@@ -45,21 +52,21 @@ module MergeRequests ...@@ -45,21 +52,21 @@ module MergeRequests
end end
def ref def ref
return @ref if project.repository.branch_exists?(@ref) return @ref if target_project.repository.branch_exists?(@ref)
project.default_branch || 'master' target_project.default_branch || 'master'
end end
def merge_request def merge_request
MergeRequests::BuildService.new(project, current_user, merge_request_params).execute MergeRequests::BuildService.new(target_project, current_user, merge_request_params).execute
end end
def merge_request_params def merge_request_params
{ {
issue_iid: @issue_iid, issue_iid: @issue_iid,
source_project_id: project.id, source_project_id: target_project.id,
source_branch: branch_name, source_branch: branch_name,
target_project_id: project.id, target_project_id: target_project.id,
target_branch: ref target_branch: ref
} }
end end
...@@ -67,5 +74,14 @@ module MergeRequests ...@@ -67,5 +74,14 @@ module MergeRequests
def success(merge_request) def success(merge_request)
super().merge(merge_request: merge_request) super().merge(merge_request: merge_request)
end end
def target_project
@target_project ||=
if @target_project_id.present?
project.forks.find_by_id(@target_project_id)
else
project
end
end
end end
end end
...@@ -404,8 +404,9 @@ module SystemNoteService ...@@ -404,8 +404,9 @@ module SystemNoteService
# Example note text: # Example note text:
# #
# "created branch `201-issue-branch-button`" # "created branch `201-issue-branch-button`"
def new_issue_branch(issue, project, author, branch) def new_issue_branch(issue, project, author, branch, branch_project: nil)
link = url_helpers.project_compare_path(project, from: project.default_branch, to: branch) branch_project ||= project
link = url_helpers.project_compare_path(branch_project, from: branch_project.default_branch, to: branch)
body = "created branch [`#{branch}`](#{link}) to address this issue" body = "created branch [`#{branch}`](#{link}) to address this issue"
...@@ -413,7 +414,7 @@ module SystemNoteService ...@@ -413,7 +414,7 @@ module SystemNoteService
end end
def new_merge_request(issue, project, author, merge_request) def new_merge_request(issue, project, author, merge_request)
body = "created merge request #{merge_request.to_reference} to address this issue" body = "created merge request #{merge_request.to_reference(project)} to address this issue"
create_note(NoteSummary.new(issue, project, author, body, action: 'merge')) create_note(NoteSummary.new(issue, project, author, body, action: 'merge'))
end end
......
...@@ -92,7 +92,7 @@ describe Projects::BranchesController do ...@@ -92,7 +92,7 @@ describe Projects::BranchesController do
end end
it 'posts a system note' do it 'posts a system note' do
expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, "1-feature-branch") expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, "1-feature-branch", branch_project: project)
post :create, post :create,
params: { params: {
...@@ -103,6 +103,75 @@ describe Projects::BranchesController do ...@@ -103,6 +103,75 @@ describe Projects::BranchesController do
} }
end end
context 'confidential_issue_project_id is present' do
let(:confidential_issue_project) { create(:project) }
def create_branch_with_confidential_issue_project
post(
:create,
params: {
namespace_id: project.namespace,
project_id: project,
branch_name: branch,
confidential_issue_project_id: confidential_issue_project.id,
issue_iid: issue.iid
}
)
end
context 'create_confidential_merge_request feature is enabled' do
before do
stub_feature_flags(create_confidential_merge_request: true)
end
context 'user cannot update issue' do
let(:issue) { create(:issue, project: confidential_issue_project) }
it 'does not post a system note' do
expect(SystemNoteService).not_to receive(:new_issue_branch)
create_branch_with_confidential_issue_project
end
end
context 'user can update issue' do
before do
confidential_issue_project.add_reporter(user)
end
context 'issue is under the specified project' do
let(:issue) { create(:issue, project: confidential_issue_project) }
it 'posts a system note' do
expect(SystemNoteService).to receive(:new_issue_branch).with(issue, confidential_issue_project, user, "1-feature-branch", branch_project: project)
create_branch_with_confidential_issue_project
end
end
context 'issue is not under the specified project' do
it 'does not post a system note' do
expect(SystemNoteService).not_to receive(:new_issue_branch)
create_branch_with_confidential_issue_project
end
end
end
end
context 'create_confidential_merge_request feature is disabled' do
before do
stub_feature_flags(create_confidential_merge_request: false)
end
it 'posts a system note on project' do
expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, "1-feature-branch", branch_project: project)
create_branch_with_confidential_issue_project
end
end
end
context 'repository-less project' do context 'repository-less project' do
let(:project) { create :project } let(:project) { create :project }
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Projects::IssuesController do describe Projects::IssuesController do
include ProjectForksHelper
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:issue) { create(:issue, project: project) } let(:issue) { create(:issue, project: project) }
...@@ -1130,6 +1132,7 @@ describe Projects::IssuesController do ...@@ -1130,6 +1132,7 @@ describe Projects::IssuesController do
end end
describe 'POST create_merge_request' do describe 'POST create_merge_request' do
let(:target_project_id) { nil }
let(:project) { create(:project, :repository, :public) } let(:project) { create(:project, :repository, :public) }
before do before do
...@@ -1163,13 +1166,42 @@ describe Projects::IssuesController do ...@@ -1163,13 +1166,42 @@ describe Projects::IssuesController do
expect(response).to have_gitlab_http_status(404) expect(response).to have_gitlab_http_status(404)
end end
context 'target_project_id is set' do
let(:target_project) { fork_project(project, user, repository: true) }
let(:target_project_id) { target_project.id }
context 'create_confidential_merge_request feature is enabled' do
before do
stub_feature_flags(create_confidential_merge_request: true)
end
it 'creates a new merge request' do
expect { create_merge_request }.to change(target_project.merge_requests, :count).by(1)
end
end
context 'create_confidential_merge_request feature is disabled' do
before do
stub_feature_flags(create_confidential_merge_request: false)
end
it 'creates a new merge request' do
expect { create_merge_request }.to change(project.merge_requests, :count).by(1)
end
end
end
def create_merge_request def create_merge_request
post :create_merge_request, params: { post(
namespace_id: project.namespace.to_param, :create_merge_request,
project_id: project.to_param, params: {
id: issue.to_param namespace_id: project.namespace.to_param,
}, project_id: project.to_param,
format: :json id: issue.to_param,
target_project_id: target_project_id
},
format: :json
)
end end
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe MergeRequests::CreateFromIssueService do describe MergeRequests::CreateFromIssueService do
include ProjectForksHelper
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:label_ids) { create_pair(:label, project: project).map(&:id) } let(:label_ids) { create_pair(:label, project: project).map(&:id) }
...@@ -10,139 +12,174 @@ describe MergeRequests::CreateFromIssueService do ...@@ -10,139 +12,174 @@ describe MergeRequests::CreateFromIssueService do
let(:issue) { create(:issue, project: project, milestone_id: milestone_id) } let(:issue) { create(:issue, project: project, milestone_id: milestone_id) }
let(:custom_source_branch) { 'custom-source-branch' } let(:custom_source_branch) { 'custom-source-branch' }
subject(:service) { described_class.new(project, user, issue_iid: issue.iid) } subject(:service) { described_class.new(project, user, service_params) }
subject(:service_with_custom_source_branch) { described_class.new(project, user, issue_iid: issue.iid, branch_name: custom_source_branch) } subject(:service_with_custom_source_branch) { described_class.new(project, user, branch_name: custom_source_branch, **service_params) }
before do before do
project.add_developer(user) project.add_developer(user)
end end
describe '#execute' do describe '#execute' do
it 'returns an error with invalid issue iid' do shared_examples_for 'a service that creates a merge request from an issue' do
result = described_class.new(project, user, issue_iid: -1).execute it 'returns an error when user can not create merge request on target project' do
result = described_class.new(project, create(:user), service_params).execute
expect(result[:status]).to eq(:error) expect(result[:status]).to eq(:error)
expect(result[:message]).to eq('Invalid issue iid') expect(result[:message]).to eq('Not allowed to create merge request')
end end
it 'delegates issue search to IssuesFinder' do it 'returns an error with invalid issue iid' do
expect_any_instance_of(IssuesFinder).to receive(:find_by).once.and_call_original result = described_class.new(project, user, issue_iid: -1).execute
described_class.new(project, user, issue_iid: -1).execute expect(result[:status]).to eq(:error)
end expect(result[:message]).to eq('Invalid issue iid')
end
it "inherits labels" do
issue.assign_attributes(label_ids: label_ids)
result = service.execute it 'creates a branch based on issue title' do
service.execute
expect(result[:merge_request].label_ids).to eq(label_ids) expect(target_project.repository.branch_exists?(issue.to_branch_name)).to be_truthy
end end
it "inherits milestones" do it 'creates a branch using passed name' do
result = service.execute service_with_custom_source_branch.execute
expect(result[:merge_request].milestone_id).to eq(milestone_id) expect(target_project.repository.branch_exists?(custom_source_branch)).to be_truthy
end end
it 'delegates the branch creation to CreateBranchService' do it 'creates the new_merge_request system note' do
expect_any_instance_of(CreateBranchService).to receive(:execute).once.and_call_original expect(SystemNoteService).to receive(:new_merge_request).with(issue, project, user, instance_of(MergeRequest))
service.execute service.execute
end end
it 'creates a branch based on issue title' do it 'creates the new_issue_branch system note when the branch could be created but the merge_request cannot be created' do
service.execute expect_any_instance_of(MergeRequest).to receive(:valid?).at_least(:once).and_return(false)
expect(project.repository.branch_exists?(issue.to_branch_name)).to be_truthy expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, issue.to_branch_name, branch_project: target_project)
end
it 'creates a branch using passed name' do service.execute
service_with_custom_source_branch.execute end
expect(project.repository.branch_exists?(custom_source_branch)).to be_truthy it 'creates a merge request' do
end expect { service.execute }.to change(target_project.merge_requests, :count).by(1)
end
it 'creates the new_merge_request system note' do it 'sets the merge request author to current user' do
expect(SystemNoteService).to receive(:new_merge_request).with(issue, project, user, instance_of(MergeRequest)) result = service.execute
service.execute expect(result[:merge_request].author).to eq(user)
end end
it 'creates the new_issue_branch system note when the branch could be created but the merge_request cannot be created' do it 'sets the merge request source branch to the new issue branch' do
project.project_feature.update!(merge_requests_access_level: ProjectFeature::DISABLED) result = service.execute
expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, issue.to_branch_name) expect(result[:merge_request].source_branch).to eq(issue.to_branch_name)
end
service.execute it 'sets the merge request source branch to the passed branch name' do
end result = service_with_custom_source_branch.execute
it 'creates a merge request' do expect(result[:merge_request].source_branch).to eq(custom_source_branch)
expect { service.execute }.to change(project.merge_requests, :count).by(1) end
end
it 'sets the merge request title to: "WIP: Resolves "$issue-title"' do it 'sets the merge request target branch to the project default branch' do
result = service.execute result = service.execute
expect(result[:merge_request].title).to eq("WIP: Resolve \"#{issue.title}\"") expect(result[:merge_request].target_branch).to eq(target_project.default_branch)
end end
it 'sets the merge request author to current user' do it 'executes quick actions if the build service sets them in the description' do
result = service.execute allow(service).to receive(:merge_request).and_wrap_original do |m, *args|
m.call(*args).tap do |merge_request|
merge_request.description = "/assign #{user.to_reference}"
end
end
expect(result[:merge_request].author).to eq(user) result = service.execute
end
it 'sets the merge request source branch to the new issue branch' do expect(result[:merge_request].assignees).to eq([user])
result = service.execute end
expect(result[:merge_request].source_branch).to eq(issue.to_branch_name) context 'when ref branch is set' do
end subject { described_class.new(project, user, ref: 'feature', **service_params).execute }
it 'sets the merge request source branch to the passed branch name' do it 'sets the merge request source branch to the new issue branch' do
result = service_with_custom_source_branch.execute expect(subject[:merge_request].source_branch).to eq(issue.to_branch_name)
end
expect(result[:merge_request].source_branch).to eq(custom_source_branch) it 'sets the merge request target branch to the ref branch' do
end expect(subject[:merge_request].target_branch).to eq('feature')
end
it 'sets the merge request target branch to the project default branch' do context 'when ref branch does not exist' do
result = service.execute subject { described_class.new(project, user, ref: 'no-such-branch', **service_params).execute }
expect(result[:merge_request].target_branch).to eq(project.default_branch) it 'creates a merge request' do
end expect { subject }.to change(target_project.merge_requests, :count).by(1)
end
it 'executes quick actions if the build service sets them in the description' do it 'sets the merge request target branch to the project default branch' do
allow(service).to receive(:merge_request).and_wrap_original do |m, *args| expect(subject[:merge_request].target_branch).to eq(target_project.default_branch)
m.call(*args).tap do |merge_request| end
merge_request.description = "/assign #{user.to_reference}"
end end
end end
end
result = service.execute context 'no target_project_id specified' do
let(:service_params) { { issue_iid: issue.iid } }
let(:target_project) { project }
expect(result[:merge_request].assignees).to eq([user]) it_behaves_like 'a service that creates a merge request from an issue'
end
context 'when ref branch is set' do it "inherits labels" do
subject { described_class.new(project, user, issue_iid: issue.iid, ref: 'feature').execute } issue.assign_attributes(label_ids: label_ids)
it 'sets the merge request source branch to the new issue branch' do result = service.execute
expect(subject[:merge_request].source_branch).to eq(issue.to_branch_name)
expect(result[:merge_request].label_ids).to eq(label_ids)
end end
it 'sets the merge request target branch to the ref branch' do it "inherits milestones" do
expect(subject[:merge_request].target_branch).to eq('feature') result = service.execute
expect(result[:merge_request].milestone_id).to eq(milestone_id)
end end
context 'when ref branch does not exist' do it 'sets the merge request title to: "WIP: Resolves "$issue-title"' do
subject { described_class.new(project, user, issue_iid: issue.iid, ref: 'no-such-branch').execute } result = service.execute
it 'creates a merge request' do expect(result[:merge_request].title).to eq("WIP: Resolve \"#{issue.title}\"")
expect { subject }.to change(project.merge_requests, :count).by(1) end
end
context 'target_project_id is specified' do
let(:service_params) { { issue_iid: issue.iid, target_project_id: target_project.id } }
context 'target project is not a fork of the project' do
let(:target_project) { create(:project, :repository) }
it 'returns an error about not finding the project' do
result = service.execute
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq('Project not found')
end end
it 'sets the merge request target branch to the project default branch' do it 'does not create merge request' do
expect(subject[:merge_request].target_branch).to eq(project.default_branch) expect { service.execute }.to change(target_project.merge_requests, :count).by(0)
end
end
context 'target project is a fork of project project' do
let(:target_project) { fork_project(project, user, repository: true) }
it_behaves_like 'a service that creates a merge request from an issue'
it 'sets the merge request title to: "WIP: $issue-branch-name' do
result = service.execute
expect(result[:merge_request].title).to eq("WIP: #{issue.to_branch_name.titleize.humanize}")
end end
end end
end end
......
...@@ -454,16 +454,32 @@ describe SystemNoteService do ...@@ -454,16 +454,32 @@ describe SystemNoteService do
end end
describe '.new_issue_branch' do describe '.new_issue_branch' do
subject { described_class.new_issue_branch(noteable, project, author, "1-mepmep") } let(:branch) { '1-mepmep' }
it_behaves_like 'a system note' do subject { described_class.new_issue_branch(noteable, project, author, branch, branch_project: branch_project) }
let(:action) { 'branch' }
end
context 'when a branch is created from the new branch button' do shared_examples_for 'a system note for new issue branch' do
it 'sets the note text' do it_behaves_like 'a system note' do
expect(subject.note).to start_with("created branch [`1-mepmep`]") let(:action) { 'branch' }
end end
context 'when a branch is created from the new branch button' do
it 'sets the note text' do
expect(subject.note).to start_with("created branch [`#{branch}`]")
end
end
end
context 'branch_project is set' do
let(:branch_project) { create(:project, :repository) }
it_behaves_like 'a system note for new issue branch'
end
context 'branch_project is not set' do
let(:branch_project) { nil }
it_behaves_like 'a system note for new issue branch'
end end
end end
...@@ -477,7 +493,7 @@ describe SystemNoteService do ...@@ -477,7 +493,7 @@ describe SystemNoteService do
end end
it 'sets the new merge request note text' do it 'sets the new merge request note text' do
expect(subject.note).to eq("created merge request #{merge_request.to_reference} to address this issue") expect(subject.note).to eq("created merge request #{merge_request.to_reference(project)} to address this issue")
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment